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 | ||
866e5b51 | 14 | import static org.junit.Assert.assertNotNull; |
866e5b51 | 15 | |
aefc5c83 | 16 | import java.nio.ByteBuffer; |
866e5b51 FC |
17 | import java.nio.ByteOrder; |
18 | ||
486efb2e | 19 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
866e5b51 FC |
20 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
21 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
22 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; | |
a4fa4e36 | 23 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
866e5b51 FC |
24 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; |
25 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
25a9b50c | 26 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
7b4f13e6 MK |
27 | import org.eclipse.linuxtools.internal.ctf.core.event.types.ByteArrayDefinition; |
28 | import org.eclipse.linuxtools.internal.ctf.core.event.types.SequenceDeclaration; | |
866e5b51 FC |
29 | import org.junit.Before; |
30 | import org.junit.Test; | |
31 | ||
a4fa4e36 MK |
32 | import com.google.common.collect.ImmutableList; |
33 | ||
866e5b51 | 34 | /** |
7b4f13e6 MK |
35 | * The class <code>SequenceDefinition2Test</code> contains tests for the class |
36 | * <code>{@link SequenceDefinition2}</code>. | |
284fdee8 | 37 | * |
866e5b51 FC |
38 | * @author ematkho |
39 | * @version $Revision: 1.0 $ | |
40 | */ | |
be6df2d8 | 41 | @SuppressWarnings("javadoc") |
7b4f13e6 | 42 | public class SequenceDefinition2Test { |
866e5b51 | 43 | |
7b4f13e6 | 44 | private ByteArrayDefinition fixture; |
866e5b51 FC |
45 | private final static int seqLen = 15; |
46 | ||
a4fa4e36 MK |
47 | private static ImmutableList<String> wrap(String s) { |
48 | return ImmutableList.<String> builder().add(s).build(); | |
49 | } | |
50 | ||
866e5b51 FC |
51 | /** |
52 | * Perform pre-test initialization. | |
a4fa4e36 | 53 | * |
fd74e6c1 | 54 | * @throws CTFReaderException |
866e5b51 FC |
55 | */ |
56 | @Before | |
25a9b50c | 57 | public void setUp() throws CTFReaderException { |
7b4f13e6 | 58 | fixture = initString(); |
866e5b51 FC |
59 | } |
60 | ||
7b4f13e6 | 61 | private static ByteArrayDefinition initString() throws CTFReaderException { |
866e5b51 FC |
62 | StructDeclaration structDec; |
63 | StructDefinition structDef; | |
64 | ||
7b4f13e6 | 65 | int len = 8; |
a4fa4e36 MK |
66 | IntegerDeclaration id = IntegerDeclaration.createDeclaration(len, false, len, |
67 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8); | |
4a9c1f07 | 68 | String lengthName = "LengthName"; |
866e5b51 FC |
69 | structDec = new StructDeclaration(0); |
70 | structDec.addField(lengthName, id); | |
866e5b51 | 71 | |
a4fa4e36 MK |
72 | structDef = new StructDefinition(structDec, null, "x", wrap(lengthName), new Definition[] { new IntegerDefinition(id, null, lengthName, seqLen) }); |
73 | ||
866e5b51 | 74 | SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); |
aefc5c83 MK |
75 | ByteBuffer allocateDirect = java.nio.ByteBuffer.allocateDirect(seqLen * len); |
76 | if( allocateDirect == null){ | |
77 | throw new IllegalStateException("Failed to allocate memory"); | |
78 | } | |
79 | BitBuffer input = new BitBuffer(allocateDirect); | |
866e5b51 FC |
80 | for (int i = 0; i < seqLen; i++) { |
81 | input.putInt(i); | |
82 | } | |
a4fa4e36 | 83 | |
7b4f13e6 | 84 | ByteArrayDefinition ret = (ByteArrayDefinition) sd.createDefinition(structDef, "TestX", input); |
866e5b51 FC |
85 | assertNotNull(ret); |
86 | return ret; | |
87 | } | |
88 | ||
89 | /** | |
7b4f13e6 | 90 | * Run the FixedStringDefinition(SequenceDeclaration,DefinitionScope,String) |
866e5b51 FC |
91 | * constructor test. |
92 | */ | |
93 | @Test | |
7b4f13e6 | 94 | public void testFixedStringDefinition() { |
866e5b51 FC |
95 | assertNotNull(fixture); |
96 | } | |
97 | ||
98 | /** | |
99 | * Run the SequenceDeclaration getDeclaration() method test. | |
100 | */ | |
101 | @Test | |
102 | public void testGetDeclaration() { | |
7b4f13e6 | 103 | SequenceDeclaration result = (SequenceDeclaration) fixture.getDeclaration(); |
866e5b51 FC |
104 | assertNotNull(result); |
105 | } | |
106 | ||
107 | /** | |
108 | * Run the Definition getElem(int) method test. | |
109 | */ | |
110 | @Test | |
111 | public void testGetElem() { | |
112 | int i = 1; | |
7b4f13e6 | 113 | Definition result = fixture.getDefinitions().get(i); |
866e5b51 FC |
114 | assertNotNull(result); |
115 | } | |
116 | ||
866e5b51 FC |
117 | /** |
118 | * Run the String toString() method test. | |
119 | */ | |
120 | @Test | |
121 | public void testToString() { | |
122 | String result = fixture.toString(); | |
123 | assertNotNull(result); | |
124 | } | |
125 | ||
866e5b51 | 126 | } |