1 // Copyright 2017 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "internal/string_view.h"
16
17 #include "gtest/gtest.h"
18
19 namespace cpu_features {
20
operator ==(const StringView & a,const StringView & b)21 bool operator==(const StringView& a, const StringView& b) {
22 return CpuFeatures_StringView_IsEquals(a, b);
23 }
24
25 namespace {
26
TEST(StringViewTest,Empty)27 TEST(StringViewTest, Empty) {
28 EXPECT_EQ(kEmptyStringView.ptr, nullptr);
29 EXPECT_EQ(kEmptyStringView.size, 0);
30 }
31
TEST(StringViewTest,Build)32 TEST(StringViewTest, Build) {
33 const auto view = str("test");
34 EXPECT_EQ(view.ptr[0], 't');
35 EXPECT_EQ(view.size, 4);
36 }
37
TEST(StringViewTest,CpuFeatures_StringView_IndexOfChar)38 TEST(StringViewTest, CpuFeatures_StringView_IndexOfChar) {
39 // Found.
40 EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 'e'), 1);
41 EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 't'), 0);
42 EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("beef"), 'e'), 1);
43 // Not found.
44 EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 'z'), -1);
45 // Empty.
46 EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(kEmptyStringView, 'z'), -1);
47 }
48
TEST(StringViewTest,CpuFeatures_StringView_IndexOf)49 TEST(StringViewTest, CpuFeatures_StringView_IndexOf) {
50 // Found.
51 EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("es")), 1);
52 EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("test")), 0);
53 EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("tesstest"), str("test")), 4);
54 // Not found.
55 EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("aa")), -1);
56 // Empty.
57 EXPECT_EQ(CpuFeatures_StringView_IndexOf(kEmptyStringView, str("aa")), -1);
58 EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("aa"), kEmptyStringView), -1);
59 }
60
TEST(StringViewTest,CpuFeatures_StringView_StartsWith)61 TEST(StringViewTest, CpuFeatures_StringView_StartsWith) {
62 EXPECT_TRUE(CpuFeatures_StringView_StartsWith(str("test"), str("te")));
63 EXPECT_TRUE(CpuFeatures_StringView_StartsWith(str("test"), str("test")));
64 EXPECT_FALSE(CpuFeatures_StringView_StartsWith(str("test"), str("st")));
65 EXPECT_FALSE(CpuFeatures_StringView_StartsWith(str("test"), str("est")));
66 EXPECT_FALSE(CpuFeatures_StringView_StartsWith(str("test"), str("")));
67 EXPECT_FALSE(
68 CpuFeatures_StringView_StartsWith(str("test"), kEmptyStringView));
69 EXPECT_FALSE(
70 CpuFeatures_StringView_StartsWith(kEmptyStringView, str("test")));
71 }
72
TEST(StringViewTest,CpuFeatures_StringView_IsEquals)73 TEST(StringViewTest, CpuFeatures_StringView_IsEquals) {
74 EXPECT_TRUE(
75 CpuFeatures_StringView_IsEquals(kEmptyStringView, kEmptyStringView));
76 EXPECT_TRUE(CpuFeatures_StringView_IsEquals(kEmptyStringView, str("")));
77 EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str(""), kEmptyStringView));
78 EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str("test"), str("test")));
79 EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str("a"), str("a")));
80 EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), str("b")));
81 EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("aa"), str("a")));
82 EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), str("aa")));
83 EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), kEmptyStringView));
84 EXPECT_FALSE(CpuFeatures_StringView_IsEquals(kEmptyStringView, str("a")));
85 }
86
TEST(StringViewTest,CpuFeatures_StringView_PopFront)87 TEST(StringViewTest, CpuFeatures_StringView_PopFront) {
88 EXPECT_EQ(CpuFeatures_StringView_PopFront(str("test"), 2), str("st"));
89 EXPECT_EQ(CpuFeatures_StringView_PopFront(str("test"), 0), str("test"));
90 EXPECT_EQ(CpuFeatures_StringView_PopFront(str("test"), 4), str(""));
91 EXPECT_EQ(CpuFeatures_StringView_PopFront(str("test"), 100), str(""));
92 }
93
TEST(StringViewTest,CpuFeatures_StringView_PopBack)94 TEST(StringViewTest, CpuFeatures_StringView_PopBack) {
95 EXPECT_EQ(CpuFeatures_StringView_PopBack(str("test"), 2), str("te"));
96 EXPECT_EQ(CpuFeatures_StringView_PopBack(str("test"), 0), str("test"));
97 EXPECT_EQ(CpuFeatures_StringView_PopBack(str("test"), 4), str(""));
98 EXPECT_EQ(CpuFeatures_StringView_PopBack(str("test"), 100), str(""));
99 }
100
TEST(StringViewTest,CpuFeatures_StringView_KeepFront)101 TEST(StringViewTest, CpuFeatures_StringView_KeepFront) {
102 EXPECT_EQ(CpuFeatures_StringView_KeepFront(str("test"), 2), str("te"));
103 EXPECT_EQ(CpuFeatures_StringView_KeepFront(str("test"), 0), str(""));
104 EXPECT_EQ(CpuFeatures_StringView_KeepFront(str("test"), 4), str("test"));
105 EXPECT_EQ(CpuFeatures_StringView_KeepFront(str("test"), 6), str("test"));
106 }
107
TEST(StringViewTest,CpuFeatures_StringView_Front)108 TEST(StringViewTest, CpuFeatures_StringView_Front) {
109 EXPECT_EQ(CpuFeatures_StringView_Front(str("apple")), 'a');
110 EXPECT_EQ(CpuFeatures_StringView_Front(str("a")), 'a');
111 }
112
TEST(StringViewTest,CpuFeatures_StringView_Back)113 TEST(StringViewTest, CpuFeatures_StringView_Back) {
114 EXPECT_EQ(CpuFeatures_StringView_Back(str("apple")), 'e');
115 EXPECT_EQ(CpuFeatures_StringView_Back(str("a")), 'a');
116 }
117
TEST(StringViewTest,CpuFeatures_StringView_TrimWhitespace)118 TEST(StringViewTest, CpuFeatures_StringView_TrimWhitespace) {
119 EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str(" first middle last ")),
120 str("first middle last"));
121 EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str("first middle last ")),
122 str("first middle last"));
123 EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str(" first middle last")),
124 str("first middle last"));
125 EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str("first middle last")),
126 str("first middle last"));
127 }
128
TEST(StringViewTest,CpuFeatures_StringView_ParsePositiveNumber)129 TEST(StringViewTest, CpuFeatures_StringView_ParsePositiveNumber) {
130 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("42")), 42);
131 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("0x2a")), 42);
132 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("0x2A")), 42);
133 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("0x2A2a")), 10794);
134 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("0x2a2A")), 10794);
135
136 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("-10")), -1);
137 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("-0x2A")), -1);
138 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("abc")), -1);
139 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("")), -1);
140 }
141
TEST(StringViewTest,CpuFeatures_StringView_CopyString)142 TEST(StringViewTest, CpuFeatures_StringView_CopyString) {
143 char buf[4];
144 buf[0] = 'X';
145
146 // Empty
147 CpuFeatures_StringView_CopyString(str(""), buf, sizeof(buf));
148 EXPECT_STREQ(buf, "");
149
150 // Less
151 CpuFeatures_StringView_CopyString(str("a"), buf, sizeof(buf));
152 EXPECT_STREQ(buf, "a");
153
154 // exact
155 CpuFeatures_StringView_CopyString(str("abc"), buf, sizeof(buf));
156 EXPECT_STREQ(buf, "abc");
157
158 // More
159 CpuFeatures_StringView_CopyString(str("abcd"), buf, sizeof(buf));
160 EXPECT_STREQ(buf, "abc");
161 }
162
TEST(StringViewTest,CpuFeatures_StringView_HasWord)163 TEST(StringViewTest, CpuFeatures_StringView_HasWord) {
164 // Find flags at beginning, middle and end.
165 EXPECT_TRUE(
166 CpuFeatures_StringView_HasWord(str("first middle last"), "first", ' '));
167 EXPECT_TRUE(
168 CpuFeatures_StringView_HasWord(str("first middle last"), "middle", ' '));
169 EXPECT_TRUE(
170 CpuFeatures_StringView_HasWord(str("first middle last"), "last", ' '));
171 // Find flags at beginning, middle and end with a different separator
172 EXPECT_TRUE(
173 CpuFeatures_StringView_HasWord(str("first-middle-last"), "first", '-'));
174 EXPECT_TRUE(
175 CpuFeatures_StringView_HasWord(str("first-middle-last"), "middle", '-'));
176 EXPECT_TRUE(
177 CpuFeatures_StringView_HasWord(str("first-middle-last"), "last", '-'));
178 // Do not match partial flags
179 EXPECT_FALSE(
180 CpuFeatures_StringView_HasWord(str("first middle last"), "irst", ' '));
181 EXPECT_FALSE(
182 CpuFeatures_StringView_HasWord(str("first middle last"), "mid", ' '));
183 EXPECT_FALSE(
184 CpuFeatures_StringView_HasWord(str("first middle last"), "las", ' '));
185 }
186
TEST(StringViewTest,CpuFeatures_StringView_GetAttributeKeyValue)187 TEST(StringViewTest, CpuFeatures_StringView_GetAttributeKeyValue) {
188 const StringView line = str(" key : first middle last ");
189 StringView key, value;
190 EXPECT_TRUE(CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value));
191 EXPECT_EQ(key, str("key"));
192 EXPECT_EQ(value, str("first middle last"));
193 }
194
TEST(StringViewTest,FailingGetAttributeKeyValue)195 TEST(StringViewTest, FailingGetAttributeKeyValue) {
196 const StringView line = str("key first middle last");
197 StringView key, value;
198 EXPECT_FALSE(CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value));
199 }
200
201 } // namespace
202 } // namespace cpu_features
203