Commit | Line | Data |
---|---|---|
3edb91f3 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal, Ericsson |
3edb91f3 GB |
3 | * |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Geneviève Bastien - Initial API and implementation | |
11 | * Alexandre Montplaisir - Split out in separate class | |
db8e8f7d | 12 | * Matthew Khouzam - update api (exceptions) |
3edb91f3 GB |
13 | *******************************************************************************/ |
14 | ||
f357bcd4 | 15 | package org.eclipse.tracecompass.ctf.core.tests.types; |
3edb91f3 GB |
16 | |
17 | import static org.junit.Assert.assertEquals; | |
18 | ||
19 | import java.nio.ByteBuffer; | |
20 | import java.nio.ByteOrder; | |
21 | ||
a4fa4e36 | 22 | import org.eclipse.jdt.annotation.NonNull; |
f357bcd4 AM |
23 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
24 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; | |
25 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; | |
26 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition; | |
27 | import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException; | |
3edb91f3 GB |
28 | import org.junit.Before; |
29 | import org.junit.Test; | |
30 | ||
31 | /** | |
32 | * Endianness test for {@link IntegerDefinition}. | |
33 | * | |
34 | * @author Geneviève Bastien | |
35 | */ | |
36 | public class IntegerEndiannessTest { | |
37 | ||
aefc5c83 MK |
38 | private static final @NonNull String name = "testInt"; |
39 | private static final @NonNull String clockName = "clock"; | |
3edb91f3 GB |
40 | |
41 | private ByteBuffer bb; | |
aefc5c83 MK |
42 | |
43 | private @NonNull BitBuffer input = new BitBuffer(); | |
3edb91f3 GB |
44 | |
45 | /** | |
46 | * Set up the bit-buffer to be used | |
47 | */ | |
48 | @Before | |
49 | public void setUp() { | |
50 | bb = java.nio.ByteBuffer.allocateDirect(8); | |
aefc5c83 MK |
51 | final ByteBuffer byb = bb; |
52 | if (byb == null) { | |
53 | throw new IllegalStateException("Failed to allocate memory"); | |
54 | } | |
3edb91f3 GB |
55 | bb.put((byte) 0xab); |
56 | bb.put((byte) 0xcd); | |
57 | bb.put((byte) 0xef); | |
58 | bb.put((byte) 0x12); | |
59 | bb.put((byte) 0x34); | |
60 | bb.put((byte) 0x56); | |
61 | bb.put((byte) 0x78); | |
62 | bb.put((byte) 0x9a); | |
aefc5c83 MK |
63 | |
64 | input = new BitBuffer(byb); | |
3edb91f3 GB |
65 | } |
66 | ||
db8e8f7d AM |
67 | /** |
68 | * Read 32-bits BE | |
69 | * | |
70 | * @throws CTFReaderException | |
71 | * error | |
72 | */ | |
3edb91f3 | 73 | @Test |
db8e8f7d | 74 | public void test32BE() throws CTFReaderException { |
a4fa4e36 MK |
75 | IntegerDeclaration be = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); |
76 | IntegerDefinition fixture_be = be.createDefinition(null, name, input); | |
3edb91f3 GB |
77 | assertEquals(0xabcdef12, fixture_be.getValue()); |
78 | } | |
79 | ||
db8e8f7d AM |
80 | /** |
81 | * Read 64-bits BE | |
82 | * | |
83 | * @throws CTFReaderException | |
84 | * error | |
85 | */ | |
3edb91f3 | 86 | @Test |
db8e8f7d | 87 | public void test64BE() throws CTFReaderException { |
a4fa4e36 MK |
88 | IntegerDeclaration be = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8); |
89 | IntegerDefinition fixture_be = be.createDefinition(null, name, input); | |
3edb91f3 GB |
90 | assertEquals(0xabcdef123456789aL, fixture_be.getValue()); |
91 | } | |
92 | ||
db8e8f7d AM |
93 | /** |
94 | * Read 32-bits LE | |
95 | * | |
96 | * @throws CTFReaderException | |
97 | * error | |
98 | */ | |
3edb91f3 | 99 | @Test |
db8e8f7d | 100 | public void test32LE() throws CTFReaderException { |
a4fa4e36 MK |
101 | IntegerDeclaration le = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8); |
102 | IntegerDefinition fixture_le = le.createDefinition(null, name, input); | |
3edb91f3 GB |
103 | assertEquals(0x12efcdab, fixture_le.getValue()); |
104 | } | |
105 | ||
db8e8f7d AM |
106 | /** |
107 | * Read 64-bits LE | |
108 | * | |
109 | * @throws CTFReaderException | |
110 | * error | |
111 | */ | |
3edb91f3 | 112 | @Test |
db8e8f7d | 113 | public void test64LE() throws CTFReaderException { |
a4fa4e36 MK |
114 | IntegerDeclaration le = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8); |
115 | IntegerDefinition fixture_le = le.createDefinition(null, name, input); | |
3edb91f3 GB |
116 | assertEquals(0x9a78563412efcdabL, fixture_le.getValue()); |
117 | } | |
118 | } |