| 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 | |
| 12 | package org.eclipse.linuxtools.ctf.core.tests.types; |
| 13 | |
| 14 | import static org.junit.Assert.assertNotNull; |
| 15 | |
| 16 | import java.nio.ByteBuffer; |
| 17 | import java.nio.ByteOrder; |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Arrays; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.eclipse.jdt.annotation.NonNull; |
| 23 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
| 24 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; |
| 25 | import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; |
| 26 | import org.eclipse.linuxtools.ctf.core.event.types.CompoundDeclaration; |
| 27 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
| 28 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; |
| 29 | import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; |
| 30 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinition; |
| 31 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
| 32 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
| 33 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; |
| 34 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; |
| 35 | import org.eclipse.linuxtools.ctf.core.tests.io.Util; |
| 36 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
| 37 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; |
| 38 | import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration; |
| 39 | import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDefinition; |
| 40 | import org.junit.Before; |
| 41 | import org.junit.Test; |
| 42 | |
| 43 | /** |
| 44 | * The class <code>ArrayDefinition2Test</code> contains tests for the class |
| 45 | * <code>{@link ArrayDefinition}</code>. |
| 46 | * |
| 47 | */ |
| 48 | public class ArrayDefinition2Test { |
| 49 | |
| 50 | private @NonNull CTFTrace trace = new CTFTrace(); |
| 51 | private ArrayDefinition charArrayFixture; |
| 52 | private ArrayDefinition stringArrayFixture; |
| 53 | private ArrayDefinition longArrayFixture; |
| 54 | |
| 55 | /** |
| 56 | * Perform pre-test initialization. |
| 57 | * |
| 58 | * structDef shouldn't be null after parsing the CTFTraceReader object, so |
| 59 | * we can ignore the warning. |
| 60 | */ |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | charArrayFixture = createCharArray(); |
| 64 | stringArrayFixture = createStringArray(); |
| 65 | longArrayFixture = createLongArray(); |
| 66 | } |
| 67 | |
| 68 | private ArrayDefinition createLongArray() { |
| 69 | IntegerDeclaration decl = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none", 8); |
| 70 | List<Definition> defs = createIntDefs(10, 32); |
| 71 | ArrayDefinition temp = setUpDeclaration(decl, defs); |
| 72 | return temp; |
| 73 | } |
| 74 | |
| 75 | private ArrayDefinition createCharArray() { |
| 76 | IntegerDeclaration decl = IntegerDeclaration.createDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none", 8); |
| 77 | List<Definition> defs = createIntDefs(4, 8); |
| 78 | ArrayDefinition temp = setUpDeclaration(decl, defs); |
| 79 | return temp; |
| 80 | } |
| 81 | |
| 82 | private ArrayDefinition createStringArray() { |
| 83 | StringDeclaration strDecl = new StringDeclaration(); |
| 84 | List<Definition> defs = createDefs(); |
| 85 | ArrayDefinition temp = setUpDeclaration(strDecl, defs); |
| 86 | return temp; |
| 87 | } |
| 88 | |
| 89 | private ArrayDefinition setUpDeclaration(IDeclaration decl, |
| 90 | @NonNull List<Definition> defs) { |
| 91 | CompoundDeclaration ad = new ArrayDeclaration(0, decl); |
| 92 | ArrayDefinition temp = new ArrayDefinition(ad, this.trace, "Testx", defs); |
| 93 | return temp; |
| 94 | } |
| 95 | |
| 96 | @NonNull |
| 97 | private static List<Definition> createIntDefs(int size, int bits) { |
| 98 | List<Definition> defs = new ArrayList<>(size); |
| 99 | for (int i = 0; i < size; i++) { |
| 100 | String content = "test" + i; |
| 101 | defs.add(new IntegerDefinition(IntegerDeclaration.createDeclaration(bits, false, |
| 102 | 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content, i)); |
| 103 | } |
| 104 | return defs; |
| 105 | } |
| 106 | |
| 107 | @NonNull |
| 108 | private static List<Definition> createDefs() { |
| 109 | int size = 4; |
| 110 | List<Definition> defs = new ArrayList<>(); |
| 111 | for (int i = 0; i < size; i++) { |
| 112 | String content = "test" + i; |
| 113 | defs.add(new StringDefinition( |
| 114 | new StringDeclaration(Encoding.UTF8), null, content, content)); |
| 115 | } |
| 116 | return defs; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String) |
| 121 | * constructor test. |
| 122 | */ |
| 123 | @Test |
| 124 | public void testArrayDefinition_baseDeclaration() { |
| 125 | CompoundDeclaration declaration = (CompoundDeclaration) charArrayFixture.getDeclaration(); |
| 126 | String fieldName = ""; |
| 127 | |
| 128 | @SuppressWarnings("null") |
| 129 | ArrayDefinition result = new ArrayDefinition(declaration, this.trace, fieldName, Arrays.asList(new Definition[0])); |
| 130 | assertNotNull(result); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String) |
| 135 | * constructor test. |
| 136 | */ |
| 137 | @Test |
| 138 | public void testArrayDefinition_newDeclaration() { |
| 139 | CompoundDeclaration declaration = new ArrayDeclaration(0, |
| 140 | new StringDeclaration()); |
| 141 | IDefinitionScope definitionScope = getDefinitionScope(); |
| 142 | |
| 143 | String fieldName = ""; |
| 144 | @SuppressWarnings("null") |
| 145 | ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName, Arrays.asList(new Definition[0])); |
| 146 | assertNotNull(result); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Run the ArrayDeclaration getDeclaration() method test. |
| 151 | */ |
| 152 | @Test |
| 153 | public void testGetDeclaration() { |
| 154 | CompoundDeclaration result = (CompoundDeclaration) charArrayFixture.getDeclaration(); |
| 155 | |
| 156 | assertNotNull(result); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Run the Definition getDefinitions().get(int) method test. |
| 161 | */ |
| 162 | @Test |
| 163 | public void testgetElem_noDefs() { |
| 164 | int i = 0; |
| 165 | IDefinition result = charArrayFixture.getDefinitions().get(i); |
| 166 | |
| 167 | assertNotNull(result); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Run the Definition getDefinitions().get(int) method test. |
| 172 | */ |
| 173 | @Test |
| 174 | public void testgetElem_withDefs() { |
| 175 | List<Definition> defs = createDefs(); |
| 176 | IDefinitionScope definitionScope = getDefinitionScope(); |
| 177 | ArrayDefinition ad = new ArrayDefinition((CompoundDeclaration) charArrayFixture.getDeclaration(), definitionScope, "test", defs); |
| 178 | int j = 1; |
| 179 | |
| 180 | IDefinition result = ad.getDefinitions().get(j); |
| 181 | |
| 182 | assertNotNull(result); |
| 183 | } |
| 184 | |
| 185 | @NonNull |
| 186 | private static IDefinitionScope getDefinitionScope() { |
| 187 | return new IDefinitionScope() { |
| 188 | |
| 189 | @Override |
| 190 | public Definition lookupDefinition(String lookupPath) { |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public LexicalScope getScopePath() { |
| 196 | return null; |
| 197 | } |
| 198 | }; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Run the void read(BitBuffer) method test. |
| 203 | * |
| 204 | * @throws CTFReaderException |
| 205 | * error |
| 206 | */ |
| 207 | @Test |
| 208 | public void testRead_noDefs() throws CTFReaderException { |
| 209 | BitBuffer input = new BitBuffer(Util.testMemory(ByteBuffer.allocateDirect(128))); |
| 210 | charArrayFixture.getDeclaration().createDefinition(null, "test", input); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Run the String toString() method test. |
| 215 | */ |
| 216 | @Test |
| 217 | public void testToString_char() { |
| 218 | String result = charArrayFixture.toString(); |
| 219 | assertNotNull(result); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Run the String toString() method test. |
| 224 | */ |
| 225 | @Test |
| 226 | public void testToString_long() { |
| 227 | String result = longArrayFixture.toString(); |
| 228 | assertNotNull(result); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Run the String toString() method test. |
| 233 | */ |
| 234 | @Test |
| 235 | public void testToString_string() { |
| 236 | String result = stringArrayFixture.toString(); |
| 237 | assertNotNull(result); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Run the String toString() method test. |
| 242 | */ |
| 243 | @Test |
| 244 | public void testToString_withDefs() { |
| 245 | String result = charArrayFixture.toString(); |
| 246 | |
| 247 | assertNotNull(result); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Run the String toString() method test. |
| 252 | */ |
| 253 | @Test |
| 254 | public void testToStringStringArray() { |
| 255 | String result = stringArrayFixture.toString(); |
| 256 | |
| 257 | assertNotNull(result); |
| 258 | } |
| 259 | } |