1 //===- llvm/unittest/ADT/StringSwitchTest.cpp - StringSwitch unit tests ---===//
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 "llvm/ADT/StringSwitch.h"
10 #include "gtest/gtest.h"
14 TEST(StringSwitchTest
, Case
) {
15 auto Translate
= [](StringRef S
) {
16 return llvm::StringSwitch
<int>(S
)
35 EXPECT_EQ(1, Translate("1"));
36 EXPECT_EQ(2, Translate("2"));
37 EXPECT_EQ(11, Translate("B"));
38 EXPECT_EQ(-1, Translate("b"));
39 EXPECT_EQ(-1, Translate(""));
40 EXPECT_EQ(-1, Translate("Test"));
43 TEST(StringSwitchTest
, CaseLower
) {
44 auto Translate
= [](StringRef S
) {
45 return llvm::StringSwitch
<int>(S
)
64 EXPECT_EQ(1, Translate("1"));
65 EXPECT_EQ(2, Translate("2"));
66 EXPECT_EQ(11, Translate("B"));
67 EXPECT_EQ(11, Translate("b"));
69 EXPECT_EQ(-1, Translate(""));
70 EXPECT_EQ(-1, Translate("Test"));
73 TEST(StringSwitchTest
, StartsWith
) {
74 auto Translate
= [](StringRef S
) {
75 return llvm::StringSwitch
<std::function
<int(int, int)>>(S
)
76 .StartsWith("add", [](int X
, int Y
) { return X
+ Y
; })
77 .StartsWith("sub", [](int X
, int Y
) { return X
- Y
; })
78 .StartsWith("mul", [](int X
, int Y
) { return X
* Y
; })
79 .StartsWith("div", [](int X
, int Y
) { return X
/ Y
; })
80 .Default([](int X
, int Y
) { return 0; });
83 EXPECT_EQ(15, Translate("adder")(10, 5));
84 EXPECT_EQ(5, Translate("subtracter")(10, 5));
85 EXPECT_EQ(50, Translate("multiplier")(10, 5));
86 EXPECT_EQ(2, Translate("divider")(10, 5));
88 EXPECT_EQ(0, Translate("nothing")(10, 5));
89 EXPECT_EQ(0, Translate("ADDER")(10, 5));
92 TEST(StringSwitchTest
, StartsWithLower
) {
93 auto Translate
= [](StringRef S
) {
94 return llvm::StringSwitch
<std::function
<int(int, int)>>(S
)
95 .StartsWithLower("add", [](int X
, int Y
) { return X
+ Y
; })
96 .StartsWithLower("sub", [](int X
, int Y
) { return X
- Y
; })
97 .StartsWithLower("mul", [](int X
, int Y
) { return X
* Y
; })
98 .StartsWithLower("div", [](int X
, int Y
) { return X
/ Y
; })
99 .Default([](int X
, int Y
) { return 0; });
102 EXPECT_EQ(15, Translate("adder")(10, 5));
103 EXPECT_EQ(5, Translate("subtracter")(10, 5));
104 EXPECT_EQ(50, Translate("multiplier")(10, 5));
105 EXPECT_EQ(2, Translate("divider")(10, 5));
107 EXPECT_EQ(15, Translate("AdDeR")(10, 5));
108 EXPECT_EQ(5, Translate("SuBtRaCtEr")(10, 5));
109 EXPECT_EQ(50, Translate("MuLtIpLiEr")(10, 5));
110 EXPECT_EQ(2, Translate("DiViDeR")(10, 5));
112 EXPECT_EQ(0, Translate("nothing")(10, 5));
115 TEST(StringSwitchTest
, EndsWith
) {
116 enum class Suffix
{ Possible
, PastTense
, Process
, InProgressAction
, Unknown
};
118 auto Translate
= [](StringRef S
) {
119 return llvm::StringSwitch
<Suffix
>(S
)
120 .EndsWith("able", Suffix::Possible
)
121 .EndsWith("ed", Suffix::PastTense
)
122 .EndsWith("ation", Suffix::Process
)
123 .EndsWith("ing", Suffix::InProgressAction
)
124 .Default(Suffix::Unknown
);
127 EXPECT_EQ(Suffix::Possible
, Translate("optimizable"));
128 EXPECT_EQ(Suffix::PastTense
, Translate("optimized"));
129 EXPECT_EQ(Suffix::Process
, Translate("optimization"));
130 EXPECT_EQ(Suffix::InProgressAction
, Translate("optimizing"));
131 EXPECT_EQ(Suffix::Unknown
, Translate("optimizer"));
132 EXPECT_EQ(Suffix::Unknown
, Translate("OPTIMIZABLE"));
135 TEST(StringSwitchTest
, EndsWithLower
) {
136 enum class Suffix
{ Possible
, PastTense
, Process
, InProgressAction
, Unknown
};
138 auto Translate
= [](StringRef S
) {
139 return llvm::StringSwitch
<Suffix
>(S
)
140 .EndsWithLower("able", Suffix::Possible
)
141 .EndsWithLower("ed", Suffix::PastTense
)
142 .EndsWithLower("ation", Suffix::Process
)
143 .EndsWithLower("ing", Suffix::InProgressAction
)
144 .Default(Suffix::Unknown
);
147 EXPECT_EQ(Suffix::Possible
, Translate("optimizable"));
148 EXPECT_EQ(Suffix::Possible
, Translate("OPTIMIZABLE"));
149 EXPECT_EQ(Suffix::PastTense
, Translate("optimized"));
150 EXPECT_EQ(Suffix::Process
, Translate("optimization"));
151 EXPECT_EQ(Suffix::InProgressAction
, Translate("optimizing"));
152 EXPECT_EQ(Suffix::Unknown
, Translate("optimizer"));
155 TEST(StringSwitchTest
, Cases
) {
156 enum class OSType
{ Windows
, Linux
, Unknown
};
158 auto Translate
= [](StringRef S
) {
159 return llvm::StringSwitch
<OSType
>(S
)
160 .Cases(StringLiteral::withInnerNUL("wind\0ws"), "win32", "winnt",
162 .Cases("linux", "unix", "*nix", "posix", OSType::Linux
)
163 .Default(OSType::Unknown
);
166 EXPECT_EQ(OSType::Windows
, Translate(llvm::StringRef("wind\0ws", 7)));
167 EXPECT_EQ(OSType::Windows
, Translate("win32"));
168 EXPECT_EQ(OSType::Windows
, Translate("winnt"));
170 EXPECT_EQ(OSType::Linux
, Translate("linux"));
171 EXPECT_EQ(OSType::Linux
, Translate("unix"));
172 EXPECT_EQ(OSType::Linux
, Translate("*nix"));
173 EXPECT_EQ(OSType::Linux
, Translate("posix"));
175 // Note that the whole null-terminator embedded string is required for the
177 EXPECT_EQ(OSType::Unknown
, Translate("wind"));
178 EXPECT_EQ(OSType::Unknown
, Translate("Windows"));
179 EXPECT_EQ(OSType::Unknown
, Translate(""));
182 TEST(StringSwitchTest
, CasesLower
) {
183 enum class OSType
{ Windows
, Linux
, Unknown
};
185 auto Translate
= [](StringRef S
) {
186 return llvm::StringSwitch
<OSType
>(S
)
187 .CasesLower(StringLiteral::withInnerNUL("wind\0ws"), "win32", "winnt",
189 .CasesLower("linux", "unix", "*nix", "posix", OSType::Linux
)
190 .Default(OSType::Unknown
);
193 EXPECT_EQ(OSType::Windows
, Translate(llvm::StringRef("WIND\0WS", 7)));
194 EXPECT_EQ(OSType::Windows
, Translate("WIN32"));
195 EXPECT_EQ(OSType::Windows
, Translate("WINNT"));
197 EXPECT_EQ(OSType::Linux
, Translate("LINUX"));
198 EXPECT_EQ(OSType::Linux
, Translate("UNIX"));
199 EXPECT_EQ(OSType::Linux
, Translate("*NIX"));
200 EXPECT_EQ(OSType::Linux
, Translate("POSIX"));
202 EXPECT_EQ(OSType::Windows
, Translate(llvm::StringRef("wind\0ws", 7)));
203 EXPECT_EQ(OSType::Linux
, Translate("linux"));
205 EXPECT_EQ(OSType::Unknown
, Translate("wind"));
206 EXPECT_EQ(OSType::Unknown
, Translate(""));