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 | ||
f357bcd4 | 12 | package org.eclipse.tracecompass.ctf.core.tests.types; |
866e5b51 | 13 | |
7b4f13e6 MK |
14 | import static org.junit.Assert.assertEquals; |
15 | import static org.junit.Assert.assertFalse; | |
e00e6663 | 16 | import static org.junit.Assert.assertNotEquals; |
7b4f13e6 MK |
17 | import static org.junit.Assert.assertNotNull; |
18 | import static org.junit.Assert.assertTrue; | |
9639bf3a | 19 | |
a4fa4e36 | 20 | import java.nio.ByteBuffer; |
9639bf3a | 21 | import java.nio.ByteOrder; |
866e5b51 | 22 | |
f357bcd4 AM |
23 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
24 | import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope; | |
25 | import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition; | |
26 | import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration; | |
27 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; | |
28 | import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; | |
29 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; | |
30 | import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration; | |
31 | import org.eclipse.tracecompass.ctf.core.tests.io.Util; | |
32 | import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException; | |
33 | import org.eclipse.tracecompass.internal.ctf.core.event.types.ArrayDeclaration; | |
866e5b51 FC |
34 | import org.junit.Before; |
35 | import org.junit.Test; | |
36 | ||
37 | /** | |
7b4f13e6 | 38 | * The class <code>ArrayDeclaration2Test</code> contains tests for the class |
866e5b51 | 39 | * <code>{@link ArrayDeclaration}</code>. |
4bd7f2db | 40 | * |
7b4f13e6 | 41 | * @author Matthew Khouzam |
866e5b51 FC |
42 | * @version $Revision: 1.0 $ |
43 | */ | |
7b4f13e6 | 44 | public class ArrayDeclaration2Test { |
866e5b51 FC |
45 | |
46 | private ArrayDeclaration fixture; | |
47 | ||
866e5b51 FC |
48 | /** |
49 | * Perform pre-test initialization. | |
50 | */ | |
51 | @Before | |
52 | public void setUp() { | |
53 | fixture = new ArrayDeclaration(1, new StringDeclaration()); | |
54 | } | |
55 | ||
866e5b51 FC |
56 | /** |
57 | * Run the ArrayDeclaration(int,Declaration) constructor test. | |
58 | */ | |
59 | @Test | |
60 | public void testArrayDeclaration() { | |
61 | int length = 1; | |
62 | IDeclaration elemType = new StringDeclaration(); | |
63 | ArrayDeclaration result = new ArrayDeclaration(length, elemType); | |
64 | ||
65 | assertNotNull(result); | |
4a9c1f07 | 66 | String left = "[declaration] array["; |
866e5b51 FC |
67 | String right = result.toString().substring(0, left.length()); |
68 | assertEquals(left, right); | |
69 | assertEquals(1, result.getLength()); | |
70 | } | |
71 | ||
72 | /** | |
73 | * Run the ArrayDefinition createDefinition(DefinitionScope,String) method | |
74 | * test. | |
a4fa4e36 MK |
75 | * |
76 | * @throws CTFReaderException | |
77 | * error in the bitbuffer | |
866e5b51 FC |
78 | */ |
79 | @Test | |
a4fa4e36 | 80 | public void testCreateDefinition() throws CTFReaderException { |
4a9c1f07 | 81 | String fieldName = ""; |
866e5b51 | 82 | IDefinitionScope definitionScope = null; |
7b4f13e6 | 83 | AbstractArrayDefinition result; |
a4fa4e36 | 84 | byte[] array = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' }; |
aefc5c83 | 85 | BitBuffer bb = new BitBuffer(Util.testMemory(ByteBuffer.wrap(array))); |
a4fa4e36 | 86 | result = fixture.createDefinition(definitionScope, fieldName, bb); |
866e5b51 FC |
87 | |
88 | assertNotNull(result); | |
89 | } | |
90 | ||
91 | /** | |
92 | * Run the Declaration getElementType() method test. | |
93 | */ | |
94 | @Test | |
95 | public void testGetElementType() { | |
96 | IDeclaration result = fixture.getElementType(); | |
97 | assertNotNull(result); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Run the int getLength() method test. | |
102 | */ | |
103 | @Test | |
104 | public void testGetLength() { | |
105 | int result = fixture.getLength(); | |
106 | assertEquals(1, result); | |
107 | } | |
108 | ||
9639bf3a MK |
109 | /** |
110 | * Run the boolean isString() method test. | |
111 | */ | |
112 | @Test | |
113 | public void testIsString_ownDefs() { | |
a4fa4e36 | 114 | // it's an array of strings, not a string |
9639bf3a MK |
115 | assertFalse(fixture.isString()); |
116 | } | |
117 | ||
118 | /** | |
119 | * Run the boolean isString() method test. | |
120 | */ | |
121 | @Test | |
122 | public void testIsString_complex() { | |
a4fa4e36 MK |
123 | final IntegerDeclaration id = IntegerDeclaration.createDeclaration(8, false, 16, |
124 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8); | |
9377d173 | 125 | CompoundDeclaration ad = new ArrayDeclaration(0, id); |
9639bf3a MK |
126 | |
127 | boolean result = ad.isString(); | |
128 | ||
129 | assertTrue(result); | |
130 | } | |
131 | ||
866e5b51 FC |
132 | /** |
133 | * Run the String toString() method test. | |
134 | */ | |
135 | @Test | |
136 | public void testToString() { | |
137 | String result = fixture.toString(); | |
4a9c1f07 | 138 | String left = "[declaration] array["; |
866e5b51 FC |
139 | String right = result.substring(0, left.length()); |
140 | ||
141 | assertEquals(left, right); | |
142 | } | |
e00e6663 MK |
143 | |
144 | /** | |
145 | * Test the hashcode | |
146 | */ | |
147 | @Test | |
148 | public void hashcodeTest() { | |
149 | assertEquals(2016, fixture.hashCode()); | |
150 | assertEquals(new ArrayDeclaration(1, new StringDeclaration()).hashCode(), fixture.hashCode()); | |
151 | } | |
152 | ||
153 | /** | |
154 | * Test the equals | |
155 | */ | |
156 | @Test | |
157 | public void equalsTest() { | |
158 | ArrayDeclaration a = new ArrayDeclaration(1, IntegerDeclaration.INT_32B_DECL); | |
159 | ArrayDeclaration b = new ArrayDeclaration(2, IntegerDeclaration.INT_32B_DECL); | |
160 | ArrayDeclaration c = new ArrayDeclaration(1, new StringDeclaration()); | |
161 | ArrayDeclaration d = new ArrayDeclaration(1, IntegerDeclaration.INT_32B_DECL); | |
162 | assertNotEquals(a, null); | |
163 | assertNotEquals(a, new Object()); | |
164 | assertNotEquals(a, b); | |
165 | assertNotEquals(a, c); | |
166 | assertEquals(a, d); | |
167 | assertEquals(a, a); | |
168 | assertNotEquals(b, a); | |
169 | assertNotEquals(c, a); | |
170 | assertEquals(d, a); | |
171 | assertEquals(a, a); | |
f9d7c59f MK |
172 | assertFalse(a.isBinaryEquivalent(b)); |
173 | assertFalse(b.isBinaryEquivalent(a)); | |
174 | assertFalse(a.isBinaryEquivalent(c)); | |
175 | assertFalse(c.isBinaryEquivalent(a)); | |
176 | assertTrue(a.isBinaryEquivalent(d)); | |
177 | assertTrue(d.isBinaryEquivalent(a)); | |
178 | assertTrue(a.isBinaryEquivalent(a)); | |
e00e6663 | 179 | } |
866e5b51 | 180 | } |