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