< prev index next >

test/java/nio/Buffer/BasicLong.java

Print this page




  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 import java.lang.reflect.Method;
  35 
  36 
  37 public class BasicLong
  38     extends Basic
  39 {
  40 
  41     private static final long[] VALUES = {
  42         Long.MIN_VALUE,
  43         (long) -1,
  44         (long) 0,
  45         (long) 1,
  46         Long.MAX_VALUE,
  47 
  48 
  49 
  50 
  51 
  52 
  53 
  54 
  55 
  56 
  57 
  58 
  59     };
  60 
  61     private static void relGet(LongBuffer b) {
  62         int n = b.capacity();
  63         long v;
  64         for (int i = 0; i < n; i++)
  65             ck(b, (long)b.get(), (long)((long)ic(i)));

  66         b.rewind();
  67     }
  68 
  69     private static void relGet(LongBuffer b, int start) {
  70         int n = b.remaining();
  71         long v;
  72         for (int i = start; i < n; i++)
  73             ck(b, (long)b.get(), (long)((long)ic(i)));

  74         b.rewind();
  75     }
  76 
  77     private static void absGet(LongBuffer b) {
  78         int n = b.capacity();
  79         long v;
  80         for (int i = 0; i < n; i++)
  81             ck(b, (long)b.get(), (long)((long)ic(i)));

  82         b.rewind();
  83     }
  84 
  85     private static void bulkGet(LongBuffer b) {
  86         int n = b.capacity();
  87         long[] a = new long[n + 7];
  88         b.get(a, 7, n);
  89         for (int i = 0; i < n; i++)
  90             ck(b, (long)a[i + 7], (long)((long)ic(i)));
  91     }

  92 
  93     private static void relPut(LongBuffer b) {
  94         int n = b.capacity();
  95         b.clear();
  96         for (int i = 0; i < n; i++)
  97             b.put((long)ic(i));

  98         b.flip();
  99     }
 100 
 101     private static void absPut(LongBuffer b) {
 102         int n = b.capacity();
 103         b.clear();
 104         for (int i = 0; i < n; i++)
 105             b.put(i, (long)ic(i));

 106         b.limit(n);
 107         b.position(0);
 108     }
 109 
 110     private static void bulkPutArray(LongBuffer b) {
 111         int n = b.capacity();
 112         b.clear();
 113         long[] a = new long[n + 7];
 114         for (int i = 0; i < n; i++)
 115             a[i + 7] = (long)ic(i);

 116         b.put(a, 7, n);
 117         b.flip();
 118     }
 119 
 120     private static void bulkPutBuffer(LongBuffer b) {
 121         int n = b.capacity();
 122         b.clear();
 123         LongBuffer c = LongBuffer.allocate(n + 7);
 124         c.position(7);
 125         for (int i = 0; i < n; i++)
 126             c.put((long)ic(i));

 127         c.flip();
 128         c.position(7);
 129         b.put(c);
 130         b.flip();
 131         try {
 132             b.put(b);
 133             fail("IllegalArgumentException expected for put into same buffer");
 134         } catch (IllegalArgumentException e) {
 135             if (e.getMessage() == null) {
 136                 fail("Non-null IllegalArgumentException message expected from"
 137                      + " put into same buffer");
 138             }
 139         }
 140     }
 141 
 142     //6231529
 143     private static void callReset(LongBuffer b) {
 144         b.position(0);
 145         b.mark();
 146 


 171         LongBuffer nondirect4 = ByteBuffer.allocate(cap).asLongBuffer();
 172         nondirect3.put(nondirect4);
 173     }
 174 
 175 
 176 
 177 
 178 
 179 
 180 
 181 
 182 
 183 
 184 
 185 
 186 
 187 
 188 
 189 
 190 

 191     private static void checkSlice(LongBuffer b, LongBuffer slice) {
 192         ck(slice, 0, slice.position());
 193         ck(slice, b.remaining(), slice.limit());
 194         ck(slice, b.remaining(), slice.capacity());
 195         if (b.isDirect() != slice.isDirect())
 196             fail("Lost direction", slice);
 197         if (b.isReadOnly() != slice.isReadOnly())

 198             fail("Lost read-only", slice);
 199     }








 200 
 201 
 202 
 203 
 204 
 205 
 206 
 207 
 208 
 209 
 210 
 211 
 212 
 213 
 214 
 215 
 216 
 217 
 218 
 219 


 435 
 436 
 437 
 438     private static void fail(String problem,
 439                              LongBuffer xb, LongBuffer yb,
 440                              long x, long y) {
 441         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
 442     }
 443 
 444     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
 445         boolean caught = false;
 446         try {
 447             thunk.run();
 448         } catch (Throwable x) {
 449             if (ex.isAssignableFrom(x.getClass())) {
 450                 caught = true;
 451             } else {
 452                 fail(x.getMessage() + " not expected");
 453             }
 454         }
 455         if (!caught)
 456             fail(ex.getName() + " not thrown", b);
 457     }

 458 
 459     private static void tryCatch(long [] t, Class<?> ex, Runnable thunk) {
 460         tryCatch(LongBuffer.wrap(t), ex, thunk);
 461     }
 462 
 463     public static void test(int level, final LongBuffer b, boolean direct) {
 464 
 465         show(level, b);
 466 
 467         if (direct != b.isDirect())
 468             fail("Wrong direction", b);

 469 
 470         // Gets and puts
 471 
 472         relPut(b);
 473         relGet(b);
 474         absGet(b);
 475         bulkGet(b);
 476 
 477         absPut(b);
 478         relGet(b);
 479         absGet(b);
 480         bulkGet(b);
 481 
 482         bulkPutArray(b);
 483         relGet(b);
 484 
 485         bulkPutBuffer(b);
 486         relGet(b);
 487 
 488 


 505 
 506 
 507 
 508 
 509 
 510 
 511 
 512 
 513 
 514 
 515 
 516 
 517 
 518 
 519 
 520 
 521 
 522 
 523 
 524 
 525 
 526         // Compact
 527 
 528         relPut(b);
 529         b.position(13);
 530         b.compact();
 531         b.flip();
 532         relGet(b, 13);
 533 
 534         // Exceptions
 535 
 536         relPut(b);
 537         b.limit(b.capacity() / 2);
 538         b.position(b.limit());
 539 
 540         tryCatch(b, BufferUnderflowException.class, new Runnable() {
 541                 public void run() {
 542                     b.get();
 543                 }});
 544 
 545         tryCatch(b, BufferOverflowException.class, new Runnable() {
 546                 public void run() {
 547                     b.put((long)42);
 548                 }});
 549 
 550         // The index must be non-negative and lesss than the buffer's limit.
 551         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 552                 public void run() {
 553                     b.get(b.limit());
 554                 }});
 555         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 556                 public void run() {
 557                     b.get(-1);
 558                 }});
 559 
 560         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 561                 public void run() {
 562                     b.put(b.limit(), (long)42);
 563                 }});
 564 
 565         tryCatch(b, InvalidMarkException.class, new Runnable() {
 566                 public void run() {
 567                     b.position(0);
 568                     b.mark();
 569                     b.compact();
 570                     b.reset();
 571                 }});
 572 
 573         try {
 574             b.position(b.limit() + 1);
 575             fail("IllegalArgumentException expected for position beyond limit");
 576         } catch (IllegalArgumentException e) {
 577             if (e.getMessage() == null) {
 578                 fail("Non-null IllegalArgumentException message expected for"
 579                      + " position beyond limit");
 580             }
 581         }
 582 
 583         try {
 584             b.position(-1);
 585             fail("IllegalArgumentException expected for negative position");
 586         } catch (IllegalArgumentException e) {
 587             if (e.getMessage() == null) {
 588                 fail("Non-null IllegalArgumentException message expected for"
 589                      + " negative position");
 590             }
 591         }


 618         b.put((long)1);
 619         b.put(Long.MAX_VALUE);
 620         b.put(Long.MIN_VALUE);
 621 
 622 
 623 
 624 
 625 
 626 
 627 
 628 
 629 
 630 
 631 
 632 
 633 
 634 
 635 
 636 
 637 
 638         long v;
 639         b.flip();
 640         ck(b, b.get(), 0);
 641         ck(b, b.get(), (long)-1);
 642         ck(b, b.get(), 1);
 643         ck(b, b.get(), Long.MAX_VALUE);
 644         ck(b, b.get(), Long.MIN_VALUE);
 645 
 646 
 647 
 648 
 649 
 650 
 651 
 652 
 653 
 654 
 655 
 656 
 657 
 658 
 659 
 660 
 661 
 662 
 663 
 664 
 665 
 666 
 667 




 668         // Comparison
 669         b.rewind();
 670         LongBuffer b2 = LongBuffer.allocate(b.capacity());
 671         b2.put(b);
 672         b2.flip();
 673         b.position(2);
 674         b2.position(2);
 675         if (!b.equals(b2)) {
 676             for (int i = 2; i < b.limit(); i++) {
 677                 long x = b.get(i);
 678                 long y = b2.get(i);
 679                 if (x != y
 680 
 681 
 682 
 683 
 684 
 685 
 686                     )
 687                     out.println("[" + i + "] " + x + " != " + y);
 688             }

 689             fail("Identical buffers not equal", b, b2);
 690         }
 691         if (b.compareTo(b2) != 0)
 692             fail("Comparison to identical buffer != 0", b, b2);
 693 
 694         b.limit(b.limit() + 1);
 695         b.position(b.limit() - 1);
 696         b.put((long)99);
 697         b.rewind();
 698         b2.rewind();
 699         if (b.equals(b2))
 700             fail("Non-identical buffers equal", b, b2);
 701         if (b.compareTo(b2) <= 0)

 702             fail("Comparison to shorter buffer <= 0", b, b2);

 703         b.limit(b.limit() - 1);
 704 
 705         b.put(2, (long)42);
 706         if (b.equals(b2))
 707             fail("Non-identical buffers equal", b, b2);
 708         if (b.compareTo(b2) <= 0)

 709             fail("Comparison to lesser buffer <= 0", b, b2);

 710 
 711         // Check equals and compareTo with interesting values
 712         for (long x : VALUES) {
 713             LongBuffer xb = LongBuffer.wrap(new long[] { x });
 714             if (xb.compareTo(xb) != 0) {
 715                 fail("compareTo not reflexive", xb, xb, x, x);
 716             }
 717             if (! xb.equals(xb)) {
 718                 fail("equals not reflexive", xb, xb, x, x);
 719             }
 720             for (long y : VALUES) {
 721                 LongBuffer yb = LongBuffer.wrap(new long[] { y });
 722                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
 723                     fail("compareTo not anti-symmetric",
 724                          xb, yb, x, y);
 725                 }
 726                 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {
 727                     fail("compareTo inconsistent with equals",
 728                          xb, yb, x, y);
 729                 }
 730                 if (xb.compareTo(yb) != Long.compare(x, y)) {
 731 
 732 
 733 
 734 
 735 
 736 
 737                     fail("Incorrect results for LongBuffer.compareTo",


 746 
 747         // Sub, dup
 748 
 749         relPut(b);
 750         relGet(b.duplicate());
 751         b.position(13);
 752         relGet(b.duplicate(), 13);
 753         relGet(b.duplicate().slice(), 13);
 754         relGet(b.slice(), 13);
 755         relGet(b.slice().duplicate(), 13);
 756 
 757         // Slice
 758 
 759         b.position(5);
 760         LongBuffer sb = b.slice();
 761         checkSlice(b, sb);
 762         b.position(0);
 763         LongBuffer sb2 = sb.slice();
 764         checkSlice(sb, sb2);
 765 
 766         if (!sb.equals(sb2))
 767             fail("Sliced slices do not match", sb, sb2);
 768         if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset()))

 769             fail("Array offsets do not match: "
 770                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);

 771 
 772 
 773 
 774 
 775 
 776 
 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         // Read-only views
 804 
 805         b.rewind();
 806         final LongBuffer rb = b.asReadOnlyBuffer();
 807         if (!b.equals(rb))
 808             fail("Buffer not equal to read-only view", b, rb);

 809         show(level + 1, rb);
 810 
 811         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 812                 public void run() {
 813                     relPut(rb);
 814                 }});
 815 
 816         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 817                 public void run() {
 818                     absPut(rb);
 819                 }});
 820 
 821         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 822                 public void run() {
 823                     bulkPutArray(rb);
 824                 }});
 825 
 826         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 827                 public void run() {
 828                     bulkPutBuffer(rb);
 829                 }});
 830 
 831         // put(LongBuffer) should not change source position
 832         final LongBuffer src = LongBuffer.allocate(1);
 833         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 834                 public void run() {
 835                     rb.put(src);
 836                  }});
 837         ck(src, src.position(), 0);
 838 
 839         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 840                 public void run() {
 841                     rb.compact();
 842                 }});
 843 
 844 
 845 
 846 
 847 
 848 
 849 
 850 
 851 
 852 
 853 
 854 
 855 
 856 
 857 
 858 
 859 
 860 
 861 
 862 
 863 
 864 
 865 
 866 
 867 
 868 
 869 
 870 
 871 
 872 
 873 
 874 
 875 
 876 
 877 
 878 
 879 
 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         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 919 
 920             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 921                     public void run() {
 922                         rb.array();
 923                     }});
 924 
 925             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 926                     public void run() {
 927                         rb.arrayOffset();
 928                     }});
 929 
 930             if (rb.hasArray())
 931                 fail("Read-only heap buffer's backing array is accessible",
 932                      rb);
 933 
 934         }
 935 
 936         // Bulk puts from read-only buffers
 937 
 938         b.clear();
 939         rb.rewind();
 940         b.put(rb);
 941 
 942 
 943 
 944 
 945 
 946 
 947 
 948 
 949 
 950 
 951 
 952         relPut(b);                       // Required by testViews
 953 


