|
20 | 20 | * or visit www.oracle.com if you need additional information or have any |
21 | 21 | * questions. |
22 | 22 | */ |
| 23 | +import java.nio.ByteOrder; |
| 24 | +import java.util.List; |
23 | 25 | import java.util.Locale; |
24 | 26 | import java.util.stream.Stream; |
25 | 27 |
|
26 | 28 | import jdk.internal.util.Architecture; |
27 | 29 | import jdk.internal.misc.Unsafe; |
28 | 30 |
|
29 | | -import static jdk.internal.util.Architecture.OTHER; |
30 | 31 | import static jdk.internal.util.Architecture.AARCH64; |
31 | 32 | import static jdk.internal.util.Architecture.ARM; |
32 | 33 | import static jdk.internal.util.Architecture.PPC64; |
| 34 | +import static jdk.internal.util.Architecture.PPC64LE; |
33 | 35 | import static jdk.internal.util.Architecture.RISCV64; |
34 | 36 | import static jdk.internal.util.Architecture.LOONGARCH64; |
35 | 37 | import static jdk.internal.util.Architecture.S390; |
|
44 | 46 | import org.junit.jupiter.params.provider.MethodSource; |
45 | 47 |
|
46 | 48 | import static org.junit.jupiter.api.Assertions.assertEquals; |
47 | | -import static org.junit.jupiter.api.Assertions.fail; |
48 | 49 |
|
49 | 50 | /** |
50 | 51 | * @test |
51 | | - * @bug 8304915 |
| 52 | + * @bug 8304915 8308452 8310982 |
52 | 53 | * @summary Verify Architecture enum maps to system property os.arch |
53 | 54 | * @modules java.base/jdk.internal.util |
54 | 55 | * @modules java.base/jdk.internal.misc |
55 | 56 | * @run junit ArchTest |
56 | 57 | */ |
57 | 58 | public class ArchTest { |
58 | | - private static boolean IS_BIG_ENDIAN = Unsafe.getUnsafe().isBigEndian(); |
| 59 | + private static final boolean IS_BIG_ENDIAN = Unsafe.getUnsafe().isBigEndian(); |
| 60 | + |
| 61 | + private static final boolean IS_64BIT_ADDRESS = Unsafe.getUnsafe().addressSize() == 8; |
| 62 | + |
| 63 | + /** |
| 64 | + * Test data for Architecture name vs Arch enums, address bits, endian-ness and boolean isXXX() methods.. |
| 65 | + * Each Argument contains: |
| 66 | + * - the common os.arch name, |
| 67 | + * - the Architecture Enum, |
| 68 | + * - address bits 32/64, |
| 69 | + * - the byte-order (little or big), |
| 70 | + * - the result of invoking the architecture specific static method |
| 71 | + * @return a stream of arguments for parameterized tests |
| 72 | + */ |
| 73 | + private static Stream<Arguments> archParams() { |
| 74 | + // In alphabetical order |
| 75 | + return Stream.of( |
| 76 | + Arguments.of("aarch64", AARCH64, 64, ByteOrder.LITTLE_ENDIAN, Architecture.isAARCH64()), |
| 77 | + Arguments.of("amd64", X64, 64, ByteOrder.LITTLE_ENDIAN, Architecture.isX64()), |
| 78 | + Arguments.of("arm", ARM, 32, ByteOrder.LITTLE_ENDIAN, Architecture.isARM()), |
| 79 | + Arguments.of("i386", X86, 32, ByteOrder.LITTLE_ENDIAN, Architecture.isX86()), |
| 80 | + Arguments.of("loongarch64", LOONGARCH64, 64, ByteOrder.LITTLE_ENDIAN, Architecture.isLOONGARCH64()), |
| 81 | + Arguments.of("mips64el", MIPS64EL, 64, ByteOrder.LITTLE_ENDIAN, Architecture.isMIPS64EL()), |
| 82 | + Arguments.of("mipsel", MIPSEL, 32, ByteOrder.LITTLE_ENDIAN, Architecture.isMIPSEL()), |
| 83 | + Arguments.of("ppc64", PPC64, 64, ByteOrder.BIG_ENDIAN, Architecture.isPPC64()), |
| 84 | + Arguments.of("ppc64le", PPC64LE, 64, ByteOrder.LITTLE_ENDIAN, Architecture.isPPC64LE()), |
| 85 | + Arguments.of("riscv64", RISCV64, 64, ByteOrder.LITTLE_ENDIAN, Architecture.isRISCV64()), |
| 86 | + Arguments.of("s390", S390, 64, ByteOrder.BIG_ENDIAN, Architecture.isS390()), |
| 87 | + Arguments.of("s390x", S390, 64, ByteOrder.BIG_ENDIAN, Architecture.isS390()), |
| 88 | + Arguments.of("x64", X64, 64, ByteOrder.LITTLE_ENDIAN, Architecture.isX64()), |
| 89 | + Arguments.of("x86", X86, 32, ByteOrder.LITTLE_ENDIAN, Architecture.isX86()), |
| 90 | + Arguments.of("x86_64", X64, 64, ByteOrder.LITTLE_ENDIAN, Architecture.isX64()) |
| 91 | + ); |
| 92 | + } |
59 | 93 |
|
60 | | - private static boolean IS_64BIT_ADDRESS = Unsafe.getUnsafe().addressSize() == 8; |
61 | 94 |
|
62 | 95 | /** |
63 | 96 | * Test consistency of System property "os.arch" with Architecture.current(). |
64 | 97 | */ |
65 | 98 | @Test |
66 | 99 | public void nameVsCurrent() { |
67 | 100 | String osArch = System.getProperty("os.arch").toLowerCase(Locale.ROOT); |
68 | | - System.out.printf("System property os.arch: \"%s\", Architecture.current(): \"%s\"%n", |
| 101 | + System.err.printf("System property os.arch: \"%s\", Architecture.current(): \"%s\"%n", |
69 | 102 | osArch, Architecture.current()); |
70 | | - Architecture arch = switch (osArch) { |
71 | | - case "x86_64", "amd64" -> X64; |
72 | | - case "x86", "i386" -> X86; |
73 | | - case "aarch64" -> AARCH64; |
74 | | - case "arm" -> ARM; |
75 | | - case "riscv64" -> RISCV64; |
76 | | - case "loongarch64" -> LOONGARCH64; |
77 | | - case "s390x", "s390" -> S390; |
78 | | - case "ppc64", "ppc64le" -> PPC64; |
79 | | - case "mipsel" -> MIPSEL; |
80 | | - case "mips64el" -> MIPS64EL; |
81 | | - default -> OTHER; |
82 | | - }; |
83 | | - assertEquals(Architecture.current(), arch, "mismatch in Architecture.current vs " + osArch); |
84 | | - } |
85 | 103 |
|
86 | | - /** |
87 | | - * Test various Architecture enum values vs boolean isXXX() methods. |
88 | | - * @return a stream of arguments for parameterized test |
89 | | - */ |
90 | | - private static Stream<Arguments> archParams() { |
91 | | - return Stream.of( |
92 | | - Arguments.of(X64, Architecture.isX64()), |
93 | | - Arguments.of(X86, Architecture.isX86()), |
94 | | - Arguments.of(AARCH64, Architecture.isAARCH64()), |
95 | | - Arguments.of(ARM, Architecture.isARM()), |
96 | | - Arguments.of(RISCV64, Architecture.isRISCV64()), |
97 | | - Arguments.of(LOONGARCH64, Architecture.isLOONGARCH64()), |
98 | | - Arguments.of(S390, Architecture.isS390()), |
99 | | - Arguments.of(MIPSEL, Architecture.isMIPSEL()), |
100 | | - Arguments.of(MIPS64EL, Architecture.isMIPS64EL()), |
101 | | - Arguments.of(PPC64, Architecture.isPPC64()) |
102 | | - ); |
| 104 | + // Map os.arch system property to expected Architecture |
| 105 | + List<Architecture> argList = archParams() |
| 106 | + .filter(p -> p.get()[0].equals(osArch)) |
| 107 | + .map(a -> (Architecture)a.get()[1]) |
| 108 | + .toList(); |
| 109 | + assertEquals(1, argList.size(), osArch + " too few or too many matching system property os.arch cases: " + argList); |
| 110 | + assertEquals(Architecture.current(), argList.get(0), "mismatch in Architecture.current vs " + osArch); |
103 | 111 | } |
104 | 112 |
|
105 | 113 | @ParameterizedTest |
106 | 114 | @MethodSource("archParams") |
107 | | - public void isArch(Architecture arch, boolean isArch) { |
| 115 | + public void checkParams(String archName, Architecture arch, int addrSize, ByteOrder byteOrder, boolean isArch) { |
| 116 | + Architecture actual = Architecture.lookupByName(archName); |
| 117 | + assertEquals(actual, arch, "Wrong Architecture from lookupByName"); |
| 118 | + |
| 119 | + actual = Architecture.lookupByName(archName.toUpperCase(Locale.ROOT)); |
| 120 | + assertEquals(actual, arch, "Wrong Architecture from lookupByName (upper-case)"); |
| 121 | + |
| 122 | + actual = Architecture.lookupByName(archName.toLowerCase(Locale.ROOT)); |
| 123 | + assertEquals(actual, arch, "Wrong Architecture from lookupByName (lower-case)"); |
| 124 | + |
| 125 | + assertEquals(addrSize, actual.addressSize(), "Wrong address size"); |
| 126 | + assertEquals(byteOrder, actual.byteOrder(), "Wrong byteOrder"); |
| 127 | + |
108 | 128 | Architecture current = Architecture.current(); |
109 | 129 | assertEquals(arch == current, isArch, |
110 | 130 | "Method is" + arch + "(): returned " + isArch + ", should be (" + arch + " == " + current + ")"); |
|
0 commit comments