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.io; |
13 | ||
14 | import static org.junit.Assert.assertEquals; | |
15 | import static org.junit.Assert.assertNotNull; | |
16 | ||
17 | import java.nio.ByteBuffer; | |
18 | import java.nio.ByteOrder; | |
19 | ||
231c4e1f | 20 | import org.eclipse.jdt.annotation.NonNull; |
486efb2e | 21 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
4c67e724 | 22 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 FC |
23 | import org.junit.Before; |
24 | import org.junit.Test; | |
25 | ||
26 | /** | |
27 | * The class <code>BitBufferTest</code> contains tests for the class | |
28 | * <code>{@link BitBuffer}</code>. | |
8b8e48ed | 29 | * |
866e5b51 FC |
30 | * @author ematkho |
31 | * @version $Revision: 1.0 $ | |
32 | */ | |
33 | public class BitBufferTest { | |
34 | ||
35 | private BitBuffer fixture; | |
36 | ||
866e5b51 FC |
37 | /** |
38 | * Perform pre-test initialization. | |
231c4e1f MK |
39 | * |
40 | * @throws CTFReaderException | |
41 | * An error that cannot happen (position is under 128) | |
866e5b51 FC |
42 | */ |
43 | @Before | |
4c67e724 | 44 | public void setUp() throws CTFReaderException { |
aefc5c83 | 45 | fixture = new BitBuffer(Util.testMemory(ByteBuffer.allocateDirect(1))); |
866e5b51 | 46 | fixture.setByteOrder(ByteOrder.BIG_ENDIAN); |
866e5b51 FC |
47 | fixture.position(1); |
48 | } | |
49 | ||
866e5b51 FC |
50 | /** |
51 | * Run the BitBuffer() constructor test. | |
52 | */ | |
53 | @Test | |
54 | public void testBitBuffer() { | |
55 | BitBuffer result = new BitBuffer(); | |
56 | ||
57 | assertNotNull(result); | |
58 | assertEquals(0, result.position()); | |
aefc5c83 | 59 | assertNotNull(result.getByteBuffer()); |
866e5b51 FC |
60 | } |
61 | ||
62 | /** | |
63 | * Run the BitBuffer(ByteBuffer) constructor test. | |
64 | */ | |
65 | @Test | |
66 | public void testBitBuffer_fromByteBuffer() { | |
aefc5c83 | 67 | BitBuffer result = new BitBuffer(Util.testMemory(ByteBuffer.allocate(0))); |
866e5b51 FC |
68 | assertNotNull(result); |
69 | assertEquals(0, result.position()); | |
70 | } | |
71 | ||
72 | /** | |
73 | * Run the boolean canRead(int) method test. | |
74 | */ | |
75 | @Test | |
76 | public void testCanRead_1param() { | |
77 | int length = 1; | |
78 | boolean result = fixture.canRead(length); | |
79 | ||
91052124 | 80 | assertEquals(true, result); |
866e5b51 FC |
81 | } |
82 | ||
866e5b51 FC |
83 | /** |
84 | * Run the void clear() method test. | |
85 | */ | |
86 | @Test | |
87 | public void testClear() { | |
88 | fixture.clear(); | |
89 | } | |
90 | ||
91 | /** | |
92 | * Run the ByteBuffer getByteBuffer() method test. | |
93 | */ | |
94 | @Test | |
95 | public void testGetByteBuffer() { | |
96 | ByteBuffer result = fixture.getByteBuffer(); | |
97 | ||
98 | assertNotNull(result); | |
91052124 | 99 | assertEquals("java.nio.DirectByteBuffer[pos=0 lim=1 cap=1]", result.toString()); |
733c614c MK |
100 | assertEquals(true, result.isDirect()); |
101 | assertEquals(false, result.hasArray()); | |
91052124 MK |
102 | assertEquals(1, result.limit()); |
103 | assertEquals(1, result.remaining()); | |
866e5b51 | 104 | assertEquals(0, result.position()); |
91052124 MK |
105 | assertEquals(1, result.capacity()); |
106 | assertEquals(true, result.hasRemaining()); | |
866e5b51 FC |
107 | assertEquals(false, result.isReadOnly()); |
108 | } | |
109 | ||
110 | /** | |
111 | * Run the ByteOrder getByteOrder() method test. | |
112 | */ | |
113 | @Test | |
114 | public void testGetByteOrder() { | |
115 | ByteOrder result = fixture.getByteOrder(); | |
116 | ||
117 | assertNotNull(result); | |
4a9c1f07 | 118 | assertEquals("BIG_ENDIAN", result.toString()); |
866e5b51 FC |
119 | } |
120 | ||
121 | /** | |
122 | * Run the ByteOrder order() method test. | |
123 | */ | |
124 | @Test | |
125 | public void testGetOrder() { | |
8b8e48ed | 126 | ByteOrder result = fixture.getByteOrder(); |
866e5b51 FC |
127 | |
128 | assertNotNull(result); | |
4a9c1f07 | 129 | assertEquals("BIG_ENDIAN", result.toString()); |
866e5b51 FC |
130 | } |
131 | ||
132 | /** | |
133 | * Run the void order(ByteOrder) method test. | |
134 | */ | |
135 | @Test | |
136 | public void testSetOrder() { | |
137 | ByteOrder order = ByteOrder.BIG_ENDIAN; | |
138 | ||
8b8e48ed | 139 | fixture.setByteOrder(order); |
866e5b51 FC |
140 | } |
141 | ||
142 | /** | |
143 | * Run the int position() method test. | |
144 | */ | |
145 | @Test | |
146 | public void testGetPosition() { | |
47ca6c05 | 147 | long result = fixture.position(); |
866e5b51 FC |
148 | |
149 | assertEquals(1, result); | |
150 | } | |
151 | ||
152 | /** | |
153 | * Run the void position(int) method test. | |
4c67e724 MK |
154 | * |
155 | * @throws CTFReaderException | |
156 | * out of bounds? won't happen | |
866e5b51 FC |
157 | */ |
158 | @Test | |
4c67e724 | 159 | public void testSetPosition() throws CTFReaderException { |
866e5b51 FC |
160 | int newPosition = 1; |
161 | fixture.position(newPosition); | |
162 | } | |
163 | ||
866e5b51 FC |
164 | /** |
165 | * Run the void setByteOrder(ByteOrder) method test. | |
166 | */ | |
167 | @Test | |
168 | public void testSetByteOrder() { | |
169 | ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; | |
170 | fixture.setByteOrder(byteOrder); | |
171 | } | |
231c4e1f MK |
172 | |
173 | /** | |
174 | * Test the get function | |
175 | */ | |
176 | @Test | |
177 | public void testGetBytes() { | |
733c614c MK |
178 | @NonNull |
179 | byte[] data = new byte[2]; | |
231c4e1f MK |
180 | ByteBuffer bb = ByteBuffer.allocate(10); |
181 | bb.put((byte) 0); | |
182 | bb.put((byte) 1); | |
183 | bb.put((byte) 1); | |
184 | bb.put((byte) 0); | |
185 | fixture = new BitBuffer(bb); | |
186 | fixture.get(data); | |
187 | assertEquals(0, data[0]); | |
188 | assertEquals(1, data[1]); | |
189 | fixture.get(data); | |
190 | assertEquals(1, data[0]); | |
191 | assertEquals(0, data[1]); | |
192 | } | |
193 | ||
194 | /** | |
195 | * Test the get function | |
196 | * | |
197 | * @throws CTFReaderException | |
198 | * won't happen but we seek in a buffer | |
199 | */ | |
200 | @Test | |
201 | public void testGetBytesMiddle() throws CTFReaderException { | |
733c614c MK |
202 | @NonNull |
203 | byte[] data = new byte[5]; | |
231c4e1f MK |
204 | // this string has been carefully selected and tested... don't change |
205 | // the string and expect the result to be the same. | |
aefc5c83 | 206 | fixture = new BitBuffer(Util.testMemory(ByteBuffer.wrap(new String("hello world").getBytes()))); |
231c4e1f MK |
207 | fixture.position(6 * 8); |
208 | fixture.get(data); | |
209 | String actual = new String(data); | |
210 | assertEquals("world", actual); | |
211 | } | |
866e5b51 | 212 | } |