• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pw_unit_test/framework.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(), key.end());
39 }
40 
TEST(Key,ConstructorString)41 TEST(Key, ConstructorString) {
42   std::string str{kTestStringView};
43   Key key{str};
44   EXPECT_EQ(key.size(), kTestStringView.size());
45   EXPECT_FALSE(key.empty());
46   EXPECT_EQ(key.data(), str.data());
47   EXPECT_EQ(key.front(), kTestStringView.front());
48   EXPECT_EQ(key.back(), kTestStringView.back());
49 }
50 
TEST(Key,ConstructorStringView)51 TEST(Key, ConstructorStringView) {
52   Key key{kTestStringView};
53   EXPECT_EQ(key.size(), kTestStringView.size());
54   EXPECT_FALSE(key.empty());
55   EXPECT_EQ(key.data(), kTestStringView.data());
56   EXPECT_EQ(key.front(), kTestStringView.front());
57   EXPECT_EQ(key.back(), kTestStringView.back());
58 }
59 
TEST(Key,ConstructorNullTermString)60 TEST(Key, ConstructorNullTermString) {
61   Key key{kTestString};
62   EXPECT_EQ(key.size(), kTestStringView.size());
63   EXPECT_FALSE(key.empty());
64   EXPECT_EQ(key.data(), kTestString);
65   EXPECT_EQ(key.front(), kTestStringView.front());
66   EXPECT_EQ(key.back(), kTestStringView.back());
67 }
68 
TEST(Key,ConstructorCharPtrLength)69 TEST(Key, ConstructorCharPtrLength) {
70   Key key{kTestString, kTestStringView.size() + 1};  // include null terminator
71   EXPECT_EQ(key.size(), kTestStringView.size() + 1);
72   EXPECT_FALSE(key.empty());
73   EXPECT_EQ(key.data(), kTestStringView.data());
74   EXPECT_EQ(key.front(), kTestStringView.front());
75   EXPECT_EQ(key.back(), '\0');
76 }
77 
TEST(Key,ConstructorCopy)78 TEST(Key, ConstructorCopy) {
79   Key key1{kTestString};
80   Key key{key1};
81   EXPECT_EQ(key.size(), kTestStringView.size());
82   EXPECT_FALSE(key.empty());
83   EXPECT_EQ(key.data(), kTestStringView.data());
84   EXPECT_EQ(key.front(), kTestStringView.front());
85   EXPECT_EQ(key.back(), kTestStringView.back());
86 }
87 
TEST(Key,Access)88 TEST(Key, Access) {
89   Key key{kTestStringView};
90   for (size_t i = 0; i < key.size(); i++) {
91     EXPECT_EQ(key[i], kTestStringView[i]);
92     EXPECT_EQ(key.at(i), kTestStringView.at(i));
93   }
94 }
95 
TEST(Key,Iterator)96 TEST(Key, Iterator) {
97   size_t i = 0;
98   for (auto c : Key{kTestString}) {
99     EXPECT_EQ(c, kTestString[i++]);
100   }
101 }
102 
TEST(Key,Same)103 TEST(Key, Same) {
104   // Since start of two test strings are the same, verify those are equal.
105   EXPECT_TRUE((Key{kTestString} == Key{kTestString2, kTestStringView.size()}));
106   EXPECT_FALSE((Key{kTestString} != Key{kTestString2, kTestStringView.size()}));
107 }
108 
TEST(Key,Different)109 TEST(Key, Different) {
110   EXPECT_FALSE(Key{kTestString} == Key{kTestString2});
111   EXPECT_TRUE(Key{kTestString} != Key{kTestString2});
112 }
113 
TEST(Key,DifferentWithSameLength)114 TEST(Key, DifferentWithSameLength) {
115   // Start second test string offset by one.
116   EXPECT_FALSE(
117       (Key{kTestString} == Key{kTestString2 + 1, kTestStringView.size()}));
118   EXPECT_TRUE(
119       (Key{kTestString} != Key{kTestString2 + 1, kTestStringView.size()}));
120 }
121 
TEST(Key,ConvertToStringView)122 TEST(Key, ConvertToStringView) {
123   std::string_view view = Key{kTestString};
124   EXPECT_TRUE(view == kTestStringView);
125 }
126 
127 }  // namespace pw::kvs
128