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 BasicDouble
  37     extends Basic
  38 {
  39 
  40     private static final double[] VALUES = {
  41         Double.MIN_VALUE,
  42         (double) -1,
  43         (double) 0,
  44         (double) 1,
  45         Double.MAX_VALUE,
  46 
  47 
  48 
  49 
  50 
  51 
  52 
  53         Double.NEGATIVE_INFINITY,
  54         Double.POSITIVE_INFINITY,
  55         Double.NaN,
  56         (double) -0.0,
  57 
  58     };
  59 
  60     private static void relGet(DoubleBuffer b) {
  61         int n = b.capacity();
  62         for (int i = 0; i < n; i++) {
  63             ck(b, (long)b.get(), (long)((double)ic(i)));
  64         }
  65         b.rewind();
  66     }
  67 
  68     private static void relGet(DoubleBuffer b, int start) {
  69         int n = b.remaining();
  70         for (int i = start; i < n; i++) {
  71             ck(b, (long)b.get(), (long)((double)ic(i)));
  72         }
  73         b.rewind();
  74     }
  75 
  76     private static void absGet(DoubleBuffer b) {
  77         int n = b.capacity();
  78         for (int i = 0; i < n; i++) {
  79             ck(b, (long)b.get(), (long)((double)ic(i)));
  80         }
  81         b.rewind();
  82     }
  83 
  84     private static void bulkGet(DoubleBuffer b) {
  85         int n = b.capacity();
  86         double[] a = new double[n + 7];
  87         b.get(a, 7, n);
  88         for (int i = 0; i < n; i++) {
  89             ck(b, (long)a[i + 7], (long)((double)ic(i)));
  90         }
  91     }
  92 
  93     private static void relPut(DoubleBuffer b) {
  94         int n = b.capacity();
  95         b.clear();
  96         for (int i = 0; i < n; i++) {
  97             b.put((double)ic(i));
  98         }
  99         b.flip();
 100     }
 101 
 102     private static void absPut(DoubleBuffer b) {
 103         int n = b.capacity();
 104         b.clear();
 105         for (int i = 0; i < n; i++) {
 106             b.put(i, (double)ic(i));
 107         }
 108         b.limit(n);
 109         b.position(0);
 110     }
 111 
 112     private static void bulkPutArray(DoubleBuffer b) {
 113         int n = b.capacity();
 114         b.clear();
 115         double[] a = new double[n + 7];
 116         for (int i = 0; i < n; i++) {
 117             a[i + 7] = (double)ic(i);
 118         }
 119         b.put(a, 7, n);
 120         b.flip();
 121     }
 122 
 123     private static void bulkPutBuffer(DoubleBuffer b) {
 124         int n = b.capacity();
 125         b.clear();
 126         DoubleBuffer c = DoubleBuffer.allocate(n + 7);
 127         c.position(7);
 128         for (int i = 0; i < n; i++) {
 129             c.put((double)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(DoubleBuffer b) {
 148         b.position(0);
 149         b.mark();
 150 
 151         b.duplicate().reset();
 152         b.asReadOnlyBuffer().reset();
 153     }
 154 
 155 
 156 
 157     // 6221101-6234263
 158 
 159     private static void putBuffer() {
 160         final int cap = 10;
 161 
 162         DoubleBuffer direct1 = ByteBuffer.allocateDirect(cap).asDoubleBuffer();
 163         DoubleBuffer nondirect1 = ByteBuffer.allocate(cap).asDoubleBuffer();
 164         direct1.put(nondirect1);
 165 
 166         DoubleBuffer direct2 = ByteBuffer.allocateDirect(cap).asDoubleBuffer();
 167         DoubleBuffer nondirect2 = ByteBuffer.allocate(cap).asDoubleBuffer();
 168         nondirect2.put(direct2);
 169 
 170         DoubleBuffer direct3 = ByteBuffer.allocateDirect(cap).asDoubleBuffer();
 171         DoubleBuffer direct4 = ByteBuffer.allocateDirect(cap).asDoubleBuffer();
 172         direct3.put(direct4);
 173 
 174         DoubleBuffer nondirect3 = ByteBuffer.allocate(cap).asDoubleBuffer();
 175         DoubleBuffer nondirect4 = ByteBuffer.allocate(cap).asDoubleBuffer();
 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(DoubleBuffer b, DoubleBuffer 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 
 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 
 450 
 451 
 452     private static void fail(String problem,
 453                              DoubleBuffer xb, DoubleBuffer yb,
 454                              double x, double 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(double [] t, Class<?> ex, Runnable thunk) {
 475         tryCatch(DoubleBuffer.wrap(t), ex, thunk);
 476     }
 477 
 478     public static void test(int level, final DoubleBuffer 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 
 505 
 506 
 507 
 508 
 509 
 510 
 511 
 512 
 513 
 514 
 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         // 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((double)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(), (double)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         }
 582 
 583         try {
 584             b.limit(b.capacity() + 1);
 585             fail("IllegalArgumentException expected for limit beyond capacity");
 586         } catch (IllegalArgumentException e) {
 587             if (e.getMessage() == null) {
 588                 fail("Non-null IllegalArgumentException message expected for"
 589                      + " limit beyond capacity");
 590             }
 591         }
 592 
 593         try {
 594             b.limit(-1);
 595             fail("IllegalArgumentException expected for negative limit");
 596         } catch (IllegalArgumentException e) {
 597             if (e.getMessage() == null) {
 598                 fail("Non-null IllegalArgumentException message expected for"
 599                      + " negative limit");
 600             }
 601         }
 602 
 603         // Values
 604 
 605         b.clear();
 606         b.put((double)0);
 607         b.put((double)-1);
 608         b.put((double)1);
 609         b.put(Double.MAX_VALUE);
 610         b.put(Double.MIN_VALUE);
 611 
 612 
 613 
 614 
 615 
 616 
 617 
 618 
 619 
 620         b.put(-Double.MAX_VALUE);
 621         b.put(-Double.MIN_VALUE);
 622         b.put(Double.NEGATIVE_INFINITY);
 623         b.put(Double.POSITIVE_INFINITY);
 624         b.put(Double.NaN);
 625         b.put(0.5121609353879392);      // Changes value if incorrectly swapped
 626 
 627 
 628         b.flip();
 629         ck(b, b.get(), 0);
 630         ck(b, b.get(), (double)-1);
 631         ck(b, b.get(), 1);
 632         ck(b, b.get(), Double.MAX_VALUE);
 633         ck(b, b.get(), Double.MIN_VALUE);
 634 
 635 
 636 
 637 
 638 
 639 
 640 
 641 
 642 
 643 
 644 
 645 
 646 
 647 
 648         double v;
 649         ck(b, b.get(), -Double.MAX_VALUE);
 650         ck(b, b.get(), -Double.MIN_VALUE);
 651         ck(b, b.get(), Double.NEGATIVE_INFINITY);
 652         ck(b, b.get(), Double.POSITIVE_INFINITY);
 653         if (Double.doubleToRawLongBits(v = b.get())
 654             != Double.doubleToRawLongBits(Double.NaN)) {
 655             fail(b, (long)Double.NaN, (long)v);
 656         }
 657         ck(b, b.get(), 0.5121609353879392);
 658 
 659 
 660 
 661         // Comparison
 662         b.rewind();
 663         DoubleBuffer b2 = DoubleBuffer.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                 double x = b.get(i);
 671                 double y = b2.get(i);
 672                 if (x != y
 673 
 674                     || Double.compare(x, y) != 0
 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((double)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, (double)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 (double x : VALUES) {
 711             DoubleBuffer xb = DoubleBuffer.wrap(new double[] { 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 (double y : VALUES) {
 719                 DoubleBuffer yb = DoubleBuffer.wrap(new double[] { 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) != Double.compare(x, y)) {
 729 
 730 
 731 
 732 
 733                     if (x == 0.0 && y == 0.0) continue;
 734 
 735                     fail("Incorrect results for DoubleBuffer.compareTo",
 736                          xb, yb, x, y);
 737                 }
 738                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
 739                     fail("Incorrect results for DoubleBuffer.equals",
 740                          xb, yb, x, y);
 741                 }
 742             }
 743         }
 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         DoubleBuffer sb = b.slice();
 759         checkSlice(b, sb);
 760         b.position(0);
 761         DoubleBuffer 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 DoubleBuffer 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(DoubleBuffer) should not change source position
 818         final DoubleBuffer src = DoubleBuffer.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 
 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 
 932 
 933 
 934 
 935 
 936 
 937 
 938 
 939 
 940 
 941 
 942 
 943 
 944 
 945 
 946 
 947 
 948 
 949 
 950 
 951 
 952 
 953 
 954 
 955 
 956 
 957 
 958 
 959 
 960 
 961 
 962 
 963 
 964 
 965 
 966 
 967 
 968 
 969 
 970 
 971 
 972 
 973 
 974     public static void test(final double [] ba) {
 975         int offset = 47;
 976         int length = 900;
 977         final DoubleBuffer b = DoubleBuffer.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, () -> DoubleBuffer.wrap(ba, -1, ba.length));
 985         tryCatch(ba, IndexOutOfBoundsException.class, () -> DoubleBuffer.wrap(ba, ba.length + 1, ba.length));
 986         tryCatch(ba, IndexOutOfBoundsException.class, () -> DoubleBuffer.wrap(ba, 0, -1));
 987         tryCatch(ba, IndexOutOfBoundsException.class, () -> DoubleBuffer.wrap(ba, 0, ba.length + 1));
 988 
 989         // A NullPointerException will be thrown if the array is null.
 990         tryCatch(ba, NullPointerException.class, () -> DoubleBuffer.wrap((double []) null, 0, 5));
 991         tryCatch(ba, NullPointerException.class, () -> DoubleBuffer.wrap((double []) null));
 992     }
 993 
 994     private static void testAllocate() {
 995         // An IllegalArgumentException will be thrown for negative capacities.
 996         tryCatch((Buffer) null, IllegalArgumentException.class, () -> DoubleBuffer.allocate(-1));
 997         try {
 998             DoubleBuffer.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, DoubleBuffer.allocate(7 * 1024), false);
1021         test(0, DoubleBuffer.wrap(new double[7 * 1024], 0, 7 * 1024), false);
1022         test(new double[1024]);
1023 
1024 
1025 
1026 
1027 
1028 
1029 
1030 
1031 
1032 
1033 
1034         callReset(DoubleBuffer.allocate(10));
1035 
1036 
1037 
1038         putBuffer();
1039 
1040     }
1041 
1042 }