1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.io
;
14 import static org
.junit
.Assert
.assertEquals
;
15 import static org
.junit
.Assert
.assertNotNull
;
17 import java
.nio
.ByteBuffer
;
18 import java
.nio
.ByteOrder
;
20 import org
.eclipse
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
23 import org
.junit
.Before
;
24 import org
.junit
.Test
;
27 * The class <code>BitBufferTest</code> contains tests for the class
28 * <code>{@link BitBuffer}</code>.
31 * @version $Revision: 1.0 $
33 public class BitBufferTest
{
35 private BitBuffer fixture
;
38 * Perform pre-test initialization.
40 * @throws CTFException
41 * An error that cannot happen (position is under 128)
44 public void setUp() throws CTFException
{
45 fixture
= new BitBuffer(Util
.testMemory(ByteBuffer
.allocateDirect(1)));
46 fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
51 * Run the BitBuffer() constructor test.
54 public void testBitBuffer() {
55 BitBuffer result
= new BitBuffer();
57 assertNotNull(result
);
58 assertEquals(0, result
.position());
59 assertNotNull(result
.getByteBuffer());
63 * Run the BitBuffer(ByteBuffer) constructor test.
66 public void testBitBuffer_fromByteBuffer() {
67 BitBuffer result
= new BitBuffer(Util
.testMemory(ByteBuffer
.allocate(0)));
68 assertNotNull(result
);
69 assertEquals(0, result
.position());
73 * Run the boolean canRead(int) method test.
76 public void testCanRead_1param() {
78 boolean result
= fixture
.canRead(length
);
80 assertEquals(true, result
);
84 * Run the void clear() method test.
87 public void testClear() {
92 * Run the ByteBuffer getByteBuffer() method test.
95 public void testGetByteBuffer() {
96 ByteBuffer result
= fixture
.getByteBuffer();
98 assertNotNull(result
);
99 assertEquals("java.nio.DirectByteBuffer[pos=0 lim=1 cap=1]", result
.toString());
100 assertEquals(true, result
.isDirect());
101 assertEquals(false, result
.hasArray());
102 assertEquals(1, result
.limit());
103 assertEquals(1, result
.remaining());
104 assertEquals(0, result
.position());
105 assertEquals(1, result
.capacity());
106 assertEquals(true, result
.hasRemaining());
107 assertEquals(false, result
.isReadOnly());
111 * Run the ByteOrder getByteOrder() method test.
114 public void testGetByteOrder() {
115 ByteOrder result
= fixture
.getByteOrder();
117 assertNotNull(result
);
118 assertEquals("BIG_ENDIAN", result
.toString());
122 * Run the ByteOrder order() method test.
125 public void testGetOrder() {
126 ByteOrder result
= fixture
.getByteOrder();
128 assertNotNull(result
);
129 assertEquals("BIG_ENDIAN", result
.toString());
133 * Run the void order(ByteOrder) method test.
136 public void testSetOrder() {
137 ByteOrder order
= ByteOrder
.BIG_ENDIAN
;
139 fixture
.setByteOrder(order
);
143 * Run the int position() method test.
146 public void testGetPosition() {
147 long result
= fixture
.position();
149 assertEquals(1, result
);
153 * Run the void position(int) method test.
155 * @throws CTFException
156 * out of bounds? won't happen
159 public void testSetPosition() throws CTFException
{
161 fixture
.position(newPosition
);
165 * Run the void setByteOrder(ByteOrder) method test.
168 public void testSetByteOrder() {
169 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
170 fixture
.setByteOrder(byteOrder
);
174 * Test the get function
177 public void testGetBytes() {
179 byte[] data
= new byte[2];
180 ByteBuffer bb
= ByteBuffer
.allocate(10);
185 fixture
= new BitBuffer(bb
);
187 assertEquals(0, data
[0]);
188 assertEquals(1, data
[1]);
190 assertEquals(1, data
[0]);
191 assertEquals(0, data
[1]);
195 * Test the get function
197 * @throws CTFException
198 * won't happen but we seek in a buffer
201 public void testGetBytesMiddle() throws CTFException
{
203 byte[] data
= new byte[5];
204 // this string has been carefully selected and tested... don't change
205 // the string and expect the result to be the same.
206 fixture
= new BitBuffer(Util
.testMemory(ByteBuffer
.wrap(new String("hello world").getBytes())));
207 fixture
.position(6 * 8);
209 String actual
= new String(data
);
210 assertEquals("world", actual
);
This page took 0.035497 seconds and 5 git commands to generate.