1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.core; 18 19 import junit.framework.TestCase; 20 21 import java.nio.Buffer; 22 import java.nio.BufferOverflowException; 23 import java.nio.BufferUnderflowException; 24 import java.nio.ByteBuffer; 25 import java.nio.ByteOrder; 26 import java.nio.FloatBuffer; 27 import java.nio.IntBuffer; 28 import java.nio.ShortBuffer; 29 import android.test.suitebuilder.annotation.SmallTest; 30 31 /** 32 * Tests for some buffers from the java.nio package. 33 */ 34 public class NIOTest extends TestCase { 35 checkBuffer(Buffer b)36 void checkBuffer(Buffer b) { 37 assertTrue(0 <= b.position()); 38 assertTrue(b.position() <= b.limit()); 39 assertTrue(b.limit() <= b.capacity()); 40 } 41 42 @SmallTest testNIO()43 public void testNIO() throws Exception { 44 ByteBuffer b; 45 46 // Test byte array-based buffer 47 b = ByteBuffer.allocate(12); 48 byteBufferTest(b); 49 50 // Test native heap-allocated buffer 51 b = ByteBuffer.allocateDirect(12); 52 byteBufferTest(b); 53 54 // Test short array-based buffer 55 short[] shortArray = new short[8]; 56 ShortBuffer sb = ShortBuffer.wrap(shortArray); 57 shortBufferTest(sb); 58 59 // Test int array-based buffer 60 int[] intArray = new int[8]; 61 IntBuffer ib = IntBuffer.wrap(intArray); 62 intBufferTest(ib); 63 64 // Test float array-based buffer 65 float[] floatArray = new float[8]; 66 FloatBuffer fb = FloatBuffer.wrap(floatArray); 67 floatBufferTest(fb); 68 } 69 byteBufferTest(ByteBuffer b)70 private void byteBufferTest(ByteBuffer b) { 71 checkBuffer(b); 72 73 // Bounds checks 74 try { 75 b.put(-1, (byte) 0); 76 fail("expected exception not thrown"); 77 } catch (IndexOutOfBoundsException e) { 78 // expected 79 } 80 81 try { 82 b.put(b.limit(), (byte) 0); 83 fail("expected exception not thrown"); 84 } catch (IndexOutOfBoundsException e) { 85 // expected 86 } 87 88 // IndexOutOfBoundsException: offset < 0 89 try { 90 byte[] data = new byte[8]; 91 b.position(0); 92 b.put(data, -1, 2); 93 fail("expected exception not thrown"); 94 } catch (IndexOutOfBoundsException e) { 95 // expected 96 } 97 98 // IndexOutOfBoundsException: length > array.length - offset 99 try { 100 byte[] data = new byte[8]; 101 b.position(0); 102 b.put(data, 1, 8); 103 fail("expected exception not thrown"); 104 } catch (IndexOutOfBoundsException e) { 105 // expected 106 } 107 108 // BufferOverflowException: length > remaining() 109 try { 110 byte[] data = new byte[8]; 111 b.position(b.limit() - 2); 112 b.put(data, 0, 3); 113 fail("expected exception not thrown"); 114 } catch (BufferOverflowException e) { 115 // expected 116 } 117 118 // Fill buffer with bytes A0 A1 A2 A3 ... 119 b.position(0); 120 for (int i = 0; i < b.capacity(); i++) { 121 b.put((byte) (0xA0 + i)); 122 } 123 try { 124 b.put((byte) 0xFF); 125 fail("expected exception not thrown"); 126 } catch (BufferOverflowException e) { 127 // expected 128 } 129 130 b.position(0); 131 assertEquals((byte) 0xA7, b.get(7)); 132 try { 133 b.get(12); 134 fail("expected exception not thrown"); 135 } catch (IndexOutOfBoundsException e) { 136 // expected 137 } 138 try { 139 b.get(-10); 140 fail("expected exception not thrown"); 141 } catch (IndexOutOfBoundsException e) { 142 // expected 143 } 144 145 b.position(0); 146 b.order(ByteOrder.LITTLE_ENDIAN); 147 assertEquals((byte) 0xA0, b.get()); 148 assertEquals((byte) 0xA1, b.get()); 149 assertEquals((byte) 0xA2, b.get()); 150 assertEquals((byte) 0xA3, b.get()); 151 assertEquals((byte) 0xA4, b.get()); 152 assertEquals((byte) 0xA5, b.get()); 153 assertEquals((byte) 0xA6, b.get()); 154 assertEquals((byte) 0xA7, b.get()); 155 assertEquals((byte) 0xA8, b.get()); 156 assertEquals((byte) 0xA9, b.get()); 157 assertEquals((byte) 0xAA, b.get()); 158 assertEquals((byte) 0xAB, b.get()); 159 try { 160 b.get(); 161 fail("expected exception not thrown"); 162 } catch (BufferUnderflowException e) { 163 // expected 164 } 165 166 b.position(0); 167 b.order(ByteOrder.BIG_ENDIAN); 168 assertEquals((byte) 0xA0, b.get()); 169 assertEquals((byte) 0xA1, b.get()); 170 assertEquals((byte) 0xA2, b.get()); 171 assertEquals((byte) 0xA3, b.get()); 172 assertEquals((byte) 0xA4, b.get()); 173 assertEquals((byte) 0xA5, b.get()); 174 assertEquals((byte) 0xA6, b.get()); 175 assertEquals((byte) 0xA7, b.get()); 176 assertEquals((byte) 0xA8, b.get()); 177 assertEquals((byte) 0xA9, b.get()); 178 assertEquals((byte) 0xAA, b.get()); 179 assertEquals((byte) 0xAB, b.get()); 180 try { 181 b.get(); 182 fail("expected exception not thrown"); 183 } catch (BufferUnderflowException e) { 184 // expected 185 } 186 187 b.position(6); 188 b.limit(10); 189 assertEquals((byte) 0xA6, b.get()); 190 191 // Check sliced buffer 192 b.position(6); 193 194 ByteBuffer bb = b.slice(); 195 checkBuffer(bb); 196 197 assertEquals(0, bb.position()); 198 assertEquals(4, bb.limit()); 199 assertEquals(4, bb.capacity()); 200 201 assertEquals((byte) 0xA6, bb.get()); 202 assertEquals((byte) 0xA7, bb.get()); 203 assertEquals((byte) 0xA8, bb.get()); 204 assertEquals((byte) 0xA9, bb.get()); 205 try { 206 bb.get(); 207 fail("expected exception not thrown"); 208 } catch (BufferUnderflowException e) { 209 // expected 210 } 211 212 // Reset position and limit 213 b.position(0); 214 b.limit(b.capacity()); 215 216 // Check 'getShort' 217 b.order(ByteOrder.LITTLE_ENDIAN); 218 b.position(0); 219 assertEquals((short) 0xA1A0, b.getShort()); 220 assertEquals((short) 0xA3A2, b.getShort()); 221 assertEquals((short) 0xA5A4, b.getShort()); 222 assertEquals((short) 0xA7A6, b.getShort()); 223 assertEquals((short) 0xA9A8, b.getShort()); 224 assertEquals((short) 0xABAA, b.getShort()); 225 try { 226 bb.getShort(); 227 fail("expected exception not thrown"); 228 } catch (BufferUnderflowException e) { 229 // expected 230 } 231 232 b.order(ByteOrder.BIG_ENDIAN); 233 b.position(0); 234 assertEquals((short) 0xA0A1, b.getShort()); 235 assertEquals((short) 0xA2A3, b.getShort()); 236 assertEquals((short) 0xA4A5, b.getShort()); 237 assertEquals((short) 0xA6A7, b.getShort()); 238 assertEquals((short) 0xA8A9, b.getShort()); 239 assertEquals((short) 0xAAAB, b.getShort()); 240 try { 241 bb.getShort(); 242 fail("expected exception not thrown"); 243 } catch (BufferUnderflowException e) { 244 // expected 245 } 246 247 // Check 'getInt' 248 b.order(ByteOrder.LITTLE_ENDIAN); 249 b.position(0); 250 assertEquals(0xA3A2A1A0, b.getInt()); 251 assertEquals(0xA7A6A5A4, b.getInt()); 252 assertEquals(0xABAAA9A8, b.getInt()); 253 try { 254 bb.getInt(); 255 fail("expected exception not thrown"); 256 } catch (BufferUnderflowException e) { 257 // expected 258 } 259 260 b.order(ByteOrder.BIG_ENDIAN); 261 b.position(0); 262 assertEquals(0xA0A1A2A3, b.getInt()); 263 assertEquals(0xA4A5A6A7, b.getInt()); 264 assertEquals(0xA8A9AAAB, b.getInt()); 265 try { 266 bb.getInt(); 267 fail("expected exception not thrown"); 268 } catch (BufferUnderflowException e) { 269 // expected 270 } 271 272 // Check 'getFloat' 273 b.order(ByteOrder.LITTLE_ENDIAN); 274 b.position(0); 275 assertEquals(0xA3A2A1A0, Float.floatToIntBits(b.getFloat())); 276 assertEquals(0xA7A6A5A4, Float.floatToIntBits(b.getFloat())); 277 assertEquals(0xABAAA9A8, Float.floatToIntBits(b.getFloat())); 278 try { 279 b.getFloat(); 280 fail("expected exception not thrown"); 281 } catch (BufferUnderflowException e) { 282 // expected 283 } 284 285 b.order(ByteOrder.BIG_ENDIAN); 286 b.position(0); 287 assertEquals(0xA0A1A2A3, Float.floatToIntBits(b.getFloat())); 288 assertEquals(0xA4A5A6A7, Float.floatToIntBits(b.getFloat())); 289 assertEquals(0xA8A9AAAB, Float.floatToIntBits(b.getFloat())); 290 try { 291 b.getFloat(); 292 fail("expected exception not thrown"); 293 } catch (BufferUnderflowException e) { 294 // expected 295 } 296 297 // Check 'getDouble(int position)' 298 b.order(ByteOrder.LITTLE_ENDIAN); 299 assertEquals(0xA7A6A5A4A3A2A1A0L, Double.doubleToLongBits(b.getDouble(0))); 300 assertEquals(0xA8A7A6A5A4A3A2A1L, Double.doubleToLongBits(b.getDouble(1))); 301 try { 302 b.getDouble(-1); 303 fail("expected exception not thrown"); 304 } catch (IndexOutOfBoundsException e) { 305 // expected 306 } 307 try { 308 b.getDouble(5); 309 fail("expected exception not thrown"); 310 } catch (IndexOutOfBoundsException e) { 311 // expected 312 } 313 314 b.order(ByteOrder.BIG_ENDIAN); 315 assertEquals(0xA0A1A2A3A4A5A6A7L, Double.doubleToLongBits(b.getDouble(0))); 316 assertEquals(0xA1A2A3A4A5A6A7A8L, Double.doubleToLongBits(b.getDouble(1))); 317 try { 318 b.getDouble(-1); 319 fail("expected exception not thrown"); 320 } catch (IndexOutOfBoundsException e) { 321 // expected 322 } 323 try { 324 b.getDouble(5); 325 fail("expected exception not thrown"); 326 } catch (IndexOutOfBoundsException e) { 327 // expected 328 } 329 330 // Slice and check 'getInt' 331 b.position(1); 332 b.limit(5); 333 b.order(ByteOrder.LITTLE_ENDIAN); 334 bb = b.slice(); 335 assertEquals(4, bb.capacity()); 336 assertEquals(0xA4A3A2A1, bb.getInt(0)); 337 338 bb.order(ByteOrder.LITTLE_ENDIAN); 339 ShortBuffer sb = bb.asShortBuffer(); 340 341 checkBuffer(sb); 342 assertEquals(2, sb.capacity()); 343 assertEquals((short) 0xA2A1, sb.get()); 344 assertEquals((short) 0xA4A3, sb.get()); 345 346 bb.order(ByteOrder.BIG_ENDIAN); 347 sb = bb.asShortBuffer(); 348 349 checkBuffer(sb); 350 assertEquals(2, sb.capacity()); 351 assertEquals((short) 0xA1A2, sb.get()); 352 assertEquals((short) 0xA3A4, sb.get()); 353 354 bb.order(ByteOrder.LITTLE_ENDIAN); 355 IntBuffer ib = bb.asIntBuffer(); 356 357 checkBuffer(ib); 358 assertEquals(1, ib.capacity()); 359 assertEquals(0xA4A3A2A1, ib.get()); 360 361 bb.order(ByteOrder.BIG_ENDIAN); 362 ib = bb.asIntBuffer(); 363 364 checkBuffer(ib); 365 assertEquals(1, ib.capacity()); 366 assertEquals(0xA1A2A3A4, ib.get()); 367 368 bb.order(ByteOrder.LITTLE_ENDIAN); 369 FloatBuffer fb = bb.asFloatBuffer(); 370 371 checkBuffer(fb); 372 assertEquals(1, fb.capacity()); 373 assertEquals(0xA4A3A2A1, Float.floatToIntBits(fb.get())); 374 375 bb.order(ByteOrder.BIG_ENDIAN); 376 fb = bb.asFloatBuffer(); 377 378 checkBuffer(fb); 379 assertEquals(1, fb.capacity()); 380 assertEquals(0xA1A2A3A4, Float.floatToIntBits(fb.get())); 381 } 382 shortBufferTest(ShortBuffer sb)383 private void shortBufferTest(ShortBuffer sb) { 384 checkBuffer(sb); 385 386 try { 387 sb.put(-1, (short) 0); 388 fail("expected exception not thrown"); 389 } catch (IndexOutOfBoundsException e) { 390 // expected 391 } 392 393 try { 394 sb.put(sb.limit(), (short) 0); 395 fail("expected exception not thrown"); 396 } catch (IndexOutOfBoundsException e) { 397 // expected 398 } 399 400 // IndexOutOfBoundsException: offset < 0 401 try { 402 short[] data = new short[8]; 403 sb.position(0); 404 sb.put(data, -1, 2); 405 fail("expected exception not thrown"); 406 } catch (IndexOutOfBoundsException e) { 407 // expected 408 } 409 410 // IndexOutOfBoundsException: length > array.length - offset 411 try { 412 short[] data = new short[8]; 413 sb.position(0); 414 sb.put(data, 1, 8); 415 fail("expected exception not thrown"); 416 } catch (IndexOutOfBoundsException e) { 417 // expected 418 } 419 420 // BufferOverflowException: length > remaining() 421 try { 422 short[] data = new short[8]; 423 sb.position(sb.limit() - 2); 424 sb.put(data, 0, 3); 425 fail("expected exception not thrown"); 426 } catch (BufferOverflowException e) { 427 // expected 428 } 429 430 short[] data = {0, 10, 20, 30, 40, 50, 60, 70}; 431 sb.position(0); 432 sb.put(data); 433 434 try { 435 sb.get(); 436 fail("expected exception not thrown"); 437 } catch (BufferUnderflowException e) { 438 // expected 439 } 440 441 sb.position(0); 442 assertEquals((short) 0, sb.get()); 443 assertEquals((short) 10, sb.get()); 444 assertEquals((short) 20, sb.get()); 445 assertEquals((short) 30, sb.get()); 446 assertEquals((short) 40, sb.get()); 447 assertEquals((short) 50, sb.get()); 448 assertEquals((short) 60, sb.get()); 449 assertEquals((short) 70, sb.get()); 450 try { 451 sb.get(); 452 fail("expected exception not thrown"); 453 } catch (BufferUnderflowException e) { 454 // expected 455 } 456 sb.position(1); 457 sb.put((short) 11); 458 assertEquals((short) 11, sb.get(1)); 459 460 short[] ss1 = {33, 44, 55, 66}; 461 sb.position(3); 462 sb.put(ss1); 463 sb.position(0); 464 assertEquals((short) 0, sb.get()); 465 assertEquals((short) 11, sb.get()); 466 assertEquals((short) 20, sb.get()); 467 assertEquals((short) 33, sb.get()); 468 assertEquals((short) 44, sb.get()); 469 assertEquals((short) 55, sb.get()); 470 assertEquals((short) 66, sb.get()); 471 assertEquals((short) 70, sb.get()); 472 473 short[] ss2 = {10, 22, 30}; 474 sb.position(2); 475 sb.put(ss2, 1, 1); 476 sb.position(0); 477 assertEquals((short) 0, sb.get()); 478 assertEquals((short) 11, sb.get()); 479 assertEquals((short) 22, sb.get()); 480 assertEquals((short) 33, sb.get()); 481 assertEquals((short) 44, sb.get()); 482 assertEquals((short) 55, sb.get()); 483 assertEquals((short) 66, sb.get()); 484 assertEquals((short) 70, sb.get()); 485 } 486 intBufferTest(IntBuffer ib)487 private void intBufferTest(IntBuffer ib) { 488 checkBuffer(ib); 489 490 try { 491 ib.put(-1, (int) 0); 492 fail("expected exception not thrown"); 493 } catch (IndexOutOfBoundsException e) { 494 // expected 495 } 496 497 try { 498 ib.put(ib.limit(), (int) 0); 499 fail("expected exception not thrown"); 500 } catch (IndexOutOfBoundsException e) { 501 // expected 502 } 503 504 // IndexOutOfBoundsException: offset < 0 505 try { 506 int[] data = new int[8]; 507 ib.position(0); 508 ib.put(data, -1, 2); 509 fail("expected exception not thrown"); 510 } catch (IndexOutOfBoundsException e) { 511 // expected 512 } 513 514 // IndexOutOfBoundsException: length > array.length - offset 515 try { 516 int[] data = new int[8]; 517 ib.position(0); 518 ib.put(data, 1, 8); 519 fail("expected exception not thrown"); 520 } catch (IndexOutOfBoundsException e) { 521 // expected 522 } 523 524 // BufferOverflowException: length > remaining() 525 try { 526 int[] data = new int[8]; 527 ib.position(ib.limit() - 2); 528 ib.put(data, 0, 3); 529 fail("expected exception not thrown"); 530 } catch (BufferOverflowException e) { 531 // expected 532 } 533 534 int[] data = {0, 10, 20, 30, 40, 50, 60, 70}; 535 ib.position(0); 536 ib.put(data); 537 538 try { 539 ib.get(); 540 fail("expected exception not thrown"); 541 } catch (BufferUnderflowException e) { 542 // expected 543 } 544 545 ib.position(0); 546 assertEquals((int) 0, ib.get()); 547 assertEquals((int) 10, ib.get()); 548 assertEquals((int) 20, ib.get()); 549 assertEquals((int) 30, ib.get()); 550 assertEquals((int) 40, ib.get()); 551 assertEquals((int) 50, ib.get()); 552 assertEquals((int) 60, ib.get()); 553 assertEquals((int) 70, ib.get()); 554 try { 555 ib.get(); 556 fail("expected exception not thrown"); 557 } catch (BufferUnderflowException e) { 558 // expected 559 } 560 ib.position(1); 561 ib.put((int) 11); 562 assertEquals((int) 11, ib.get(1)); 563 564 int[] ss1 = {33, 44, 55, 66}; 565 ib.position(3); 566 ib.put(ss1); 567 ib.position(0); 568 assertEquals((int) 0, ib.get()); 569 assertEquals((int) 11, ib.get()); 570 assertEquals((int) 20, ib.get()); 571 assertEquals((int) 33, ib.get()); 572 assertEquals((int) 44, ib.get()); 573 assertEquals((int) 55, ib.get()); 574 assertEquals((int) 66, ib.get()); 575 assertEquals((int) 70, ib.get()); 576 577 int[] ss2 = {10, 22, 30}; 578 ib.position(2); 579 ib.put(ss2, 1, 1); 580 ib.position(0); 581 assertEquals((int) 0, ib.get()); 582 assertEquals((int) 11, ib.get()); 583 assertEquals((int) 22, ib.get()); 584 assertEquals((int) 33, ib.get()); 585 assertEquals((int) 44, ib.get()); 586 assertEquals((int) 55, ib.get()); 587 assertEquals((int) 66, ib.get()); 588 assertEquals((int) 70, ib.get()); 589 } 590 floatBufferTest(FloatBuffer fb)591 void floatBufferTest(FloatBuffer fb) { 592 checkBuffer(fb); 593 594 try { 595 fb.put(-1, (float) 0); 596 fail("expected exception not thrown"); 597 } catch (IndexOutOfBoundsException e) { 598 // expected 599 } 600 601 try { 602 fb.put(fb.limit(), (float) 0); 603 fail("expected exception not thrown"); 604 } catch (IndexOutOfBoundsException e) { 605 // expected 606 } 607 608 // IndexOutOfBoundsException: offset < 0 609 try { 610 float[] data = new float[8]; 611 fb.position(0); 612 fb.put(data, -1, 2); 613 fail("expected exception not thrown"); 614 } catch (IndexOutOfBoundsException e) { 615 // expected 616 } 617 618 // IndexOutOfBoundsException: length > array.length - offset 619 try { 620 float[] data = new float[8]; 621 fb.position(0); 622 fb.put(data, 1, 8); 623 fail("expected exception not thrown"); 624 } catch (IndexOutOfBoundsException e) { 625 // expected 626 } 627 628 // BufferOverflowException: length > remaining() 629 try { 630 float[] data = new float[8]; 631 fb.position(fb.limit() - 2); 632 fb.put(data, 0, 3); 633 fail("expected exception not thrown"); 634 } catch (BufferOverflowException e) { 635 // expected 636 } 637 638 float[] data = {0, 10, 20, 30, 40, 50, 60, 70}; 639 fb.position(0); 640 fb.put(data); 641 642 try { 643 fb.get(); 644 fail("expected exception not thrown"); 645 } catch (BufferUnderflowException e) { 646 // expected 647 } 648 649 fb.position(0); 650 assertEquals((float) 0, fb.get()); 651 assertEquals((float) 10, fb.get()); 652 assertEquals((float) 20, fb.get()); 653 assertEquals((float) 30, fb.get()); 654 assertEquals((float) 40, fb.get()); 655 assertEquals((float) 50, fb.get()); 656 assertEquals((float) 60, fb.get()); 657 assertEquals((float) 70, fb.get()); 658 try { 659 fb.get(); 660 fail("expected exception not thrown"); 661 } catch (BufferUnderflowException e) { 662 // expected 663 } 664 fb.position(1); 665 fb.put((float) 11); 666 assertEquals((float) 11, fb.get(1)); 667 668 float[] ss1 = {33, 44, 55, 66}; 669 fb.position(3); 670 fb.put(ss1); 671 fb.position(0); 672 assertEquals((float) 0, fb.get()); 673 assertEquals((float) 11, fb.get()); 674 assertEquals((float) 20, fb.get()); 675 assertEquals((float) 33, fb.get()); 676 assertEquals((float) 44, fb.get()); 677 assertEquals((float) 55, fb.get()); 678 assertEquals((float) 66, fb.get()); 679 assertEquals((float) 70, fb.get()); 680 681 float[] ss2 = {10, 22, 30}; 682 fb.position(2); 683 fb.put(ss2, 1, 1); 684 fb.position(0); 685 assertEquals((float) 0, fb.get()); 686 assertEquals((float) 11, fb.get()); 687 assertEquals((float) 22, fb.get()); 688 assertEquals((float) 33, fb.get()); 689 assertEquals((float) 44, fb.get()); 690 assertEquals((float) 55, fb.get()); 691 assertEquals((float) 66, fb.get()); 692 assertEquals((float) 70, fb.get()); 693 } 694 } 695