1 /* 2 * Copyright (c) 2015, 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 // Android-added: package for test. 25 package test.java.lang.invoke.VarHandles; 26 27 import org.testng.annotations.BeforeClass; 28 import org.testng.annotations.DataProvider; 29 30 import java.lang.invoke.VarHandle; 31 import java.nio.ByteBuffer; 32 import java.nio.ByteOrder; 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 import java.util.EnumSet; 36 import java.util.List; 37 import java.util.function.Function; 38 39 public abstract class VarHandleBaseByteArrayTest extends VarHandleBaseTest { 40 41 enum MemoryMode { 42 ALIGNED(0, false), UNALIGNED(0, true), 43 BIG_ENDIAN(1, false), LITTLE_ENDIAN(1, true), 44 READ_WRITE(2, false), READ_ONLY(2, true),; 45 46 final int bit; 47 final int value; 48 MemoryMode(int bit, boolean value)49 MemoryMode(int bit, boolean value) { 50 this.bit = bit; 51 this.value = value ? 1 << bit : 0; 52 } 53 isSet(int bitSet)54 boolean isSet(int bitSet) { 55 return (bitSet & (1 << bit)) == value; 56 } 57 bitSet(MemoryMode... modes)58 static int bitSet(MemoryMode... modes) { 59 if (modes == null) return 0; 60 61 int set = 0; 62 for (MemoryMode m : modes) { 63 set = (set & ~(1 << m.bit)) | m.value; 64 } 65 return set; 66 } 67 enumSet(int bitSet)68 static EnumSet<MemoryMode> enumSet(int bitSet) { 69 EnumSet<MemoryMode> es = EnumSet.noneOf(MemoryMode.class); 70 for (MemoryMode m : values()) { 71 if (m.isSet(bitSet)) { 72 es.add(m); 73 } 74 } 75 return es; 76 } 77 } 78 79 static class Source<T> { 80 final T s; 81 final int memoryModes; 82 Source(T s, MemoryMode... modes)83 public Source(T s, MemoryMode... modes) { 84 this.s = s; 85 memoryModes = MemoryMode.bitSet(modes); 86 } 87 88 @Override toString()89 public String toString() { 90 return s.getClass().getCanonicalName() + " " + MemoryMode.enumSet(memoryModes); 91 } 92 } 93 94 static abstract class ByteArrayViewSource<T> extends Source<T> { ByteArrayViewSource(T t, MemoryMode... modes)95 public ByteArrayViewSource(T t, MemoryMode... modes) { 96 super(t, modes); 97 } 98 fill(byte value)99 abstract void fill(byte value); 100 fill(byte[] values)101 abstract void fill(byte[] values); 102 } 103 104 static class ByteArraySource extends ByteArrayViewSource<byte[]> { ByteArraySource(byte[] bytes, MemoryMode... modes)105 public ByteArraySource(byte[] bytes, MemoryMode... modes) { 106 super(bytes, modes); 107 } 108 fill(byte value)109 void fill(byte value) { 110 Arrays.fill(s, value); 111 } 112 fill(byte[] values)113 void fill(byte[] values) { 114 for (int i = 0; i < s.length; i++) { 115 s[i] = values[i % values.length]; 116 } 117 } 118 } 119 120 static class ByteBufferSource extends ByteArrayViewSource<ByteBuffer> { ByteBufferSource(ByteBuffer buffer, MemoryMode... modes)121 public ByteBufferSource(ByteBuffer buffer, MemoryMode... modes) { 122 super(buffer, modes); 123 } 124 fill(byte value)125 void fill(byte value) { 126 for (int i = 0; i < s.limit(); i++) { 127 s.put(i, value); 128 } 129 } 130 fill(byte[] values)131 void fill(byte[] values) { 132 for (int i = 0; i < s.limit(); i++) { 133 s.put(i, values[i % values.length]); 134 } 135 } 136 137 @Override toString()138 public String toString() { 139 return s + " " + MemoryMode.enumSet(memoryModes); 140 } 141 } 142 143 static class ByteBufferReadOnlySource extends ByteBufferSource { 144 final ByteBuffer rwSource; 145 ByteBufferReadOnlySource(ByteBuffer roBuffer, ByteBuffer rwSource, MemoryMode... modes)146 public ByteBufferReadOnlySource(ByteBuffer roBuffer, ByteBuffer rwSource, MemoryMode... modes) { 147 super(roBuffer, modes); 148 this.rwSource = rwSource; 149 } 150 fill(byte value)151 void fill(byte value) { 152 for (int i = 0; i < rwSource.limit(); i++) { 153 rwSource.put(i, value); 154 } 155 } 156 fill(byte[] values)157 void fill(byte[] values) { 158 for (int i = 0; i < rwSource.limit(); i++) { 159 rwSource.put(i, values[i % values.length]); 160 } 161 } 162 } 163 164 static class VarHandleSource extends Source<VarHandle> { VarHandleSource(VarHandle vh, MemoryMode... modes)165 VarHandleSource(VarHandle vh, MemoryMode... modes) { 166 super(vh, modes); 167 } 168 matches(ByteArrayViewSource<?> bav)169 boolean matches(ByteArrayViewSource<?> bav) { 170 return s.coordinateTypes().get(0).isAssignableFrom(bav.s.getClass()); 171 } 172 173 @Override toString()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 VarHandleSourceAccessTestCase(String desc, ByteArrayViewSource<?> bs, VarHandleSource vhs, AccessTestAction<VarHandleSource> ata)183 VarHandleSourceAccessTestCase(String desc, ByteArrayViewSource<?> bs, VarHandleSource vhs, AccessTestAction<VarHandleSource> ata) { 184 this(desc, bs, vhs, ata, true); 185 } 186 VarHandleSourceAccessTestCase(String desc, ByteArrayViewSource<?> bs, VarHandleSource vhs, AccessTestAction<VarHandleSource> ata, boolean loop)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 get()194 VarHandleSource get() { 195 return vhs; 196 } 197 } 198 199 rotateLeft(double i, int distance)200 static double rotateLeft(double i, int distance) { 201 return Double.longBitsToDouble( 202 Long.rotateLeft(Double.doubleToRawLongBits(i), distance)); 203 } 204 rotateRight(double i, int distance)205 static double rotateRight(double i, int distance) { 206 return Double.longBitsToDouble( 207 Long.rotateRight(Double.doubleToRawLongBits(i), distance)); 208 } 209 rotateLeft(float i, int distance)210 static float rotateLeft(float i, int distance) { 211 return Float.intBitsToFloat( 212 Integer.rotateLeft(Float.floatToRawIntBits(i), distance)); 213 } 214 rotateRight(float i, int distance)215 static float rotateRight(float i, int distance) { 216 return Float.intBitsToFloat( 217 Integer.rotateRight(Float.floatToRawIntBits(i), distance)); 218 } 219 rotateLeft(long i, int distance)220 static long rotateLeft(long i, int distance) { 221 return Long.rotateLeft(i, distance); 222 } 223 rotateRight(long i, int distance)224 static long rotateRight(long i, int distance) { 225 return Long.rotateRight(i, distance); 226 } 227 rotateLeft(int i, int distance)228 static int rotateLeft(int i, int distance) { 229 return Integer.rotateLeft(i, distance); 230 } 231 rotateRight(int i, int distance)232 static int rotateRight(int i, int distance) { 233 return Integer.rotateRight(i, distance); 234 } 235 rotateLeft(short i, int distance)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 rotateRight(short i, int distance)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 rotateLeft(char i, int distance)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 rotateRight(char i, int distance)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 setupByteSources()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 // Android-changed: Use ByteBuffer.slice() rather than Buffer.slice(). 323 ByteBufferSource dbb_offset_aligned = 324 new ByteBufferSource(((ByteBuffer) 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 // Android-changed: Use ByteBuffer.slice() rather than Buffer.slice(). 333 ByteBufferSource dbb_offset_unaligned = 334 new ByteBufferSource(((ByteBuffer) dbb.s.slice().position(array.length / 4 - 1).limit(array.length / 4 - 1 + array.length / 2)).slice(), 335 MemoryMode.UNALIGNED, ne, MemoryMode.READ_WRITE); 336 bavss.add(dbb_offset_unaligned); 337 ByteBufferReadOnlySource dbb_offset_unaligned_ro = 338 new ByteBufferReadOnlySource(dbb_offset_unaligned.s.asReadOnlyBuffer(), dbb_offset_unaligned.s, 339 MemoryMode.UNALIGNED, ne, MemoryMode.READ_ONLY); 340 bavss.add(dbb_offset_unaligned_ro); 341 } 342 343 @BeforeClass setup()344 public void setup() { 345 setupByteSources(); 346 setupVarHandleSources(); 347 } 348 setupVarHandleSources()349 abstract void setupVarHandleSources(); 350 351 352 @DataProvider varHandlesProvider()353 public Object[][] varHandlesProvider() throws Exception { 354 return vhss.stream().map(cvh -> new Object[]{cvh}).toArray(Object[][]::new); 355 } 356 357 @DataProvider typesProvider()358 public Object[][] typesProvider() throws Exception { 359 List<java.lang.Class<?>> aepts = Arrays.asList(byte[].class, int.class); 360 List<java.lang.Class<?>> bbpts = Arrays.asList(ByteBuffer.class, int.class); 361 362 Function<VarHandle, List<Class<?>>> vhToPts = vh -> 363 vh.coordinateTypes().get(0) == byte[].class ? aepts : bbpts; 364 365 return vhss.stream().map(vh -> new Object[]{vh.s, vhToPts.apply(vh.s)}).toArray(Object[][]::new); 366 } 367 } 368