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