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