test/jdk/java/nio/Buffer/BasicShort.java
changeset 53855 7c362992527a
parent 47216 71c04702a3d5
child 53959 1542e63eb537
equal deleted inserted replaced
53854:792c8a5fbb29 53855:7c362992527a
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    85         for (int i = 0; i < n; i++) {
    85         for (int i = 0; i < n; i++) {
    86             ck(b, (long)a[i + 7], (long)((short)ic(i)));
    86             ck(b, (long)a[i + 7], (long)((short)ic(i)));
    87         }
    87         }
    88     }
    88     }
    89 
    89 
       
    90     private static void absBulkGet(ShortBuffer b) {
       
    91         int n = b.capacity();
       
    92         int len = n - 7*2;
       
    93         short[] a = new short[n + 7];
       
    94         b.position(42);
       
    95         b.get(7, a, 7, len);
       
    96         ck(b, b.position() == 42);
       
    97         for (int i = 0; i < len; i++) {
       
    98             ck(b, (long)a[i + 7], (long)((short)ic(i)));
       
    99         }
       
   100     }
       
   101 
    90     private static void relPut(ShortBuffer b) {
   102     private static void relPut(ShortBuffer b) {
    91         int n = b.capacity();
   103         int n = b.capacity();
    92         b.clear();
   104         b.clear();
    93         for (int i = 0; i < n; i++)
   105         for (int i = 0; i < n; i++)
    94             b.put((short)ic(i));
   106             b.put((short)ic(i));
   134                      + " put into same buffer");
   146                      + " put into same buffer");
   135             }
   147             }
   136         }
   148         }
   137     }
   149     }
   138 
   150 
       
   151     private static void absBulkPutArray(ShortBuffer b) {
       
   152         int n = b.capacity();
       
   153         b.clear();
       
   154         int lim = n - 7;
       
   155         int len = lim - 7;
       
   156         b.limit(lim);
       
   157         short[] a = new short[len + 7];
       
   158         for (int i = 0; i < len; i++)
       
   159             a[i + 7] = (short)ic(i);
       
   160         b.position(42);
       
   161         b.put(7, a, 7, len);
       
   162         ck(b, b.position() == 42);
       
   163     }
       
   164 
   139     //6231529
   165     //6231529
   140     private static void callReset(ShortBuffer b) {
   166     private static void callReset(ShortBuffer b) {
   141         b.position(0);
   167         b.position(0);
   142         b.mark();
   168         b.mark();
   143 
   169 
   450                              ShortBuffer xb, ShortBuffer yb,
   476                              ShortBuffer xb, ShortBuffer yb,
   451                              short x, short y) {
   477                              short x, short y) {
   452         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
   478         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
   453     }
   479     }
   454 
   480 
       
   481     private static void catchNullArgument(Buffer b, Runnable thunk) {
       
   482         tryCatch(b, NullPointerException.class, thunk);
       
   483     }
       
   484 
   455     private static void catchIllegalArgument(Buffer b, Runnable thunk) {
   485     private static void catchIllegalArgument(Buffer b, Runnable thunk) {
   456         tryCatch(b, IllegalArgumentException.class, thunk);
   486         tryCatch(b, IllegalArgumentException.class, thunk);
   457     }
   487     }
   458 
   488 
   459     private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
   489     private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
   474             thunk.run();
   504             thunk.run();
   475         } catch (Throwable x) {
   505         } catch (Throwable x) {
   476             if (ex.isAssignableFrom(x.getClass())) {
   506             if (ex.isAssignableFrom(x.getClass())) {
   477                 caught = true;
   507                 caught = true;
   478             } else {
   508             } else {
   479                 fail(x.getMessage() + " not expected");
   509                 String s = x.getMessage();
       
   510                 if (s == null)
       
   511                     s = x.getClass().getName();
       
   512                 fail(s + " not expected");
   480             }
   513             }
   481         }
   514         }
   482         if (!caught) {
   515         if (!caught) {
   483             fail(ex.getName() + " not thrown", b);
   516             fail(ex.getName() + " not thrown", b);
   484         }
   517         }
   510         bulkPutArray(b);
   543         bulkPutArray(b);
   511         relGet(b);
   544         relGet(b);
   512 
   545 
   513         bulkPutBuffer(b);
   546         bulkPutBuffer(b);
   514         relGet(b);
   547         relGet(b);
       
   548 
       
   549         absBulkPutArray(b);
       
   550         absBulkGet(b);
   515 
   551 
   516 
   552 
   517 
   553 
   518 
   554 
   519 
   555 
   609             if (e.getMessage() == null) {
   645             if (e.getMessage() == null) {
   610                 fail("Non-null IllegalArgumentException message expected for"
   646                 fail("Non-null IllegalArgumentException message expected for"
   611                      + " negative limit");
   647                      + " negative limit");
   612             }
   648             }
   613         }
   649         }
       
   650 
       
   651         // Exceptions in absolute bulk operations
       
   652 
       
   653         catchNullArgument(b, () -> b.get(7, null, 0, 42));
       
   654         catchNullArgument(b, () -> b.put(7, (short[])null, 0, 42));
       
   655 
       
   656         short[] tmpa = new short[42];
       
   657         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42));
       
   658         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1));
       
   659         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1));
       
   660         catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1));
       
   661         catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1));
       
   662         catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42));
       
   663 
       
   664         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42));
       
   665         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1));
       
   666         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1));
       
   667         catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1));
       
   668         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
       
   669         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
   614 
   670 
   615         // Values
   671         // Values
   616 
   672 
   617         b.clear();
   673         b.clear();
   618         b.put((short)0);
   674         b.put((short)0);
   817 
   873 
   818         catchReadOnlyBuffer(b, () -> relPut(rb));
   874         catchReadOnlyBuffer(b, () -> relPut(rb));
   819         catchReadOnlyBuffer(b, () -> absPut(rb));
   875         catchReadOnlyBuffer(b, () -> absPut(rb));
   820         catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
   876         catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
   821         catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
   877         catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
       
   878         catchReadOnlyBuffer(b, () -> absBulkPutArray(rb));
   822 
   879 
   823         // put(ShortBuffer) should not change source position
   880         // put(ShortBuffer) should not change source position
   824         final ShortBuffer src = ShortBuffer.allocate(1);
   881         final ShortBuffer src = ShortBuffer.allocate(1);
   825         catchReadOnlyBuffer(b, () -> rb.put(src));
   882         catchReadOnlyBuffer(b, () -> rb.put(src));
   826         ck(src, src.position(), 0);
   883         ck(src, src.position(), 0);
   882 
   939 
   883 
   940 
   884 
   941 
   885 
   942 
   886     }
   943     }
       
   944 
       
   945 
       
   946 
       
   947 
       
   948 
       
   949 
       
   950 
   887 
   951 
   888 
   952 
   889 
   953 
   890 
   954 
   891 
   955