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