J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 2 | * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | * |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 BasicByte |
| 37 | extends Basic |
| 38 | { |
| 39 | |
Jesse Wilson | 1c4f3aa | 2009-12-08 12:41:01 -0800 | [diff] [blame] | 40 | private static final byte[] VALUES = { |
| 41 | Byte.MIN_VALUE, |
| 42 | (byte) -1, |
| 43 | (byte) 0, |
| 44 | (byte) 1, |
| 45 | Byte.MAX_VALUE, |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | }; |
| 59 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 60 | private static void relGet(ByteBuffer b) { |
| 61 | int n = b.capacity(); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 62 | for (int i = 0; i < n; i++) |
| 63 | ck(b, (long)b.get(), (long)((byte)ic(i))); |
| 64 | b.rewind(); |
| 65 | } |
| 66 | |
| 67 | private static void relGet(ByteBuffer b, int start) { |
| 68 | int n = b.remaining(); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 69 | for (int i = start; i < n; i++) |
| 70 | ck(b, (long)b.get(), (long)((byte)ic(i))); |
| 71 | b.rewind(); |
| 72 | } |
| 73 | |
| 74 | private static void absGet(ByteBuffer b) { |
| 75 | int n = b.capacity(); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 76 | for (int i = 0; i < n; i++) |
| 77 | ck(b, (long)b.get(), (long)((byte)ic(i))); |
| 78 | b.rewind(); |
| 79 | } |
| 80 | |
| 81 | private static void bulkGet(ByteBuffer b) { |
| 82 | int n = b.capacity(); |
| 83 | byte[] a = new byte[n + 7]; |
| 84 | b.get(a, 7, n); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 85 | for (int i = 0; i < n; i++) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 86 | ck(b, (long)a[i + 7], (long)((byte)ic(i))); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 87 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private static void relPut(ByteBuffer b) { |
| 91 | int n = b.capacity(); |
| 92 | b.clear(); |
| 93 | for (int i = 0; i < n; i++) |
| 94 | b.put((byte)ic(i)); |
| 95 | b.flip(); |
| 96 | } |
| 97 | |
| 98 | private static void absPut(ByteBuffer b) { |
| 99 | int n = b.capacity(); |
| 100 | b.clear(); |
| 101 | for (int i = 0; i < n; i++) |
| 102 | b.put(i, (byte)ic(i)); |
| 103 | b.limit(n); |
| 104 | b.position(0); |
| 105 | } |
| 106 | |
| 107 | private static void bulkPutArray(ByteBuffer b) { |
| 108 | int n = b.capacity(); |
| 109 | b.clear(); |
| 110 | byte[] a = new byte[n + 7]; |
| 111 | for (int i = 0; i < n; i++) |
| 112 | a[i + 7] = (byte)ic(i); |
| 113 | b.put(a, 7, n); |
| 114 | b.flip(); |
| 115 | } |
| 116 | |
| 117 | private static void bulkPutBuffer(ByteBuffer b) { |
| 118 | int n = b.capacity(); |
| 119 | b.clear(); |
| 120 | ByteBuffer c = ByteBuffer.allocate(n + 7); |
| 121 | c.position(7); |
| 122 | for (int i = 0; i < n; i++) |
| 123 | c.put((byte)ic(i)); |
| 124 | c.flip(); |
| 125 | c.position(7); |
| 126 | b.put(c); |
| 127 | b.flip(); |
Brian Burkhalter | 7cd090f | 2015-07-24 11:52:30 -0700 | [diff] [blame] | 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 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | //6231529 |
| 140 | private static void callReset(ByteBuffer b) { |
| 141 | b.position(0); |
| 142 | b.mark(); |
| 143 | |
| 144 | b.duplicate().reset(); |
| 145 | b.asReadOnlyBuffer().reset(); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | private static void checkSlice(ByteBuffer b, ByteBuffer 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 | private static void checkBytes(ByteBuffer b, byte[] bs) { |
| 201 | int n = bs.length; |
| 202 | int p = b.position(); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 203 | if (b.order() == ByteOrder.BIG_ENDIAN) { |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 204 | for (int i = 0; i < n; i++) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 205 | ck(b, b.get(), bs[i]); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 206 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 207 | } else { |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 208 | for (int i = n - 1; i >= 0; i--) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 209 | ck(b, b.get(), bs[i]); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 210 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 211 | } |
| 212 | b.position(p); |
| 213 | } |
| 214 | |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 215 | private static void compact(Buffer b) { |
| 216 | try { |
| 217 | Class<?> cl = b.getClass(); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 218 | java.lang.reflect.Method m = cl.getDeclaredMethod("compact"); |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 219 | m.setAccessible(true); |
| 220 | m.invoke(b); |
| 221 | } catch (Exception e) { |
| 222 | fail(e.getMessage(), b); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private static void checkInvalidMarkException(final Buffer b) { |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 227 | tryCatch(b, InvalidMarkException.class, () -> { |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 228 | b.mark(); |
| 229 | compact(b); |
| 230 | b.reset(); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 231 | }); |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 232 | } |
| 233 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 234 | private static void testViews(int level, ByteBuffer b, boolean direct) { |
| 235 | |
| 236 | ShortBuffer sb = b.asShortBuffer(); |
| 237 | BasicShort.test(level, sb, direct); |
| 238 | checkBytes(b, new byte[] { 0, (byte)ic(0) }); |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 239 | checkInvalidMarkException(sb); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 240 | |
| 241 | CharBuffer cb = b.asCharBuffer(); |
| 242 | BasicChar.test(level, cb, direct); |
| 243 | checkBytes(b, new byte[] { 0, (byte)ic(0) }); |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 244 | checkInvalidMarkException(cb); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 245 | |
| 246 | IntBuffer ib = b.asIntBuffer(); |
| 247 | BasicInt.test(level, ib, direct); |
| 248 | checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) }); |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 249 | checkInvalidMarkException(ib); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 250 | |
| 251 | LongBuffer lb = b.asLongBuffer(); |
| 252 | BasicLong.test(level, lb, direct); |
| 253 | checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) }); |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 254 | checkInvalidMarkException(lb); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 255 | |
| 256 | FloatBuffer fb = b.asFloatBuffer(); |
| 257 | BasicFloat.test(level, fb, direct); |
| 258 | checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 }); |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 259 | checkInvalidMarkException(fb); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 260 | |
| 261 | DoubleBuffer db = b.asDoubleBuffer(); |
| 262 | BasicDouble.test(level, db, direct); |
| 263 | checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 }); |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 264 | checkInvalidMarkException(db); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | private static void testHet(int level, ByteBuffer b) { |
| 268 | |
| 269 | int p = b.position(); |
| 270 | b.limit(b.capacity()); |
| 271 | show(level, b); |
| 272 | out.print(" put:"); |
| 273 | |
| 274 | b.putChar((char)1); |
| 275 | b.putChar((char)Character.MAX_VALUE); |
| 276 | out.print(" char"); |
| 277 | |
| 278 | b.putShort((short)1); |
| 279 | b.putShort((short)Short.MAX_VALUE); |
| 280 | out.print(" short"); |
| 281 | |
| 282 | b.putInt(1); |
| 283 | b.putInt(Integer.MAX_VALUE); |
| 284 | out.print(" int"); |
| 285 | |
| 286 | b.putLong((long)1); |
| 287 | b.putLong((long)Long.MAX_VALUE); |
| 288 | out.print(" long"); |
| 289 | |
| 290 | b.putFloat((float)1); |
| 291 | b.putFloat((float)Float.MIN_VALUE); |
| 292 | b.putFloat((float)Float.MAX_VALUE); |
| 293 | out.print(" float"); |
| 294 | |
| 295 | b.putDouble((double)1); |
| 296 | b.putDouble((double)Double.MIN_VALUE); |
| 297 | b.putDouble((double)Double.MAX_VALUE); |
| 298 | out.print(" double"); |
| 299 | |
| 300 | out.println(); |
| 301 | b.limit(b.position()); |
| 302 | b.position(p); |
| 303 | show(level, b); |
| 304 | out.print(" get:"); |
| 305 | |
| 306 | ck(b, b.getChar(), 1); |
| 307 | ck(b, b.getChar(), Character.MAX_VALUE); |
| 308 | out.print(" char"); |
| 309 | |
| 310 | ck(b, b.getShort(), 1); |
| 311 | ck(b, b.getShort(), Short.MAX_VALUE); |
| 312 | out.print(" short"); |
| 313 | |
| 314 | ck(b, b.getInt(), 1); |
| 315 | ck(b, b.getInt(), Integer.MAX_VALUE); |
| 316 | out.print(" int"); |
| 317 | |
| 318 | ck(b, b.getLong(), 1); |
| 319 | ck(b, b.getLong(), Long.MAX_VALUE); |
| 320 | out.print(" long"); |
| 321 | |
| 322 | ck(b, (long)b.getFloat(), 1); |
| 323 | ck(b, (long)b.getFloat(), (long)Float.MIN_VALUE); |
| 324 | ck(b, (long)b.getFloat(), (long)Float.MAX_VALUE); |
| 325 | out.print(" float"); |
| 326 | |
| 327 | ck(b, (long)b.getDouble(), 1); |
| 328 | ck(b, (long)b.getDouble(), (long)Double.MIN_VALUE); |
| 329 | ck(b, (long)b.getDouble(), (long)Double.MAX_VALUE); |
| 330 | out.print(" double"); |
| 331 | |
| 332 | out.println(); |
| 333 | |
| 334 | } |
| 335 | |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 336 | private static void testAlign(final ByteBuffer b, boolean direct) { |
| 337 | // index out-of bounds |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 338 | catchIllegalArgument(b, () -> b.alignmentOffset(-1, (short) 1)); |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 339 | |
| 340 | // unit size values |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 341 | catchIllegalArgument(b, () -> b.alignmentOffset(0, (short) 0)); |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 342 | for (int us = 1; us < 65; us++) { |
| 343 | int _us = us; |
| 344 | if ((us & (us - 1)) != 0) { |
| 345 | // unit size not a power of two |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 346 | catchIllegalArgument(b, () -> b.alignmentOffset(0, _us)); |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 347 | } else { |
| 348 | if (direct || us <= 8) { |
| 349 | b.alignmentOffset(0, us); |
| 350 | } else { |
| 351 | // unit size > 8 with non-direct buffer |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 352 | tryCatch(b, UnsupportedOperationException.class, |
| 353 | () -> b.alignmentOffset(0, _us)); |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // Probe for long misalignment at index zero for a newly created buffer |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 359 | ByteBuffer empty = |
| 360 | direct ? ByteBuffer.allocateDirect(0) : ByteBuffer.allocate(0); |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 361 | int longMisalignmentAtZero = empty.alignmentOffset(0, 8); |
| 362 | |
| 363 | if (direct) { |
| 364 | // Freshly created direct byte buffers should be aligned at index 0 |
| 365 | // for ref and primitive values (see Unsafe.allocateMemory) |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 366 | if (longMisalignmentAtZero != 0) { |
| 367 | fail("Direct byte buffer misaligned at index 0" |
| 368 | + " for ref and primitive values " |
| 369 | + longMisalignmentAtZero); |
| 370 | } |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 371 | } else { |
| 372 | // For heap byte buffers misalignment may occur on 32-bit systems |
| 373 | // where Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 == 4 and not 0 |
| 374 | // Note the GC will preserve alignment of the base address of the |
| 375 | // array |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 376 | if (jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 |
| 377 | != longMisalignmentAtZero) { |
| 378 | fail("Heap byte buffer misaligned at index 0" |
| 379 | + " for ref and primitive values " |
| 380 | + longMisalignmentAtZero); |
| 381 | } |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // Ensure test buffer is correctly aligned at index 0 |
| 385 | if (b.alignmentOffset(0, 8) != longMisalignmentAtZero) |
| 386 | fail("Test input buffer not correctly aligned at index 0", b); |
| 387 | |
| 388 | // Test misalignment values |
| 389 | for (int us : new int[]{1, 2, 4, 8}) { |
| 390 | for (int i = 0; i < us * 2; i++) { |
| 391 | int am = b.alignmentOffset(i, us); |
| 392 | int expectedAm = (longMisalignmentAtZero + i) % us; |
| 393 | |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 394 | if (am != expectedAm) { |
| 395 | String f = "b.alignmentOffset(%d, %d) == %d incorrect, expected %d"; |
| 396 | fail(String.format(f, i, us, am, expectedAm)); |
| 397 | } |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | // Created aligned slice to test against |
| 402 | int ap = 8 - longMisalignmentAtZero; |
| 403 | int al = b.limit() - b.alignmentOffset(b.limit(), 8); |
| 404 | ByteBuffer ab = b.position(ap).limit(al). |
| 405 | slice(); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 406 | if (ab.limit() == 0) { |
| 407 | fail("Test input buffer not sufficiently sized to cover" + |
| 408 | " an aligned region for all values", b); |
| 409 | } |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 410 | if (ab.alignmentOffset(0, 8) != 0) |
| 411 | fail("Aligned test input buffer not correctly aligned at index 0", ab); |
| 412 | |
| 413 | for (int us : new int[]{1, 2, 4, 8}) { |
| 414 | for (int p = 1; p < 16; p++) { |
| 415 | int l = ab.limit() - p; |
| 416 | |
| 417 | ByteBuffer as = ab.slice().position(p).limit(l). |
| 418 | alignedSlice(us); |
| 419 | |
| 420 | ck(as, 0, as.position()); |
| 421 | ck(as, as.capacity(), as.limit()); |
| 422 | if (b.isDirect() != as.isDirect()) |
| 423 | fail("Lost direction", as); |
| 424 | if (b.isReadOnly() != as.isReadOnly()) |
| 425 | fail("Lost read-only", as); |
| 426 | |
| 427 | if (as.alignmentOffset(0, us) != 0) |
| 428 | fail("Buffer not correctly aligned at index 0", as); |
| 429 | |
| 430 | if (as.alignmentOffset(as.limit(), us) != 0) |
| 431 | fail("Buffer not correctly aligned at limit", as); |
| 432 | |
| 433 | int p_mod = ab.alignmentOffset(p, us); |
| 434 | int l_mod = ab.alignmentOffset(l, us); |
| 435 | // Round up position |
| 436 | p = (p_mod > 0) ? p + (us - p_mod) : p; |
| 437 | // Round down limit |
| 438 | l = l - l_mod; |
| 439 | |
| 440 | int ec = l - p; |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 441 | if (as.limit() != ec) { |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 442 | fail("Buffer capacity incorrect, expected: " + ec, as); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 443 | } |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 447 | |
| 448 | |
Jesse Wilson | 1c4f3aa | 2009-12-08 12:41:01 -0800 | [diff] [blame] | 449 | private static void fail(String problem, |
| 450 | ByteBuffer xb, ByteBuffer yb, |
| 451 | byte x, byte y) { |
| 452 | fail(problem + String.format(": x=%s y=%s", x, y), xb, yb); |
| 453 | } |
| 454 | |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 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(byte[] t, Runnable thunk) { |
| 468 | tryCatch(t, IndexOutOfBoundsException.class, thunk); |
| 469 | } |
| 470 | |
Alan Bateman | 3607544 | 2012-09-21 15:39:10 +0100 | [diff] [blame] | 471 | private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 472 | boolean caught = false; |
| 473 | try { |
| 474 | thunk.run(); |
| 475 | } catch (Throwable x) { |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 476 | if (ex.isAssignableFrom(x.getClass())) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 477 | caught = true; |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 478 | } else { |
| 479 | fail(x.getMessage() + " not expected"); |
| 480 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 481 | } |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 482 | if (!caught) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 483 | fail(ex.getName() + " not thrown", b); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 484 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 487 | private static void tryCatch(byte[] t, Class<?> ex, Runnable thunk) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 488 | tryCatch(ByteBuffer.wrap(t), ex, thunk); |
| 489 | } |
| 490 | |
| 491 | public static void test(int level, final ByteBuffer 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 | |
Alan Bateman | 9e2590b | 2009-04-15 14:53:34 +0100 | [diff] [blame] | 538 | |
| 539 | |
| 540 | |
Jonathan Lu | f349c3d | 2012-08-13 19:51:48 +0800 | [diff] [blame] | 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 552 | // Compact |
| 553 | |
| 554 | relPut(b); |
| 555 | b.position(13); |
| 556 | b.compact(); |
| 557 | b.flip(); |
| 558 | relGet(b, 13); |
| 559 | |
| 560 | // Exceptions |
| 561 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 562 | relPut(b); |
| 563 | b.limit(b.capacity() / 2); |
| 564 | b.position(b.limit()); |
| 565 | |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 566 | tryCatch(b, BufferUnderflowException.class, () -> b.get()); |
| 567 | tryCatch(b, BufferOverflowException.class, () -> b.put((byte)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(), (byte)42)); |
| 572 | tryCatch(b, InvalidMarkException.class, |
| 573 | () -> b.position(0).mark().compact().reset()); |
Alan Bateman | efd62d5 | 2008-11-25 19:26:54 +0000 | [diff] [blame] | 574 | |
Brian Burkhalter | 7cd090f | 2015-07-24 11:52:30 -0700 | [diff] [blame] | 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 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 615 | // Values |
| 616 | |
| 617 | b.clear(); |
| 618 | b.put((byte)0); |
| 619 | b.put((byte)-1); |
| 620 | b.put((byte)1); |
| 621 | b.put(Byte.MAX_VALUE); |
| 622 | b.put(Byte.MIN_VALUE); |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 640 | b.flip(); |
| 641 | ck(b, b.get(), 0); |
| 642 | ck(b, b.get(), (byte)-1); |
| 643 | ck(b, b.get(), 1); |
| 644 | ck(b, b.get(), Byte.MAX_VALUE); |
| 645 | ck(b, b.get(), Byte.MIN_VALUE); |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
Brian Burkhalter | 7cd090f | 2015-07-24 11:52:30 -0700 | [diff] [blame] | 668 | |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 669 | |
| 670 | |
| 671 | |
| 672 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 673 | // Comparison |
| 674 | b.rewind(); |
| 675 | ByteBuffer b2 = ByteBuffer.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 | byte x = b.get(i); |
| 683 | byte y = b2.get(i); |
| 684 | if (x != y |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 691 | ) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 692 | out.println("[" + i + "] " + x + " != " + y); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 693 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 694 | } |
| 695 | fail("Identical buffers not equal", b, b2); |
| 696 | } |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 697 | if (b.compareTo(b2) != 0) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 698 | fail("Comparison to identical buffer != 0", b, b2); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 699 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 700 | b.limit(b.limit() + 1); |
| 701 | b.position(b.limit() - 1); |
| 702 | b.put((byte)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, (byte)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 | |
Jesse Wilson | 1c4f3aa | 2009-12-08 12:41:01 -0800 | [diff] [blame] | 717 | // Check equals and compareTo with interesting values |
| 718 | for (byte x : VALUES) { |
| 719 | ByteBuffer xb = ByteBuffer.wrap(new byte[] { x }); |
| 720 | if (xb.compareTo(xb) != 0) { |
| 721 | fail("compareTo not reflexive", xb, xb, x, x); |
| 722 | } |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 723 | if (!xb.equals(xb)) { |
Jesse Wilson | 1c4f3aa | 2009-12-08 12:41:01 -0800 | [diff] [blame] | 724 | fail("equals not reflexive", xb, xb, x, x); |
| 725 | } |
| 726 | for (byte y : VALUES) { |
| 727 | ByteBuffer yb = ByteBuffer.wrap(new byte[] { 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) != Byte.compare(x, y)) { |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | fail("Incorrect results for ByteBuffer.compareTo", |
| 744 | xb, yb, x, y); |
| 745 | } |
| 746 | if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) { |
| 747 | fail("Incorrect results for ByteBuffer.equals", |
| 748 | xb, yb, x, y); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | ByteBuffer sb = b.slice(); |
| 767 | checkSlice(b, sb); |
| 768 | b.position(0); |
| 769 | ByteBuffer sb2 = sb.slice(); |
| 770 | checkSlice(sb, sb2); |
| 771 | |
| 772 | if (!sb.equals(sb2)) |
| 773 | fail("Sliced slices do not match", sb, sb2); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 774 | if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 775 | fail("Array offsets do not match: " |
| 776 | + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 777 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 778 | |
| 779 | |
| 780 | |
| 781 | // Views |
| 782 | |
| 783 | b.clear(); |
| 784 | b.order(ByteOrder.BIG_ENDIAN); |
| 785 | testViews(level + 1, b, direct); |
| 786 | |
| 787 | for (int i = 1; i <= 9; i++) { |
| 788 | b.position(i); |
| 789 | show(level + 1, b); |
| 790 | testViews(level + 2, b, direct); |
| 791 | } |
| 792 | |
| 793 | b.position(0); |
| 794 | b.order(ByteOrder.LITTLE_ENDIAN); |
| 795 | testViews(level + 1, b, direct); |
| 796 | |
| 797 | // Heterogeneous accessors |
| 798 | |
| 799 | b.order(ByteOrder.BIG_ENDIAN); |
| 800 | for (int i = 0; i <= 9; i++) { |
| 801 | b.position(i); |
| 802 | testHet(level + 1, b); |
| 803 | } |
| 804 | b.order(ByteOrder.LITTLE_ENDIAN); |
| 805 | b.position(3); |
| 806 | testHet(level + 1, b); |
| 807 | |
| 808 | |
| 809 | |
| 810 | // Read-only views |
| 811 | |
| 812 | b.rewind(); |
| 813 | final ByteBuffer 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 | |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 818 | catchReadOnlyBuffer(b, () -> relPut(rb)); |
| 819 | catchReadOnlyBuffer(b, () -> absPut(rb)); |
| 820 | catchReadOnlyBuffer(b, () -> bulkPutArray(rb)); |
| 821 | catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb)); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 822 | |
Alan Bateman | 3607544 | 2012-09-21 15:39:10 +0100 | [diff] [blame] | 823 | // put(ByteBuffer) should not change source position |
| 824 | final ByteBuffer src = ByteBuffer.allocate(1); |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 825 | catchReadOnlyBuffer(b, () -> rb.put(src)); |
Alan Bateman | 3607544 | 2012-09-21 15:39:10 +0100 | [diff] [blame] | 826 | ck(src, src.position(), 0); |
| 827 | |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 828 | catchReadOnlyBuffer(b, () -> rb.compact()); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 829 | |
| 830 | |
| 831 | |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 832 | catchReadOnlyBuffer(b, () -> rb.putChar((char)1)); |
| 833 | catchReadOnlyBuffer(b, () -> rb.putChar(0, (char)1)); |
| 834 | catchReadOnlyBuffer(b, () -> rb.putShort((short)1)); |
| 835 | catchReadOnlyBuffer(b, () -> rb.putShort(0, (short)1)); |
| 836 | catchReadOnlyBuffer(b, () -> rb.putInt(1)); |
| 837 | catchReadOnlyBuffer(b, () -> rb.putInt(0, 1)); |
| 838 | catchReadOnlyBuffer(b, () -> rb.putLong((long)1)); |
| 839 | catchReadOnlyBuffer(b, () -> rb.putLong(0, (long)1)); |
| 840 | catchReadOnlyBuffer(b, () -> rb.putFloat((float)1)); |
| 841 | catchReadOnlyBuffer(b, () -> rb.putFloat(0, (float)1)); |
| 842 | catchReadOnlyBuffer(b, () -> rb.putDouble((double)1)); |
| 843 | catchReadOnlyBuffer(b, () -> rb.putDouble(0, (double)1)); |
Alan Bateman | 3607544 | 2012-09-21 15:39:10 +0100 | [diff] [blame] | 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 855 | if (rb.getClass().getName().startsWith("java.nio.Heap")) { |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 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 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | // Bulk puts from read-only buffers |
| 864 | |
| 865 | b.clear(); |
| 866 | rb.rewind(); |
| 867 | b.put(rb); |
| 868 | |
| 869 | |
| 870 | // For byte buffers, test both the direct and non-direct cases |
| 871 | ByteBuffer ob |
| 872 | = (b.isDirect() |
| 873 | ? ByteBuffer.allocate(rb.capacity()) |
| 874 | : ByteBuffer.allocateDirect(rb.capacity())); |
| 875 | rb.rewind(); |
| 876 | ob.put(rb); |
| 877 | |
| 878 | |
| 879 | relPut(b); // Required by testViews |
| 880 | |
Paul Sandoz | 4485394 | 2016-03-24 11:21:18 +0100 | [diff] [blame] | 881 | |
| 882 | // Test alignment |
| 883 | |
| 884 | testAlign(b, direct); |
| 885 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 931 | public static void test(final byte [] ba) { |
| 932 | int offset = 47; |
| 933 | int length = 900; |
| 934 | final ByteBuffer b = ByteBuffer.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>. |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 941 | catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, -1, ba.length)); |
| 942 | catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, ba.length + 1, ba.length)); |
| 943 | catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, 0, -1)); |
| 944 | catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, 0, ba.length + 1)); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 945 | |
| 946 | // A NullPointerException will be thrown if the array is null. |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 947 | tryCatch(ba, NullPointerException.class, |
| 948 | () -> ByteBuffer.wrap((byte []) null, 0, 5)); |
| 949 | tryCatch(ba, NullPointerException.class, |
| 950 | () -> ByteBuffer.wrap((byte []) null)); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | private static void testAllocate() { |
| 954 | // An IllegalArgumentException will be thrown for negative capacities. |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 955 | catchIllegalArgument((Buffer) null, () -> ByteBuffer.allocate(-1)); |
Brian Burkhalter | 7cd090f | 2015-07-24 11:52:30 -0700 | [diff] [blame] | 956 | try { |
| 957 | ByteBuffer.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 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 964 | |
Pavel Rappo | 976b541 | 2016-05-16 09:54:01 +0100 | [diff] [blame] | 965 | catchIllegalArgument((Buffer) null, () -> ByteBuffer.allocateDirect(-1)); |
Brian Burkhalter | 7cd090f | 2015-07-24 11:52:30 -0700 | [diff] [blame] | 966 | try { |
| 967 | ByteBuffer.allocateDirect(-1); |
| 968 | } catch (IllegalArgumentException e) { |
| 969 | if (e.getMessage() == null) { |
| 970 | fail("Non-null IllegalArgumentException message expected for" |
| 971 | + " attempt to allocate negative capacity direct buffer"); |
| 972 | } |
| 973 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 974 | |
| 975 | } |
| 976 | |
| 977 | public static void test() { |
| 978 | testAllocate(); |
| 979 | test(0, ByteBuffer.allocate(7 * 1024), false); |
| 980 | test(0, ByteBuffer.wrap(new byte[7 * 1024], 0, 7 * 1024), false); |
| 981 | test(new byte[1024]); |
| 982 | |
| 983 | ByteBuffer b = ByteBuffer.allocateDirect(7 * 1024); |
| 984 | for (b.position(0); b.position() < b.limit(); ) |
| 985 | ck(b, b.get(), 0); |
| 986 | test(0, b, true); |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | callReset(ByteBuffer.allocate(10)); |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | } |
| 999 | |
| 1000 | } |