Skip to content

Commit 8557392

Browse files
ctf: Add unit tests
Generated by Claude Sonnet 4.5, manually fixed by me. Change-Id: Ieec23f0b0f783775a166b530c9c5c75bc0190c53 Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
1 parent b031686 commit 8557392

5 files changed

Lines changed: 343 additions & 0 deletions

File tree

ctf/org.eclipse.tracecompass.ctf.core.tests/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Export-Package: org.eclipse.tracecompass.ctf.core.tests;x-friends:="org.eclipse.
2424
org.eclipse.tracecompass.ctf.core.tests.trace;x-internal:=true,
2525
org.eclipse.tracecompass.ctf.core.tests.types;x-internal:=true
2626
Import-Package: com.google.common.collect,
27+
com.google.gson,
2728
org.antlr.runtime;version="3.2.0",
2829
org.apache.commons.io,
2930
org.eclipse.test.performance,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.types.IntegerDeclaration;
18+
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
19+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode;
20+
import org.junit.Test;
21+
22+
import com.google.gson.JsonArray;
23+
import com.google.gson.JsonObject;
24+
import com.google.gson.JsonPrimitive;
25+
26+
/**
27+
* Integration test class for CTF2 parsing functionality
28+
*/
29+
public class CTF2IntegrationTest {
30+
31+
/**
32+
* Test parsing integer with mappings for enumeration-like behavior
33+
*
34+
* @throws Exception if parsing fails
35+
*/
36+
@Test
37+
public void testIntegerWithMappings() throws Exception {
38+
CTFTrace trace = new CTFTrace();
39+
40+
JsonObject fieldClass = new JsonObject();
41+
fieldClass.add("type", new JsonPrimitive("fixed-length-unsigned-integer"));
42+
fieldClass.add("length", new JsonPrimitive(8));
43+
fieldClass.add("byte-order", new JsonPrimitive("le"));
44+
45+
// Add mappings for enumeration-like behavior
46+
JsonObject mappings = new JsonObject();
47+
JsonArray range1 = new JsonArray();
48+
range1.add(new JsonPrimitive(0));
49+
range1.add(new JsonPrimitive(0));
50+
JsonArray ranges1 = new JsonArray();
51+
ranges1.add(range1);
52+
mappings.add("ZERO", ranges1);
53+
54+
JsonArray range2 = new JsonArray();
55+
range2.add(new JsonPrimitive(1));
56+
range2.add(new JsonPrimitive(1));
57+
JsonArray ranges2 = new JsonArray();
58+
ranges2.add(range2);
59+
mappings.add("ONE", ranges2);
60+
61+
fieldClass.add("mappings", mappings);
62+
63+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "fixed-length-unsigned-integer", "test", "int_field", fieldClass);
64+
65+
IntegerDeclaration result = org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.integer.IntegerDeclarationParser.INSTANCE.parse(node,
66+
new org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.integer.IntegerDeclarationParser.Param(trace));
67+
68+
assertNotNull(result);
69+
assertEquals(8, result.getLength());
70+
assertNotNull(result.getMappings());
71+
assertEquals(2, result.getMappings().size());
72+
assertTrue(result.getMappings().containsKey("ZERO"));
73+
assertTrue(result.getMappings().containsKey("ONE"));
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
/**
28+
* Test class for FloatDeclarationParser CTF2 support
29+
*/
30+
public class FloatDeclarationParserTest {
31+
32+
/**
33+
* Test parsing 32-bit floating point from CTF2 JSON
34+
*
35+
* @throws Exception if parsing fails
36+
*/
37+
@Test
38+
public void testCTF2Float32Parsing() throws Exception {
39+
CTFTrace trace = new CTFTrace();
40+
41+
JsonObject fieldClass = new JsonObject();
42+
fieldClass.add("length", new JsonPrimitive(32));
43+
fieldClass.add("byte-order", new JsonPrimitive("le"));
44+
45+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "test", "test", "float_field", fieldClass);
46+
47+
FloatDeclaration result = FloatDeclarationParser.INSTANCE.parse(node,
48+
new FloatDeclarationParser.Param(trace));
49+
50+
assertNotNull(result);
51+
assertEquals(8, result.getExponent());
52+
assertEquals(24, result.getMantissa());
53+
assertEquals(ByteOrder.LITTLE_ENDIAN, result.getByteOrder());
54+
}
55+
56+
/**
57+
* Test parsing 64-bit floating point from CTF2 JSON
58+
*
59+
* @throws Exception if parsing fails
60+
*/
61+
@Test
62+
public void testCTF2Float64Parsing() throws Exception {
63+
CTFTrace trace = new CTFTrace();
64+
65+
JsonObject fieldClass = new JsonObject();
66+
fieldClass.add("length", new JsonPrimitive(64));
67+
fieldClass.add("byte-order", new JsonPrimitive("be"));
68+
fieldClass.add("alignment", new JsonPrimitive(8));
69+
70+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "test", "test", "double_field", fieldClass);
71+
72+
FloatDeclaration result = FloatDeclarationParser.INSTANCE.parse(node,
73+
new FloatDeclarationParser.Param(trace));
74+
75+
assertNotNull(result);
76+
assertEquals(11, result.getExponent());
77+
assertEquals(53, result.getMantissa());
78+
assertEquals(ByteOrder.BIG_ENDIAN, result.getByteOrder());
79+
assertEquals(8, result.getAlignment());
80+
}
81+
}
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 org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonTraceMetadataNode;
17+
import org.junit.Test;
18+
19+
import com.google.gson.JsonObject;
20+
21+
/**
22+
* Test class for JsonTraceMetadataNode environment parsing
23+
*/
24+
public class JsonTraceMetadataNodeTest {
25+
26+
/**
27+
* Test parsing environment object from CTF2 JSON
28+
*
29+
* @throws Exception if parsing fails
30+
*/
31+
@Test
32+
public void testEnvironmentParsing() throws Exception {
33+
JsonTraceMetadataNode node = new JsonTraceMetadataNode(null, "trace-class", "test");
34+
35+
JsonObject environment = new JsonObject();
36+
environment.addProperty("hostname", "test-host");
37+
environment.addProperty("domain", "kernel");
38+
environment.addProperty("tracer_name", "lttng-modules");
39+
40+
// Simulate Gson deserialization by setting the field directly
41+
java.lang.reflect.Field envField = JsonTraceMetadataNode.class.getDeclaredField("fEnvironment");
42+
envField.setAccessible(true);
43+
envField.set(node, environment);
44+
45+
node.initialize();
46+
47+
assertNotNull(node.getEnvironment());
48+
assertEquals("test-host", node.getEnvironment().get("hostname").getAsString());
49+
assertEquals("kernel", node.getEnvironment().get("domain").getAsString());
50+
assertEquals("lttng-modules", node.getEnvironment().get("tracer_name").getAsString());
51+
}
52+
53+
/**
54+
* Test UID and packet header parsing
55+
*
56+
* @throws Exception if parsing fails
57+
*/
58+
@Test
59+
public void testUidParsing() throws Exception {
60+
JsonTraceMetadataNode node = new JsonTraceMetadataNode(null, "trace-class", "test");
61+
62+
// Simulate Gson deserialization
63+
java.lang.reflect.Field uidField = JsonTraceMetadataNode.class.getDeclaredField("fUid");
64+
uidField.setAccessible(true);
65+
uidField.set(node, "test-uid-123");
66+
67+
node.initialize();
68+
69+
assertEquals("test-uid-123", node.getUid());
70+
}
71+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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.event.types.IntegerDeclaration;
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.TypeAliasParser;
23+
import org.junit.Test;
24+
25+
import com.google.gson.JsonObject;
26+
import com.google.gson.JsonPrimitive;
27+
28+
/**
29+
* Test class for TypeAliasParser CTF2 field type support
30+
*/
31+
public class TypeAliasParserTest {
32+
33+
/**
34+
* Test parsing fixed-length floating point field class
35+
*
36+
* @throws Exception if parsing fails
37+
*/
38+
@Test
39+
public void testFixedLengthFloatingPointParsing() throws Exception {
40+
CTFTrace trace = new CTFTrace();
41+
DeclarationScope scope = new DeclarationScope(null, "test");
42+
43+
JsonObject fieldClass = new JsonObject();
44+
fieldClass.add("type", new JsonPrimitive("fixed-length-floating-point-number"));
45+
fieldClass.add("length", new JsonPrimitive(32));
46+
fieldClass.add("byte-order", new JsonPrimitive("le"));
47+
48+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "fixed-length-floating-point-number", "test", "float_field", fieldClass);
49+
50+
IDeclaration result = TypeAliasParser.INSTANCE.parse(node,
51+
new TypeAliasParser.Param(trace, scope));
52+
53+
assertNotNull(result);
54+
assertTrue(result instanceof FloatDeclaration);
55+
56+
FloatDeclaration floatDecl = (FloatDeclaration) result;
57+
assertTrue(floatDecl.getExponent() > 0);
58+
assertTrue(floatDecl.getMantissa() > 0);
59+
}
60+
61+
/**
62+
* Test parsing static-length string field class
63+
*
64+
* @throws Exception if parsing fails
65+
*/
66+
@Test
67+
public void testStaticLengthStringParsing() throws Exception {
68+
CTFTrace trace = new CTFTrace();
69+
DeclarationScope scope = new DeclarationScope(null, "test");
70+
71+
JsonObject fieldClass = new JsonObject();
72+
fieldClass.add("type", new JsonPrimitive("static-length-string"));
73+
fieldClass.add("length", new JsonPrimitive(16));
74+
fieldClass.add("encoding", new JsonPrimitive("utf-8"));
75+
76+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "static-length-string", "test", "string_field", fieldClass);
77+
78+
IDeclaration result = TypeAliasParser.INSTANCE.parse(node,
79+
new TypeAliasParser.Param(trace, scope));
80+
81+
assertNotNull(result);
82+
}
83+
84+
/**
85+
* Test parsing dynamic-length string field class
86+
*
87+
* @throws Exception if parsing fails
88+
*/
89+
@Test
90+
public void testDynamicLengthStringParsing() throws Exception {
91+
// Test that the parser doesn't throw an exception for dynamic length strings
92+
// The actual parsing logic may not be fully implemented yet
93+
CTFTrace trace = new CTFTrace();
94+
DeclarationScope scope = new DeclarationScope(null, "test");
95+
96+
// Register a length field in the scope
97+
IntegerDeclaration lengthDecl = IntegerDeclaration.UINT_5L_DECL;
98+
scope.registerIdentifier("length", lengthDecl);
99+
100+
JsonObject fieldClass = new JsonObject();
101+
fieldClass.add("type", new JsonPrimitive("dynamic-length-string"));
102+
fieldClass.add("encoding", new JsonPrimitive("utf-8"));
103+
JsonObject lengthFieldLocation = new JsonObject();
104+
lengthFieldLocation.add("path", new JsonPrimitive("length"));
105+
fieldClass.add("length-field-location", lengthFieldLocation);
106+
107+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "dynamic-length-string", "test", "dyn_string_field", fieldClass);
108+
109+
IDeclaration result = TypeAliasParser.INSTANCE.parse(node,
110+
new TypeAliasParser.Param(trace, scope));
111+
// If we get here without exception, the basic parsing works
112+
assertNotNull(result);
113+
114+
}
115+
}

0 commit comments

Comments
 (0)