blob: ca3e023a7ad38f074336d6ba0b91263915625d2f [file] [log] [blame]
Guillaume Chatelet3cc8f312020-10-12 08:55:20 +00001// Copyright 2017 Google LLC
Guillaume Chatelet439d3712018-02-01 10:03:09 +01002//
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
19namespace cpu_features {
20
21bool operator==(const StringView& a, const StringView& b) {
Arvid Gerstmannfd483902018-05-03 17:07:07 +020022 return CpuFeatures_StringView_IsEquals(a, b);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010023}
24
25namespace {
26
27TEST(StringViewTest, Empty) {
28 EXPECT_EQ(kEmptyStringView.ptr, nullptr);
29 EXPECT_EQ(kEmptyStringView.size, 0);
30}
31
32TEST(StringViewTest, Build) {
33 const auto view = str("test");
34 EXPECT_EQ(view.ptr[0], 't');
35 EXPECT_EQ(view.size, 4);
36}
37
Arvid Gerstmannfd483902018-05-03 17:07:07 +020038TEST(StringViewTest, CpuFeatures_StringView_IndexOfChar) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +010039 // Found.
Arvid Gerstmannfd483902018-05-03 17:07:07 +020040 EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 'e'), 1);
Henry Lee9e03e132020-09-21 17:39:58 +100041 EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 't'), 0);
42 EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("beef"), 'e'), 1);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010043 // Not found.
Arvid Gerstmannfd483902018-05-03 17:07:07 +020044 EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 'z'), -1);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010045 // Empty.
Arvid Gerstmannfd483902018-05-03 17:07:07 +020046 EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(kEmptyStringView, 'z'), -1);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010047}
48
Arvid Gerstmannfd483902018-05-03 17:07:07 +020049TEST(StringViewTest, CpuFeatures_StringView_IndexOf) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +010050 // Found.
Arvid Gerstmannfd483902018-05-03 17:07:07 +020051 EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("es")), 1);
Henry Lee9e03e132020-09-21 17:39:58 +100052 EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("test")), 0);
53 EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("tesstest"), str("test")), 4);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010054 // Not found.
Arvid Gerstmannfd483902018-05-03 17:07:07 +020055 EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("aa")), -1);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010056 // Empty.
Arvid Gerstmannfd483902018-05-03 17:07:07 +020057 EXPECT_EQ(CpuFeatures_StringView_IndexOf(kEmptyStringView, str("aa")), -1);
58 EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("aa"), kEmptyStringView), -1);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010059}
60
Arvid Gerstmannfd483902018-05-03 17:07:07 +020061TEST(StringViewTest, CpuFeatures_StringView_StartsWith) {
62 EXPECT_TRUE(CpuFeatures_StringView_StartsWith(str("test"), str("te")));
Henry Lee9e03e132020-09-21 17:39:58 +100063 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")));
Arvid Gerstmannfd483902018-05-03 17:07:07 +020066 EXPECT_FALSE(CpuFeatures_StringView_StartsWith(str("test"), str("")));
Arvid Gerstmannd9689912018-05-04 09:32:17 +020067 EXPECT_FALSE(
68 CpuFeatures_StringView_StartsWith(str("test"), kEmptyStringView));
69 EXPECT_FALSE(
70 CpuFeatures_StringView_StartsWith(kEmptyStringView, str("test")));
Guillaume Chatelet439d3712018-02-01 10:03:09 +010071}
72
Arvid Gerstmannfd483902018-05-03 17:07:07 +020073TEST(StringViewTest, CpuFeatures_StringView_IsEquals) {
Arvid Gerstmannd9689912018-05-04 09:32:17 +020074 EXPECT_TRUE(
75 CpuFeatures_StringView_IsEquals(kEmptyStringView, kEmptyStringView));
Arvid Gerstmannfd483902018-05-03 17:07:07 +020076 EXPECT_TRUE(CpuFeatures_StringView_IsEquals(kEmptyStringView, str("")));
77 EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str(""), kEmptyStringView));
Henry Lee9e03e132020-09-21 17:39:58 +100078 EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str("test"), str("test")));
Arvid Gerstmannfd483902018-05-03 17:07:07 +020079 EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str("a"), str("a")));
80 EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), str("b")));
Henry Lee9e03e132020-09-21 17:39:58 +100081 EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("aa"), str("a")));
82 EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), str("aa")));
Arvid Gerstmannfd483902018-05-03 17:07:07 +020083 EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), kEmptyStringView));
84 EXPECT_FALSE(CpuFeatures_StringView_IsEquals(kEmptyStringView, str("a")));
Guillaume Chatelet439d3712018-02-01 10:03:09 +010085}
86
Arvid Gerstmannfd483902018-05-03 17:07:07 +020087TEST(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(""));
Guillaume Chatelet439d3712018-02-01 10:03:09 +010092}
93
Henry Lee9e03e132020-09-21 17:39:58 +100094TEST(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
101TEST(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
108TEST(StringViewTest, CpuFeatures_StringView_Front) {
109 EXPECT_EQ(CpuFeatures_StringView_Front(str("apple")), 'a');
110 EXPECT_EQ(CpuFeatures_StringView_Front(str("a")), 'a');
111}
112
113TEST(StringViewTest, CpuFeatures_StringView_Back) {
114 EXPECT_EQ(CpuFeatures_StringView_Back(str("apple")), 'e');
115 EXPECT_EQ(CpuFeatures_StringView_Back(str("a")), 'a');
116}
117
118TEST(StringViewTest, CpuFeatures_StringView_TrimWhitespace) {
119 EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str(" first middle last ")),
Guillaume Chatelet22a53622020-09-23 11:52:20 +0200120 str("first middle last"));
Henry Lee9e03e132020-09-21 17:39:58 +1000121 EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str("first middle last ")),
Guillaume Chatelet22a53622020-09-23 11:52:20 +0200122 str("first middle last"));
Henry Lee9e03e132020-09-21 17:39:58 +1000123 EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str(" first middle last")),
Guillaume Chatelet22a53622020-09-23 11:52:20 +0200124 str("first middle last"));
Henry Lee9e03e132020-09-21 17:39:58 +1000125 EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str("first middle last")),
Guillaume Chatelet22a53622020-09-23 11:52:20 +0200126 str("first middle last"));
Henry Lee9e03e132020-09-21 17:39:58 +1000127}
128
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200129TEST(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);
Henry Lee9e03e132020-09-21 17:39:58 +1000133 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("0x2A2a")), 10794);
134 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("0x2a2A")), 10794);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100135
Henry Lee9e03e132020-09-21 17:39:58 +1000136 EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("-10")), -1);
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200137 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);
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100140}
141
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200142TEST(StringViewTest, CpuFeatures_StringView_CopyString) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100143 char buf[4];
144 buf[0] = 'X';
145
146 // Empty
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200147 CpuFeatures_StringView_CopyString(str(""), buf, sizeof(buf));
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100148 EXPECT_STREQ(buf, "");
149
150 // Less
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200151 CpuFeatures_StringView_CopyString(str("a"), buf, sizeof(buf));
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100152 EXPECT_STREQ(buf, "a");
153
154 // exact
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200155 CpuFeatures_StringView_CopyString(str("abc"), buf, sizeof(buf));
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100156 EXPECT_STREQ(buf, "abc");
157
158 // More
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200159 CpuFeatures_StringView_CopyString(str("abcd"), buf, sizeof(buf));
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100160 EXPECT_STREQ(buf, "abc");
161}
162
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200163TEST(StringViewTest, CpuFeatures_StringView_HasWord) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100164 // Find flags at beginning, middle and end.
Arvid Gerstmannd9689912018-05-04 09:32:17 +0200165 EXPECT_TRUE(
166 CpuFeatures_StringView_HasWord(str("first middle last"), "first"));
167 EXPECT_TRUE(
168 CpuFeatures_StringView_HasWord(str("first middle last"), "middle"));
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200169 EXPECT_TRUE(CpuFeatures_StringView_HasWord(str("first middle last"), "last"));
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100170 // Do not match partial flags
Arvid Gerstmannd9689912018-05-04 09:32:17 +0200171 EXPECT_FALSE(
172 CpuFeatures_StringView_HasWord(str("first middle last"), "irst"));
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200173 EXPECT_FALSE(CpuFeatures_StringView_HasWord(str("first middle last"), "mid"));
174 EXPECT_FALSE(CpuFeatures_StringView_HasWord(str("first middle last"), "las"));
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100175}
176
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200177TEST(StringViewTest, CpuFeatures_StringView_GetAttributeKeyValue) {
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100178 const StringView line = str(" key : first middle last ");
179 StringView key, value;
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200180 EXPECT_TRUE(CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value));
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100181 EXPECT_EQ(key, str("key"));
182 EXPECT_EQ(value, str("first middle last"));
183}
184
185TEST(StringViewTest, FailingGetAttributeKeyValue) {
186 const StringView line = str("key first middle last");
187 StringView key, value;
Arvid Gerstmannfd483902018-05-03 17:07:07 +0200188 EXPECT_FALSE(CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value));
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100189}
190
191} // namespace
192} // namespace cpu_features