Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | package org.eclipse.linuxtools.ctf.core.tests.types; |
2 | ||
3 | import static org.junit.Assert.assertEquals; | |
4 | import static org.junit.Assert.assertNotNull; | |
5 | import static org.junit.Assert.assertTrue; | |
6 | ||
7 | import java.nio.ByteOrder; | |
8 | ||
9 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; | |
10 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
11 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
12 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; | |
13 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; | |
14 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; | |
15 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
16 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
25a9b50c | 17 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 FC |
18 | import org.junit.After; |
19 | import org.junit.Before; | |
20 | import org.junit.Test; | |
21 | ||
22 | /** | |
23 | * The class <code>SequenceDefinitionTest</code> contains tests for the class | |
24 | * <code>{@link SequenceDefinition}</code>. | |
25 | * | |
26 | * @author ematkho | |
27 | * @version $Revision: 1.0 $ | |
28 | */ | |
29 | public class SequenceDefinitionTest { | |
30 | ||
31 | private SequenceDefinition fixture; | |
32 | private final static int seqLen = 15; | |
33 | ||
34 | /** | |
35 | * Launch the test. | |
36 | * | |
37 | * @param args | |
38 | * the command line arguments | |
39 | */ | |
40 | public static void main(String[] args) { | |
41 | new org.junit.runner.JUnitCore().run(SequenceDefinitionTest.class); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Perform pre-test initialization. | |
25a9b50c | 46 | * @throws CTFReaderException |
866e5b51 FC |
47 | */ |
48 | @Before | |
25a9b50c | 49 | public void setUp() throws CTFReaderException { |
866e5b51 FC |
50 | StructDeclaration structDec; |
51 | StructDefinition structDef; | |
52 | ||
53 | IntegerDeclaration id = new IntegerDeclaration(8, false, 8, | |
54 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8); | |
55 | String lengthName = "LengthName"; //$NON-NLS-1$ | |
56 | structDec = new StructDeclaration(0); | |
57 | structDec.addField(lengthName, id); | |
58 | structDef = new StructDefinition(structDec, null, "x"); //$NON-NLS-1$ | |
59 | ||
60 | structDef.lookupInteger(lengthName).setValue(seqLen); | |
61 | SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); | |
62 | fixture = new SequenceDefinition(sd, structDef, "TestX"); //$NON-NLS-1$ | |
63 | BitBuffer input = new BitBuffer( | |
64 | java.nio.ByteBuffer.allocateDirect(seqLen * 8)); | |
65 | for (int i = 0; i < seqLen; i++) { | |
66 | input.putInt(i); | |
67 | } | |
68 | fixture.read(input); | |
69 | assert (fixture != null); | |
70 | } | |
71 | ||
72 | /** | |
73 | * Perform post-test clean-up. | |
74 | */ | |
75 | @After | |
76 | public void tearDown() { | |
77 | // Add additional tear down code here | |
78 | } | |
79 | ||
25a9b50c | 80 | private static SequenceDefinition initNonString() throws CTFReaderException { |
866e5b51 FC |
81 | StructDeclaration structDec; |
82 | StructDefinition structDef; | |
83 | ||
84 | int len = 32; | |
85 | IntegerDeclaration id = new IntegerDeclaration(len, false, len, | |
86 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8); | |
87 | String lengthName = "LengthName"; //$NON-NLS-1$ | |
88 | structDec = new StructDeclaration(0); | |
89 | structDec.addField(lengthName, id); | |
90 | structDef = new StructDefinition(structDec, null, "x"); //$NON-NLS-1$ | |
91 | ||
92 | structDef.lookupInteger(lengthName).setValue(seqLen); | |
93 | SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); | |
94 | SequenceDefinition ret = new SequenceDefinition(sd, structDef, "TestX"); //$NON-NLS-1$ | |
95 | BitBuffer input = new BitBuffer( | |
96 | java.nio.ByteBuffer.allocateDirect(seqLen * len)); | |
97 | for (int i = 0; i < seqLen; i++) { | |
98 | input.putInt(i); | |
99 | } | |
100 | ret.read(input); | |
101 | assertNotNull(ret); | |
102 | return ret; | |
103 | } | |
104 | ||
105 | /** | |
106 | * Run the SequenceDefinition(SequenceDeclaration,DefinitionScope,String) | |
107 | * constructor test. | |
108 | */ | |
109 | @Test | |
110 | public void testSequenceDefinition() { | |
111 | assertNotNull(fixture); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Run the SequenceDeclaration getDeclaration() method test. | |
116 | */ | |
117 | @Test | |
118 | public void testGetDeclaration() { | |
119 | SequenceDeclaration result = fixture.getDeclaration(); | |
120 | assertNotNull(result); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Run the Definition getElem(int) method test. | |
125 | */ | |
126 | @Test | |
127 | public void testGetElem() { | |
128 | int i = 1; | |
129 | Definition result = fixture.getElem(i); | |
130 | assertNotNull(result); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Run the int getLength() method test. | |
135 | */ | |
136 | @Test | |
137 | public void testGetLength() { | |
138 | int result = fixture.getLength(); | |
139 | ||
140 | assertEquals(seqLen, result); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Run the boolean isString() method test. | |
145 | */ | |
146 | @Test | |
147 | public void testIsString() { | |
148 | boolean result = fixture.isString(); | |
149 | assertTrue(result); | |
150 | } | |
151 | ||
152 | /** | |
153 | * Run the void read(BitBuffer) method test. | |
154 | */ | |
155 | @Test | |
156 | public void testRead() { | |
157 | BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); | |
158 | fixture.read(input); | |
159 | } | |
160 | ||
161 | /** | |
162 | * Run the String toString() method test. | |
163 | */ | |
164 | @Test | |
165 | public void testToString() { | |
166 | String result = fixture.toString(); | |
167 | assertNotNull(result); | |
168 | } | |
169 | ||
170 | /** | |
171 | * Run the String toString() method test. | |
172 | */ | |
173 | @Test | |
174 | public void testToString_nonString() throws Exception { | |
175 | SequenceDefinition nonStringFixture = initNonString(); | |
176 | String result = nonStringFixture.toString(); | |
177 | assertNotNull(result); | |
178 | } | |
179 | } |