Path: blob/master/test/jdk/java/foreign/TestReshape.java
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @run testng TestReshape26*/2728import jdk.incubator.foreign.MemoryLayout;29import jdk.incubator.foreign.MemoryLayouts;30import jdk.incubator.foreign.SequenceLayout;3132import java.util.ArrayList;33import java.util.Iterator;34import java.util.List;35import java.util.stream.LongStream;3637import org.testng.annotations.*;38import static org.testng.Assert.*;3940public class TestReshape {4142@Test(dataProvider = "shapes")43public void testReshape(MemoryLayout layout, long[] expectedShape) {44long flattenedSize = LongStream.of(expectedShape).reduce(1L, Math::multiplyExact);45SequenceLayout seq_flattened = MemoryLayout.sequenceLayout(flattenedSize, layout);46assertDimensions(seq_flattened, flattenedSize);47for (long[] shape : new Shape(expectedShape)) {48SequenceLayout seq_shaped = seq_flattened.reshape(shape);49assertDimensions(seq_shaped, expectedShape);50assertEquals(seq_shaped.flatten(), seq_flattened);51}52}5354@Test(expectedExceptions = IllegalArgumentException.class)55public void testInvalidReshape() {56SequenceLayout seq = MemoryLayout.sequenceLayout(4, MemoryLayouts.JAVA_INT);57seq.reshape(3, 2);58}5960@Test(expectedExceptions = IllegalArgumentException.class)61public void testBadReshapeInference() {62SequenceLayout seq = MemoryLayout.sequenceLayout(4, MemoryLayouts.JAVA_INT);63seq.reshape(-1, -1);64}6566@Test(expectedExceptions = IllegalArgumentException.class)67public void testBadReshapeParameterZero() {68SequenceLayout seq = MemoryLayout.sequenceLayout(4, MemoryLayouts.JAVA_INT);69seq.reshape(0, 4);70}7172@Test(expectedExceptions = IllegalArgumentException.class)73public void testBadReshapeParameterNegative() {74SequenceLayout seq = MemoryLayout.sequenceLayout(4, MemoryLayouts.JAVA_INT);75seq.reshape(-2, 2);76}7778@Test(expectedExceptions = UnsupportedOperationException.class)79public void testReshapeOnUnboundSequence() {80SequenceLayout seq = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT);81seq.reshape(3, 2);82}8384@Test(expectedExceptions = UnsupportedOperationException.class)85public void testFlattenOnUnboundSequence() {86SequenceLayout seq = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT);87seq.flatten();88}8990@Test(expectedExceptions = UnsupportedOperationException.class)91public void testFlattenOnUnboundNestedSequence() {92SequenceLayout seq = MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_INT));93seq.flatten();94}9596static void assertDimensions(SequenceLayout layout, long... dims) {97SequenceLayout prev = null;98for (int i = 0 ; i < dims.length ; i++) {99if (prev != null) {100layout = (SequenceLayout)prev.elementLayout();101}102assertEquals(layout.elementCount().getAsLong(), dims[i]);103prev = layout;104}105}106107static class Shape implements Iterable<long[]> {108long[] shape;109110Shape(long... shape) {111this.shape = shape;112}113114public Iterator<long[]> iterator() {115List<long[]> shapes = new ArrayList<>();116shapes.add(shape);117for (int i = 0 ; i < shape.length ; i++) {118long[] inferredShape = shape.clone();119inferredShape[i] = -1;120shapes.add(inferredShape);121}122return shapes.iterator();123}124}125126static MemoryLayout POINT = MemoryLayout.structLayout(127MemoryLayouts.JAVA_INT,128MemoryLayouts.JAVA_INT129);130131@DataProvider(name = "shapes")132Object[][] shapes() {133return new Object[][] {134{ MemoryLayouts.JAVA_BYTE, new long[] { 256 } },135{ MemoryLayouts.JAVA_BYTE, new long[] { 16, 16 } },136{ MemoryLayouts.JAVA_BYTE, new long[] { 4, 4, 4, 4 } },137{ MemoryLayouts.JAVA_BYTE, new long[] { 2, 8, 16 } },138{ MemoryLayouts.JAVA_BYTE, new long[] { 16, 8, 2 } },139{ MemoryLayouts.JAVA_BYTE, new long[] { 8, 16, 2 } },140141{ MemoryLayouts.JAVA_SHORT, new long[] { 256 } },142{ MemoryLayouts.JAVA_SHORT, new long[] { 16, 16 } },143{ MemoryLayouts.JAVA_SHORT, new long[] { 4, 4, 4, 4 } },144{ MemoryLayouts.JAVA_SHORT, new long[] { 2, 8, 16 } },145{ MemoryLayouts.JAVA_SHORT, new long[] { 16, 8, 2 } },146{ MemoryLayouts.JAVA_SHORT, new long[] { 8, 16, 2 } },147148{ MemoryLayouts.JAVA_CHAR, new long[] { 256 } },149{ MemoryLayouts.JAVA_CHAR, new long[] { 16, 16 } },150{ MemoryLayouts.JAVA_CHAR, new long[] { 4, 4, 4, 4 } },151{ MemoryLayouts.JAVA_CHAR, new long[] { 2, 8, 16 } },152{ MemoryLayouts.JAVA_CHAR, new long[] { 16, 8, 2 } },153{ MemoryLayouts.JAVA_CHAR, new long[] { 8, 16, 2 } },154155{ MemoryLayouts.JAVA_INT, new long[] { 256 } },156{ MemoryLayouts.JAVA_INT, new long[] { 16, 16 } },157{ MemoryLayouts.JAVA_INT, new long[] { 4, 4, 4, 4 } },158{ MemoryLayouts.JAVA_INT, new long[] { 2, 8, 16 } },159{ MemoryLayouts.JAVA_INT, new long[] { 16, 8, 2 } },160{ MemoryLayouts.JAVA_INT, new long[] { 8, 16, 2 } },161162{ MemoryLayouts.JAVA_LONG, new long[] { 256 } },163{ MemoryLayouts.JAVA_LONG, new long[] { 16, 16 } },164{ MemoryLayouts.JAVA_LONG, new long[] { 4, 4, 4, 4 } },165{ MemoryLayouts.JAVA_LONG, new long[] { 2, 8, 16 } },166{ MemoryLayouts.JAVA_LONG, new long[] { 16, 8, 2 } },167{ MemoryLayouts.JAVA_LONG, new long[] { 8, 16, 2 } },168169{ MemoryLayouts.JAVA_FLOAT, new long[] { 256 } },170{ MemoryLayouts.JAVA_FLOAT, new long[] { 16, 16 } },171{ MemoryLayouts.JAVA_FLOAT, new long[] { 4, 4, 4, 4 } },172{ MemoryLayouts.JAVA_FLOAT, new long[] { 2, 8, 16 } },173{ MemoryLayouts.JAVA_FLOAT, new long[] { 16, 8, 2 } },174{ MemoryLayouts.JAVA_FLOAT, new long[] { 8, 16, 2 } },175176{ MemoryLayouts.JAVA_DOUBLE, new long[] { 256 } },177{ MemoryLayouts.JAVA_DOUBLE, new long[] { 16, 16 } },178{ MemoryLayouts.JAVA_DOUBLE, new long[] { 4, 4, 4, 4 } },179{ MemoryLayouts.JAVA_DOUBLE, new long[] { 2, 8, 16 } },180{ MemoryLayouts.JAVA_DOUBLE, new long[] { 16, 8, 2 } },181{ MemoryLayouts.JAVA_DOUBLE, new long[] { 8, 16, 2 } },182183{ POINT, new long[] { 256 } },184{ POINT, new long[] { 16, 16 } },185{ POINT, new long[] { 4, 4, 4, 4 } },186{ POINT, new long[] { 2, 8, 16 } },187{ POINT, new long[] { 16, 8, 2 } },188{ POINT, new long[] { 8, 16, 2 } },189};190}191}192193194