Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / __support / CPP / stringview_test.cpp
blob33eb8ab4765728af6a55c121d6e27a35ce7730a3
1 //===-- Unittests for string_view -----------------------------------------===//
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 "src/__support/CPP/string_view.h"
10 #include "test/UnitTest/Test.h"
12 using LIBC_NAMESPACE::cpp::string_view;
14 TEST(LlvmLibcStringViewTest, InitializeCheck) {
15 string_view v;
16 ASSERT_EQ(v.size(), size_t(0));
17 ASSERT_TRUE(v.data() == nullptr);
19 v = string_view("");
20 ASSERT_EQ(v.size(), size_t(0));
21 ASSERT_TRUE(v.data() != nullptr);
23 v = string_view("abc", 0);
24 ASSERT_EQ(v.size(), size_t(0));
25 ASSERT_TRUE(v.data() != nullptr);
27 v = string_view("123456789");
28 ASSERT_EQ(v.size(), size_t(9));
31 TEST(LlvmLibcStringViewTest, Equals) {
32 string_view v("abc");
33 ASSERT_EQ(v, string_view("abc"));
34 ASSERT_NE(v, string_view());
35 ASSERT_NE(v, string_view(""));
36 ASSERT_NE(v, string_view("123"));
37 ASSERT_NE(v, string_view("abd"));
38 ASSERT_NE(v, string_view("aaa"));
39 ASSERT_NE(v, string_view("abcde"));
42 TEST(LlvmLibcStringViewTest, startsWith) {
43 string_view v("abc");
44 ASSERT_TRUE(v.starts_with('a'));
45 ASSERT_TRUE(v.starts_with(string_view("a")));
46 ASSERT_TRUE(v.starts_with(string_view("ab")));
47 ASSERT_TRUE(v.starts_with(string_view("abc")));
48 ASSERT_TRUE(v.starts_with(string_view()));
49 ASSERT_TRUE(v.starts_with(string_view("")));
50 ASSERT_FALSE(v.starts_with('1'));
51 ASSERT_FALSE(v.starts_with(string_view("123")));
52 ASSERT_FALSE(v.starts_with(string_view("abd")));
53 ASSERT_FALSE(v.starts_with(string_view("aaa")));
54 ASSERT_FALSE(v.starts_with(string_view("abcde")));
57 TEST(LlvmLibcStringViewTest, endsWith) {
58 string_view v("abc");
59 ASSERT_TRUE(v.ends_with('c'));
60 ASSERT_TRUE(v.ends_with(string_view("c")));
61 ASSERT_TRUE(v.ends_with(string_view("bc")));
62 ASSERT_TRUE(v.ends_with(string_view("abc")));
63 ASSERT_TRUE(v.ends_with(string_view()));
64 ASSERT_TRUE(v.ends_with(string_view("")));
65 ASSERT_FALSE(v.ends_with('1'));
66 ASSERT_FALSE(v.ends_with(string_view("123")));
67 ASSERT_FALSE(v.ends_with(string_view("abd")));
68 ASSERT_FALSE(v.ends_with(string_view("aaa")));
69 ASSERT_FALSE(v.ends_with(string_view("abcde")));
72 TEST(LlvmLibcStringViewTest, RemovePrefix) {
73 string_view a("123456789");
74 a.remove_prefix(0);
75 ASSERT_EQ(a.size(), size_t(9));
76 ASSERT_TRUE(a == "123456789");
78 string_view b("123456789");
79 b.remove_prefix(4);
80 ASSERT_EQ(b.size(), size_t(5));
81 ASSERT_TRUE(b == "56789");
83 string_view c("123456789");
84 c.remove_prefix(9);
85 ASSERT_EQ(c.size(), size_t(0));
88 TEST(LlvmLibcStringViewTest, RemoveSuffix) {
89 string_view a("123456789");
90 a.remove_suffix(0);
91 ASSERT_EQ(a.size(), size_t(9));
92 ASSERT_TRUE(a == "123456789");
94 string_view b("123456789");
95 b.remove_suffix(4);
96 ASSERT_EQ(b.size(), size_t(5));
97 ASSERT_TRUE(b == "12345");
99 string_view c("123456789");
100 c.remove_suffix(9);
101 ASSERT_EQ(c.size(), size_t(0));
104 TEST(LlvmLibcStringViewTest, Observer) {
105 string_view ABC("abc");
106 ASSERT_EQ(ABC.size(), size_t(3));
107 ASSERT_FALSE(ABC.empty());
108 ASSERT_EQ(ABC.front(), 'a');
109 ASSERT_EQ(ABC.back(), 'c');
112 bool isDigit(char c) { return c >= '0' && c <= '9'; }
114 TEST(LlvmLibcStringViewTest, FindFirstOf) {
115 string_view Tmp("abca");
116 ASSERT_TRUE(Tmp.find_first_of('a') == 0);
117 ASSERT_TRUE(Tmp.find_first_of('d') == string_view::npos);
118 ASSERT_TRUE(Tmp.find_first_of('b') == 1);
119 ASSERT_TRUE(Tmp.find_first_of('a', 0) == 0);
120 ASSERT_TRUE(Tmp.find_first_of('b', 1) == 1);
121 ASSERT_TRUE(Tmp.find_first_of('a', 1) == 3);
122 ASSERT_TRUE(Tmp.find_first_of('a', 42) == string_view::npos);
123 ASSERT_FALSE(Tmp.find_first_of('c') == 1);
124 ASSERT_FALSE(Tmp.find_first_of('c', 0) == 1);
125 ASSERT_FALSE(Tmp.find_first_of('c', 1) == 1);
128 TEST(LlvmLibcStringViewTest, FindLastOf) {
129 string_view Tmp("abada");
131 ASSERT_EQ(Tmp.find_last_of('a'), size_t(4));
132 ASSERT_EQ(Tmp.find_last_of('a', 123), size_t(4));
133 ASSERT_EQ(Tmp.find_last_of('a', 5), size_t(4));
134 ASSERT_EQ(Tmp.find_last_of('a', 4), size_t(4));
135 ASSERT_EQ(Tmp.find_last_of('a', 3), size_t(2));
136 ASSERT_EQ(Tmp.find_last_of('a', 2), size_t(2));
137 ASSERT_EQ(Tmp.find_last_of('a', 1), size_t(0));
138 ASSERT_EQ(Tmp.find_last_of('a', 0), size_t(0));
140 ASSERT_EQ(Tmp.find_last_of('b'), size_t(1));
141 ASSERT_EQ(Tmp.find_last_of('b', 123), size_t(1));
142 ASSERT_EQ(Tmp.find_last_of('b', 5), size_t(1));
143 ASSERT_EQ(Tmp.find_last_of('b', 4), size_t(1));
144 ASSERT_EQ(Tmp.find_last_of('b', 3), size_t(1));
145 ASSERT_EQ(Tmp.find_last_of('b', 2), size_t(1));
146 ASSERT_EQ(Tmp.find_last_of('b', 1), size_t(1));
147 ASSERT_EQ(Tmp.find_last_of('b', 0), string_view::npos);
149 ASSERT_EQ(Tmp.find_last_of('d'), size_t(3));
150 ASSERT_EQ(Tmp.find_last_of('d', 123), size_t(3));
151 ASSERT_EQ(Tmp.find_last_of('d', 5), size_t(3));
152 ASSERT_EQ(Tmp.find_last_of('d', 4), size_t(3));
153 ASSERT_EQ(Tmp.find_last_of('d', 3), size_t(3));
154 ASSERT_EQ(Tmp.find_last_of('d', 2), string_view::npos);
155 ASSERT_EQ(Tmp.find_last_of('d', 1), string_view::npos);
156 ASSERT_EQ(Tmp.find_last_of('d', 0), string_view::npos);
158 ASSERT_EQ(Tmp.find_last_of('e'), string_view::npos);
159 ASSERT_EQ(Tmp.find_last_of('e', 123), string_view::npos);
160 ASSERT_EQ(Tmp.find_last_of('e', 5), string_view::npos);
161 ASSERT_EQ(Tmp.find_last_of('e', 4), string_view::npos);
162 ASSERT_EQ(Tmp.find_last_of('e', 3), string_view::npos);
163 ASSERT_EQ(Tmp.find_last_of('e', 2), string_view::npos);
164 ASSERT_EQ(Tmp.find_last_of('e', 1), string_view::npos);
165 ASSERT_EQ(Tmp.find_last_of('e', 0), string_view::npos);
167 string_view Empty;
168 ASSERT_EQ(Empty.find_last_of('a'), string_view::npos);
169 ASSERT_EQ(Empty.find_last_of('a', 0), string_view::npos);
170 ASSERT_EQ(Empty.find_last_of('a', 123), string_view::npos);
172 string_view Empty1("");
173 ASSERT_EQ(Empty1.find_last_of('a'), string_view::npos);
174 ASSERT_EQ(Empty1.find_last_of('a', 0), string_view::npos);
175 ASSERT_EQ(Empty1.find_last_of('a', 123), string_view::npos);