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