1035 
1036 
1037 
1038 
1039 
1040 
1041 
1042 
1043 
1044 
1045     public static void test(final long [] ba) {
1046         int offset = 47;
1047         int length = 900;
1048         final LongBuffer b = LongBuffer.wrap(ba, offset, length);
1049         show(0, b);
1050         ck(b, b.capacity(), ba.length);
1051         ck(b, b.position(), offset);
1052         ck(b, b.limit(), offset + length);
1053 
1054         // The offset must be non-negative and no larger than <array.length>.
1055         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
1056                 public void run() {
1057                     LongBuffer.wrap(ba, -1, ba.length);
1058                 }});
1059         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
1060                 public void run() {
1061                     LongBuffer.wrap(ba, ba.length + 1, ba.length);
1062                 }});
1063         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
1064                 public void run() {
1065                     LongBuffer.wrap(ba, 0, -1);
1066                 }});
1067         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
1068                 public void run() {
1069                     LongBuffer.wrap(ba, 0, ba.length + 1);
1070                 }});
1071 
1072         // A NullPointerException will be thrown if the array is null.
1073         tryCatch(ba, NullPointerException.class, new Runnable() {
1074                 public void run() {
1075                     LongBuffer.wrap((long []) null, 0, 5);
1076                 }});
1077         tryCatch(ba, NullPointerException.class, new Runnable() {
1078                 public void run() {
1079                     LongBuffer.wrap((long []) null);
1080                 }});
1081     }
1082 
1083     private static void testAllocate() {
1084         // An IllegalArgumentException will be thrown for negative capacities.
1085         tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
1086                 public void run() {
1087                     LongBuffer.allocate(-1);
1088                 }});
1089         try {
1090             LongBuffer.allocate(-1);
1091         } catch (IllegalArgumentException e) {
1092             if (e.getMessage() == null) {
1093                 fail("Non-null IllegalArgumentException message expected for"
1094                      + " attempt to allocate negative capacity buffer");
1095             }
1096         }
1097 
1098 
1099 
1100 
1101 
1102 
1103 
1104 
1105 
1106 
1107 
1108 
1109 
1110 
1111     }
1112 
1113     public static void test() {
1114         testAllocate();
1115         test(0, LongBuffer.allocate(7 * 1024), false);
1116         test(0, LongBuffer.wrap(new long[7 * 1024], 0, 7 * 1024), false);
1117         test(new long[1024]);
1118 
1119 
1120 
1121 
1122 
1123 
1124 
1125 
1126 
1127 

1128         callReset(LongBuffer.allocate(10));
1129 
1130 
1131 
1132         putBuffer();
1133 
1134     }
1135 
1136 }


  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         }
  65         b.rewind();
  66     }
  67 
  68     private static void relGet(LongBuffer b, int start) {
  69         int n = b.remaining();
  70         for (int i = start; i < n; i++) {

  71             ck(b, (long)b.get(), (long)((long)ic(i)));
  72         }
  73         b.rewind();
  74     }
  75 
  76     private static void absGet(LongBuffer b) {
  77         int n = b.capacity();
  78         for (int i = 0; i < n; i++) {

  79             ck(b, (long)b.get(), (long)((long)ic(i)));
  80         }
  81         b.rewind();
  82     }
  83 
  84     private static void bulkGet(LongBuffer b) {
  85         int n = b.capacity();
  86         long[] a = new long[n + 7];
  87         b.get(a, 7, n);
  88         for (int i = 0; i < n; i++) {
  89             ck(b, (long)a[i + 7], (long)((long)ic(i)));
  90         }
  91     }
  92 
  93     private static void relPut(LongBuffer b) {
  94         int n = b.capacity();
  95         b.clear();
  96         for (int i = 0; i < n; i++) {
  97             b.put((long)ic(i));
  98         }
  99         b.flip();
 100     }
 101 
 102     private static void absPut(LongBuffer b) {
 103         int n = b.capacity();
 104         b.clear();
 105         for (int i = 0; i < n; i++) {
 106             b.put(i, (long)ic(i));
 107         }
 108         b.limit(n);
 109         b.position(0);
 110     }
 111 
 112     private static void bulkPutArray(LongBuffer b) {
 113         int n = b.capacity();
 114         b.clear();
 115         long[] a = new long[n + 7];
 116         for (int i = 0; i < n; i++) {
 117             a[i + 7] = (long)ic(i);
 118         }
 119         b.put(a, 7, n);
 120         b.flip();
 121     }
 122 
 123     private static void bulkPutBuffer(LongBuffer b) {
 124         int n = b.capacity();
 125         b.clear();
 126         LongBuffer c = LongBuffer.allocate(n + 7);
 127         c.position(7);
 128         for (int i = 0; i < n; i++) {
 129             c.put((long)ic(i));
 130         }
 131         c.flip();
 132         c.position(7);
 133         b.put(c);
 134         b.flip();
 135         try {
 136             b.put(b);
 137             fail("IllegalArgumentException expected for put into same buffer");
 138         } catch (IllegalArgumentException e) {
 139             if (e.getMessage() == null) {
 140                 fail("Non-null IllegalArgumentException message expected from"
 141                      + " put into same buffer");
 142             }
 143         }
 144     }
 145 
 146     //6231529
 147     private static void callReset(LongBuffer b) {
 148         b.position(0);
 149         b.mark();
 150 


 175         LongBuffer nondirect4 = ByteBuffer.allocate(cap).asLongBuffer();
 176         nondirect3.put(nondirect4);
 177     }
 178 
 179 
 180 
 181 
 182 
 183 
 184 
 185 
 186 
 187 
 188 
 189 
 190 
 191 
 192 
 193 
 194 
 195 
 196     private static void checkSlice(LongBuffer b, LongBuffer slice) {
 197         ck(slice, 0, slice.position());
 198         ck(slice, b.remaining(), slice.limit());
 199         ck(slice, b.remaining(), slice.capacity());
 200         if (b.isDirect() != slice.isDirect()) {
 201             fail("Lost direction", slice);
 202         }
 203         if (b.isReadOnly() != slice.isReadOnly()) {
 204             fail("Lost read-only", slice);
 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 


 449 
 450 
 451 
 452     private static void fail(String problem,
 453                              LongBuffer xb, LongBuffer yb,
 454                              long x, long y) {
 455         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
 456     }
 457 
 458     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
 459         boolean caught = false;
 460         try {
 461             thunk.run();
 462         } catch (Throwable x) {
 463             if (ex.isAssignableFrom(x.getClass())) {
 464                 caught = true;
 465             } else {
 466                 fail(x.getMessage() + " not expected");
 467             }
 468         }
 469         if (!caught) {
 470             fail(ex.getName() + " not thrown", b);
 471         }
 472     }
 473 
 474     private static void tryCatch(long [] t, Class<?> ex, Runnable thunk) {
 475         tryCatch(LongBuffer.wrap(t), ex, thunk);
 476     }
 477 
 478     public static void test(int level, final LongBuffer b, boolean direct) {
 479 
 480         show(level, b);
 481 
 482         if (direct != b.isDirect()) {
 483             fail("Wrong direction", b);
 484         }
 485 
 486         // Gets and puts
 487 
 488         relPut(b);
 489         relGet(b);
 490         absGet(b);
 491         bulkGet(b);
 492 
 493         absPut(b);
 494         relGet(b);
 495         absGet(b);
 496         bulkGet(b);
 497 
 498         bulkPutArray(b);
 499         relGet(b);
 500 
 501         bulkPutBuffer(b);
 502         relGet(b);
 503 
 504 


 521 
 522 
 523 
 524 
 525 
 526 
 527 
 528 
 529 
 530 
 531 
 532 
 533 
 534 
 535 
 536 
 537 
 538 
 539 
 540 

 541         // Compact
 542 
 543         relPut(b);
 544         b.position(13);
 545         b.compact();
 546         b.flip();
 547         relGet(b, 13);
 548 
 549         // Exceptions
 550 
 551         relPut(b);
 552         b.limit(b.capacity() / 2);
 553         b.position(b.limit());
 554 
 555         tryCatch(b, BufferUnderflowException.class, () -> b.get());
 556         tryCatch(b, BufferOverflowException.class, () -> b.put((long)42));
 557         // The index must be non-negative and less than the buffer's limit.
 558         tryCatch(b, IndexOutOfBoundsException.class, () -> b.get(b.limit()));
 559         tryCatch(b, IndexOutOfBoundsException.class, () -> b.get(-1));
 560         tryCatch(b, IndexOutOfBoundsException.class, () -> b.put(b.limit(), (long)42));
 561         tryCatch(b, InvalidMarkException.class, () -> b.position(0).mark().compact().reset());

























 562 
 563         try {
 564             b.position(b.limit() + 1);
 565             fail("IllegalArgumentException expected for position beyond limit");
 566         } catch (IllegalArgumentException e) {
 567             if (e.getMessage() == null) {
 568                 fail("Non-null IllegalArgumentException message expected for"
 569                      + " position beyond limit");
 570             }
 571         }
 572 
 573         try {
 574             b.position(-1);
 575             fail("IllegalArgumentException expected for negative position");
 576         } catch (IllegalArgumentException e) {
 577             if (e.getMessage() == null) {
 578                 fail("Non-null IllegalArgumentException message expected for"
 579                      + " negative position");
 580             }
 581         }


 608         b.put((long)1);
 609         b.put(Long.MAX_VALUE);
 610         b.put(Long.MIN_VALUE);
 611 
 612 
 613 
 614 
 615 
 616 
 617 
 618 
 619 
 620 
 621 
 622 
 623 
 624 
 625 
 626 
 627 

 628         b.flip();
 629         ck(b, b.get(), 0);
 630         ck(b, b.get(), (long)-1);
 631         ck(b, b.get(), 1);
 632         ck(b, b.get(), Long.MAX_VALUE);
 633         ck(b, b.get(), Long.MIN_VALUE);
 634 
 635 
 636 
 637 
 638 
 639 
 640 
 641 
 642 
 643 
 644 
 645 
 646 
 647 
 648 
 649 
 650 
 651 
 652 
 653 
 654 
 655 
 656 
 657 
 658 
 659 
 660 
 661         // Comparison
 662         b.rewind();
 663         LongBuffer b2 = LongBuffer.allocate(b.capacity());
 664         b2.put(b);
 665         b2.flip();
 666         b.position(2);
 667         b2.position(2);
 668         if (!b.equals(b2)) {
 669             for (int i = 2; i < b.limit(); i++) {
 670                 long x = b.get(i);
 671                 long y = b2.get(i);
 672                 if (x != y
 673 
 674 
 675 
 676 
 677 
 678 
 679                     ) {
 680                     out.println("[" + i + "] " + x + " != " + y);
 681                 }
 682             }
 683             fail("Identical buffers not equal", b, b2);
 684         }
 685         if (b.compareTo(b2) != 0) {
 686             fail("Comparison to identical buffer != 0", b, b2);
 687         }
 688         b.limit(b.limit() + 1);
 689         b.position(b.limit() - 1);
 690         b.put((long)99);
 691         b.rewind();
 692         b2.rewind();
 693         if (b.equals(b2)) {
 694             fail("Non-identical buffers equal", b, b2);
 695         }
 696         if (b.compareTo(b2) <= 0) {
 697             fail("Comparison to shorter buffer <= 0", b, b2);
 698         }
 699         b.limit(b.limit() - 1);
 700 
 701         b.put(2, (long)42);
 702         if (b.equals(b2)) {
 703             fail("Non-identical buffers equal", b, b2);
 704         }
 705         if (b.compareTo(b2) <= 0) {
 706             fail("Comparison to lesser buffer <= 0", b, b2);
 707         }
 708 
 709         // Check equals and compareTo with interesting values
 710         for (long x : VALUES) {
 711             LongBuffer xb = LongBuffer.wrap(new long[] { x });
 712             if (xb.compareTo(xb) != 0) {
 713                 fail("compareTo not reflexive", xb, xb, x, x);
 714             }
 715             if (!xb.equals(xb)) {
 716                 fail("equals not reflexive", xb, xb, x, x);
 717             }
 718             for (long y : VALUES) {
 719                 LongBuffer yb = LongBuffer.wrap(new long[] { y });
 720                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
 721                     fail("compareTo not anti-symmetric",
 722                          xb, yb, x, y);
 723                 }
 724                 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {
 725                     fail("compareTo inconsistent with equals",
 726                          xb, yb, x, y);
 727                 }
 728                 if (xb.compareTo(yb) != Long.compare(x, y)) {
 729 
 730 
 731 
 732 
 733 
 734 
 735                     fail("Incorrect results for LongBuffer.compareTo",


 744 
 745         // Sub, dup
 746 
 747         relPut(b);
 748         relGet(b.duplicate());
 749         b.position(13);
 750         relGet(b.duplicate(), 13);
 751         relGet(b.duplicate().slice(), 13);
 752         relGet(b.slice(), 13);
 753         relGet(b.slice().duplicate(), 13);
 754 
 755         // Slice
 756 
 757         b.position(5);
 758         LongBuffer sb = b.slice();
 759         checkSlice(b, sb);
 760         b.position(0);
 761         LongBuffer sb2 = sb.slice();
 762         checkSlice(sb, sb2);
 763 
 764         if (!sb.equals(sb2)) {
 765             fail("Sliced slices do not match", sb, sb2);
 766         }
 767         if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) {
 768             fail("Array offsets do not match: "
 769                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
 770         }
 771 
 772 
 773 
 774 
 775 
 776 
 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         // Read-only views
 804 
 805         b.rewind();
 806         final LongBuffer rb = b.asReadOnlyBuffer();
 807         if (!b.equals(rb)) {
 808             fail("Buffer not equal to read-only view", b, rb);
 809         }
 810         show(level + 1, rb);
 811 
 812         tryCatch(b, ReadOnlyBufferException.class, () -> relPut(rb));
 813         tryCatch(b, ReadOnlyBufferException.class, () -> absPut(rb));
 814         tryCatch(b, ReadOnlyBufferException.class, () -> bulkPutArray(rb));
 815         tryCatch(b, ReadOnlyBufferException.class, () -> bulkPutBuffer(rb));















 816 
 817         // put(LongBuffer) should not change source position
 818         final LongBuffer src = LongBuffer.allocate(1);
 819         tryCatch(b, ReadOnlyBufferException.class, () -> rb.put(src));



 820         ck(src, src.position(), 0);
 821 
 822         tryCatch(b, ReadOnlyBufferException.class, () -> rb.compact());












































 823 
 824 
 825 
 826 
 827 
 828 
 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 
 856 
 857         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 858             tryCatch(b, ReadOnlyBufferException.class, () -> rb.array());
 859             tryCatch(b, ReadOnlyBufferException.class, () -> rb.arrayOffset());
 860             if (rb.hasArray()) {
 861                 fail("Read-only heap buffer's backing array is accessible", rb);
 862             }










 863         }
 864 
 865         // Bulk puts from read-only buffers
 866 
 867         b.clear();
 868         rb.rewind();
 869         b.put(rb);
 870 
 871 
 872 
 873 
 874 
 875 
 876 
 877 
 878 
 879 
 880 
 881         relPut(b);                       // Required by testViews
 882 


 964 
 965 
 966 
 967 
 968 
 969 
 970 
 971 
 972 
 973 
 974     public static void test(final long [] ba) {
 975         int offset = 47;
 976         int length = 900;
 977         final LongBuffer b = LongBuffer.wrap(ba, offset, length);
 978         show(0, b);
 979         ck(b, b.capacity(), ba.length);
 980         ck(b, b.position(), offset);
 981         ck(b, b.limit(), offset + length);
 982 
 983         // The offset must be non-negative and no larger than <array.length>.
 984         tryCatch(ba, IndexOutOfBoundsException.class, () -> LongBuffer.wrap(ba, -1, ba.length));
 985         tryCatch(ba, IndexOutOfBoundsException.class, () -> LongBuffer.wrap(ba, ba.length + 1, ba.length));
 986         tryCatch(ba, IndexOutOfBoundsException.class, () -> LongBuffer.wrap(ba, 0, -1));
 987         tryCatch(ba, IndexOutOfBoundsException.class, () -> LongBuffer.wrap(ba, 0, ba.length + 1));












 988 
 989         // A NullPointerException will be thrown if the array is null.
 990         tryCatch(ba, NullPointerException.class, () -> LongBuffer.wrap((long []) null, 0, 5));
 991         tryCatch(ba, NullPointerException.class, () -> LongBuffer.wrap((long []) null));






 992     }
 993 
 994     private static void testAllocate() {
 995         // An IllegalArgumentException will be thrown for negative capacities.
 996         tryCatch((Buffer) null, IllegalArgumentException.class, () -> LongBuffer.allocate(-1));



 997         try {
 998             LongBuffer.allocate(-1);
 999         } catch (IllegalArgumentException e) {
1000             if (e.getMessage() == null) {
1001                 fail("Non-null IllegalArgumentException message expected for"
1002                      + " attempt to allocate negative capacity buffer");
1003             }
1004         }
1005 
1006 
1007 
1008 
1009 
1010 
1011 
1012 
1013 
1014 
1015 



1016     }
1017 
1018     public static void test() {
1019         testAllocate();
1020         test(0, LongBuffer.allocate(7 * 1024), false);
1021         test(0, LongBuffer.wrap(new long[7 * 1024], 0, 7 * 1024), false);
1022         test(new long[1024]);
1023 
1024 
1025 
1026 
1027 
1028 
1029 
1030 
1031 
1032 
1033 
1034         callReset(LongBuffer.allocate(10));
1035 
1036 
1037 
1038         putBuffer();
1039 
1040     }
1041 
1042 }
< prev index next >