< prev index next >

test/jdk/java/foreign/TestMismatch.java

Print this page

 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 /*
 25  * @test
 26  * @run testng TestMismatch
 27  */
 28 
 29 import java.lang.invoke.VarHandle;
 30 import java.util.ArrayList;
 31 import java.util.List;
 32 import java.util.concurrent.atomic.AtomicReference;
 33 import java.util.function.IntFunction;
 34 
 35 import jdk.incubator.foreign.MemoryLayouts;
 36 import jdk.incubator.foreign.MemorySegment;
 37 import jdk.incubator.foreign.ResourceScope;

 38 import org.testng.annotations.DataProvider;
 39 import org.testng.annotations.Test;
 40 import static java.lang.System.out;
 41 import static org.testng.Assert.assertEquals;
 42 import static org.testng.Assert.assertThrows;
 43 
 44 public class TestMismatch {
 45 
 46     final static VarHandle BYTE_HANDLE = MemoryLayouts.JAVA_BYTE.varHandle(byte.class);
 47 
 48     // stores a increasing sequence of values into the memory of the given segment
 49     static MemorySegment initializeSegment(MemorySegment segment) {
 50         for (int i = 0 ; i < segment.byteSize() ; i++) {
 51             BYTE_HANDLE.set(segment.asSlice(i), (byte)i);
 52         }
 53         return segment;
 54     }
 55 
 56     @Test(dataProvider = "slices")
 57     public void testSameValues(MemorySegment ss1, MemorySegment ss2) {
 58         out.format("testSameValues s1:%s, s2:%s\n", ss1, ss2);
 59         MemorySegment s1 = initializeSegment(ss1);
 60         MemorySegment s2 = initializeSegment(ss2);
 61 
 62         if (s1.byteSize() == s2.byteSize()) {
 63             assertEquals(s1.mismatch(s2), -1);  // identical
 64             assertEquals(s2.mismatch(s1), -1);
 65         } else if (s1.byteSize() > s2.byteSize()) {
 66             assertEquals(s1.mismatch(s2), s2.byteSize());  // proper prefix

 95                 assertEquals(s2.mismatch(s1), off);
 96             }
 97         }
 98     }
 99 
