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.types; |
13 | ||
14 | import static org.junit.Assert.assertEquals; | |
15 | import static org.junit.Assert.assertNotNull; | |
16 | import static org.junit.Assert.assertTrue; | |
17 | ||
18 | import java.nio.ByteOrder; | |
19 | ||
486efb2e | 20 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
866e5b51 FC |
21 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
22 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
23 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; | |
a4fa4e36 | 24 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
866e5b51 FC |
25 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; |
26 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; | |
27 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
28 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
25a9b50c | 29 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 FC |
30 | import org.junit.Before; |
31 | import org.junit.Test; | |
32 | ||
a4fa4e36 MK |
33 | import com.google.common.collect.ImmutableList; |
34 | ||
866e5b51 FC |
35 | /** |
36 | * The class <code>SequenceDefinitionTest</code> contains tests for the class | |
37 | * <code>{@link SequenceDefinition}</code>. | |
284fdee8 | 38 | * |
866e5b51 FC |
39 | * @author ematkho |
40 | * @version $Revision: 1.0 $ | |
41 | */ | |
be6df2d8 | 42 | @SuppressWarnings("javadoc") |
866e5b51 FC |
43 | public class SequenceDefinitionTest { |
44 | ||
45 | private SequenceDefinition fixture; | |
46 | private final static int seqLen = 15; | |
47 | ||
a4fa4e36 MK |
48 | private static ImmutableList<String> wrap(String s) { |
49 | return ImmutableList.<String> builder().add(s).build(); | |
50 | } | |
51 | ||
866e5b51 FC |
52 | /** |
53 | * Perform pre-test initialization. | |
a4fa4e36 | 54 | * |
fd74e6c1 | 55 | * @throws CTFReaderException |
866e5b51 FC |
56 | */ |
57 | @Before | |
25a9b50c | 58 | public void setUp() throws CTFReaderException { |
866e5b51 FC |
59 | StructDeclaration structDec; |
60 | StructDefinition structDef; | |
61 | ||
a4fa4e36 MK |
62 | IntegerDeclaration id = IntegerDeclaration.createDeclaration(8, false, 8, |
63 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8); | |
4a9c1f07 | 64 | String lengthName = "LengthName"; |
866e5b51 FC |
65 | structDec = new StructDeclaration(0); |
66 | structDec.addField(lengthName, id); | |
a4fa4e36 MK |
67 | structDef = new StructDefinition(structDec, null, "x", |
68 | wrap(lengthName), | |
69 | new Definition[] { new IntegerDefinition(id, null, lengthName, seqLen) }); | |
866e5b51 | 70 | |
866e5b51 | 71 | SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); |
866e5b51 FC |
72 | BitBuffer input = new BitBuffer( |
73 | java.nio.ByteBuffer.allocateDirect(seqLen * 8)); | |
74 | for (int i = 0; i < seqLen; i++) { | |
75 | input.putInt(i); | |
76 | } | |
a4fa4e36 MK |
77 | |
78 | fixture = sd.createDefinition(structDef, "TestX", input); | |
866e5b51 FC |
79 | assert (fixture != null); |
80 | } | |
81 | ||
25a9b50c | 82 | private static SequenceDefinition initNonString() throws CTFReaderException { |
866e5b51 FC |
83 | StructDeclaration structDec; |
84 | StructDefinition structDef; | |
85 | ||
86 | int len = 32; | |
a4fa4e36 MK |
87 | IntegerDeclaration id = IntegerDeclaration.createDeclaration(len, false, len, |
88 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8); | |
4a9c1f07 | 89 | String lengthName = "LengthName"; |
866e5b51 FC |
90 | structDec = new StructDeclaration(0); |
91 | structDec.addField(lengthName, id); | |
866e5b51 | 92 | |
a4fa4e36 MK |
93 | structDef = new StructDefinition(structDec, null, "x", wrap(lengthName), new Definition[] { new IntegerDefinition(id, null, lengthName, seqLen) }); |
94 | ||
866e5b51 | 95 | SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); |
866e5b51 FC |
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 | } | |
a4fa4e36 MK |
101 | |
102 | SequenceDefinition ret = sd.createDefinition(structDef, "TestX", input); | |
866e5b51 FC |
103 | assertNotNull(ret); |
104 | return ret; | |
105 | } | |
106 | ||
107 | /** | |
108 | * Run the SequenceDefinition(SequenceDeclaration,DefinitionScope,String) | |
109 | * constructor test. | |
110 | */ | |
111 | @Test | |
112 | public void testSequenceDefinition() { | |
113 | assertNotNull(fixture); | |
114 | } | |
115 | ||
116 | /** | |
117 | * Run the SequenceDeclaration getDeclaration() method test. | |
118 | */ | |
119 | @Test | |
120 | public void testGetDeclaration() { | |
121 | SequenceDeclaration result = fixture.getDeclaration(); | |
122 | assertNotNull(result); | |
123 | } | |
124 | ||
125 | /** | |
126 | * Run the Definition getElem(int) method test. | |
127 | */ | |
128 | @Test | |
129 | public void testGetElem() { | |
130 | int i = 1; | |
131 | Definition result = fixture.getElem(i); | |
132 | assertNotNull(result); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Run the int getLength() method test. | |
137 | */ | |
138 | @Test | |
139 | public void testGetLength() { | |
140 | int result = fixture.getLength(); | |
141 | ||
142 | assertEquals(seqLen, result); | |
143 | } | |
144 | ||
145 | /** | |
146 | * Run the boolean isString() method test. | |
147 | */ | |
148 | @Test | |
149 | public void testIsString() { | |
a4fa4e36 | 150 | boolean result = fixture.getDeclaration().isString(); |
866e5b51 FC |
151 | assertTrue(result); |
152 | } | |
153 | ||
866e5b51 FC |
154 | /** |
155 | * Run the String toString() method test. | |
156 | */ | |
157 | @Test | |
158 | public void testToString() { | |
159 | String result = fixture.toString(); | |
160 | assertNotNull(result); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Run the String toString() method test. | |
165 | */ | |
166 | @Test | |
167 | public void testToString_nonString() throws Exception { | |
024373d7 MK |
168 | fixture = initNonString(); |
169 | String result = fixture.toString(); | |
866e5b51 FC |
170 | assertNotNull(result); |
171 | } | |
172 | } |