1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal, Ericsson
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
10 * Geneviève Bastien - Initial API and implementation
11 * Alexandre Montplaisir - Split out in separate class
12 * Matthew Khouzam - update api (exceptions)
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.types
;
17 import static org
.junit
.Assert
.assertEquals
;
19 import java
.nio
.ByteBuffer
;
20 import java
.nio
.ByteOrder
;
22 import org
.eclipse
.jdt
.annotation
.NonNull
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.CTFReaderException
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Encoding
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
28 import org
.junit
.Before
;
29 import org
.junit
.Test
;
32 * Endianness test for {@link IntegerDefinition}.
34 * @author Geneviève Bastien
36 public class IntegerEndiannessTest
{
38 private static final @NonNull String name
= "testInt";
39 private static final @NonNull String clockName
= "clock";
41 private ByteBuffer bb
;
43 private @NonNull BitBuffer input
= new BitBuffer();
46 * Set up the bit-buffer to be used
50 bb
= java
.nio
.ByteBuffer
.allocateDirect(8);
51 final ByteBuffer byb
= bb
;
53 throw new IllegalStateException("Failed to allocate memory");
64 input
= new BitBuffer(byb
);
70 * @throws CTFReaderException
74 public void test32BE() throws CTFReaderException
{
75 IntegerDeclaration be
= IntegerDeclaration
.createDeclaration(32, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, clockName
, 8);
76 IntegerDefinition fixture_be
= be
.createDefinition(null, name
, input
);
77 assertEquals(0xabcdef12, fixture_be
.getValue());
83 * @throws CTFReaderException
87 public void test64BE() throws CTFReaderException
{
88 IntegerDeclaration be
= IntegerDeclaration
.createDeclaration(64, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, clockName
, 8);
89 IntegerDefinition fixture_be
= be
.createDefinition(null, name
, input
);
90 assertEquals(0xabcdef123456789aL
, fixture_be
.getValue());
96 * @throws CTFReaderException
100 public void test32LE() throws CTFReaderException
{
101 IntegerDeclaration le
= IntegerDeclaration
.createDeclaration(32, true, 1, ByteOrder
.LITTLE_ENDIAN
, Encoding
.NONE
, clockName
, 8);
102 IntegerDefinition fixture_le
= le
.createDefinition(null, name
, input
);
103 assertEquals(0x12efcdab, fixture_le
.getValue());
109 * @throws CTFReaderException
113 public void test64LE() throws CTFReaderException
{
114 IntegerDeclaration le
= IntegerDeclaration
.createDeclaration(64, true, 1, ByteOrder
.LITTLE_ENDIAN
, Encoding
.NONE
, clockName
, 8);
115 IntegerDefinition fixture_le
= le
.createDefinition(null, name
, input
);
116 assertEquals(0x9a78563412efcdabL
, fixture_le
.getValue());