1 /* 2 * Copyright (c) 2015, 2018, 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 import org.testng.annotations.BeforeClass; 25 import org.testng.annotations.DataProvider; 26 27 import java.lang.invoke.VarHandle; 28 import java.nio.ByteBuffer; 29 import java.nio.ByteOrder; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.EnumSet; 33 import java.util.List; 34 import java.util.function.Function; 35 36 public abstract class VarHandleBaseByteArrayTest extends VarHandleBaseTest { 37 38 enum MemoryMode { 39 ALIGNED(0, false), UNALIGNED(0, true), 40 BIG_ENDIAN(1, false), LITTLE_ENDIAN(1, true), 41 READ_WRITE(2, false), READ_ONLY(2, true),; 42 43 final int bit; 44 final int value; 45 46 MemoryMode(int bit, boolean value) { 47 this.bit = bit; 48 this.value = value ? 1 << bit : 0; 49 } 50 51 boolean isSet(int bitSet) { 52 return (bitSet & (1 << bit)) == value; 53 } 54 55 static int bitSet(MemoryMode... modes) { 56 if (modes == null) return 0; 57 58 int set = 0; 59 for (MemoryMode m : modes) { 60 set = (set & ~(1 << m.bit)) | m.value; 61 } 62 return set; 63 } 64 65 static EnumSet<MemoryMode> enumSet(int bitSet) { 66 EnumSet<MemoryMode> es = EnumSet.noneOf(MemoryMode.class); 67 for (MemoryMode m : values()) { 68 if (m.isSet(bitSet)) { 69 es.add(m); 70 } 71 } 72 return es; 73 } 74 } 75 76 static class Source<T> { 77 final T s; 78 final int memoryModes; 79 80 public Source(T s, MemoryMode... modes) { 81 this.s = s; 82 memoryModes = MemoryMode.bitSet(modes); 83 } 84 85 @Override 86 public String toString() { 87 return s.getClass().getCanonicalName() + " " + MemoryMode.enumSet(memoryModes); 88 } 89 } 90 91 static abstract class ByteArrayViewSource<T> extends Source<T> { 92 public ByteArrayViewSource(T t, MemoryMode... modes) { 93 super(t, modes); 94 } 95 96 abstract void fill(byte value); 97 98 abstract void fill(byte[] values); 99 } 100 101 static class ByteArraySource extends ByteArrayViewSource<byte[]> { 102 public ByteArraySource(byte[] bytes, MemoryMode... modes) { 103 super(bytes, modes); 104 } 105 106 void fill(byte value) { 107 Arrays.fill(s, value); 108 } 109 110 void fill(byte[] values) { 111 for (int i = 0; i < s.length; i++) { 112 s[i] = values[i % values.length]; 113 } 114 } 115 } 116 117 static class ByteBufferSource extends ByteArrayViewSource<ByteBuffer> { 118 public ByteBufferSource(ByteBuffer buffer, MemoryMode... modes) { 119 super(buffer, modes); 120 } 121 122 void fill(byte value) { 123 for (int i = 0; i < s.limit(); i++) { 124 s.put(i, value); 125 } 126 } 127 128 void fill(byte[] values) { 129 for (int i = 0; i < s.limit(); i++) { 130 s.put(i, values[i % values.length]); 131 } 132 } 133 134 @Override 135 public String toString() { 136 return s + " " + MemoryMode.enumSet(memoryModes); 137 } 138 } 139 140 static class ByteBufferReadOnlySource extends ByteBufferSource { 141 final ByteBuffer rwSource; 142 143 public ByteBufferReadOnlySource(ByteBuffer roBuffer, ByteBuffer rwSource, MemoryMode... modes) { 144 super(roBuffer, modes); 145 this.rwSource = rwSource; 146 } 147 148 void fill(byte value) { 149 for (int i = 0; i < rwSource.limit(); i++) { 150 rwSource.put(i, value); 151 } 152 } 153 154 void fill(byte[] values) { 155 for (int i = 0; i < rwSource.limit(); i++) { 156 rwSource.put(i, values[i % values.length]); 157 } 158 } 159 } 160 161 static class VarHandleSource extends Source<VarHandle> { 162 final boolean supportsAtomicAccess; 163 164 VarHandleSource(VarHandle vh, boolean supportsAtomicAccess, MemoryMode... modes) { 165 super(vh, modes); 166 this.supportsAtomicAccess = supportsAtomicAccess; 167 } 168 169 boolean matches(ByteArrayViewSource<?> bav) { 170 return s.coordinateTypes().get(0).isAssignableFrom(bav.s.getClass()); 171 } 172 173 @Override 174 public String toString() { 175 return " VarHandle " + MemoryMode.enumSet(memoryModes); 176 } 177 } 178 179 static class VarHandleSourceAccessTestCase extends AccessTestCase<VarHandleSource> { 180 final ByteArrayViewSource<?> bs; 181 final VarHandleSource vhs; 182 183 VarHandleSourceAccessTestCase(String desc, ByteArrayViewSource<?> bs, VarHandleSource vhs, AccessTestAction<VarHandleSource> ata) { 184 this(desc, bs, vhs, ata, true); 185 } 186 187 VarHandleSourceAccessTestCase(String desc, ByteArrayViewSource<?> bs, VarHandleSource vhs, AccessTestAction<VarHandleSource> ata, boolean loop) { 188 super(vhs + " -> " + bs + " " + desc, ata, loop); 189 this.bs = bs; 190 this.vhs = vhs; 191 } 192 193 @Override 194 VarHandleSource get() { 195 return vhs; 196 } 197 } 198 199 200 static double rotateLeft(double i, int distance) { 201 return Double.longBitsToDouble( 202 Long.rotateLeft(Double.doubleToRawLongBits(i), distance)); 203 } 204 205 static double rotateRight(double i, int distance) { 206 return Double.longBitsToDouble( 207 Long.rotateRight(Double.doubleToRawLongBits(i), distance)); 208 } 209 210 static float rotateLeft(float i, int distance) { 211 return Float.intBitsToFloat( 212 Integer.rotateLeft(Float.floatToRawIntBits(i), distance)); 213 } 214 215 static float rotateRight(float i, int distance) { 216 return Float.intBitsToFloat( 217 Integer.rotateRight(Float.floatToRawIntBits(i), distance)); 218 } 219 220 static long rotateLeft(long i, int distance) { 221 return Long.rotateLeft(i, distance); 222 } 223 224 static long rotateRight(long i, int distance) { 225 return Long.rotateRight(i, distance); 226 } 227 228 static int rotateLeft(int i, int distance) { 229 return Integer.rotateLeft(i, distance); 230 } 231 232 static int rotateRight(int i, int distance) { 233 return Integer.rotateRight(i, distance); 234 } 235 236 static short rotateLeft(short i, int distance) { 237 int v = (i << 16) | i; 238 v = Integer.rotateLeft(v, distance); 239 return (short) v; 240 } 241 242 static short rotateRight(short i, int distance) { 243 int v = (i << 16) | i; 244 v = Integer.rotateRight(v, distance); 245 return (short) v; 246 } 247 248 static char rotateLeft(char i, int distance) { 249 int v = (i << 16) | i; 250 v = Integer.rotateLeft(v, distance); 251 return (char) v; 252 } 253 254 static char rotateRight(char i, int distance) { 255 int v = (i << 16) | i; 256 v = Integer.rotateRight(v, distance); 257 return (char) v; 258 } 259 260 static final int LENGTH_BYTES = 32; 261 262 byte[] array; 263 264 List<ByteArrayViewSource<?>> bavss; 265 266 List<VarHandleSource> vhss; 267 268 public void setupByteSources() { 269 array = new byte[LENGTH_BYTES]; 270 271 // Native endianess 272 MemoryMode ne = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN 273 ? MemoryMode.BIG_ENDIAN : MemoryMode.LITTLE_ENDIAN; 274 275 bavss = new ArrayList<>(); 276 277 // byte[] source 278 ByteArraySource a = 279 new ByteArraySource(array, 280 ne, MemoryMode.READ_WRITE); 281 bavss.add(a); 282 283 284 // Combinations of ByteBuffer sources 285 ByteBufferSource hbb = 286 new ByteBufferSource(ByteBuffer.wrap(array), 287 MemoryMode.ALIGNED, ne, MemoryMode.READ_WRITE); 288 bavss.add(hbb); 289 ByteBufferReadOnlySource hbb_ro = 290 new ByteBufferReadOnlySource(hbb.s.asReadOnlyBuffer(), hbb.s, 291 MemoryMode.ALIGNED, ne, MemoryMode.READ_ONLY); 292 bavss.add(hbb_ro); 293 294 ByteBufferSource hbb_offset_aligned = 295 new ByteBufferSource(ByteBuffer.wrap(array, array.length / 4, array.length / 2).slice(), 296 MemoryMode.ALIGNED, ne, MemoryMode.READ_WRITE); 297 bavss.add(hbb_offset_aligned); 298 ByteBufferReadOnlySource hbb_offset_aligned_ro = 299 new ByteBufferReadOnlySource(hbb_offset_aligned.s.asReadOnlyBuffer(), hbb_offset_aligned.s, 300 MemoryMode.ALIGNED, ne, MemoryMode.READ_ONLY); 301 bavss.add(hbb_offset_aligned_ro); 302 303 ByteBufferSource hbb_offset_unaligned = 304 new ByteBufferSource(ByteBuffer.wrap(array, array.length / 4 - 1, array.length / 2).slice(), 305 MemoryMode.UNALIGNED, ne, MemoryMode.READ_WRITE); 306 bavss.add(hbb_offset_unaligned); 307 ByteBufferReadOnlySource hbb_offset_unaligned_ro = 308 new ByteBufferReadOnlySource(hbb_offset_unaligned.s.asReadOnlyBuffer(), hbb_offset_unaligned.s, 309 MemoryMode.UNALIGNED, ne, MemoryMode.READ_ONLY); 310 bavss.add(hbb_offset_unaligned_ro); 311 312 313 ByteBufferSource dbb = 314 new ByteBufferSource(ByteBuffer.allocateDirect(array.length), 315 MemoryMode.ALIGNED, ne, MemoryMode.READ_WRITE); 316 bavss.add(dbb); 317 ByteBufferReadOnlySource dbb_ro = 318 new ByteBufferReadOnlySource(dbb.s.asReadOnlyBuffer(), dbb.s, 319 MemoryMode.ALIGNED, ne, MemoryMode.READ_ONLY); 320 bavss.add(dbb_ro); 321 322 ByteBufferSource dbb_offset_aligned = 323 new ByteBufferSource(dbb.s.slice().position(array.length / 4).limit(array.length / 4 + array.length / 2).slice(), 324 MemoryMode.ALIGNED, ne, MemoryMode.READ_WRITE); 325 bavss.add(dbb_offset_aligned); 326 ByteBufferReadOnlySource dbb_offset_aligned_ro = 327 new ByteBufferReadOnlySource(dbb_offset_aligned.s.asReadOnlyBuffer(), dbb_offset_aligned.s, 328 MemoryMode.ALIGNED, ne, MemoryMode.READ_ONLY); 329 bavss.add(dbb_offset_aligned_ro); 330 331 ByteBufferSource dbb_offset_unaligned = 332 new ByteBufferSource(dbb.s.slice().position(array.length / 4 - 1).limit(array.length / 4 - 1 + array.length / 2).slice(), 333 MemoryMode.UNALIGNED, ne, MemoryMode.READ_WRITE); 334 bavss.add(dbb_offset_unaligned); 335 ByteBufferReadOnlySource dbb_offset_unaligned_ro = 336 new ByteBufferReadOnlySource(dbb_offset_unaligned.s.asReadOnlyBuffer(), dbb_offset_unaligned.s, 337 MemoryMode.UNALIGNED, ne, MemoryMode.READ_ONLY); 338 bavss.add(dbb_offset_unaligned_ro); 339 } 340 341 @BeforeClass 342 public void setup() { 343 setupByteSources(); 344 vhss = setupVarHandleSources(true); 345 } 346 347 abstract List<VarHandleSource> setupVarHandleSources(boolean same); 348 349 350 @DataProvider 351 public Object[][] varHandlesProvider() throws Exception { 352 return vhss.stream().map(cvh -> new Object[]{cvh}).toArray(Object[][]::new); 353 } 354 355 @DataProvider 356 public Object[][] typesProvider() throws Exception { 357 List<java.lang.Class<?>> aepts = Arrays.asList(byte[].class, int.class); 358 List<java.lang.Class<?>> bbpts = Arrays.asList(ByteBuffer.class, int.class); 359 360 Function<VarHandle, List<Class<?>>> vhToPts = vh -> 361 vh.coordinateTypes().get(0) == byte[].class ? aepts : bbpts; 362 363 return vhss.stream().map(vh -> new Object[]{vh.s, vhToPts.apply(vh.s)}).toArray(Object[][]::new); 364 } 365 }