1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.tests.java.nio; 19 20import java.nio.BufferOverflowException; 21import java.nio.BufferUnderflowException; 22import java.nio.ByteOrder; 23import java.nio.InvalidMarkException; 24import java.nio.ShortBuffer; 25 26/** 27 * Tests java.nio.ShortBuffer 28 * 29 */ 30public class ShortBufferTest extends AbstractBufferTest { 31 32 protected static final int SMALL_TEST_LENGTH = 5; 33 34 protected static final int BUFFER_LENGTH = 20; 35 36 protected ShortBuffer buf; 37 38 protected void setUp() throws Exception { 39 buf = ShortBuffer.allocate(BUFFER_LENGTH); 40 loadTestData1(buf); 41 baseBuf = buf; 42 } 43 44 protected void tearDown() throws Exception { 45 buf = null; 46 baseBuf = null; 47 } 48 49 public void testArray() { 50 short array[] = buf.array(); 51 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 52 53 loadTestData1(array, buf.arrayOffset(), buf.capacity()); 54 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 55 56 loadTestData2(array, buf.arrayOffset(), buf.capacity()); 57 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 58 59 loadTestData1(buf); 60 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 61 62 loadTestData2(buf); 63 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 64 } 65 66 public void testArrayOffset() { 67 short array[] = buf.array(); 68 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 69 70 loadTestData1(array, buf.arrayOffset(), buf.capacity()); 71 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 72 73 loadTestData2(array, buf.arrayOffset(), buf.capacity()); 74 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 75 76 loadTestData1(buf); 77 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 78 79 loadTestData2(buf); 80 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 81 } 82 83 public void testAsReadOnlyBuffer() { 84 buf.clear(); 85 buf.mark(); 86 buf.position(buf.limit()); 87 88 // readonly's contents should be the same as buf 89 ShortBuffer readonly = buf.asReadOnlyBuffer(); 90 assertNotSame(buf, readonly); 91 assertTrue(readonly.isReadOnly()); 92 assertEquals(buf.position(), readonly.position()); 93 assertEquals(buf.limit(), readonly.limit()); 94 assertEquals(buf.isDirect(), readonly.isDirect()); 95 assertEquals(buf.order(), readonly.order()); 96 assertContentEquals(buf, readonly); 97 98 // readonly's position, mark, and limit should be independent to buf 99 readonly.reset(); 100 assertEquals(readonly.position(), 0); 101 readonly.clear(); 102 assertEquals(buf.position(), buf.limit()); 103 buf.reset(); 104 assertEquals(buf.position(), 0); 105 } 106 107 public void testCompact() { 108 // case: buffer is full 109 buf.clear(); 110 buf.mark(); 111 loadTestData1(buf); 112 ShortBuffer ret = buf.compact(); 113 assertSame(ret, buf); 114 assertEquals(buf.position(), buf.capacity()); 115 assertEquals(buf.limit(), buf.capacity()); 116 assertContentLikeTestData1(buf, 0, (short) 0, buf.capacity()); 117 try { 118 buf.reset(); 119 fail("Should throw Exception"); //$NON-NLS-1$ 120 } catch (InvalidMarkException e) { 121 // expected 122 } 123 124 // case: buffer is empty 125 buf.position(0); 126 buf.limit(0); 127 buf.mark(); 128 ret = buf.compact(); 129 assertSame(ret, buf); 130 assertEquals(buf.position(), 0); 131 assertEquals(buf.limit(), buf.capacity()); 132 assertContentLikeTestData1(buf, 0, (short) 0, buf.capacity()); 133 try { 134 buf.reset(); 135 fail("Should throw Exception"); //$NON-NLS-1$ 136 } catch (InvalidMarkException e) { 137 // expected 138 } 139 140 // case: normal 141 assertTrue(buf.capacity() > 5); 142 buf.position(1); 143 buf.limit(5); 144 buf.mark(); 145 ret = buf.compact(); 146 assertSame(ret, buf); 147 assertEquals(buf.position(), 4); 148 assertEquals(buf.limit(), buf.capacity()); 149 assertContentLikeTestData1(buf, 0, (short) 1, 4); 150 try { 151 buf.reset(); 152 fail("Should throw Exception"); //$NON-NLS-1$ 153 } catch (InvalidMarkException e) { 154 // expected 155 } 156 } 157 158 public void testCompareTo() { 159 // compare to self 160 assertEquals(0, buf.compareTo(buf)); 161 162 // normal cases 163 assertTrue(buf.capacity() > 5); 164 buf.clear(); 165 ShortBuffer other = ShortBuffer.allocate(buf.capacity()); 166 loadTestData1(other); 167 assertEquals(0, buf.compareTo(other)); 168 assertEquals(0, other.compareTo(buf)); 169 buf.position(1); 170 assertTrue(buf.compareTo(other) > 0); 171 assertTrue(other.compareTo(buf) < 0); 172 other.position(2); 173 assertTrue(buf.compareTo(other) < 0); 174 assertTrue(other.compareTo(buf) > 0); 175 buf.position(2); 176 other.limit(5); 177 assertTrue(buf.compareTo(other) > 0); 178 assertTrue(other.compareTo(buf) < 0); 179 } 180 181 public void testDuplicate() { 182 buf.clear(); 183 buf.mark(); 184 buf.position(buf.limit()); 185 186 // duplicate's contents should be the same as buf 187 ShortBuffer duplicate = buf.duplicate(); 188 assertNotSame(buf, duplicate); 189 assertEquals(buf.position(), duplicate.position()); 190 assertEquals(buf.limit(), duplicate.limit()); 191 assertEquals(buf.isReadOnly(), duplicate.isReadOnly()); 192 assertEquals(buf.isDirect(), duplicate.isDirect()); 193 assertEquals(buf.order(), duplicate.order()); 194 assertContentEquals(buf, duplicate); 195 196 // duplicate's position, mark, and limit should be independent to buf 197 duplicate.reset(); 198 assertEquals(duplicate.position(), 0); 199 duplicate.clear(); 200 assertEquals(buf.position(), buf.limit()); 201 buf.reset(); 202 assertEquals(buf.position(), 0); 203 204 // duplicate share the same content with buf 205 if (!duplicate.isReadOnly()) { 206 loadTestData1(buf); 207 assertContentEquals(buf, duplicate); 208 loadTestData2(duplicate); 209 assertContentEquals(buf, duplicate); 210 } 211 } 212 213 public void testEquals() { 214 // equal to self 215 assertTrue(buf.equals(buf)); 216 ShortBuffer readonly = buf.asReadOnlyBuffer(); 217 assertTrue(buf.equals(readonly)); 218 ShortBuffer duplicate = buf.duplicate(); 219 assertTrue(buf.equals(duplicate)); 220 221 // always false, if type mismatch 222 assertFalse(buf.equals(Boolean.TRUE)); 223 224 assertTrue(buf.capacity() > 5); 225 226 buf.limit(buf.capacity()).position(0); 227 readonly.limit(readonly.capacity()).position(1); 228 assertFalse(buf.equals(readonly)); 229 230 buf.limit(buf.capacity() - 1).position(0); 231 duplicate.limit(duplicate.capacity()).position(0); 232 assertFalse(buf.equals(duplicate)); 233 } 234 235 /* 236 * Class under test for short get() 237 */ 238 public void testGet() { 239 buf.clear(); 240 for (int i = 0; i < buf.capacity(); i++) { 241 assertEquals(buf.position(), i); 242 assertEquals(buf.get(), buf.get(i)); 243 } 244 try { 245 buf.get(); 246 fail("Should throw Exception"); //$NON-NLS-1$ 247 } catch (BufferUnderflowException e) { 248 // expected 249 } 250 } 251 252 /* 253 * Class under test for java.nio.ShortBuffer get(short[]) 254 */ 255 public void testGetshortArray() { 256 short array[] = new short[1]; 257 buf.clear(); 258 for (int i = 0; i < buf.capacity(); i++) { 259 assertEquals(buf.position(), i); 260 ShortBuffer ret = buf.get(array); 261 assertEquals(array[0], buf.get(i)); 262 assertSame(ret, buf); 263 } 264 try { 265 buf.get(array); 266 fail("Should throw Exception"); //$NON-NLS-1$ 267 } catch (BufferUnderflowException e) { 268 // expected 269 } 270 } 271 272 /* 273 * Class under test for java.nio.ShortBuffer get(short[], int, int) 274 */ 275 public void testGetshortArrayintint() { 276 buf.clear(); 277 short array[] = new short[buf.capacity()]; 278 279 try { 280 buf.get(new short[buf.capacity() + 1], 0, buf.capacity() + 1); 281 fail("Should throw Exception"); //$NON-NLS-1$ 282 } catch (BufferUnderflowException e) { 283 // expected 284 } 285 assertEquals(buf.position(), 0); 286 try { 287 buf.get(array, -1, array.length); 288 fail("Should throw Exception"); //$NON-NLS-1$ 289 } catch (IndexOutOfBoundsException e) { 290 // expected 291 } 292 buf.get(array, array.length, 0); 293 try { 294 buf.get(array, array.length + 1, 1); 295 fail("Should throw Exception"); //$NON-NLS-1$ 296 } catch (IndexOutOfBoundsException e) { 297 // expected 298 } 299 assertEquals(buf.position(), 0); 300 try { 301 buf.get((short[])null, 2, -1); 302 fail("Should throw Exception"); //$NON-NLS-1$ 303 } catch (NullPointerException e) { 304 // expected 305 } 306 try { 307 buf.get(array, 2, array.length); 308 fail("Should throw Exception"); //$NON-NLS-1$ 309 } catch (IndexOutOfBoundsException e) { 310 // expected 311 } 312 try { 313 buf.get(array, 1, Integer.MAX_VALUE); 314 fail("Should throw Exception"); //$NON-NLS-1$ 315 } catch (BufferUnderflowException expected) { 316 } catch (IndexOutOfBoundsException expected) { 317 } 318 try { 319 buf.get(array, Integer.MAX_VALUE, 1); 320 fail("Should throw Exception"); //$NON-NLS-1$ 321 } catch (IndexOutOfBoundsException e) { 322 // expected 323 } 324 assertEquals(buf.position(), 0); 325 326 buf.clear(); 327 ShortBuffer ret = buf.get(array, 0, array.length); 328 assertEquals(buf.position(), buf.capacity()); 329 assertContentEquals(buf, array, 0, array.length); 330 assertSame(ret, buf); 331 } 332 333 /* 334 * Class under test for short get(int) 335 */ 336 public void testGetint() { 337 buf.clear(); 338 for (int i = 0; i < buf.capacity(); i++) { 339 assertEquals(buf.position(), i); 340 assertEquals(buf.get(), buf.get(i)); 341 } 342 try { 343 buf.get(-1); 344 fail("Should throw Exception"); //$NON-NLS-1$ 345 } catch (IndexOutOfBoundsException e) { 346 // expected 347 } 348 try { 349 buf.get(buf.limit()); 350 fail("Should throw Exception"); //$NON-NLS-1$ 351 } catch (IndexOutOfBoundsException e) { 352 // expected 353 } 354 } 355 356 public void testHasArray() { 357 assertNotNull(buf.array()); 358 } 359 360 public void testHashCode() { 361 buf.clear(); 362 ShortBuffer readonly = buf.asReadOnlyBuffer(); 363 ShortBuffer duplicate = buf.duplicate(); 364 assertTrue(buf.hashCode() == readonly.hashCode()); 365 366 assertTrue(buf.capacity() > 5); 367 duplicate.position(buf.capacity() / 2); 368 assertTrue(buf.hashCode() != duplicate.hashCode()); 369 } 370 371 public void testIsDirect() { 372 assertFalse(buf.isDirect()); 373 } 374 375 public void testOrder() { 376 buf.order(); 377 assertEquals(ByteOrder.nativeOrder(), buf.order()); 378 } 379 380 /* 381 * Class under test for java.nio.ShortBuffer put(short) 382 */ 383 public void testPutshort() { 384 buf.clear(); 385 for (int i = 0; i < buf.capacity(); i++) { 386 assertEquals(buf.position(), i); 387 ShortBuffer ret = buf.put((short) i); 388 assertEquals(buf.get(i), (short) i); 389 assertSame(ret, buf); 390 } 391 try { 392 buf.put((short) 0); 393 fail("Should throw Exception"); //$NON-NLS-1$ 394 } catch (BufferOverflowException e) { 395 // expected 396 } 397 } 398 399 /* 400 * Class under test for java.nio.ShortBuffer put(short[]) 401 */ 402 public void testPutshortArray() { 403 short array[] = new short[1]; 404 buf.clear(); 405 for (int i = 0; i < buf.capacity(); i++) { 406 assertEquals(buf.position(), i); 407 array[0] = (short) i; 408 ShortBuffer ret = buf.put(array); 409 assertEquals(buf.get(i), (short) i); 410 assertSame(ret, buf); 411 } 412 try { 413 buf.put(array); 414 fail("Should throw Exception"); //$NON-NLS-1$ 415 } catch (BufferOverflowException e) { 416 // expected 417 } 418 try { 419 buf.position(buf.limit()); 420 buf.put((short[])null); 421 fail("Should throw Exception"); //$NON-NLS-1$ 422 } catch (NullPointerException e) { 423 // expected 424 } 425 } 426 427 /* 428 * Class under test for java.nio.ShortBuffer put(short[], int, int) 429 */ 430 public void testPutshortArrayintint() { 431 buf.clear(); 432 short array[] = new short[buf.capacity()]; 433 try { 434 buf.put(new short[buf.capacity() + 1], 0, buf.capacity() + 1); 435 fail("Should throw Exception"); //$NON-NLS-1$ 436 } catch (BufferOverflowException e) { 437 // expected 438 } 439 assertEquals(buf.position(), 0); 440 try { 441 buf.put(array, -1, array.length); 442 fail("Should throw Exception"); //$NON-NLS-1$ 443 } catch (IndexOutOfBoundsException e) { 444 // expected 445 } 446 try { 447 buf.put(array, array.length + 1, 0); 448 fail("Should throw Exception"); //$NON-NLS-1$ 449 } catch (IndexOutOfBoundsException e) { 450 // expected 451 } 452 buf.put(array, array.length, 0); 453 assertEquals(buf.position(), 0); 454 try { 455 buf.put(array, 0, -1); 456 fail("Should throw Exception"); //$NON-NLS-1$ 457 } catch (IndexOutOfBoundsException e) { 458 // expected 459 } 460 try { 461 buf.put((short[])null, 0, -1); 462 fail("Should throw Exception"); //$NON-NLS-1$ 463 } catch (NullPointerException e) { 464 // expected 465 } 466 try { 467 buf.put(array, 2, array.length); 468 fail("Should throw Exception"); //$NON-NLS-1$ 469 } catch (IndexOutOfBoundsException e) { 470 // expected 471 } 472 try { 473 buf.put(array, Integer.MAX_VALUE, 1); 474 fail("Should throw Exception"); //$NON-NLS-1$ 475 } catch (IndexOutOfBoundsException e) { 476 // expected 477 } 478 try { 479 buf.put(array, 1, Integer.MAX_VALUE); 480 fail("Should throw Exception"); //$NON-NLS-1$ 481 } catch (BufferOverflowException expected) { 482 } catch (IndexOutOfBoundsException expected) { 483 } 484 assertEquals(buf.position(), 0); 485 486 loadTestData2(array, 0, array.length); 487 ShortBuffer ret = buf.put(array, 0, array.length); 488 assertEquals(buf.position(), buf.capacity()); 489 assertContentEquals(buf, array, 0, array.length); 490 assertSame(ret, buf); 491 } 492 493 /* 494 * Class under test for java.nio.ShortBuffer put(java.nio.ShortBuffer) 495 */ 496 public void testPutShortBuffer() { 497 ShortBuffer other = ShortBuffer.allocate(buf.capacity()); 498 try { 499 buf.put(buf); 500 fail("Should throw Exception"); //$NON-NLS-1$ 501 } catch (IllegalArgumentException e) { 502 // expected 503 } 504 try { 505 buf.put(ShortBuffer.allocate(buf.capacity() + 1)); 506 fail("Should throw Exception"); //$NON-NLS-1$ 507 } catch (BufferOverflowException e) { 508 // expected 509 } 510 try { 511 buf.flip(); 512 buf.put((ShortBuffer)null); 513 fail("Should throw Exception"); //$NON-NLS-1$ 514 } catch (NullPointerException e) { 515 // expected 516 } 517 518 loadTestData2(other); 519 other.clear(); 520 buf.clear(); 521 ShortBuffer ret = buf.put(other); 522 assertEquals(other.position(), other.capacity()); 523 assertEquals(buf.position(), buf.capacity()); 524 assertContentEquals(other, buf); 525 assertSame(ret, buf); 526 } 527 528 /* 529 * Class under test for java.nio.ShortBuffer put(int, short) 530 */ 531 public void testPutintshort() { 532 buf.clear(); 533 for (int i = 0; i < buf.capacity(); i++) { 534 assertEquals(buf.position(), 0); 535 ShortBuffer ret = buf.put(i, (short) i); 536 assertEquals(buf.get(i), (short) i); 537 assertSame(ret, buf); 538 } 539 try { 540 buf.put(-1, (short) 0); 541 fail("Should throw Exception"); //$NON-NLS-1$ 542 } catch (IndexOutOfBoundsException e) { 543 // expected 544 } 545 try { 546 buf.put(buf.limit(), (short) 0); 547 fail("Should throw Exception"); //$NON-NLS-1$ 548 } catch (IndexOutOfBoundsException e) { 549 // expected 550 } 551 } 552 553 public void testSlice() { 554 assertTrue(buf.capacity() > 5); 555 buf.position(1); 556 buf.limit(buf.capacity() - 1); 557 558 ShortBuffer slice = buf.slice(); 559 assertEquals(buf.isReadOnly(), slice.isReadOnly()); 560 assertEquals(buf.isDirect(), slice.isDirect()); 561 assertEquals(buf.order(), slice.order()); 562 assertEquals(slice.position(), 0); 563 assertEquals(slice.limit(), buf.remaining()); 564 assertEquals(slice.capacity(), buf.remaining()); 565 try { 566 slice.reset(); 567 fail("Should throw Exception"); //$NON-NLS-1$ 568 } catch (InvalidMarkException e) { 569 // expected 570 } 571 572 // slice share the same content with buf 573 if (!slice.isReadOnly()) { 574 loadTestData1(slice); 575 assertContentLikeTestData1(buf, 1, (short) 0, slice.capacity()); 576 buf.put(2, (short) 500); 577 assertEquals(slice.get(1), 500); 578 } 579 } 580 581 public void testToString() { 582 String str = buf.toString(); 583 assertTrue(str.indexOf("Short") >= 0 || str.indexOf("short") >= 0); 584 assertTrue(str.indexOf("" + buf.position()) >= 0); 585 assertTrue(str.indexOf("" + buf.limit()) >= 0); 586 assertTrue(str.indexOf("" + buf.capacity()) >= 0); 587 } 588 589 void loadTestData1(short array[], int offset, int length) { 590 for (int i = 0; i < length; i++) { 591 array[offset + i] = (short) i; 592 } 593 } 594 595 void loadTestData2(short array[], int offset, int length) { 596 for (int i = 0; i < length; i++) { 597 array[offset + i] = (short) (length - i); 598 } 599 } 600 601 void loadTestData1(ShortBuffer buf) { 602 buf.clear(); 603 for (int i = 0; i < buf.capacity(); i++) { 604 buf.put(i, (short) i); 605 } 606 } 607 608 void loadTestData2(ShortBuffer buf) { 609 buf.clear(); 610 for (int i = 0; i < buf.capacity(); i++) { 611 buf.put(i, (short) (buf.capacity() - i)); 612 } 613 } 614 615 void assertContentEquals(ShortBuffer buf, short array[], 616 int offset, int length) { 617 for (int i = 0; i < length; i++) { 618 assertEquals(buf.get(i), array[offset + i]); 619 } 620 } 621 622 void assertContentEquals(ShortBuffer buf, ShortBuffer other) { 623 assertEquals(buf.capacity(), other.capacity()); 624 for (int i = 0; i < buf.capacity(); i++) { 625 assertEquals(buf.get(i), other.get(i)); 626 } 627 } 628 629 void assertContentLikeTestData1(ShortBuffer buf, 630 int startIndex, short startValue, int length) { 631 short value = startValue; 632 for (int i = 0; i < length; i++) { 633 assertEquals(buf.get(startIndex + i), value); 634 value = (short) (value + 1); 635 } 636 } 637} 638