1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_kvs/key.h"
16
17 #include <string_view>
18
19 #include "gtest/gtest.h"
20
21 namespace pw::kvs {
22
23 namespace {
24
25 constexpr const char kTestString[] = "test_string";
26 constexpr std::string_view kTestStringView{kTestString};
27
28 // kTestString2 starts with same string as kTestString.
29 constexpr const char kTestString2[] = "test_string2";
30
31 } // namespace
32
TEST(Key,ConstructorEmpty)33 TEST(Key, ConstructorEmpty) {
34 Key key;
35 EXPECT_EQ(key.size(), 0u);
36 EXPECT_TRUE(key.empty());
37 EXPECT_EQ(key.data(), nullptr);
38 EXPECT_EQ(key.begin(), nullptr);
39 EXPECT_EQ(key.end(), nullptr);
40 }
41
TEST(Key,ConstructorString)42 TEST(Key, ConstructorString) {
43 std::string str{kTestStringView};
44 Key key{str};
45 EXPECT_EQ(key.size(), kTestStringView.size());
46 EXPECT_FALSE(key.empty());
47 EXPECT_EQ(key.data(), str.data());
48 EXPECT_EQ(key.front(), kTestStringView.front());
49 EXPECT_EQ(key.back(), kTestStringView.back());
50 }
51
TEST(Key,ConstructorStringView)52 TEST(Key, ConstructorStringView) {
53 Key key{kTestStringView};
54 EXPECT_EQ(key.size(), kTestStringView.size());
55 EXPECT_FALSE(key.empty());
56 EXPECT_EQ(key.data(), kTestStringView.data());
57 EXPECT_EQ(key.front(), kTestStringView.front());
58 EXPECT_EQ(key.back(), kTestStringView.back());
59 }
60
TEST(Key,ConstructorNullTermString)61 TEST(Key, ConstructorNullTermString) {
62 Key key{kTestString};
63 EXPECT_EQ(key.size(), kTestStringView.size());
64 EXPECT_FALSE(key.empty());
65 EXPECT_EQ(key.data(), kTestString);
66 EXPECT_EQ(key.front(), kTestStringView.front());
67 EXPECT_EQ(key.back(), kTestStringView.back());
68 }
69
TEST(Key,ConstructorCharPtrLength)70 TEST(Key, ConstructorCharPtrLength) {
71 Key key{kTestString, kTestStringView.size() + 1}; // include null terminator
72 EXPECT_EQ(key.size(), kTestStringView.size() + 1);
73 EXPECT_FALSE(key.empty());
74 EXPECT_EQ(key.data(), kTestStringView.data());
75 EXPECT_EQ(key.front(), kTestStringView.front());
76 EXPECT_EQ(key.back(), '\0');
77 }
78
TEST(Key,ConstructorCopy)79 TEST(Key, ConstructorCopy) {
80 Key key1{kTestString};
81 Key key{key1};
82 EXPECT_EQ(key.size(), kTestStringView.size());
83 EXPECT_FALSE(key.empty());
84 EXPECT_EQ(key.data(), kTestStringView.data());
85 EXPECT_EQ(key.front(), kTestStringView.front());
86 EXPECT_EQ(key.back(), kTestStringView.back());
87 }
88
TEST(Key,Access)89 TEST(Key, Access) {
90 Key key{kTestStringView};
91 for (size_t i = 0; i < key.size(); i++) {
92 EXPECT_EQ(key[i], kTestStringView[i]);
93 EXPECT_EQ(key.at(i), kTestStringView.at(i));
94 }
95 }
96
TEST(Key,Iterator)97 TEST(Key, Iterator) {
98 size_t i = 0;
99 for (auto c : Key{kTestString}) {
100 EXPECT_EQ(c, kTestString[i++]);
101 }
102 }
103
TEST(Key,Same)104 TEST(Key, Same) {
105 // Since start of two test strings are the same, verify those are equal.
106 EXPECT_TRUE((Key{kTestString} == Key{kTestString2, kTestStringView.size()}));
107 EXPECT_FALSE((Key{kTestString} != Key{kTestString2, kTestStringView.size()}));
108 }
109
TEST(Key,Different)110 TEST(Key, Different) {
111 EXPECT_FALSE(Key{kTestString} == Key{kTestString2});
112 EXPECT_TRUE(Key{kTestString} != Key{kTestString2});
113 }
114
TEST(Key,DifferentWithSameLength)115 TEST(Key, DifferentWithSameLength) {
116 // Start second test string offset by one.
117 EXPECT_FALSE(
118 (Key{kTestString} == Key{kTestString2 + 1, kTestStringView.size()}));
119 EXPECT_TRUE(
120 (Key{kTestString} != Key{kTestString2 + 1, kTestStringView.size()}));
121 }
122
TEST(Key,ConvertToStringView)123 TEST(Key, ConvertToStringView) {
124 std::string_view view = Key{kTestString};
125 EXPECT_TRUE(view == kTestStringView);
126 }
127
128 } // namespace pw::kvs
129