1 /*
   2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* Type-specific source code for unit test
  25  *
  26  * Regenerate the BasicX classes via genBasic.sh whenever this file changes.
  27  * We check in the generated source files so that the test tree can be used
  28  * independently of the rest of the source tree.
  29  */
  30 
  31 // -- This file was mechanically generated: Do not edit! -- //
  32 
  33 import java.nio.*;
  34 
  35 
  36 public class BasicLong
  37     extends Basic
  38 {
  39 
  40     private static final long[] VALUES = {
  41         Long.MIN_VALUE,
  42         (long) -1,
  43         (long) 0,
  44         (long) 1,
  45         Long.MAX_VALUE,
  46 
  47 
  48 
  49 
  50 
  51 
  52 
  53 
  54 
  55 
  56 
  57 
  58     };
  59 
  60     private static void relGet(LongBuffer b) {
  61         int n = b.capacity();
  62         for (int i = 0; i < n; i++)
  63             ck(b, (long)b.get(), (long)((long)ic(i)));
  64         b.rewind();
  65     }
  66 
  67     private static void relGet(LongBuffer b, int start) {
  68         int n = b.remaining();
  69         for (int i = start; i < n; i++)
  70             ck(b, (long)b.get(), (long)((long)ic(i)));
  71         b.rewind();
  72     }
  73 
  74     private static void absGet(LongBuffer b) {
  75         int n = b.capacity();
  76         for (int i = 0; i < n; i++)
  77             ck(b, (long)b.get(), (long)((long)ic(i)));
  78         b.rewind();
  79     }
  80 
  81     private static void bulkGet(LongBuffer b) {
  82         int n = b.capacity();
  83         long[] a = new long[n + 7];
  84         b.get(a, 7, n);
  85         for (int i = 0; i < n; i++) {
  86             ck(b, (long)a[i + 7], (long)((long)ic(i)));
  87         }
  88     }
  89 
  90     private static void relPut(LongBuffer b) {
  91         int n = b.capacity();
  92         b.clear();
  93         for (int i = 0; i < n; i++)
  94             b.put((long)ic(i));
  95         b.flip();
  96     }
  97 
  98     private static void absPut(LongBuffer b) {
  99         int n = b.capacity();
 100         b.clear();
 101         for (int i = 0; i < n; i++)
 102             b.put(i, (long)ic(i));
 103         b.limit(n);
 104         b.position(0);
 105     }
 106 
 107     private static void bulkPutArray(LongBuffer b) {
 108         int n = b.capacity();
 109         b.clear();
 110         long[] a = new long[n + 7];
 111         for (int i = 0; i < n; i++)
 112             a[i + 7] = (long)ic(i);
 113         b.put(a, 7, n);
 114         b.flip();
 115     }
 116 
 117     private static void bulkPutBuffer(LongBuffer b) {
 118         int n = b.capacity();
 119         b.clear();
 120         LongBuffer c = LongBuffer.allocate(n + 7);
 121         c.position(7);
 122         for (int i = 0; i < n; i++)
 123             c.put((long)ic(i));
 124         c.flip();
 125         c.position(7);
 126         b.put(c);
 127         b.flip();
 128         try {
 129             b.put(b);
 130             fail("IllegalArgumentException expected for put into same buffer");
 131         } catch (IllegalArgumentException e) {
 132             if (e.getMessage() == null) {
 133                 fail("Non-null IllegalArgumentException message expected from"
 134                      + " put into same buffer");
 135             }
 136         }
 137     }
 138 
 139     //6231529
 140     private static void callReset(LongBuffer b) {
 141         b.position(0);
 142         b.mark();
 143 
 144         b.duplicate().reset();
 145         b.asReadOnlyBuffer().reset();
 146     }
 147 
 148 
 149 
 150     // 6221101-6234263
 151 
 152     private static void putBuffer() {
 153         final int cap = 10;
 154 
 155         LongBuffer direct1 = ByteBuffer.allocateDirect(cap).asLongBuffer();
 156         LongBuffer nondirect1 = ByteBuffer.allocate(cap).asLongBuffer();
 157         direct1.put(nondirect1);
 158 
 159         LongBuffer direct2 = ByteBuffer.allocateDirect(cap).asLongBuffer();
 160         LongBuffer nondirect2 = ByteBuffer.allocate(cap).asLongBuffer();
 161         nondirect2.put(direct2);
 162 
 163         LongBuffer direct3 = ByteBuffer.allocateDirect(cap).asLongBuffer();
 164         LongBuffer direct4 = ByteBuffer.allocateDirect(cap).asLongBuffer();
 165         direct3.put(direct4);
 166 
 167         LongBuffer nondirect3 = ByteBuffer.allocate(cap).asLongBuffer();
 168         LongBuffer nondirect4 = ByteBuffer.allocate(cap).asLongBuffer();
 169         nondirect3.put(nondirect4);
 170     }
 171 
 172 
 173 
 174 
 175 
 176 
 177 
 178 
 179 
 180 
 181 
 182 
 183 
 184 
 185 
 186 
 187 
 188     private static void checkSlice(LongBuffer b, LongBuffer slice) {
 189         ck(slice, 0, slice.position());
 190         ck(slice, b.remaining(), slice.limit());
 191         ck(slice, b.remaining(), slice.capacity());
 192         if (b.isDirect() != slice.isDirect())
 193             fail("Lost direction", slice);
 194         if (b.isReadOnly() != slice.isReadOnly())
 195             fail("Lost read-only", slice);
 196     }
 197 
 198 
 199 
 200 
 201 
 202 
 203 
 204 
 205 
 206 
 207 
 208 
 209 
 210 
 211 
 212 
 213 
 214 
 215 
 216 
 217 
 218 
 219 
 220 
 221 
 222 
 223 
 224 
 225 
 226 
 227 
 228 
 229 
 230 
 231 
 232 
 233 
 234 
 235 
 236 
 237 
 238 
 239 
 240 
 241 
 242 
 243 
 244 
 245 
 246 
 247 
 248 
 249 
 250 
 251 
 252 
 253 
 254 
 255 
 256 
 257 
 258 
 259 
 260 
 261 
 262 
 263 
 264 
 265 
 266 
 267 
 268 
 269 
 270 
 271 
 272 
 273 
 274 
 275 
 276 
 277 
 278 
 279 
 280 
 281 
 282 
 283 
 284 
 285 
 286 
 287 
 288 
 289 
 290 
 291 
 292 
 293 
 294 
 295 
 296 
 297 
 298 
 299 
 300 
 301 
 302 
 303 
 304 
 305 
 306 
 307 
 308 
 309 
 310 
 311 
 312 
 313 
 314 
 315 
 316 
 317 
 318 
 319 
 320 
 321 
 322 
 323 
 324 
 325 
 326 
 327 
 328 
 329 
 330 
 331 
 332 
 333 
 334 
 335 
 336 
 337 
 338 
 339 
 340 
 341 
 342 
 343 
 344 
 345 
 346 
 347 
 348 
 349 
 350 
 351 
 352 
 353 
 354 
 355 
 356 
 357 
 358 
 359 
 360 
 361 
 362 
 363 
 364 
 365 
 366 
 367 
 368 
 369 
 370 
 371 
 372 
 373 
 374 
 375 
 376 
 377 
 378 
 379 
 380 
 381 
 382 
 383 
 384 
 385 
 386 
 387 
 388 
 389 
 390 
 391 
 392 
 393 
 394 
 395 
 396 
 397 
 398 
 399 
 400 
 401 
 402 
 403 
 404 
 405 
 406 
 407 
 408 
 409 
 410 
 411 
 412 
 413 
 414 
 415 
 416 
 417 
 418 
 419 
 420 
 421 
 422 
 423 
 424 
 425 
 426 
 427 
 428 
 429 
 430 
 431 
 432 
 433 
 434 
 435 
 436 
 437 
 438 
 439 
 440 
 441 
 442 
 443 
 444 
 445 
 446 
 447 
 448 
 449     private static void fail(String problem,
 450                              LongBuffer xb, LongBuffer yb,
 451                              long x, long y) {
 452         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
 453     }
 454 
 455     private static void catchIllegalArgument(Buffer b, Runnable thunk) {
 456         tryCatch(b, IllegalArgumentException.class, thunk);
 457     }
 458 
 459     private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
 460         tryCatch(b, ReadOnlyBufferException.class, thunk);
 461     }
 462 
 463     private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {
 464         tryCatch(b, IndexOutOfBoundsException.class, thunk);
 465     }
 466 
 467     private static void catchIndexOutOfBounds(long[] t, Runnable thunk) {
 468         tryCatch(t, IndexOutOfBoundsException.class, thunk);
 469     }
 470 
 471     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
 472         boolean caught = false;
 473         try {
 474             thunk.run();
 475         } catch (Throwable x) {
 476             if (ex.isAssignableFrom(x.getClass())) {
 477                 caught = true;
 478             } else {
 479                 fail(x.getMessage() + " not expected");
 480             }
 481         }
 482         if (!caught) {
 483             fail(ex.getName() + " not thrown", b);
 484         }
 485     }
 486 
 487     private static void tryCatch(long[] t, Class<?> ex, Runnable thunk) {
 488         tryCatch(LongBuffer.wrap(t), ex, thunk);
 489     }
 490 
 491     public static void test(int level, final LongBuffer b, boolean direct) {
 492 
 493         show(level, b);
 494 
 495         if (direct != b.isDirect())
 496             fail("Wrong direction", b);
 497 
 498         // Gets and puts
 499 
 500         relPut(b);
 501         relGet(b);
 502         absGet(b);
 503         bulkGet(b);
 504 
 505         absPut(b);
 506         relGet(b);
 507         absGet(b);
 508         bulkGet(b);
 509 
 510         bulkPutArray(b);
 511         relGet(b);
 512 
 513         bulkPutBuffer(b);
 514         relGet(b);
 515 
 516 
 517 
 518 
 519 
 520 
 521 
 522 
 523 
 524 
 525 
 526 
 527 
 528 
 529 
 530 
 531 
 532 
 533 
 534 
 535 
 536 
 537 
 538 
 539 
 540 
 541 
 542 
 543 
 544 
 545 
 546 
 547 
 548 
 549 
 550 
 551 
 552         // Compact
 553 
 554         relPut(b);
 555         b.position(13);
 556         b.compact();
 557         b.flip();
 558         relGet(b, 13);
 559 
 560         // Exceptions
 561 
 562         relPut(b);
 563         b.limit(b.capacity() / 2);
 564         b.position(b.limit());
 565 
 566         tryCatch(b, BufferUnderflowException.class, () -> b.get());
 567         tryCatch(b, BufferOverflowException.class, () -> b.put((long)42));
 568         // The index must be non-negative and less than the buffer's limit.
 569         catchIndexOutOfBounds(b, () -> b.get(b.limit()));
 570         catchIndexOutOfBounds(b, () -> b.get(-1));
 571         catchIndexOutOfBounds(b, () -> b.put(b.limit(), (long)42));
 572         tryCatch(b, InvalidMarkException.class,
 573                 () -> b.position(0).mark().compact().reset());
 574 
 575         try {
 576             b.position(b.limit() + 1);
 577             fail("IllegalArgumentException expected for position beyond limit");
 578         } catch (IllegalArgumentException e) {
 579             if (e.getMessage() == null) {
 580                 fail("Non-null IllegalArgumentException message expected for"
 581                      + " position beyond limit");
 582             }
 583         }
 584 
 585         try {
 586             b.position(-1);
 587             fail("IllegalArgumentException expected for negative position");
 588         } catch (IllegalArgumentException e) {
 589             if (e.getMessage() == null) {
 590                 fail("Non-null IllegalArgumentException message expected for"
 591                      + " negative position");
 592             }
 593         }
 594 
 595         try {
 596             b.limit(b.capacity() + 1);
 597             fail("IllegalArgumentException expected for limit beyond capacity");
 598         } catch (IllegalArgumentException e) {
 599             if (e.getMessage() == null) {
 600                 fail("Non-null IllegalArgumentException message expected for"
 601                      + " limit beyond capacity");
 602             }
 603         }
 604 
 605         try {
 606             b.limit(-1);
 607             fail("IllegalArgumentException expected for negative limit");
 608         } catch (IllegalArgumentException e) {
 609             if (e.getMessage() == null) {
 610                 fail("Non-null IllegalArgumentException message expected for"
 611                      + " negative limit");
 612             }
 613         }
 614 
 615         // Values
 616 
 617         b.clear();
 618         b.put((long)0);
 619         b.put((long)-1);
 620         b.put((long)1);
 621         b.put(Long.MAX_VALUE);
 622         b.put(Long.MIN_VALUE);
 623 
 624 
 625 
 626 
 627 
 628 
 629 
 630 
 631 
 632 
 633 
 634 
 635 
 636 
 637 
 638 
 639 
 640         b.flip();
 641         ck(b, b.get(), 0);
 642         ck(b, b.get(), (long)-1);
 643         ck(b, b.get(), 1);
 644         ck(b, b.get(), Long.MAX_VALUE);
 645         ck(b, b.get(), Long.MIN_VALUE);
 646 
 647 
 648 
 649 
 650 
 651 
 652 
 653 
 654 
 655 
 656 
 657 
 658 
 659 
 660 
 661 
 662 
 663 
 664 
 665 
 666 
 667 
 668 
 669 
 670 
 671 
 672 
 673         // Comparison
 674         b.rewind();
 675         LongBuffer b2 = LongBuffer.allocate(b.capacity());
 676         b2.put(b);
 677         b2.flip();
 678         b.position(2);
 679         b2.position(2);
 680         if (!b.equals(b2)) {
 681             for (int i = 2; i < b.limit(); i++) {
 682                 long x = b.get(i);
 683                 long y = b2.get(i);
 684                 if (x != y
 685 
 686 
 687 
 688 
 689 
 690 
 691                     ) {
 692                     out.println("[" + i + "] " + x + " != " + y);
 693                 }
 694             }
 695             fail("Identical buffers not equal", b, b2);
 696         }
 697         if (b.compareTo(b2) != 0) {
 698             fail("Comparison to identical buffer != 0", b, b2);
 699         }
 700         b.limit(b.limit() + 1);
 701         b.position(b.limit() - 1);
 702         b.put((long)99);
 703         b.rewind();
 704         b2.rewind();
 705         if (b.equals(b2))
 706             fail("Non-identical buffers equal", b, b2);
 707         if (b.compareTo(b2) <= 0)
 708             fail("Comparison to shorter buffer <= 0", b, b2);
 709         b.limit(b.limit() - 1);
 710 
 711         b.put(2, (long)42);
 712         if (b.equals(b2))
 713             fail("Non-identical buffers equal", b, b2);
 714         if (b.compareTo(b2) <= 0)
 715             fail("Comparison to lesser buffer <= 0", b, b2);
 716 
 717         // Check equals and compareTo with interesting values
 718         for (long x : VALUES) {
 719             LongBuffer xb = LongBuffer.wrap(new long[] { x });
 720             if (xb.compareTo(xb) != 0) {
 721                 fail("compareTo not reflexive", xb, xb, x, x);
 722             }
 723             if (!xb.equals(xb)) {
 724                 fail("equals not reflexive", xb, xb, x, x);
 725             }
 726             for (long y : VALUES) {
 727                 LongBuffer yb = LongBuffer.wrap(new long[] { y });
 728                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
 729                     fail("compareTo not anti-symmetric",
 730                          xb, yb, x, y);
 731                 }
 732                 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {
 733                     fail("compareTo inconsistent with equals",
 734                          xb, yb, x, y);
 735                 }
 736                 if (xb.compareTo(yb) != Long.compare(x, y)) {
 737 
 738 
 739 
 740 
 741 
 742 
 743                     fail("Incorrect results for LongBuffer.compareTo",
 744                          xb, yb, x, y);
 745                 }
 746                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
 747                     fail("Incorrect results for LongBuffer.equals",
 748                          xb, yb, x, y);
 749                 }
 750             }
 751         }
 752 
 753         // Sub, dup
 754 
 755         relPut(b);
 756         relGet(b.duplicate());
 757         b.position(13);
 758         relGet(b.duplicate(), 13);
 759         relGet(b.duplicate().slice(), 13);
 760         relGet(b.slice(), 13);
 761         relGet(b.slice().duplicate(), 13);
 762 
 763         // Slice
 764 
 765         b.position(5);
 766         LongBuffer sb = b.slice();
 767         checkSlice(b, sb);
 768         b.position(0);
 769         LongBuffer sb2 = sb.slice();
 770         checkSlice(sb, sb2);
 771 
 772         if (!sb.equals(sb2))
 773             fail("Sliced slices do not match", sb, sb2);
 774         if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) {
 775             fail("Array offsets do not match: "
 776                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
 777         }
 778 
 779 
 780 
 781 
 782 
 783 
 784 
 785 
 786 
 787 
 788 
 789 
 790 
 791 
 792 
 793 
 794 
 795 
 796 
 797 
 798 
 799 
 800 
 801 
 802 
 803 
 804 
 805 
 806 
 807 
 808 
 809 
 810         // Read-only views
 811 
 812         b.rewind();
 813         final LongBuffer rb = b.asReadOnlyBuffer();
 814         if (!b.equals(rb))
 815             fail("Buffer not equal to read-only view", b, rb);
 816         show(level + 1, rb);
 817 
 818         catchReadOnlyBuffer(b, () -> relPut(rb));
 819         catchReadOnlyBuffer(b, () -> absPut(rb));
 820         catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
 821         catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
 822 
 823         // put(LongBuffer) should not change source position
 824         final LongBuffer src = LongBuffer.allocate(1);
 825         catchReadOnlyBuffer(b, () -> rb.put(src));
 826         ck(src, src.position(), 0);
 827 
 828         catchReadOnlyBuffer(b, () -> rb.compact());
 829 
 830 
 831 
 832 
 833 
 834 
 835 
 836 
 837 
 838 
 839 
 840 
 841 
 842 
 843 
 844 
 845 
 846 
 847 
 848 
 849 
 850 
 851 
 852 
 853 
 854 
 855         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 856             catchReadOnlyBuffer(b, () -> rb.array());
 857             catchReadOnlyBuffer(b, () -> rb.arrayOffset());
 858             if (rb.hasArray()) {
 859                 fail("Read-only heap buffer's backing array is accessible", rb);
 860             }
 861         }
 862 
 863         // Bulk puts from read-only buffers
 864 
 865         b.clear();
 866         rb.rewind();
 867         b.put(rb);
 868 
 869 
 870 
 871 
 872 
 873 
 874 
 875 
 876 
 877 
 878 
 879         relPut(b);                       // Required by testViews
 880 
 881 
 882 
 883 
 884 
 885 
 886     }
 887 
 888 
 889 
 890 
 891 
 892 
 893 
 894 
 895 
 896 
 897 
 898 
 899 
 900 
 901 
 902 
 903 
 904 
 905 
 906 
 907 
 908 
 909 
 910 
 911 
 912 
 913 
 914 
 915 
 916 
 917 
 918 
 919 
 920 
 921 
 922 
 923 
 924 
 925 
 926 
 927 
 928 
 929 
 930 
 931     public static void test(final long [] ba) {
 932         int offset = 47;
 933         int length = 900;
 934         final LongBuffer b = LongBuffer.wrap(ba, offset, length);
 935         show(0, b);
 936         ck(b, b.capacity(), ba.length);
 937         ck(b, b.position(), offset);
 938         ck(b, b.limit(), offset + length);
 939 
 940         // The offset must be non-negative and no larger than <array.length>.
 941         catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, -1, ba.length));
 942         catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, ba.length + 1, ba.length));
 943         catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, 0, -1));
 944         catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, 0, ba.length + 1));
 945 
 946         // A NullPointerException will be thrown if the array is null.
 947         tryCatch(ba, NullPointerException.class,
 948                 () -> LongBuffer.wrap((long []) null, 0, 5));
 949         tryCatch(ba, NullPointerException.class,
 950                 () -> LongBuffer.wrap((long []) null));
 951     }
 952 
 953     private static void testAllocate() {
 954         // An IllegalArgumentException will be thrown for negative capacities.
 955         catchIllegalArgument((Buffer) null, () -> LongBuffer.allocate(-1));
 956         try {
 957             LongBuffer.allocate(-1);
 958         } catch (IllegalArgumentException e) {
 959             if (e.getMessage() == null) {
 960                 fail("Non-null IllegalArgumentException message expected for"
 961                      + " attempt to allocate negative capacity buffer");
 962             }
 963         }
 964 
 965 
 966 
 967 
 968 
 969 
 970 
 971 
 972 
 973 
 974 
 975     }
 976 
 977     public static void test() {
 978         testAllocate();
 979         test(0, LongBuffer.allocate(7 * 1024), false);
 980         test(0, LongBuffer.wrap(new long[7 * 1024], 0, 7 * 1024), false);
 981         test(new long[1024]);
 982 
 983 
 984 
 985 
 986 
 987 
 988 
 989 
 990 
 991 
 992         callReset(LongBuffer.allocate(10));
 993 
 994 
 995 
 996         putBuffer();
 997 
 998     }
 999 
1000 }