100     @Test
101     public void testEmpty() {
102         var s1 = MemorySegment.ofArray(new byte[0]);
103         assertEquals(s1.mismatch(s1), -1);
104         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
105             var nativeSegment = MemorySegment.allocateNative(4, 4, scope);
106             var s2 = nativeSegment.asSlice(0, 0);
107             assertEquals(s1.mismatch(s2), -1);
108             assertEquals(s2.mismatch(s1), -1);
109         }
110     }
111 
112     @Test
113     public void testLarge() {
114         // skip if not on 64 bits
115         if (MemoryLayouts.ADDRESS.byteSize() > 32) {
116             try (ResourceScope scope = ResourceScope.newConfinedScope()) {
117                 var s1 = MemorySegment.allocateNative((long) Integer.MAX_VALUE + 10L, 8, scope);
118                 var s2 = MemorySegment.allocateNative((long) Integer.MAX_VALUE + 10L, 8, scope);
119                 assertEquals(s1.mismatch(s1), -1);
120                 assertEquals(s1.mismatch(s2), -1);
121                 assertEquals(s2.mismatch(s1), -1);
122 
123                 testLargeAcrossMaxBoundary(s1, s2);
124 
125                 testLargeMismatchAcrossMaxBoundary(s1, s2);
126             }
127         }
128     }
129 
130     private void testLargeAcrossMaxBoundary(MemorySegment s1, MemorySegment s2) {
131         for (long i = s2.byteSize() -1 ; i >= Integer.MAX_VALUE - 10L; i--) {
132             var s3 = s1.asSlice(0, i);
133             var s4 = s2.asSlice(0, i);
134             assertEquals(s3.mismatch(s3), -1);
135             assertEquals(s3.mismatch(s4), -1);

 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 /*
 25  * @test
 26  * @run testng TestMismatch
 27  */
 28 
 29 import java.lang.invoke.VarHandle;
 30 import java.util.ArrayList;
 31 import java.util.List;
 32 import java.util.concurrent.atomic.AtomicReference;
 33 import java.util.function.IntFunction;
 34 

 35 import jdk.incubator.foreign.MemorySegment;
 36 import jdk.incubator.foreign.ResourceScope;
 37 import jdk.incubator.foreign.ValueLayout;
 38 import org.testng.annotations.DataProvider;
 39 import org.testng.annotations.Test;
 40 import static java.lang.System.out;
 41 import static org.testng.Assert.assertEquals;
 42 import static org.testng.Assert.assertThrows;
 43 
 44 public class TestMismatch {
 45 
 46     final static VarHandle BYTE_HANDLE = ValueLayout.JAVA_BYTE.varHandle();
 47 
 48     // stores a increasing sequence of values into the memory of the given segment
 49     static MemorySegment initializeSegment(MemorySegment segment) {
 50         for (int i = 0 ; i < segment.byteSize() ; i++) {
 51             BYTE_HANDLE.set(segment.asSlice(i), (byte)i);
 52         }
 53         return segment;
 54     }
 55 
 56     @Test(dataProvider = "slices")
 57     public void testSameValues(MemorySegment ss1, MemorySegment ss2) {
 58         out.format("testSameValues s1:%s, s2:%s\n", ss1, ss2);
 59         MemorySegment s1 = initializeSegment(ss1);
 60         MemorySegment s2 = initializeSegment(ss2);
 61 
 62         if (s1.byteSize() == s2.byteSize()) {
 63             assertEquals(s1.mismatch(s2), -1);  // identical
 64             assertEquals(s2.mismatch(s1), -1);
 65         } else if (s1.byteSize() > s2.byteSize()) {
 66             assertEquals(s1.mismatch(s2), s2.byteSize());  // proper prefix

 95                 assertEquals(s2.mismatch(s1), off);
 96             }
 97         }
 98     }
 99 
100     @Test
101     public void testEmpty() {
102         var s1 = MemorySegment.ofArray(new byte[0]);
103         assertEquals(s1.mismatch(s1), -1);
104         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
105             var nativeSegment = MemorySegment.allocateNative(4, 4, scope);
106             var s2 = nativeSegment.asSlice(0, 0);
107             assertEquals(s1.mismatch(s2), -1);
108             assertEquals(s2.mismatch(s1), -1);
109         }
110     }
111 
112     @Test
113     public void testLarge() {
114         // skip if not on 64 bits
115         if (ValueLayout.ADDRESS.byteSize() > 32) {
116             try (ResourceScope scope = ResourceScope.newConfinedScope()) {
117                 var s1 = MemorySegment.allocateNative((long) Integer.MAX_VALUE + 10L, 8, scope);
118                 var s2 = MemorySegment.allocateNative((long) Integer.MAX_VALUE + 10L, 8, scope);
119                 assertEquals(s1.mismatch(s1), -1);
120                 assertEquals(s1.mismatch(s2), -1);
121                 assertEquals(s2.mismatch(s1), -1);
122 
123                 testLargeAcrossMaxBoundary(s1, s2);
124 
125                 testLargeMismatchAcrossMaxBoundary(s1, s2);
126             }
127         }
128     }
129 
130     private void testLargeAcrossMaxBoundary(MemorySegment s1, MemorySegment s2) {
131         for (long i = s2.byteSize() -1 ; i >= Integer.MAX_VALUE - 10L; i--) {
132             var s3 = s1.asSlice(0, i);
133             var s4 = s2.asSlice(0, i);
134             assertEquals(s3.mismatch(s3), -1);
135             assertEquals(s3.mismatch(s4), -1);
< prev index next >