| 1 | package org.eclipse.linuxtools.ctf.core.tests.io; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | |
| 5 | import java.nio.ByteBuffer; |
| 6 | import java.nio.ByteOrder; |
| 7 | |
| 8 | import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer; |
| 9 | import org.junit.After; |
| 10 | import org.junit.Before; |
| 11 | import org.junit.Test; |
| 12 | |
| 13 | /** |
| 14 | * Part of the BitBuffet tests with test the methods to read/write integers. |
| 15 | * These are separated from the main file because the fixture is different. |
| 16 | * |
| 17 | * @author alexmont |
| 18 | * |
| 19 | */ |
| 20 | public class BitBufferIntTest { |
| 21 | |
| 22 | private BitBuffer fixture; |
| 23 | |
| 24 | /** |
| 25 | * Launch the test. |
| 26 | * |
| 27 | * @param args |
| 28 | * the command line arguments |
| 29 | */ |
| 30 | public static void main(String[] args) { |
| 31 | new org.junit.runner.JUnitCore().run(BitBufferTest.class); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Perform pre-test initialization. |
| 36 | */ |
| 37 | @Before |
| 38 | public void setUp() { |
| 39 | fixture = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); |
| 40 | fixture.setByteOrder(ByteOrder.BIG_ENDIAN); |
| 41 | createBuffer(fixture); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Perform post-test clean-up. |
| 46 | */ |
| 47 | @After |
| 48 | public void tearDown() { |
| 49 | // Add additional tear down code here |
| 50 | } |
| 51 | |
| 52 | private static void createBuffer(BitBuffer fixture) { |
| 53 | createBuffer(fixture, 16); |
| 54 | } |
| 55 | |
| 56 | private static void createBuffer(BitBuffer fixture, int j) { |
| 57 | byte[] bytes = new byte[j]; |
| 58 | for (int i = 0; i < j; i++) { |
| 59 | bytes[i] = (byte) (i % 0xff); |
| 60 | } |
| 61 | fixture.setByteBuffer(ByteBuffer.wrap(bytes)); |
| 62 | fixture.position(1); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Run the int getInt() method test. |
| 67 | */ |
| 68 | @Test |
| 69 | public void testGetInt_base() { |
| 70 | int result = fixture.getInt(); |
| 71 | assertEquals(0x020406, result); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Run the int getInt(int) method test. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testGetInt_pos0() { |
| 79 | fixture.position(0); |
| 80 | int result = fixture.getInt(); |
| 81 | assertEquals(0x010203, result); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Run the int getInt(int,boolean) method test. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testGetInt_pos1() { |
| 89 | fixture.position(1); |
| 90 | int length = 1; |
| 91 | boolean signed = true; |
| 92 | |
| 93 | int result = fixture.getInt(length, signed); |
| 94 | assertEquals(0, result); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Run the int getInt(int,boolean) method test. |
| 99 | */ |
| 100 | @Test |
| 101 | public void testGetInt_pos2() { |
| 102 | fixture.position(2); |
| 103 | int length = 0; |
| 104 | boolean signed = true; |
| 105 | |
| 106 | int result = fixture.getInt(length, signed); |
| 107 | assertEquals(0, result); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Run the int getInt(int,int,boolean) method test. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testGetInt_signed() { |
| 115 | fixture.position(1); |
| 116 | int length = 0; |
| 117 | boolean signed = true; |
| 118 | |
| 119 | int result = fixture.getInt(length, signed); |
| 120 | assertEquals(0, result); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Run the int getInt(int,int,boolean) method test. |
| 125 | */ |
| 126 | @Test |
| 127 | public void testGetInt_signed_length1() { |
| 128 | fixture.position(1); |
| 129 | int length = 1; |
| 130 | boolean signed = true; |
| 131 | |
| 132 | int result = fixture.getInt(length, signed); |
| 133 | assertEquals(0, result); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Run the int getInt(int,int,boolean) method test with a little-endian |
| 138 | * BitBuffer. |
| 139 | */ |
| 140 | @Test |
| 141 | public void testGetInt_le1() { |
| 142 | BitBuffer le_fixture = new BitBuffer( |
| 143 | java.nio.ByteBuffer.allocateDirect(128)); |
| 144 | le_fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN); |
| 145 | createBuffer(le_fixture); |
| 146 | le_fixture.position(1); |
| 147 | int length = 24; |
| 148 | int result = le_fixture.getInt(length, false); |
| 149 | |
| 150 | /* 0x020100 downshifted */ |
| 151 | assertEquals(0x810080, result); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Run the int getInt(int,int,boolean) method test with a little-endian |
| 156 | * BitBuffer. |
| 157 | */ |
| 158 | @Test |
| 159 | public void testGetInt_le2() { |
| 160 | BitBuffer le_fixture = new BitBuffer( |
| 161 | java.nio.ByteBuffer.allocateDirect(128)); |
| 162 | le_fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN); |
| 163 | createBuffer(le_fixture); |
| 164 | le_fixture.position(0); |
| 165 | int length = 24; |
| 166 | int result = le_fixture.getInt(length, false); |
| 167 | assertEquals(0x020100, result); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Run the int getInt(int,boolean) method test and expect an overflow. |
| 172 | */ |
| 173 | @Test(expected = java.nio.BufferOverflowException.class) |
| 174 | public void testGetInt_invalid() { |
| 175 | BitBuffer small_fixture = new BitBuffer( |
| 176 | java.nio.ByteBuffer.allocateDirect(128)); |
| 177 | small_fixture.setByteOrder(ByteOrder.BIG_ENDIAN); |
| 178 | createBuffer(small_fixture, 2); |
| 179 | small_fixture.position(10); |
| 180 | int length = 32; |
| 181 | boolean signed = true; |
| 182 | |
| 183 | int result = small_fixture.getInt(length, signed); |
| 184 | assertEquals(0, result); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Run the int getInt(int,int,boolean) method test and expect an overflow. |
| 189 | */ |
| 190 | @Test(expected = java.nio.BufferOverflowException.class) |
| 191 | public void testGetInt_invalid2() { |
| 192 | BitBuffer small_fixture = new BitBuffer( |
| 193 | java.nio.ByteBuffer.allocateDirect(128)); |
| 194 | small_fixture.setByteOrder(ByteOrder.BIG_ENDIAN); |
| 195 | createBuffer(small_fixture, 2); |
| 196 | small_fixture.position(1); |
| 197 | int length = 64; |
| 198 | boolean signed = true; |
| 199 | |
| 200 | int result = small_fixture.getInt(length, signed); |
| 201 | assertEquals(0, result); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Run the void putInt(int) method test. |
| 206 | */ |
| 207 | @Test |
| 208 | public void testPutInt() { |
| 209 | int value = 1; |
| 210 | fixture.position(1); |
| 211 | fixture.putInt(value); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Run the void putInt(int,int,boolean) method test. |
| 216 | */ |
| 217 | @Test |
| 218 | public void testPutInt_signed() { |
| 219 | int length = 1; |
| 220 | int value = 1; |
| 221 | |
| 222 | fixture.position(1); |
| 223 | fixture.putInt(length, value); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Run the void putInt(int,int,int,boolean) method test. |
| 228 | */ |
| 229 | @Test |
| 230 | public void testPutInt_length0() { |
| 231 | int length = 0; |
| 232 | int value = 1; |
| 233 | |
| 234 | fixture.position(1); |
| 235 | fixture.putInt(length, value); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Run the void putInt(int,int,int,boolean) method test. |
| 240 | */ |
| 241 | @Test |
| 242 | public void testPutInt_length1() { |
| 243 | int length = 1; |
| 244 | int value = 1; |
| 245 | |
| 246 | fixture.position(1); |
| 247 | fixture.putInt(length, value); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Run the void putInt(int) method test. |
| 252 | */ |
| 253 | @Test |
| 254 | public void testPutInt_hex() { |
| 255 | final int value = 0x010203; |
| 256 | int read; |
| 257 | |
| 258 | for (int i = 0; i <= 32; i++) { |
| 259 | fixture.position(i); |
| 260 | fixture.putInt(value); |
| 261 | |
| 262 | fixture.position(i); |
| 263 | read = fixture.getInt(); |
| 264 | |
| 265 | assertEquals(value, read); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Run the void putInt(int,int,int,boolean) method test. |
| 271 | */ |
| 272 | @Test(expected = java.nio.BufferOverflowException.class) |
| 273 | public void testPutInt_invalid() { |
| 274 | BitBuffer fixture2; |
| 275 | fixture2 = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); |
| 276 | fixture2.setByteOrder(ByteOrder.BIG_ENDIAN); |
| 277 | createBuffer(fixture2, 4); |
| 278 | fixture2.position(1); |
| 279 | |
| 280 | int length = 32; |
| 281 | int value = 1; |
| 282 | |
| 283 | fixture2.putInt(length, value); |
| 284 | |
| 285 | int read = fixture2.getInt(1, true); |
| 286 | assertEquals(value, read); |
| 287 | } |
| 288 | } |