1 /*
 2  * Copyright (c) 2020, 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 
25 /*
26  * @test
27  * @modules jdk.incubator.foreign/jdk.internal.foreign
28  *
29  * @run testng TestLayoutEquality
30  */
31 
32 import jdk.incubator.foreign.MemoryLayout;
33 import jdk.incubator.foreign.ValueLayout;
34 import jdk.internal.foreign.PlatformLayouts;
35 import org.testng.annotations.DataProvider;
36 import org.testng.annotations.Test;
37 
38 import java.lang.reflect.Field;
39 import java.util.ArrayList;
40 import java.util.List;
41 
42 import static org.testng.Assert.*;
43 
44 public class TestLayoutEquality {
45 
46     @Test(dataProvider = "layoutConstants")
47     public void testReconstructedEquality(ValueLayout layout) {
48         ValueLayout newLayout = MemoryLayout.valueLayout(layout.bitSize(), layout.order());
49 
50         // properties should be equal
51         assertEquals(newLayout.bitSize(), layout.bitSize());
52         assertEquals(newLayout.bitAlignment(), layout.bitAlignment());
53         assertEquals(newLayout.name(), layout.name());
54         assertEquals(newLayout.attributes().toArray().length, 0);
55         assertEquals(layout.attributes().toArray().length, 1);
56 
57         // but equals should return false, because one is a ValueLayout with a CLinker kind
58         assertNotEquals(newLayout, layout);
59     }
60 
61     @DataProvider
62     public static Object[][] layoutConstants() throws ReflectiveOperationException {
63         List<ValueLayout> testValues = new ArrayList<>();
64 
65         addLayoutConstants(testValues, PlatformLayouts.SysV.class);
66         addLayoutConstants(testValues, PlatformLayouts.Win64.class);
67         addLayoutConstants(testValues, PlatformLayouts.AArch64.class);
68 
69         return testValues.stream().map(e -> new Object[]{ e }).toArray(Object[][]::new);
70     }
71 
72     private static void addLayoutConstants(List<ValueLayout> testValues, Class<?> cls) throws ReflectiveOperationException {
73         for (Field f : cls.getFields()) {
74             if (f.getName().startsWith("C_"))
75                 testValues.add((ValueLayout) f.get(null));
76         }
77     }
78 
79 }