1 //===- llvm/unittest/ADT/StringSwitchTest.cpp - StringSwitch unit tests ---===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/StringSwitch.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm;
14
TEST(StringSwitchTest,Case)15 TEST(StringSwitchTest, Case) {
16 auto Translate = [](StringRef S) {
17 return llvm::StringSwitch<int>(S)
18 .Case("0", 0)
19 .Case("1", 1)
20 .Case("2", 2)
21 .Case("3", 3)
22 .Case("4", 4)
23 .Case("5", 5)
24 .Case("6", 6)
25 .Case("7", 7)
26 .Case("8", 8)
27 .Case("9", 9)
28 .Case("A", 10)
29 .Case("B", 11)
30 .Case("C", 12)
31 .Case("D", 13)
32 .Case("E", 14)
33 .Case("F", 15)
34 .Default(-1);
35 };
36 EXPECT_EQ(1, Translate("1"));
37 EXPECT_EQ(2, Translate("2"));
38 EXPECT_EQ(11, Translate("B"));
39 EXPECT_EQ(-1, Translate("b"));
40 EXPECT_EQ(-1, Translate(""));
41 EXPECT_EQ(-1, Translate("Test"));
42 }
43
TEST(StringSwitchTest,CaseLower)44 TEST(StringSwitchTest, CaseLower) {
45 auto Translate = [](StringRef S) {
46 return llvm::StringSwitch<int>(S)
47 .Case("0", 0)
48 .Case("1", 1)
49 .Case("2", 2)
50 .Case("3", 3)
51 .Case("4", 4)
52 .Case("5", 5)
53 .Case("6", 6)
54 .Case("7", 7)
55 .Case("8", 8)
56 .Case("9", 9)
57 .CaseLower("A", 10)
58 .CaseLower("B", 11)
59 .CaseLower("C", 12)
60 .CaseLower("D", 13)
61 .CaseLower("E", 14)
62 .CaseLower("F", 15)
63 .Default(-1);
64 };
65 EXPECT_EQ(1, Translate("1"));
66 EXPECT_EQ(2, Translate("2"));
67 EXPECT_EQ(11, Translate("B"));
68 EXPECT_EQ(11, Translate("b"));
69
70 EXPECT_EQ(-1, Translate(""));
71 EXPECT_EQ(-1, Translate("Test"));
72 }
73
TEST(StringSwitchTest,StartsWith)74 TEST(StringSwitchTest, StartsWith) {
75 auto Translate = [](StringRef S) {
76 return llvm::StringSwitch<std::function<int(int, int)>>(S)
77 .StartsWith("add", [](int X, int Y) { return X + Y; })
78 .StartsWith("sub", [](int X, int Y) { return X - Y; })
79 .StartsWith("mul", [](int X, int Y) { return X * Y; })
80 .StartsWith("div", [](int X, int Y) { return X / Y; })
81 .Default([](int X, int Y) { return 0; });
82 };
83
84 EXPECT_EQ(15, Translate("adder")(10, 5));
85 EXPECT_EQ(5, Translate("subtracter")(10, 5));
86 EXPECT_EQ(50, Translate("multiplier")(10, 5));
87 EXPECT_EQ(2, Translate("divider")(10, 5));
88
89 EXPECT_EQ(0, Translate("nothing")(10, 5));
90 EXPECT_EQ(0, Translate("ADDER")(10, 5));
91 }
92
TEST(StringSwitchTest,StartsWithLower)93 TEST(StringSwitchTest, StartsWithLower) {
94 auto Translate = [](StringRef S) {
95 return llvm::StringSwitch<std::function<int(int, int)>>(S)
96 .StartsWithLower("add", [](int X, int Y) { return X + Y; })
97 .StartsWithLower("sub", [](int X, int Y) { return X - Y; })
98 .StartsWithLower("mul", [](int X, int Y) { return X * Y; })
99 .StartsWithLower("div", [](int X, int Y) { return X / Y; })
100 .Default([](int X, int Y) { return 0; });
101 };
102
103 EXPECT_EQ(15, Translate("adder")(10, 5));
104 EXPECT_EQ(5, Translate("subtracter")(10, 5));
105 EXPECT_EQ(50, Translate("multiplier")(10, 5));
106 EXPECT_EQ(2, Translate("divider")(10, 5));
107
108 EXPECT_EQ(15, Translate("AdDeR")(10, 5));
109 EXPECT_EQ(5, Translate("SuBtRaCtEr")(10, 5));
110 EXPECT_EQ(50, Translate("MuLtIpLiEr")(10, 5));
111 EXPECT_EQ(2, Translate("DiViDeR")(10, 5));
112
113 EXPECT_EQ(0, Translate("nothing")(10, 5));
114 }
115
TEST(StringSwitchTest,EndsWith)116 TEST(StringSwitchTest, EndsWith) {
117 enum class Suffix { Possible, PastTense, Process, InProgressAction, Unknown };
118
119 auto Translate = [](StringRef S) {
120 return llvm::StringSwitch<Suffix>(S)
121 .EndsWith("able", Suffix::Possible)
122 .EndsWith("ed", Suffix::PastTense)
123 .EndsWith("ation", Suffix::Process)
124 .EndsWith("ing", Suffix::InProgressAction)
125 .Default(Suffix::Unknown);
126 };
127
128 EXPECT_EQ(Suffix::Possible, Translate("optimizable"));
129 EXPECT_EQ(Suffix::PastTense, Translate("optimized"));
130 EXPECT_EQ(Suffix::Process, Translate("optimization"));
131 EXPECT_EQ(Suffix::InProgressAction, Translate("optimizing"));
132 EXPECT_EQ(Suffix::Unknown, Translate("optimizer"));
133 EXPECT_EQ(Suffix::Unknown, Translate("OPTIMIZABLE"));
134 }
135
TEST(StringSwitchTest,EndsWithLower)136 TEST(StringSwitchTest, EndsWithLower) {
137 enum class Suffix { Possible, PastTense, Process, InProgressAction, Unknown };
138
139 auto Translate = [](StringRef S) {
140 return llvm::StringSwitch<Suffix>(S)
141 .EndsWithLower("able", Suffix::Possible)
142 .EndsWithLower("ed", Suffix::PastTense)
143 .EndsWithLower("ation", Suffix::Process)
144 .EndsWithLower("ing", Suffix::InProgressAction)
145 .Default(Suffix::Unknown);
146 };
147
148 EXPECT_EQ(Suffix::Possible, Translate("optimizable"));
149 EXPECT_EQ(Suffix::Possible, Translate("OPTIMIZABLE"));
150 EXPECT_EQ(Suffix::PastTense, Translate("optimized"));
151 EXPECT_EQ(Suffix::Process, Translate("optimization"));
152 EXPECT_EQ(Suffix::InProgressAction, Translate("optimizing"));
153 EXPECT_EQ(Suffix::Unknown, Translate("optimizer"));
154 }
155
TEST(StringSwitchTest,Cases)156 TEST(StringSwitchTest, Cases) {
157 enum class OSType { Windows, Linux, Unknown };
158
159 auto Translate = [](StringRef S) {
160 return llvm::StringSwitch<OSType>(S)
161 .Cases(StringLiteral::withInnerNUL("wind\0ws"), "win32", "winnt",
162 OSType::Windows)
163 .Cases("linux", "unix", "*nix", "posix", OSType::Linux)
164 .Default(OSType::Unknown);
165 };
166
167 EXPECT_EQ(OSType::Windows, Translate(llvm::StringRef("wind\0ws", 7)));
168 EXPECT_EQ(OSType::Windows, Translate("win32"));
169 EXPECT_EQ(OSType::Windows, Translate("winnt"));
170
171 EXPECT_EQ(OSType::Linux, Translate("linux"));
172 EXPECT_EQ(OSType::Linux, Translate("unix"));
173 EXPECT_EQ(OSType::Linux, Translate("*nix"));
174 EXPECT_EQ(OSType::Linux, Translate("posix"));
175
176 // Note that the whole null-terminator embedded string is required for the
177 // case to match.
178 EXPECT_EQ(OSType::Unknown, Translate("wind"));
179 EXPECT_EQ(OSType::Unknown, Translate("Windows"));
180 EXPECT_EQ(OSType::Unknown, Translate(""));
181 }
182
TEST(StringSwitchTest,CasesLower)183 TEST(StringSwitchTest, CasesLower) {
184 enum class OSType { Windows, Linux, Unknown };
185
186 auto Translate = [](StringRef S) {
187 return llvm::StringSwitch<OSType>(S)
188 .CasesLower(StringLiteral::withInnerNUL("wind\0ws"), "win32", "winnt",
189 OSType::Windows)
190 .CasesLower("linux", "unix", "*nix", "posix", OSType::Linux)
191 .Default(OSType::Unknown);
192 };
193
194 EXPECT_EQ(OSType::Windows, Translate(llvm::StringRef("WIND\0WS", 7)));
195 EXPECT_EQ(OSType::Windows, Translate("WIN32"));
196 EXPECT_EQ(OSType::Windows, Translate("WINNT"));
197
198 EXPECT_EQ(OSType::Linux, Translate("LINUX"));
199 EXPECT_EQ(OSType::Linux, Translate("UNIX"));
200 EXPECT_EQ(OSType::Linux, Translate("*NIX"));
201 EXPECT_EQ(OSType::Linux, Translate("POSIX"));
202
203 EXPECT_EQ(OSType::Windows, Translate(llvm::StringRef("wind\0ws", 7)));
204 EXPECT_EQ(OSType::Linux, Translate("linux"));
205
206 EXPECT_EQ(OSType::Unknown, Translate("wind"));
207 EXPECT_EQ(OSType::Unknown, Translate(""));
208 }
209