Skip to content

Commit a278a00

Browse files
Add unit tests
Generated by Claude Sonnet 4.5 Change-Id: Ieec23f0b0f783775a166b530c9c5c75bc0190c53 Signed-off-by: Matthew Khouzam <[email protected]>
1 parent b6481f5 commit a278a00

File tree

4 files changed

+337
-0
lines changed

4 files changed

+337
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Ericsson
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
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 org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
18+
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
19+
import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
20+
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
21+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode;
22+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.struct.StructParser;
23+
import org.junit.Test;
24+
25+
import com.google.gson.JsonArray;
26+
import com.google.gson.JsonObject;
27+
import com.google.gson.JsonPrimitive;
28+
29+
@SuppressWarnings("javadoc")
30+
public class CTF2IntegrationTest {
31+
32+
@Test
33+
public void testStructWithVariousFieldTypes() throws Exception {
34+
CTFTrace trace = new CTFTrace();
35+
DeclarationScope scope = new DeclarationScope(null, "test");
36+
37+
// Create a struct with multiple field types
38+
JsonObject structFieldClass = new JsonObject();
39+
structFieldClass.add("type", new JsonPrimitive("structure"));
40+
41+
JsonArray memberClasses = new JsonArray();
42+
43+
// Add integer field
44+
JsonObject intField = new JsonObject();
45+
intField.add("name", new JsonPrimitive("int_field"));
46+
JsonObject intFieldClass = new JsonObject();
47+
intFieldClass.add("type", new JsonPrimitive("fixed-length-unsigned-integer"));
48+
intFieldClass.add("length", new JsonPrimitive(32));
49+
intFieldClass.add("byte-order", new JsonPrimitive("le"));
50+
intField.add("field-class", intFieldClass);
51+
memberClasses.add(intField);
52+
53+
// Add float field
54+
JsonObject floatField = new JsonObject();
55+
floatField.add("name", new JsonPrimitive("float_field"));
56+
JsonObject floatFieldClass = new JsonObject();
57+
floatFieldClass.add("type", new JsonPrimitive("fixed-length-floating-point-number"));
58+
floatFieldClass.add("length", new JsonPrimitive(64));
59+
floatFieldClass.add("byte-order", new JsonPrimitive("le"));
60+
floatField.add("field-class", floatFieldClass);
61+
memberClasses.add(floatField);
62+
63+
structFieldClass.add("member-classes", memberClasses);
64+
65+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "structure", "test");
66+
node.setFieldClass(structFieldClass);
67+
68+
StructDeclaration result = StructParser.INSTANCE.parse(node,
69+
new StructParser.Param(trace, null, scope));
70+
71+
assertNotNull(result);
72+
assertEquals(2, result.getFields().size());
73+
assertTrue(result.hasField("int_field"));
74+
assertTrue(result.hasField("float_field"));
75+
}
76+
77+
@Test
78+
public void testIntegerWithMappings() throws Exception {
79+
CTFTrace trace = new CTFTrace();
80+
81+
JsonObject fieldClass = new JsonObject();
82+
fieldClass.add("type", new JsonPrimitive("fixed-length-unsigned-integer"));
83+
fieldClass.add("length", new JsonPrimitive(8));
84+
fieldClass.add("byte-order", new JsonPrimitive("le"));
85+
86+
// Add mappings for enumeration-like behavior
87+
JsonObject mappings = new JsonObject();
88+
JsonArray range1 = new JsonArray();
89+
range1.add(new JsonPrimitive(0));
90+
range1.add(new JsonPrimitive(0));
91+
JsonArray ranges1 = new JsonArray();
92+
ranges1.add(range1);
93+
mappings.add("ZERO", ranges1);
94+
95+
JsonArray range2 = new JsonArray();
96+
range2.add(new JsonPrimitive(1));
97+
range2.add(new JsonPrimitive(1));
98+
JsonArray ranges2 = new JsonArray();
99+
ranges2.add(range2);
100+
mappings.add("ONE", ranges2);
101+
102+
fieldClass.add("mappings", mappings);
103+
104+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "fixed-length-unsigned-integer", "test");
105+
node.setFieldClass(fieldClass);
106+
107+
IntegerDeclaration result = (IntegerDeclaration) org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.integer.IntegerDeclarationParser.INSTANCE.parse(node,
108+
new org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.integer.IntegerDeclarationParser.Param(trace));
109+
110+
assertNotNull(result);
111+
assertEquals(8, result.getLength());
112+
assertNotNull(result.getMappings());
113+
assertEquals(2, result.getMappings().size());
114+
assertTrue(result.getMappings().containsKey("ZERO"));
115+
assertTrue(result.getMappings().containsKey("ONE"));
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Ericsson
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
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.FloatDeclaration;
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.floatingpoint.FloatDeclarationParser;
22+
import org.junit.Test;
23+
24+
import com.google.gson.JsonObject;
25+
import com.google.gson.JsonPrimitive;
26+
27+
@SuppressWarnings("javadoc")
28+
public class FloatDeclarationParserTest {
29+
30+
@Test
31+
public void testCTF2Float32Parsing() throws Exception {
32+
CTFTrace trace = new CTFTrace();
33+
34+
JsonObject fieldClass = new JsonObject();
35+
fieldClass.add("length", new JsonPrimitive(32));
36+
fieldClass.add("byte-order", new JsonPrimitive("le"));
37+
38+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "test", "test");
39+
node.setFieldClass(fieldClass);
40+
41+
FloatDeclaration result = FloatDeclarationParser.INSTANCE.parse(node,
42+
new FloatDeclarationParser.Param(trace));
43+
44+
assertNotNull(result);
45+
assertEquals(8, result.getExponent());
46+
assertEquals(24, result.getMantissa());
47+
assertEquals(ByteOrder.LITTLE_ENDIAN, result.getByteOrder());
48+
}
49+
50+
@Test
51+
public void testCTF2Float64Parsing() throws Exception {
52+
CTFTrace trace = new CTFTrace();
53+
54+
JsonObject fieldClass = new JsonObject();
55+
fieldClass.add("length", new JsonPrimitive(64));
56+
fieldClass.add("byte-order", new JsonPrimitive("be"));
57+
fieldClass.add("alignment", new JsonPrimitive(8));
58+
59+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "test", "test");
60+
node.setFieldClass(fieldClass);
61+
62+
FloatDeclaration result = FloatDeclarationParser.INSTANCE.parse(node,
63+
new FloatDeclarationParser.Param(trace));
64+
65+
assertNotNull(result);
66+
assertEquals(11, result.getExponent());
67+
assertEquals(53, result.getMantissa());
68+
assertEquals(ByteOrder.BIG_ENDIAN, result.getByteOrder());
69+
assertEquals(8, result.getAlignment());
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Ericsson
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
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 org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonTraceMetadataNode;
17+
import org.junit.Test;
18+
19+
import com.google.gson.JsonObject;
20+
21+
@SuppressWarnings("javadoc")
22+
public class JsonTraceMetadataNodeTest {
23+
24+
@Test
25+
public void testEnvironmentParsing() throws Exception {
26+
JsonTraceMetadataNode node = new JsonTraceMetadataNode(null, "trace-class", "test");
27+
28+
JsonObject environment = new JsonObject();
29+
environment.addProperty("hostname", "test-host");
30+
environment.addProperty("domain", "kernel");
31+
environment.addProperty("tracer_name", "lttng-modules");
32+
33+
// Simulate Gson deserialization by setting the field directly
34+
java.lang.reflect.Field envField = JsonTraceMetadataNode.class.getDeclaredField("fEnvironment");
35+
envField.setAccessible(true);
36+
envField.set(node, environment);
37+
38+
node.initialize();
39+
40+
assertNotNull(node.getEnvironment());
41+
assertEquals("test-host", node.getEnvironment().get("hostname").getAsString());
42+
assertEquals("kernel", node.getEnvironment().get("domain").getAsString());
43+
assertEquals("lttng-modules", node.getEnvironment().get("tracer_name").getAsString());
44+
}
45+
46+
@Test
47+
public void testUidAndPacketHeader() throws Exception {
48+
JsonTraceMetadataNode node = new JsonTraceMetadataNode(null, "trace-class", "test");
49+
50+
// Simulate Gson deserialization
51+
java.lang.reflect.Field uidField = JsonTraceMetadataNode.class.getDeclaredField("fUid");
52+
uidField.setAccessible(true);
53+
uidField.set(node, "test-uid-123");
54+
55+
node.initialize();
56+
57+
assertEquals("test-uid-123", node.getUid());
58+
}
59+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Ericsson
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
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 org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
17+
import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration;
18+
import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
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.TypeAliasParser;
22+
import org.junit.Test;
23+
24+
import com.google.gson.JsonObject;
25+
import com.google.gson.JsonPrimitive;
26+
27+
@SuppressWarnings("javadoc")
28+
public class TypeAliasParserTest {
29+
30+
@Test
31+
public void testFixedLengthFloatingPointParsing() throws Exception {
32+
CTFTrace trace = new CTFTrace();
33+
DeclarationScope scope = new DeclarationScope(null, "test");
34+
35+
JsonObject fieldClass = new JsonObject();
36+
fieldClass.add("type", new JsonPrimitive("fixed-length-floating-point-number"));
37+
fieldClass.add("length", new JsonPrimitive(32));
38+
fieldClass.add("byte-order", new JsonPrimitive("le"));
39+
40+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "fixed-length-floating-point-number", "test");
41+
node.setFieldClass(fieldClass);
42+
43+
IDeclaration result = TypeAliasParser.INSTANCE.parse(node,
44+
new TypeAliasParser.Param(trace, scope));
45+
46+
assertNotNull(result);
47+
assertTrue(result instanceof FloatDeclaration);
48+
49+
FloatDeclaration floatDecl = (FloatDeclaration) result;
50+
assertTrue(floatDecl.getExponent() > 0);
51+
assertTrue(floatDecl.getMantissa() > 0);
52+
}
53+
54+
@Test
55+
public void testStaticLengthStringParsing() throws Exception {
56+
CTFTrace trace = new CTFTrace();
57+
DeclarationScope scope = new DeclarationScope(null, "test");
58+
59+
JsonObject fieldClass = new JsonObject();
60+
fieldClass.add("type", new JsonPrimitive("static-length-string"));
61+
fieldClass.add("length", new JsonPrimitive(16));
62+
fieldClass.add("encoding", new JsonPrimitive("utf-8"));
63+
64+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "static-length-string", "test");
65+
node.setFieldClass(fieldClass);
66+
67+
IDeclaration result = TypeAliasParser.INSTANCE.parse(node,
68+
new TypeAliasParser.Param(trace, scope));
69+
70+
assertNotNull(result);
71+
}
72+
73+
@Test
74+
public void testDynamicLengthStringParsing() throws Exception {
75+
CTFTrace trace = new CTFTrace();
76+
DeclarationScope scope = new DeclarationScope(null, "test");
77+
78+
JsonObject fieldClass = new JsonObject();
79+
fieldClass.add("type", new JsonPrimitive("dynamic-length-string"));
80+
fieldClass.add("encoding", new JsonPrimitive("utf-8"));
81+
82+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "dynamic-length-string", "test");
83+
node.setFieldClass(fieldClass);
84+
85+
IDeclaration result = TypeAliasParser.INSTANCE.parse(node,
86+
new TypeAliasParser.Param(trace, scope));
87+
88+
assertNotNull(result);
89+
}
90+
}

0 commit comments

Comments
 (0)