7ce4803986abd3e5374505b677e5df20533fefbf
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
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.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
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
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 CTFReaderException
41 * An error that cannot happen (position is under 128)
44 public void setUp() throws CTFReaderException
{
45 fixture
= new BitBuffer(java
.nio
.ByteBuffer
.allocateDirect(0));
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 assertEquals(null, result
.getByteBuffer());
63 * Run the BitBuffer(ByteBuffer) constructor test.
66 public void testBitBuffer_fromByteBuffer() {
67 ByteBuffer buf
= ByteBuffer
.allocate(0);
68 BitBuffer result
= new BitBuffer(buf
);
70 assertNotNull(result
);
71 assertEquals(0, result
.position());
75 * Run the boolean canRead(int) method test.
78 public void testCanRead_1param() {
80 boolean result
= fixture
.canRead(length
);
82 assertEquals(false, result
);
86 * Run the void clear() method test.
89 public void testClear() {
94 * Run the ByteBuffer getByteBuffer() method test.
97 public void testGetByteBuffer() {
98 ByteBuffer result
= fixture
.getByteBuffer();
100 assertNotNull(result
);
101 assertEquals("java.nio.DirectByteBuffer[pos=0 lim=0 cap=0]", result
.toString());
102 assertEquals(true, result
.isDirect());
103 assertEquals(false, result
.hasArray());
104 assertEquals(0, result
.limit());
105 assertEquals(0, result
.remaining());
106 assertEquals(0, result
.position());
107 assertEquals(0, result
.capacity());
108 assertEquals(false, result
.hasRemaining());
109 assertEquals(false, result
.isReadOnly());
113 * Run the ByteOrder getByteOrder() method test.
116 public void testGetByteOrder() {
117 ByteOrder result
= fixture
.getByteOrder();
119 assertNotNull(result
);
120 assertEquals("BIG_ENDIAN", result
.toString());
124 * Run the ByteOrder order() method test.
127 public void testGetOrder() {
128 ByteOrder result
= fixture
.getByteOrder();
130 assertNotNull(result
);
131 assertEquals("BIG_ENDIAN", result
.toString());
135 * Run the void order(ByteOrder) method test.
138 public void testSetOrder() {
139 ByteOrder order
= ByteOrder
.BIG_ENDIAN
;
141 fixture
.setByteOrder(order
);
145 * Run the int position() method test.
148 public void testGetPosition() {
149 long result
= fixture
.position();
151 assertEquals(1, result
);
155 * Run the void position(int) method test.
157 * @throws CTFReaderException
158 * out of bounds? won't happen
161 public void testSetPosition() throws CTFReaderException
{
163 fixture
.position(newPosition
);
167 * Run the void setByteOrder(ByteOrder) method test.
170 public void testSetByteOrder() {
171 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
172 fixture
.setByteOrder(byteOrder
);
176 * Test the get function
179 public void testGetBytes() {
181 byte[] data
= new byte[2];
182 ByteBuffer bb
= ByteBuffer
.allocate(10);
187 fixture
= new BitBuffer(bb
);
189 assertEquals(0, data
[0]);
190 assertEquals(1, data
[1]);
192 assertEquals(1, data
[0]);
193 assertEquals(0, data
[1]);
197 * Test the get function
199 * @throws CTFReaderException
200 * won't happen but we seek in a buffer
203 public void testGetBytesMiddle() throws CTFReaderException
{
205 byte[] data
= new byte[5];
206 // this string has been carefully selected and tested... don't change
207 // the string and expect the result to be the same.
208 ByteBuffer bb
= ByteBuffer
.wrap(new String("hello world").getBytes());
209 fixture
= new BitBuffer(bb
);
210 fixture
.position(6 * 8);
212 String actual
= new String(data
);
213 assertEquals("world", actual
);
This page took 0.064168 seconds and 5 git commands to generate.