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