1 //===- unittests/Basic/DarwinSDKInfoTest.cpp -- SDKSettings.json test -----===//
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
7 //===----------------------------------------------------------------------===//
9 #include "clang/Basic/DarwinSDKInfo.h"
10 #include "llvm/Support/JSON.h"
11 #include "gtest/gtest.h"
15 using namespace clang
;
17 // Check the version mapping logic in DarwinSDKInfo.
18 TEST(DarwinSDKInfo
, VersionMapping
) {
19 llvm::json::Object
Obj({{"3.0", "1.0"}, {"3.1", "1.2"}});
20 std::optional
<DarwinSDKInfo::RelatedTargetVersionMapping
> Mapping
=
21 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj
,
24 EXPECT_EQ(Mapping
->getMinimumValue(), VersionTuple(1));
27 EXPECT_EQ(Mapping
->map(VersionTuple(3), VersionTuple(0, 1), std::nullopt
),
29 EXPECT_EQ(Mapping
->map(VersionTuple(3, 0), VersionTuple(0, 1), std::nullopt
),
32 Mapping
->map(VersionTuple(3, 0, 0), VersionTuple(0, 1), std::nullopt
),
34 EXPECT_EQ(Mapping
->map(VersionTuple(3, 1), VersionTuple(0, 1), std::nullopt
),
37 Mapping
->map(VersionTuple(3, 1, 0), VersionTuple(0, 1), std::nullopt
),
40 // Missing mapping - fallback to major.
42 Mapping
->map(VersionTuple(3, 0, 1), VersionTuple(0, 1), std::nullopt
),
46 EXPECT_EQ(Mapping
->map(VersionTuple(2), VersionTuple(0, 1), std::nullopt
),
51 Mapping
->map(VersionTuple(4), VersionTuple(0, 1), VersionTuple(100)),
55 // Check the version mapping logic in DarwinSDKInfo.
56 TEST(DarwinSDKInfo
, VersionMappingMissingKey
) {
57 llvm::json::Object
Obj({{"3.0", "1.0"}, {"5.0", "1.2"}});
58 std::optional
<DarwinSDKInfo::RelatedTargetVersionMapping
> Mapping
=
59 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj
,
63 Mapping
->map(VersionTuple(4), VersionTuple(0, 1), VersionTuple(100)),
67 TEST(DarwinSDKInfo
, VersionMappingParseEmpty
) {
68 llvm::json::Object
Obj({});
70 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj
, VersionTuple())
74 TEST(DarwinSDKInfo
, VersionMappingParseError
) {
75 llvm::json::Object
Obj({{"test", "1.2"}});
77 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj
, VersionTuple())
81 TEST(DarwinSDKInfoTest
, ParseAndTestMappingMacCatalyst
) {
82 llvm::json::Object Obj
;
83 Obj
["Version"] = "11.0";
84 Obj
["MaximumDeploymentTarget"] = "11.99";
85 llvm::json::Object VersionMap
;
86 VersionMap
["10.15"] = "13.1";
87 VersionMap
["11.0"] = "14.0";
88 VersionMap
["11.2"] = "14.2";
89 llvm::json::Object MacOS2iOSMac
;
90 MacOS2iOSMac
["macOS_iOSMac"] = std::move(VersionMap
);
91 Obj
["VersionMap"] = std::move(MacOS2iOSMac
);
93 auto SDKInfo
= DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj
);
95 EXPECT_EQ(SDKInfo
->getVersion(), VersionTuple(11, 0));
97 auto Mapping
= SDKInfo
->getVersionMapping(
98 DarwinSDKInfo::OSEnvPair::macOStoMacCatalystPair());
100 // Verify that the macOS versions that are present in the map are translated
101 // directly to their corresponding Mac Catalyst versions.
102 EXPECT_EQ(*Mapping
->map(VersionTuple(10, 15), VersionTuple(), std::nullopt
),
103 VersionTuple(13, 1));
104 EXPECT_EQ(*Mapping
->map(VersionTuple(11, 0), VersionTuple(), std::nullopt
),
105 VersionTuple(14, 0));
106 EXPECT_EQ(*Mapping
->map(VersionTuple(11, 2), VersionTuple(), std::nullopt
),
107 VersionTuple(14, 2));
109 // Verify that a macOS version that's not present in the map is translated
110 // like the nearest major OS version.
111 EXPECT_EQ(*Mapping
->map(VersionTuple(11, 1), VersionTuple(), std::nullopt
),
112 VersionTuple(14, 0));
114 // Verify that the macOS versions that are outside of the mapped version
115 // range map to the min/max values passed to the `map` call.
117 *Mapping
->map(VersionTuple(10, 14), VersionTuple(99, 99), std::nullopt
),
118 VersionTuple(99, 99));
120 *Mapping
->map(VersionTuple(11, 5), VersionTuple(), VersionTuple(99, 99)),
121 VersionTuple(99, 99));
122 EXPECT_EQ(*Mapping
->map(VersionTuple(11, 5), VersionTuple(99, 98),
123 VersionTuple(99, 99)),
124 VersionTuple(99, 99));
127 TEST(DarwinSDKInfoTest
, ParseAndTestMappingIOSDerived
) {
128 llvm::json::Object Obj
;
129 Obj
["Version"] = "15.0";
130 Obj
["MaximumDeploymentTarget"] = "15.0.99";
131 llvm::json::Object VersionMap
;
132 VersionMap
["10.0"] = "10.0";
133 VersionMap
["10.3.1"] = "10.2";
134 VersionMap
["11.0"] = "11.0";
135 llvm::json::Object IOSToTvOS
;
136 IOSToTvOS
["iOS_tvOS"] = std::move(VersionMap
);
137 Obj
["VersionMap"] = std::move(IOSToTvOS
);
139 auto SDKInfo
= DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj
);
140 ASSERT_TRUE(SDKInfo
);
141 EXPECT_EQ(SDKInfo
->getVersion(), VersionTuple(15, 0));
143 // Verify that mapping is present for platforms that derive from iOS.
144 const auto *Mapping
= SDKInfo
->getVersionMapping(DarwinSDKInfo::OSEnvPair(
145 llvm::Triple::IOS
, llvm::Triple::UnknownEnvironment
, llvm::Triple::TvOS
,
146 llvm::Triple::UnknownEnvironment
));
147 ASSERT_TRUE(Mapping
);
149 // Verify that the iOS versions that are present in the map are translated
150 // directly to their corresponding tvOS versions.
151 EXPECT_EQ(*Mapping
->map(VersionTuple(10, 0), VersionTuple(), std::nullopt
),
152 VersionTuple(10, 0));
153 EXPECT_EQ(*Mapping
->map(VersionTuple(10, 3, 1), VersionTuple(), std::nullopt
),
154 VersionTuple(10, 2));
155 EXPECT_EQ(*Mapping
->map(VersionTuple(11, 0), VersionTuple(), std::nullopt
),
156 VersionTuple(11, 0));
158 // Verify that an iOS version that's not present in the map is translated
159 // like the nearest major OS version.
160 EXPECT_EQ(*Mapping
->map(VersionTuple(10, 1), VersionTuple(), std::nullopt
),
161 VersionTuple(10, 0));
163 // Verify that the iOS versions that are outside of the mapped version
164 // range map to the min/max values passed to the `map` call.
166 *Mapping
->map(VersionTuple(9, 0), VersionTuple(99, 99), std::nullopt
),
167 VersionTuple(99, 99));
169 *Mapping
->map(VersionTuple(13, 0), VersionTuple(), VersionTuple(99, 99)),
170 VersionTuple(99, 99));
173 TEST(DarwinSDKInfoTest
, MissingKeys
) {
174 llvm::json::Object Obj
;
175 ASSERT_FALSE(DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj
));
176 Obj
["Version"] = "11.0";
177 ASSERT_FALSE(DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj
));