Guillaume Chatelet | 3cc8f31 | 2020-10-12 08:55:20 +0000 | [diff] [blame] | 1 | // Copyright 2017 Google LLC |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 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 | |
| 21 | bool operator==(const StringView& a, const StringView& b) { |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 22 | return CpuFeatures_StringView_IsEquals(a, b); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | TEST(StringViewTest, Empty) { |
| 28 | EXPECT_EQ(kEmptyStringView.ptr, nullptr); |
| 29 | EXPECT_EQ(kEmptyStringView.size, 0); |
| 30 | } |
| 31 | |
| 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 | |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 38 | TEST(StringViewTest, CpuFeatures_StringView_IndexOfChar) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 39 | // Found. |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 40 | EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 'e'), 1); |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 41 | EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 't'), 0); |
| 42 | EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("beef"), 'e'), 1); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 43 | // Not found. |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 44 | EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 'z'), -1); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 45 | // Empty. |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 46 | EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(kEmptyStringView, 'z'), -1); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 49 | TEST(StringViewTest, CpuFeatures_StringView_IndexOf) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 50 | // Found. |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 51 | EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("es")), 1); |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 52 | EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("test")), 0); |
| 53 | EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("tesstest"), str("test")), 4); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 54 | // Not found. |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 55 | EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("aa")), -1); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 56 | // Empty. |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 57 | EXPECT_EQ(CpuFeatures_StringView_IndexOf(kEmptyStringView, str("aa")), -1); |
| 58 | EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("aa"), kEmptyStringView), -1); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 61 | TEST(StringViewTest, CpuFeatures_StringView_StartsWith) { |
| 62 | EXPECT_TRUE(CpuFeatures_StringView_StartsWith(str("test"), str("te"))); |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 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"))); |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 66 | EXPECT_FALSE(CpuFeatures_StringView_StartsWith(str("test"), str(""))); |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 67 | EXPECT_FALSE( |
| 68 | CpuFeatures_StringView_StartsWith(str("test"), kEmptyStringView)); |
| 69 | EXPECT_FALSE( |
| 70 | CpuFeatures_StringView_StartsWith(kEmptyStringView, str("test"))); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 73 | TEST(StringViewTest, CpuFeatures_StringView_IsEquals) { |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 74 | EXPECT_TRUE( |
| 75 | CpuFeatures_StringView_IsEquals(kEmptyStringView, kEmptyStringView)); |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 76 | EXPECT_TRUE(CpuFeatures_StringView_IsEquals(kEmptyStringView, str(""))); |
| 77 | EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str(""), kEmptyStringView)); |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 78 | EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str("test"), str("test"))); |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 79 | EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str("a"), str("a"))); |
| 80 | EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), str("b"))); |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 81 | EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("aa"), str("a"))); |
| 82 | EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), str("aa"))); |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 83 | EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), kEmptyStringView)); |
| 84 | EXPECT_FALSE(CpuFeatures_StringView_IsEquals(kEmptyStringView, str("a"))); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 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("")); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | |
| 118 | TEST(StringViewTest, CpuFeatures_StringView_TrimWhitespace) { |
| 119 | EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str(" first middle last ")), |
Guillaume Chatelet | 22a5362 | 2020-09-23 11:52:20 +0200 | [diff] [blame] | 120 | str("first middle last")); |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 121 | EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str("first middle last ")), |
Guillaume Chatelet | 22a5362 | 2020-09-23 11:52:20 +0200 | [diff] [blame] | 122 | str("first middle last")); |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 123 | EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str(" first middle last")), |
Guillaume Chatelet | 22a5362 | 2020-09-23 11:52:20 +0200 | [diff] [blame] | 124 | str("first middle last")); |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 125 | EXPECT_EQ(CpuFeatures_StringView_TrimWhitespace(str("first middle last")), |
Guillaume Chatelet | 22a5362 | 2020-09-23 11:52:20 +0200 | [diff] [blame] | 126 | str("first middle last")); |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 127 | } |
| 128 | |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 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); |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 133 | EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("0x2A2a")), 10794); |
| 134 | EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("0x2a2A")), 10794); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 135 | |
Henry Lee | 9e03e13 | 2020-09-21 17:39:58 +1000 | [diff] [blame] | 136 | EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("-10")), -1); |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 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); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 142 | TEST(StringViewTest, CpuFeatures_StringView_CopyString) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 143 | char buf[4]; |
| 144 | buf[0] = 'X'; |
| 145 | |
| 146 | // Empty |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 147 | CpuFeatures_StringView_CopyString(str(""), buf, sizeof(buf)); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 148 | EXPECT_STREQ(buf, ""); |
| 149 | |
| 150 | // Less |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 151 | CpuFeatures_StringView_CopyString(str("a"), buf, sizeof(buf)); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 152 | EXPECT_STREQ(buf, "a"); |
| 153 | |
| 154 | // exact |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 155 | CpuFeatures_StringView_CopyString(str("abc"), buf, sizeof(buf)); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 156 | EXPECT_STREQ(buf, "abc"); |
| 157 | |
| 158 | // More |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 159 | CpuFeatures_StringView_CopyString(str("abcd"), buf, sizeof(buf)); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 160 | EXPECT_STREQ(buf, "abc"); |
| 161 | } |
| 162 | |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 163 | TEST(StringViewTest, CpuFeatures_StringView_HasWord) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 164 | // Find flags at beginning, middle and end. |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 165 | EXPECT_TRUE( |
| 166 | CpuFeatures_StringView_HasWord(str("first middle last"), "first")); |
| 167 | EXPECT_TRUE( |
| 168 | CpuFeatures_StringView_HasWord(str("first middle last"), "middle")); |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 169 | EXPECT_TRUE(CpuFeatures_StringView_HasWord(str("first middle last"), "last")); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 170 | // Do not match partial flags |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 171 | EXPECT_FALSE( |
| 172 | CpuFeatures_StringView_HasWord(str("first middle last"), "irst")); |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 173 | EXPECT_FALSE(CpuFeatures_StringView_HasWord(str("first middle last"), "mid")); |
| 174 | EXPECT_FALSE(CpuFeatures_StringView_HasWord(str("first middle last"), "las")); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 177 | TEST(StringViewTest, CpuFeatures_StringView_GetAttributeKeyValue) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 178 | const StringView line = str(" key : first middle last "); |
| 179 | StringView key, value; |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 180 | EXPECT_TRUE(CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 181 | EXPECT_EQ(key, str("key")); |
| 182 | EXPECT_EQ(value, str("first middle last")); |
| 183 | } |
| 184 | |
| 185 | TEST(StringViewTest, FailingGetAttributeKeyValue) { |
| 186 | const StringView line = str("key first middle last"); |
| 187 | StringView key, value; |
Arvid Gerstmann | fd48390 | 2018-05-03 17:07:07 +0200 | [diff] [blame] | 188 | EXPECT_FALSE(CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | } // namespace |
| 192 | } // namespace cpu_features |