Commit | Line | Data |
---|---|---|
4bd7f2db AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 Ericsson | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Matthew Khouzam - Initial API and implementation | |
10 | *******************************************************************************/ | |
11 | ||
866e5b51 FC |
12 | package org.eclipse.linuxtools.ctf.core.tests.event; |
13 | ||
14 | import static org.junit.Assert.assertEquals; | |
15 | import static org.junit.Assert.assertNotNull; | |
16 | ||
17 | import java.nio.ByteBuffer; | |
18 | import java.nio.ByteOrder; | |
19 | ||
a4fa4e36 | 20 | import org.eclipse.jdt.annotation.NonNull; |
486efb2e | 21 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
866e5b51 FC |
22 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
23 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
cc98c947 | 24 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinition; |
866e5b51 FC |
25 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
26 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
866e5b51 FC |
27 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; |
28 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; | |
29 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
30 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
25a9b50c | 31 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
7b4f13e6 | 32 | import org.eclipse.linuxtools.internal.ctf.core.event.types.SequenceDeclaration; |
866e5b51 FC |
33 | import org.junit.Test; |
34 | ||
a4fa4e36 MK |
35 | import com.google.common.collect.ImmutableList; |
36 | ||
866e5b51 FC |
37 | /** |
38 | * The class <code>CTFEventFieldTest</code> contains tests for the class | |
39 | * <code>{@link CTFEventField}</code>. | |
284fdee8 | 40 | * |
866e5b51 FC |
41 | * @author ematkho |
42 | * @version $Revision: 1.0 $ | |
866e5b51 | 43 | */ |
be6df2d8 | 44 | @SuppressWarnings("javadoc") |
866e5b51 | 45 | public class CTFEventFieldTest { |
284fdee8 | 46 | |
aefc5c83 MK |
47 | @NonNull |
48 | private static final String fieldName = "id"; | |
284fdee8 | 49 | |
866e5b51 FC |
50 | /** |
51 | * Run the CTFEventField parseField(Definition,String) method test. | |
a4fa4e36 | 52 | * |
fd74e6c1 | 53 | * @throws CTFReaderException |
866e5b51 FC |
54 | */ |
55 | @Test | |
25a9b50c | 56 | public void testParseField_complex() throws CTFReaderException { |
866e5b51 | 57 | int len = 32; |
a4fa4e36 MK |
58 | IntegerDeclaration id = IntegerDeclaration.createDeclaration( |
59 | len, | |
60 | false, | |
61 | len, | |
62 | ByteOrder.LITTLE_ENDIAN, | |
63 | Encoding.ASCII, | |
64 | "", | |
65 | len); | |
4a9c1f07 | 66 | String lengthName = "LengthName"; |
866e5b51 FC |
67 | StructDeclaration structDec = new StructDeclaration(0); |
68 | structDec.addField(lengthName, id); | |
a4fa4e36 MK |
69 | StructDefinition structDef = new StructDefinition( |
70 | structDec, | |
71 | null, | |
72 | lengthName, | |
73 | ImmutableList.of(lengthName), | |
74 | new Definition[] { | |
75 | new IntegerDefinition( | |
76 | id, | |
77 | null, | |
78 | lengthName, | |
79 | 32) | |
80 | }); | |
866e5b51 | 81 | |
866e5b51 | 82 | SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); |
aefc5c83 | 83 | ByteBuffer byb = testMemory(ByteBuffer.allocate(1024)); |
866e5b51 FC |
84 | for (int i = 0; i < 1024; i++) { |
85 | byb.put((byte) i); | |
86 | } | |
87 | BitBuffer bb = new BitBuffer(byb); | |
cc98c947 | 88 | IDefinition fieldDef = sd.createDefinition(structDef, "fff-fffield", bb); |
866e5b51 FC |
89 | |
90 | assertNotNull(fieldDef); | |
91 | } | |
92 | ||
aefc5c83 MK |
93 | @NonNull |
94 | private static ByteBuffer testMemory(ByteBuffer buffer) { | |
95 | if (buffer == null) { | |
96 | throw new IllegalStateException("Failed to allocate memory"); | |
97 | } | |
98 | return buffer; | |
99 | } | |
100 | ||
866e5b51 FC |
101 | /** |
102 | * Run the CTFEventField parseField(Definition,String) method test. | |
a4fa4e36 | 103 | * |
fd74e6c1 | 104 | * @throws CTFReaderException |
866e5b51 FC |
105 | */ |
106 | @Test | |
a4fa4e36 | 107 | public void testParseField_simple() throws CTFReaderException { |
4c9d2941 | 108 | final StringDeclaration elemType = new StringDeclaration(); |
a4fa4e36 | 109 | byte[] bytes = { 'T', 'e', 's', 't', '\0' }; |
aefc5c83 | 110 | ByteBuffer bb = testMemory(ByteBuffer.wrap(bytes)); |
cc98c947 | 111 | IDefinition fieldDef = elemType.createDefinition(null, fieldName, new BitBuffer(bb)); |
866e5b51 FC |
112 | |
113 | assertNotNull(fieldDef); | |
114 | } | |
284fdee8 | 115 | |
866e5b51 FC |
116 | /** |
117 | * Run the CTFEventField parseField(Definition,String) method test. | |
118 | */ | |
119 | @Test | |
120 | public void testParseField_simple2() { | |
121 | IntegerDefinition fieldDef = new IntegerDefinition( | |
a4fa4e36 MK |
122 | IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, |
123 | Encoding.ASCII, "", 8), null, fieldName, 1L); | |
866e5b51 FC |
124 | |
125 | assertNotNull(fieldDef); | |
126 | } | |
284fdee8 | 127 | |
866e5b51 | 128 | /** |
284fdee8 | 129 | * |
866e5b51 FC |
130 | */ |
131 | @Test | |
132 | public void testParseField_simple3() { | |
133 | StringDefinition fieldDef = new StringDefinition( | |
a4fa4e36 | 134 | new StringDeclaration(), null, fieldName, "Hello World"); |
866e5b51 | 135 | |
4a9c1f07 | 136 | String other = "\"Hello World\""; |
866e5b51 FC |
137 | assertNotNull(fieldDef); |
138 | assertEquals(fieldDef.toString(), other); | |
139 | } | |
140 | ||
866e5b51 | 141 | } |