Skip to content

Commit b56dba4

Browse files
committed
ctf: Add tests for boolean and dynamic blob field classes
Signed-off-by: Arnaud Fiorini <fiorini.arnaud@gmail.com>
1 parent 737a54d commit b56dba4

4 files changed

Lines changed: 382 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**********************************************************************
2+
* Copyright (c) 2026 École Polytechnique de Montréal
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License 2.0 which
6+
* accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
**********************************************************************/
11+
package org.eclipse.tracecompass.ctf.core.tests.types;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNotNull;
15+
16+
import java.nio.ByteOrder;
17+
18+
import org.eclipse.tracecompass.ctf.core.event.types.BooleanDeclaration;
19+
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
20+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode;
21+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.bool.BooleanDeclarationParser;
22+
import org.junit.Test;
23+
24+
import com.google.gson.JsonObject;
25+
import com.google.gson.JsonPrimitive;
26+
27+
/**
28+
* Test class for boolean declaration parser in CTF2
29+
*
30+
* @author Arnaud Fiorini
31+
*/
32+
public class BooleanDeclarationParserTest {
33+
34+
/**
35+
* Test parsing 14-bit boolean field from CTF2 JSON
36+
*
37+
* @throws Exception if parsing fails
38+
*/
39+
@Test
40+
public void testCTF2BooleanParsingWithoutAlignment() throws Exception {
41+
CTFTrace trace = new CTFTrace();
42+
43+
JsonObject fieldClass = new JsonObject();
44+
fieldClass.add("length", new JsonPrimitive(14));
45+
fieldClass.add("byte-order", new JsonPrimitive("little-endian"));
46+
47+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "test", "test", "fixed-length-boolean", fieldClass);
48+
49+
BooleanDeclaration result = BooleanDeclarationParser.INSTANCE.parse(node,
50+
new BooleanDeclarationParser.Param(trace));
51+
52+
assertNotNull(result);
53+
assertEquals(1, result.getAlignment());
54+
assertEquals(14, result.getMaximumSize());
55+
assertEquals(ByteOrder.LITTLE_ENDIAN, result.getByteOrder());
56+
}
57+
58+
/**
59+
* Test parsing 37-bit boolean field from CTF2 JSON
60+
*
61+
* @throws Exception if parsing fails
62+
*/
63+
@Test
64+
public void testCTF2BooleanParsingWithAlignment() throws Exception {
65+
CTFTrace trace = new CTFTrace();
66+
67+
JsonObject fieldClass = new JsonObject();
68+
fieldClass.add("length", new JsonPrimitive(37));
69+
fieldClass.add("byte-order", new JsonPrimitive("big-endian"));
70+
fieldClass.add("alignment", new JsonPrimitive(8));
71+
72+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "test", "test", "fixed-length-boolean", fieldClass);
73+
74+
BooleanDeclaration result = BooleanDeclarationParser.INSTANCE.parse(node,
75+
new BooleanDeclarationParser.Param(trace));
76+
77+
assertNotNull(result);
78+
assertEquals(8, result.getAlignment());
79+
assertEquals(37, result.getMaximumSize());
80+
assertEquals(ByteOrder.BIG_ENDIAN, result.getByteOrder());
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**********************************************************************
2+
* Copyright (c) 2026 École Polytechnique de Montréal
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License 2.0 which
6+
* accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
**********************************************************************/
11+
package org.eclipse.tracecompass.ctf.core.tests.types;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNotNull;
15+
import static org.junit.Assert.assertTrue;
16+
17+
import java.nio.ByteOrder;
18+
19+
import org.eclipse.tracecompass.ctf.core.event.types.BooleanDeclaration;
20+
import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration;
21+
import org.junit.Test;
22+
23+
/**
24+
* Test for the boolean declaration class in CTF2
25+
*
26+
* @author Arnaud Fiorini
27+
*/
28+
@SuppressWarnings("javadoc")
29+
public class BooleanDeclarationTest {
30+
31+
@Test
32+
public void constructorTest() {
33+
for (int i = 1; i < 20; i++) {
34+
BooleanDeclaration booleanDeclaration = new BooleanDeclaration(i, ByteOrder.nativeOrder(), 1);
35+
assertNotNull(booleanDeclaration);
36+
}
37+
}
38+
39+
@Test
40+
public void getterTest() {
41+
BooleanDeclaration booleanDeclaration = new BooleanDeclaration(8, ByteOrder.nativeOrder(), 1);
42+
assertEquals(booleanDeclaration.getAlignment(), 1);
43+
assertEquals(booleanDeclaration.getByteOrder(), ByteOrder.nativeOrder());
44+
assertEquals(booleanDeclaration.getMaximumSize(), 8);
45+
}
46+
47+
@Test
48+
public void binaryEquivalentTest() {
49+
BooleanDeclaration a = new BooleanDeclaration(8, ByteOrder.BIG_ENDIAN, 0);
50+
BooleanDeclaration b = new BooleanDeclaration(8, ByteOrder.LITTLE_ENDIAN, 0);
51+
BooleanDeclaration c = new BooleanDeclaration(8, ByteOrder.BIG_ENDIAN, 0);
52+
BooleanDeclaration d = new BooleanDeclaration(24, ByteOrder.BIG_ENDIAN, 0);
53+
FloatDeclaration e = new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 0);
54+
55+
assertTrue(a.isBinaryEquivalent(a));
56+
assertTrue(a.isBinaryEquivalent(b));
57+
assertTrue(a.isBinaryEquivalent(c));
58+
assertTrue(!a.isBinaryEquivalent(d));
59+
assertTrue(!a.isBinaryEquivalent(e));
60+
61+
assertTrue(b.isBinaryEquivalent(a));
62+
assertTrue(b.isBinaryEquivalent(b));
63+
assertTrue(b.isBinaryEquivalent(c));
64+
assertTrue(!b.isBinaryEquivalent(d));
65+
assertTrue(!b.isBinaryEquivalent(e));
66+
67+
assertTrue(!c.isBinaryEquivalent(d));
68+
assertTrue(!c.isBinaryEquivalent(e));
69+
assertTrue(!d.isBinaryEquivalent(e));
70+
}
71+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**********************************************************************
2+
* Copyright (c) 2026 École Polytechnique de Montréal
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License 2.0 which
6+
* accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
**********************************************************************/
11+
package org.eclipse.tracecompass.ctf.core.tests.types;
12+
13+
import static org.junit.Assert.assertNotNull;
14+
import static org.junit.Assert.assertTrue;
15+
16+
import java.nio.ByteBuffer;
17+
import java.nio.ByteOrder;
18+
19+
import org.eclipse.jdt.annotation.NonNull;
20+
import org.eclipse.tracecompass.ctf.core.CTFException;
21+
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
22+
import org.eclipse.tracecompass.ctf.core.event.types.BooleanDeclaration;
23+
import org.eclipse.tracecompass.ctf.core.event.types.BooleanDefinition;
24+
import org.junit.Test;
25+
26+
/**
27+
* Tests for the boolean definition class in CTF2
28+
*
29+
* @author Arnaud Fiorini
30+
*/
31+
@SuppressWarnings("javadoc")
32+
public class BooleanDefinitionTest {
33+
34+
@NonNull private static final String FIELD_NAME = "float";
35+
36+
/**
37+
* Test a larger than long boolean field set to true
38+
* @throws CTFException
39+
*/
40+
@Test
41+
public void testBiggerThanLongTrueBoolean() throws CTFException {
42+
byte[] data = new byte[9];
43+
data[1] = (byte) (1 << 3);
44+
testBooleanCreateDefinition(9, 67, data, 0, true);
45+
}
46+
47+
/**
48+
* Test a large boolean field set to true
49+
* @throws CTFException
50+
*/
51+
@Test
52+
public void testLargeTrueBoolean() throws CTFException {
53+
byte[] data = new byte[17];
54+
data[1] = (byte) (1 << 7);
55+
testBooleanCreateDefinition(17, 129, data, 0, true);
56+
}
57+
58+
/**
59+
* Test a small boolean field set to true
60+
* @throws CTFException
61+
*/
62+
@Test
63+
public void testSmallTrueBoolean() throws CTFException {
64+
byte[] data = new byte[2];
65+
data[1] = (byte) (1 << 5);
66+
testBooleanCreateDefinition(2, 14, data, 0, true);
67+
}
68+
69+
/**
70+
* Test a large boolean field set to false
71+
* @throws CTFException
72+
*/
73+
@Test
74+
public void testLargeFalseBoolean() throws CTFException {
75+
byte[] data = new byte[17];
76+
data[16] = (byte) (1 << 7);
77+
testBooleanCreateDefinition(17, 135, data, 0, false);
78+
}
79+
80+
/**
81+
* Test a small boolean field set to false
82+
* @throws CTFException
83+
*/
84+
@Test
85+
public void testSmallFalseBoolean() throws CTFException {
86+
byte[] data = new byte[2];
87+
data[1] = (byte) (1 << 7);
88+
testBooleanCreateDefinition(2, 12, data, 0, false);
89+
}
90+
/**
91+
* Test a large boolean field with alignment set to false
92+
* @throws CTFException
93+
*/
94+
@Test
95+
public void testLargeFalseBooleanWithAlignment() throws CTFException {
96+
byte[] data = new byte[20];
97+
data[0] = 1;
98+
testBooleanCreateDefinition(20, 145, data, 8, false);
99+
}
100+
101+
/**
102+
* Test a small boolean field with alignment set to false
103+
* @throws CTFException
104+
*/
105+
@Test
106+
public void testSmallFalseBooleanWithAlignment() throws CTFException {
107+
byte[] data = new byte[3];
108+
data[0] = (byte) (1 << 1);
109+
testBooleanCreateDefinition(3, 16, data, 8, false);
110+
}
111+
112+
private static void testBooleanCreateDefinition(int bufferSize, int fieldSize, byte[] content, int alignment, boolean expectedValue) throws CTFException {
113+
BooleanDeclaration declaration = new BooleanDeclaration(fieldSize, ByteOrder.nativeOrder(), alignment);
114+
ByteBuffer byb = ByteBuffer.allocate(bufferSize);
115+
byb.order(ByteOrder.nativeOrder());
116+
byb.mark();
117+
byb.put(content);
118+
byb.reset();
119+
BitBuffer bb = new BitBuffer(byb);
120+
if (alignment > 1) {
121+
bb.position(1);
122+
}
123+
BooleanDefinition definition = (BooleanDefinition) declaration.createDefinition(null, FIELD_NAME, bb);
124+
assertNotNull(definition);
125+
if (expectedValue) {
126+
assertTrue(definition.getValue());
127+
} else {
128+
assertTrue(!definition.getValue());
129+
}
130+
}
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**********************************************************************
2+
* Copyright (c) 2026 École Polytechnique de Montréal
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License 2.0 which
6+
* accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
**********************************************************************/
11+
package org.eclipse.tracecompass.ctf.core.tests.types;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNotNull;
15+
import static org.junit.Assert.assertTrue;
16+
17+
import java.nio.ByteBuffer;
18+
import java.nio.ByteOrder;
19+
import java.util.Arrays;
20+
21+
import org.eclipse.jdt.annotation.NonNull;
22+
import org.eclipse.tracecompass.ctf.core.CTFException;
23+
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
24+
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
25+
import org.eclipse.tracecompass.ctf.core.event.types.DynamicBlobDeclaration;
26+
import org.eclipse.tracecompass.ctf.core.event.types.DynamicBlobDefinition;
27+
import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
28+
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
29+
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
30+
import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
31+
import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
32+
import org.junit.Test;
33+
34+
/**
35+
* Test class for dynamic blob declaration in CTF2
36+
*
37+
* @author Arnaud Fiorini
38+
*/
39+
@SuppressWarnings("javadoc")
40+
public class DynamicBlobDeclarationTest {
41+
42+
@Test
43+
public void testStandardBlob() throws CTFException {
44+
byte[] content = new byte[] {72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33};
45+
testDynamicBlobDeclaration(content, 12, 12, "lengthField", "application/test", 0);
46+
}
47+
48+
@Test
49+
public void testAlignedBlob() throws CTFException {
50+
byte[] content = new byte[] {0, 0, 72, 101, 108, 108, 111};
51+
testDynamicBlobDeclaration(content, 5, 7, "lengthField", "application/test", 14);
52+
}
53+
54+
@Test
55+
public void testEmptyBlob() throws CTFException {
56+
byte[] content = new byte[] {};
57+
testDynamicBlobDeclaration(content, 0, 0, "lengthField", "application/test", 0);
58+
}
59+
60+
@Test
61+
public void testBigBlob() throws CTFException {
62+
byte[] content = new byte[] {72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33};
63+
testDynamicBlobDeclaration(content, 32, 36, "lengthField", "application/test", 32);
64+
}
65+
66+
private static void testDynamicBlobDeclaration(byte[] content, int blobSize, int bufferSize, @NonNull String lengthFieldName, @NonNull String metadataType, int startingPosition) throws CTFException {
67+
IntegerDeclaration id = IntegerDeclaration.createDeclaration(8, false, 8,
68+
ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 32, null);
69+
DynamicBlobDeclaration declaration = new DynamicBlobDeclaration(lengthFieldName, metadataType);
70+
StructDeclaration structDec = new StructDeclaration(0);
71+
structDec.addField(lengthFieldName, id);
72+
73+
StructDefinition structDef = new StructDefinition(
74+
structDec,
75+
null,
76+
"x",
77+
new Definition[] {
78+
new IntegerDefinition(
79+
id,
80+
null,
81+
lengthFieldName,
82+
blobSize)
83+
});
84+
85+
ByteBuffer byb = ByteBuffer.allocate(bufferSize);
86+
byb.order(ByteOrder.nativeOrder());
87+
byb.mark();
88+
byb.put(content);
89+
byb.reset();
90+
BitBuffer bb = new BitBuffer(byb);
91+
bb.position(startingPosition);
92+
DynamicBlobDefinition definition = (DynamicBlobDefinition) declaration.createDefinition(structDef, lengthFieldName, bb);
93+
assertNotNull(definition);
94+
byte[] expected = Arrays.copyOfRange(content, (int) Math.ceil((float) startingPosition / 8), content.length);
95+
assertTrue(Arrays.equals(expected, definition.getBytes()));
96+
assertEquals(metadataType, definition.getType());
97+
}
98+
}

0 commit comments

Comments
 (0)