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; | |
e5acb357 | 16 | import static org.junit.Assume.assumeTrue; |
866e5b51 | 17 | |
a4fa4e36 MK |
18 | import java.nio.ByteBuffer; |
19 | ||
20 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; | |
21 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; | |
22 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
23 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; | |
24 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; | |
866e5b51 | 25 | import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; |
a4fa4e36 MK |
26 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
27 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
866e5b51 | 28 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; |
a4fa4e36 | 29 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; |
866e5b51 FC |
30 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; |
31 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
32 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; | |
33 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition; | |
9ac63b5b | 34 | import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace; |
13be1a8f | 35 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 FC |
36 | import org.junit.Before; |
37 | import org.junit.Test; | |
38 | ||
a4fa4e36 MK |
39 | import com.google.common.collect.ImmutableList; |
40 | ||
866e5b51 FC |
41 | /** |
42 | * The class <code>VariantDeclarationTest</code> contains tests for the class | |
43 | * <code>{@link VariantDeclaration}</code>. | |
be6df2d8 | 44 | * |
866e5b51 FC |
45 | * @author ematkho |
46 | * @version $Revision: 1.0 $ | |
47 | */ | |
48 | public class VariantDeclarationTest { | |
49 | ||
9ac63b5b | 50 | private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL; |
32bf80d2 | 51 | |
866e5b51 FC |
52 | private VariantDeclaration fixture; |
53 | ||
866e5b51 FC |
54 | /** |
55 | * Perform pre-test initialization. | |
56 | */ | |
57 | @Before | |
58 | public void setUp() { | |
59 | fixture = new VariantDeclaration(); | |
60 | } | |
61 | ||
a4fa4e36 MK |
62 | private static IDefinitionScope createDefinitionScope() throws CTFReaderException { |
63 | assumeTrue(testTrace.exists()); | |
64 | StructDeclaration declaration = new StructDeclaration(8); | |
65 | VariantDeclaration variantDeclaration = new VariantDeclaration(); | |
66 | variantDeclaration.addField("a", IntegerDeclaration.INT_32B_DECL); | |
67 | variantDeclaration.addField("b", IntegerDeclaration.INT_32L_DECL); | |
68 | variantDeclaration.setTag("a"); | |
69 | ||
70 | EnumDeclaration enumDeclaration = new EnumDeclaration(IntegerDeclaration.UINT_8_DECL); | |
71 | enumDeclaration.add(0, 1, "a"); | |
72 | enumDeclaration.add(2, 2, "b"); | |
73 | declaration.addField("tag", enumDeclaration); | |
74 | declaration.addField("variant", variantDeclaration); | |
75 | EnumDefinition tagDef = new EnumDefinition( | |
76 | enumDeclaration, | |
77 | null, | |
78 | "tag", | |
79 | new IntegerDefinition( | |
80 | IntegerDeclaration.UINT_8_DECL, | |
81 | null, | |
82 | "test", | |
83 | 0) | |
84 | ); | |
85 | VariantDefinition variantDefinition = new VariantDefinition( | |
86 | variantDeclaration, | |
87 | testTrace.getTrace(), | |
88 | "tag", | |
89 | "tag", | |
90 | new StringDefinition( | |
91 | new StringDeclaration(), | |
92 | null, | |
93 | "f", | |
94 | "tag" | |
95 | )); | |
96 | ||
97 | IDefinitionScope definitionScope = new StructDefinition( | |
98 | declaration, | |
99 | variantDefinition, | |
100 | "", | |
101 | ImmutableList.of("tag", variantDefinition.getCurrentFieldName()), | |
102 | new Definition[] { tagDef, variantDefinition } | |
103 | ); | |
104 | ||
105 | return definitionScope; | |
106 | } | |
107 | ||
866e5b51 FC |
108 | /** |
109 | * Run the VariantDeclaration() constructor test. | |
110 | */ | |
111 | @Test | |
112 | public void testVariantDeclaration() { | |
113 | assertNotNull(fixture); | |
114 | assertEquals(false, fixture.isTagged()); | |
4a9c1f07 | 115 | String left = "[declaration] variant["; |
866e5b51 FC |
116 | assertEquals(left, fixture.toString().substring(0, left.length())); |
117 | } | |
118 | ||
119 | /** | |
120 | * Run the void addField(String,Declaration) method test. | |
121 | */ | |
122 | @Test | |
123 | public void testAddField() { | |
4a9c1f07 AM |
124 | fixture.setTag(""); |
125 | String tag = ""; | |
866e5b51 FC |
126 | IDeclaration declaration = new StringDeclaration(); |
127 | fixture.addField(tag, declaration); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Run the VariantDefinition createDefinition(DefinitionScope,String) method | |
132 | * test. | |
be6df2d8 | 133 | * |
a4fa4e36 MK |
134 | * @throws CTFReaderException |
135 | * Should not happen | |
866e5b51 FC |
136 | */ |
137 | @Test | |
13be1a8f | 138 | public void testCreateDefinition() throws CTFReaderException { |
a4fa4e36 MK |
139 | fixture.setTag("tag"); |
140 | fixture.addField("a", IntegerDeclaration.UINT_64B_DECL); | |
866e5b51 | 141 | IDefinitionScope definitionScope = createDefinitionScope(); |
4a9c1f07 | 142 | String fieldName = ""; |
aefc5c83 MK |
143 | ByteBuffer allocate = ByteBuffer.allocate(100); |
144 | if( allocate == null){ | |
145 | throw new IllegalStateException("Failed to allocate memory"); | |
146 | } | |
147 | BitBuffer bb = new BitBuffer(allocate); | |
a4fa4e36 | 148 | VariantDefinition result = fixture.createDefinition(definitionScope, fieldName, bb); |
866e5b51 FC |
149 | |
150 | assertNotNull(result); | |
151 | } | |
152 | ||
866e5b51 FC |
153 | /** |
154 | * Run the boolean hasField(String) method test. | |
155 | */ | |
156 | @Test | |
157 | public void testHasField() { | |
4a9c1f07 AM |
158 | fixture.setTag(""); |
159 | String tag = ""; | |
866e5b51 FC |
160 | boolean result = fixture.hasField(tag); |
161 | ||
162 | assertEquals(false, result); | |
163 | } | |
164 | ||
165 | /** | |
166 | * Run the boolean isTagged() method test. | |
167 | */ | |
168 | @Test | |
169 | public void testIsTagged() { | |
4a9c1f07 | 170 | fixture.setTag(""); |
866e5b51 FC |
171 | boolean result = fixture.isTagged(); |
172 | ||
173 | assertEquals(true, result); | |
174 | } | |
175 | ||
176 | /** | |
177 | * Run the boolean isTagged() method test. | |
178 | */ | |
179 | @Test | |
180 | public void testIsTagged_null() { | |
181 | fixture.setTag((String) null); | |
182 | boolean result = fixture.isTagged(); | |
183 | ||
184 | assertEquals(false, result); | |
185 | } | |
186 | ||
187 | /** | |
188 | * Run the void setTag(String) method test. | |
189 | */ | |
190 | @Test | |
191 | public void testSetTag() { | |
4a9c1f07 AM |
192 | fixture.setTag(""); |
193 | String tag = ""; | |
866e5b51 FC |
194 | fixture.setTag(tag); |
195 | } | |
196 | ||
197 | /** | |
198 | * Run the String toString() method test. | |
199 | */ | |
200 | @Test | |
201 | public void testToString() { | |
4a9c1f07 | 202 | fixture.setTag(""); |
866e5b51 | 203 | String result = fixture.toString(); |
4a9c1f07 | 204 | String left = "[declaration] variant["; |
866e5b51 FC |
205 | String right = result.substring(0, left.length()); |
206 | ||
207 | assertEquals(left, right); | |
208 | } | |
209 | } |