1 //===- unittests/Basic/DarwinSDKInfoTest.cpp -- SDKSettings.json test -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "clang/Basic/DarwinSDKInfo.h"
10 #include "llvm/Support/JSON.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm;
14 using namespace clang;
15
16 // Check the version mapping logic in DarwinSDKInfo.
TEST(DarwinSDKInfo,VersionMapping)17 TEST(DarwinSDKInfo, VersionMapping) {
18 llvm::json::Object Obj({{"3.0", "1.0"}, {"3.1", "1.2"}});
19 Optional<DarwinSDKInfo::RelatedTargetVersionMapping> Mapping =
20 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj,
21 VersionTuple());
22 EXPECT_TRUE(Mapping);
23 EXPECT_EQ(Mapping->getMinimumValue(), VersionTuple(1));
24
25 // Exact mapping.
26 EXPECT_EQ(Mapping->map(VersionTuple(3), VersionTuple(0, 1), None),
27 VersionTuple(1));
28 EXPECT_EQ(Mapping->map(VersionTuple(3, 0), VersionTuple(0, 1), None),
29 VersionTuple(1));
30 EXPECT_EQ(Mapping->map(VersionTuple(3, 0, 0), VersionTuple(0, 1), None),
31 VersionTuple(1));
32 EXPECT_EQ(Mapping->map(VersionTuple(3, 1), VersionTuple(0, 1), None),
33 VersionTuple(1, 2));
34 EXPECT_EQ(Mapping->map(VersionTuple(3, 1, 0), VersionTuple(0, 1), None),
35 VersionTuple(1, 2));
36
37 // Missing mapping - fallback to major.
38 EXPECT_EQ(Mapping->map(VersionTuple(3, 0, 1), VersionTuple(0, 1), None),
39 VersionTuple(1));
40
41 // Minimum
42 EXPECT_EQ(Mapping->map(VersionTuple(2), VersionTuple(0, 1), None),
43 VersionTuple(0, 1));
44
45 // Maximum
46 EXPECT_EQ(
47 Mapping->map(VersionTuple(4), VersionTuple(0, 1), VersionTuple(100)),
48 VersionTuple(100));
49 }
50
51 // Check the version mapping logic in DarwinSDKInfo.
TEST(DarwinSDKInfo,VersionMappingMissingKey)52 TEST(DarwinSDKInfo, VersionMappingMissingKey) {
53 llvm::json::Object Obj({{"3.0", "1.0"}, {"5.0", "1.2"}});
54 Optional<DarwinSDKInfo::RelatedTargetVersionMapping> Mapping =
55 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj,
56 VersionTuple());
57 EXPECT_TRUE(Mapping);
58 EXPECT_EQ(
59 Mapping->map(VersionTuple(4), VersionTuple(0, 1), VersionTuple(100)),
60 None);
61 }
62
TEST(DarwinSDKInfo,VersionMappingParseEmpty)63 TEST(DarwinSDKInfo, VersionMappingParseEmpty) {
64 llvm::json::Object Obj({});
65 EXPECT_FALSE(
66 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj, VersionTuple())
67 .has_value());
68 }
69
TEST(DarwinSDKInfo,VersionMappingParseError)70 TEST(DarwinSDKInfo, VersionMappingParseError) {
71 llvm::json::Object Obj({{"test", "1.2"}});
72 EXPECT_FALSE(
73 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj, VersionTuple())
74 .has_value());
75 }
76
TEST(DarwinSDKInfoTest,ParseAndTestMappingMacCatalyst)77 TEST(DarwinSDKInfoTest, ParseAndTestMappingMacCatalyst) {
78 llvm::json::Object Obj;
79 Obj["Version"] = "11.0";
80 Obj["MaximumDeploymentTarget"] = "11.99";
81 llvm::json::Object VersionMap;
82 VersionMap["10.15"] = "13.1";
83 VersionMap["11.0"] = "14.0";
84 VersionMap["11.2"] = "14.2";
85 llvm::json::Object MacOS2iOSMac;
86 MacOS2iOSMac["macOS_iOSMac"] = std::move(VersionMap);
87 Obj["VersionMap"] = std::move(MacOS2iOSMac);
88
89 auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj);
90 ASSERT_TRUE(SDKInfo);
91 EXPECT_EQ(SDKInfo->getVersion(), VersionTuple(11, 0));
92
93 auto Mapping = SDKInfo->getVersionMapping(
94 DarwinSDKInfo::OSEnvPair::macOStoMacCatalystPair());
95 ASSERT_TRUE(Mapping);
96 // Verify that the macOS versions that are present in the map are translated
97 // directly to their corresponding Mac Catalyst versions.
98 EXPECT_EQ(*Mapping->map(VersionTuple(10, 15), VersionTuple(), None),
99 VersionTuple(13, 1));
100 EXPECT_EQ(*Mapping->map(VersionTuple(11, 0), VersionTuple(), None),
101 VersionTuple(14, 0));
102 EXPECT_EQ(*Mapping->map(VersionTuple(11, 2), VersionTuple(), None),
103 VersionTuple(14, 2));
104
105 // Verify that a macOS version that's not present in the map is translated
106 // like the nearest major OS version.
107 EXPECT_EQ(*Mapping->map(VersionTuple(11, 1), VersionTuple(), None),
108 VersionTuple(14, 0));
109
110 // Verify that the macOS versions that are outside of the mapped version
111 // range map to the min/max values passed to the `map` call.
112 EXPECT_EQ(*Mapping->map(VersionTuple(10, 14), VersionTuple(99, 99), None),
113 VersionTuple(99, 99));
114 EXPECT_EQ(
115 *Mapping->map(VersionTuple(11, 5), VersionTuple(), VersionTuple(99, 99)),
116 VersionTuple(99, 99));
117 EXPECT_EQ(*Mapping->map(VersionTuple(11, 5), VersionTuple(99, 98),
118 VersionTuple(99, 99)),
119 VersionTuple(99, 99));
120 }
121
TEST(DarwinSDKInfoTest,ParseAndTestMappingIOSDerived)122 TEST(DarwinSDKInfoTest, ParseAndTestMappingIOSDerived) {
123 llvm::json::Object Obj;
124 Obj["Version"] = "15.0";
125 Obj["MaximumDeploymentTarget"] = "15.0.99";
126 llvm::json::Object VersionMap;
127 VersionMap["10.0"] = "10.0";
128 VersionMap["10.3.1"] = "10.2";
129 VersionMap["11.0"] = "11.0";
130 llvm::json::Object IOSToTvOS;
131 IOSToTvOS["iOS_tvOS"] = std::move(VersionMap);
132 Obj["VersionMap"] = std::move(IOSToTvOS);
133
134 auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj);
135 ASSERT_TRUE(SDKInfo);
136 EXPECT_EQ(SDKInfo->getVersion(), VersionTuple(15, 0));
137
138 // Verify that mapping is present for platforms that derive from iOS.
139 const auto *Mapping = SDKInfo->getVersionMapping(DarwinSDKInfo::OSEnvPair(
140 llvm::Triple::IOS, llvm::Triple::UnknownEnvironment, llvm::Triple::TvOS,
141 llvm::Triple::UnknownEnvironment));
142 ASSERT_TRUE(Mapping);
143
144 // Verify that the iOS versions that are present in the map are translated
145 // directly to their corresponding tvOS versions.
146 EXPECT_EQ(*Mapping->map(VersionTuple(10, 0), VersionTuple(), None),
147 VersionTuple(10, 0));
148 EXPECT_EQ(*Mapping->map(VersionTuple(10, 3, 1), VersionTuple(), None),
149 VersionTuple(10, 2));
150 EXPECT_EQ(*Mapping->map(VersionTuple(11, 0), VersionTuple(), None),
151 VersionTuple(11, 0));
152
153 // Verify that an iOS version that's not present in the map is translated
154 // like the nearest major OS version.
155 EXPECT_EQ(*Mapping->map(VersionTuple(10, 1), VersionTuple(), None),
156 VersionTuple(10, 0));
157
158 // Verify that the iOS versions that are outside of the mapped version
159 // range map to the min/max values passed to the `map` call.
160 EXPECT_EQ(*Mapping->map(VersionTuple(9, 0), VersionTuple(99, 99), None),
161 VersionTuple(99, 99));
162 EXPECT_EQ(
163 *Mapping->map(VersionTuple(13, 0), VersionTuple(), VersionTuple(99, 99)),
164 VersionTuple(99, 99));
165 }
166
TEST(DarwinSDKInfoTest,MissingKeys)167 TEST(DarwinSDKInfoTest, MissingKeys) {
168 llvm::json::Object Obj;
169 ASSERT_FALSE(DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj));
170 Obj["Version"] = "11.0";
171 ASSERT_FALSE(DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj));
172 }
173