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 VarHandleSource(VarHandle vh, MemoryMode... modes) { 163 super(vh, modes); 164 } 165 166 boolean matches(ByteArrayViewSource<?> bav) { 167 return s.coordinateTypes().get(0).isAssignableFrom(bav.s.getClass()); 168 } 169 170 @Override 171 public String toString() { 172 return " VarHandle " + MemoryMode.enumSet(memoryModes); 173 } 174 } 175 176 static class VarHandleSourceAccessTestCase extends AccessTestCase<VarHandleSource> { 177 final ByteArrayViewSource<?> bs; 178 final VarHandleSource vhs; 179 180 VarHandleSourceAccessTestCase(String desc, ByteArrayViewSource<?> bs, VarHandleSource vhs, AccessTestAction<VarHandleSource> ata) { 181 this(desc, bs, vhs, ata, true); 182 } 183 184 VarHandleSourceAccessTestCase(String desc, ByteArrayViewSource<?> bs, VarHandleSource vhs, AccessTestAction<VarHandleSource> ata, boolean loop) { 185 super(vhs + " -> " + bs + " " + desc, ata, loop); 186 this.bs = bs; 187 this.vhs = vhs; 188 } 189 190 @Override 191 VarHandleSource get() { 192 return vhs; 193 } 194 195 public String toString() { 196 return super.toString() + ", bs: " + bs + ", vhs: " + vhs; 197 } 198 } 199 200 201 static double rotateLeft(double i, int distance) { 202 return Double.longBitsToDouble( 203 Long.rotateLeft(Double.doubleToRawLongBits(i), distance)); 204 } 205 206 static double rotateRight(double i, int distance) { 207 return Double.longBitsToDouble( 208 Long.rotateRight(Double.doubleToRawLongBits(i), distance)); 209 } 210 211 static float rotateLeft(float i, int distance) { 212 return Float.intBitsToFloat( 213 Integer.rotateLeft(Float.floatToRawIntBits(i), distance)); 214 } 215 216 static float rotateRight(float i, int distance) { 217 return Float.intBitsToFloat( 218 Integer.rotateRight(Float.floatToRawIntBits(i), distance)); 219 } 220 221 static long rotateLeft(long i, int distance) { 222 return Long.rotateLeft(i, distance); 223 } 224 225 static long rotateRight(long i, int distance) { 226 return Long.rotateRight(i, distance); 227 } 228 229 static int rotateLeft(int i, int distance) { 230 return Integer.rotateLeft(i, distance); 231 } 232 233 static int rotateRight(int i, int distance) { 234 return Integer.rotateRight(i, distance); 235 } 236 237 static short rotateLeft(short i, int distance) { 238 int v = (i << 16) | i; 239 v = Integer.rotateLeft(v, distance); 240 return (short) v; 241 } 242 243 static short rotateRight(short i, int distance) { 244 int v = (i << 16) | i; 245 v = Integer.rotateRight(v, distance); 246 return (short) v; 247 } 248 249 static char rotateLeft(char i, int distance) { 250 int v = (i << 16) | i; 251 v = Integer.rotateLeft(v, distance); 252 return (char) v; 253 } 254 255 static char rotateRight(char i, int distance) { 256 int v = (i << 16) | i; 257 v = Integer.rotateRight(v, distance); 258 return (char) v; 259 } 260 261 static final int LENGTH_BYTES = 32; 262 263 byte[] array; 264 265 List<ByteArrayViewSource<?>> bavss; 266 267 List<VarHandleSource> vhss; 268 269 public void setupByteSources() { 270 array = new byte[LENGTH_BYTES]; 271 272 // Native endianess 273 MemoryMode ne = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN 274 ? MemoryMode.BIG_ENDIAN : MemoryMode.LITTLE_ENDIAN; 275 276 bavss = new ArrayList<>(); 277 278 // byte[] source 279 ByteArraySource a = 280 new ByteArraySource(array, 281 ne, MemoryMode.READ_WRITE); 282 bavss.add(a); 283 284 285 // Combinations of ByteBuffer sources 286 ByteBufferSource hbb = 287 new ByteBufferSource(ByteBuffer.wrap(array), 288 MemoryMode.ALIGNED, ne, MemoryMode.READ_WRITE); 289 bavss.add(hbb); 290 ByteBufferReadOnlySource hbb_ro = 291 new ByteBufferReadOnlySource(hbb.s.asReadOnlyBuffer(), hbb.s, 292 MemoryMode.ALIGNED, ne, MemoryMode.READ_ONLY); 293 bavss.add(hbb_ro); 294 295 ByteBufferSource hbb_offset_aligned = 296 new ByteBufferSource(ByteBuffer.wrap(array, array.length / 4, array.length / 2).slice(), 297 MemoryMode.ALIGNED, ne, MemoryMode.READ_WRITE); 298 bavss.add(hbb_offset_aligned); 299 ByteBufferReadOnlySource hbb_offset_aligned_ro = 300 new ByteBufferReadOnlySource(hbb_offset_aligned.s.asReadOnlyBuffer(), hbb_offset_aligned.s, 301 MemoryMode.ALIGNED, ne, MemoryMode.READ_ONLY); 302 bavss.add(hbb_offset_aligned_ro); 303 304 ByteBufferSource hbb_offset_unaligned = 305 new ByteBufferSource(ByteBuffer.wrap(array, array.length / 4 - 1, array.length / 2).slice(), 306 MemoryMode.UNALIGNED, ne, MemoryMode.READ_WRITE); 307 bavss.add(hbb_offset_unaligned); 308 ByteBufferReadOnlySource hbb_offset_unaligned_ro = 309 new ByteBufferReadOnlySource(hbb_offset_unaligned.s.asReadOnlyBuffer(), hbb_offset_unaligned.s, 310 MemoryMode.UNALIGNED, ne, MemoryMode.READ_ONLY); 311 bavss.add(hbb_offset_unaligned_ro); 312 313 314 ByteBufferSource dbb = 315 new ByteBufferSource(ByteBuffer.allocateDirect(array.length), 316 MemoryMode.ALIGNED, ne, MemoryMode.READ_WRITE); 317 bavss.add(dbb); 318 ByteBufferReadOnlySource dbb_ro = 319 new ByteBufferReadOnlySource(dbb.s.asReadOnlyBuffer(), dbb.s, 320 MemoryMode.ALIGNED, ne, MemoryMode.READ_ONLY); 321 bavss.add(dbb_ro); 322 323 ByteBufferSource dbb_offset_aligned = 324 new ByteBufferSource(dbb.s.slice().position(array.length / 4).limit(array.length / 4 + array.length / 2).slice(), 325 MemoryMode.ALIGNED, ne, MemoryMode.READ_WRITE); 326 bavss.add(dbb_offset_aligned); 327 ByteBufferReadOnlySource dbb_offset_aligned_ro = 328 new ByteBufferReadOnlySource(dbb_offset_aligned.s.asReadOnlyBuffer(), dbb_offset_aligned.s, 329 MemoryMode.ALIGNED, ne, MemoryMode.READ_ONLY); 330 bavss.add(dbb_offset_aligned_ro); 331 332 ByteBufferSource dbb_offset_unaligned = 333 new ByteBufferSource(dbb.s.slice().position(array.length / 4 - 1).limit(array.length / 4 - 1 + array.length / 2).slice(), 334 MemoryMode.UNALIGNED, ne, MemoryMode.READ_WRITE); 335 bavss.add(dbb_offset_unaligned); 336 ByteBufferReadOnlySource dbb_offset_unaligned_ro = 337 new ByteBufferReadOnlySource(dbb_offset_unaligned.s.asReadOnlyBuffer(), dbb_offset_unaligned.s, 338 MemoryMode.UNALIGNED, ne, MemoryMode.READ_ONLY); 339 bavss.add(dbb_offset_unaligned_ro); 340 } 341 342 @BeforeClass 343 public void setup() { 344 setupByteSources(); 345 vhss = setupVarHandleSources(true); 346 } 347 348 abstract List<VarHandleSource> setupVarHandleSources(boolean same); 349 350 351 @DataProvider 352 public Object[][] varHandlesProvider() throws Exception { 353 return vhss.stream().map(cvh -> new Object[]{cvh}).toArray(Object[][]::new); 354 } 355 356 @DataProvider 357 public Object[][] typesProvider() throws Exception { 358 List<java.lang.Class<?>> aepts = Arrays.asList(byte[].class, int.class); 359 List<java.lang.Class<?>> bbpts = Arrays.asList(ByteBuffer.class, int.class); 360 361 Function<VarHandle, List<Class<?>>> vhToPts = vh -> 362 vh.coordinateTypes().get(0) == byte[].class ? aepts : bbpts; 363 364 return vhss.stream().map(vh -> new Object[]{vh.s, vhToPts.apply(vh.s)}).toArray(Object[][]::new); 365 } 366 }