1 /*
 2  * Copyright (c) 2020, 2023, 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  * @enablePreview
28  * @compile platform/PlatformLayouts.java
29  * @modules java.base/jdk.internal.foreign.abi
30  * @modules java.base/jdk.internal.foreign.layout
31  * @run testng TestLayoutEquality
32  */
33 
34 import java.lang.foreign.AddressLayout;
35 import java.lang.foreign.ValueLayout;
36 
37 import jdk.internal.foreign.layout.ValueLayouts;
38 import platform.PlatformLayouts;
39 import org.testng.annotations.DataProvider;
40 import org.testng.annotations.Test;
41 
42 import java.lang.reflect.Field;
43 import java.util.ArrayList;
44 import java.util.List;
45 
46 import static org.testng.Assert.*;
47 
48 public class TestLayoutEquality {
49 
50     @Test(dataProvider = "layoutConstants")
51     public void testReconstructedEquality(ValueLayout layout) {
52         ValueLayout newLayout = ValueLayouts.valueLayout(layout.carrier(), layout.order());
53         newLayout = newLayout.withByteAlignment(layout.byteAlignment());
54         if (layout instanceof AddressLayout addressLayout && addressLayout.targetLayout().isPresent()) {
55             newLayout = ((AddressLayout)newLayout).withTargetLayout(addressLayout.targetLayout().get());
56         }
57 
58         // properties should be equal
59         assertEquals(newLayout.byteSize(), layout.byteSize());
60         assertEquals(newLayout.byteAlignment(), layout.byteAlignment());
61         assertEquals(newLayout.name(), layout.name());
62 
63         // layouts should be equals
64         assertEquals(newLayout, layout);
65     }
66 
67     @DataProvider
68     public static Object[][] layoutConstants() throws ReflectiveOperationException {
69         List<ValueLayout> testValues = new ArrayList<>();
70 
71         addLayoutConstants(testValues, PlatformLayouts.SysV.class);
72         addLayoutConstants(testValues, PlatformLayouts.Win64.class);
73         addLayoutConstants(testValues, PlatformLayouts.AArch64.class);
74         addLayoutConstants(testValues, PlatformLayouts.RISCV64.class);
75 
76         return testValues.stream().map(e -> new Object[]{ e }).toArray(Object[][]::new);
77     }
78 
79     private static void addLayoutConstants(List<ValueLayout> testValues, Class<?> cls) throws ReflectiveOperationException {
80         for (Field f : cls.getFields()) {
81             if (f.getName().startsWith("C_"))
82                 testValues.add((ValueLayout) f.get(null));
83         }
84     }
85 }