[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / unittests / ADT / StringSwitchTest.cpp
blob2ce6cdca8d36a3117e6f8527c833c0627b9079a3
1 //===- llvm/unittest/ADT/StringSwitchTest.cpp - StringSwitch unit tests ---===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/StringSwitch.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
14 TEST(StringSwitchTest, Case) {
15 auto Translate = [](StringRef S) {
16 return llvm::StringSwitch<int>(S)
17 .Case("0", 0)
18 .Case("1", 1)
19 .Case("2", 2)
20 .Case("3", 3)
21 .Case("4", 4)
22 .Case("5", 5)
23 .Case("6", 6)
24 .Case("7", 7)
25 .Case("8", 8)
26 .Case("9", 9)
27 .Case("A", 10)
28 .Case("B", 11)
29 .Case("C", 12)
30 .Case("D", 13)
31 .Case("E", 14)
32 .Case("F", 15)
33 .Default(-1);
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)
46 .Case("0", 0)
47 .Case("1", 1)
48 .Case("2", 2)
49 .Case("3", 3)
50 .Case("4", 4)
51 .Case("5", 5)
52 .Case("6", 6)
53 .Case("7", 7)
54 .Case("8", 8)
55 .Case("9", 9)
56 .CaseLower("A", 10)
57 .CaseLower("B", 11)
58 .CaseLower("C", 12)
59 .CaseLower("D", 13)
60 .CaseLower("E", 14)
61 .CaseLower("F", 15)
62 .Default(-1);
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",
161 OSType::Windows)
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
176 // case to match.
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",
188 OSType::Windows)
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(""));