aboutsummaryrefslogtreecommitdiff
path: root/pw_kvs/key_test.cc
blob: c09399f4af99dd5747ab314db104021405875968 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "pw_kvs/key.h"

#include <string_view>

#include "gtest/gtest.h"

namespace pw::kvs {

namespace {

constexpr const char kTestString[] = "test_string";
constexpr std::string_view kTestStringView{kTestString};

// kTestString2 starts with same string as kTestString.
constexpr const char kTestString2[] = "test_string2";

}  // namespace

TEST(Key, ConstructorEmpty) {
  Key key;
  EXPECT_EQ(key.size(), 0u);
  EXPECT_TRUE(key.empty());
  EXPECT_EQ(key.data(), nullptr);
  EXPECT_EQ(key.begin(), nullptr);
  EXPECT_EQ(key.end(), nullptr);
}

TEST(Key, ConstructorString) {
  std::string str{kTestStringView};
  Key key{str};
  EXPECT_EQ(key.size(), kTestStringView.size());
  EXPECT_FALSE(key.empty());
  EXPECT_EQ(key.data(), str.data());
  EXPECT_EQ(key.front(), kTestStringView.front());
  EXPECT_EQ(key.back(), kTestStringView.back());
}

TEST(Key, ConstructorStringView) {
  Key key{kTestStringView};
  EXPECT_EQ(key.size(), kTestStringView.size());
  EXPECT_FALSE(key.empty());
  EXPECT_EQ(key.data(), kTestStringView.data());
  EXPECT_EQ(key.front(), kTestStringView.front());
  EXPECT_EQ(key.back(), kTestStringView.back());
}

TEST(Key, ConstructorNullTermString) {
  Key key{kTestString};
  EXPECT_EQ(key.size(), kTestStringView.size());
  EXPECT_FALSE(key.empty());
  EXPECT_EQ(key.data(), kTestString);
  EXPECT_EQ(key.front(), kTestStringView.front());
  EXPECT_EQ(key.back(), kTestStringView.back());
}

TEST(Key, ConstructorCharPtrLength) {
  Key key{kTestString, kTestStringView.size() + 1};  // include null terminator
  EXPECT_EQ(key.size(), kTestStringView.size() + 1);
  EXPECT_FALSE(key.empty());
  EXPECT_EQ(key.data(), kTestStringView.data());
  EXPECT_EQ(key.front(), kTestStringView.front());
  EXPECT_EQ(key.back(), '\0');
}

TEST(Key, ConstructorCopy) {
  Key key1{kTestString};
  Key key{key1};
  EXPECT_EQ(key.size(), kTestStringView.size());
  EXPECT_FALSE(key.empty());
  EXPECT_EQ(key.data(), kTestStringView.data());
  EXPECT_EQ(key.front(), kTestStringView.front());
  EXPECT_EQ(key.back(), kTestStringView.back());
}

TEST(Key, Access) {
  Key key{kTestStringView};
  for (size_t i = 0; i < key.size(); i++) {
    EXPECT_EQ(key[i], kTestStringView[i]);
    EXPECT_EQ(key.at(i), kTestStringView.at(i));
  }
}

TEST(Key, Iterator) {
  size_t i = 0;
  for (auto c : Key{kTestString}) {
    EXPECT_EQ(c, kTestString[i++]);
  }
}

TEST(Key, Same) {
  // Since start of two test strings are the same, verify those are equal.
  EXPECT_TRUE((Key{kTestString} == Key{kTestString2, kTestStringView.size()}));
  EXPECT_FALSE((Key{kTestString} != Key{kTestString2, kTestStringView.size()}));
}

TEST(Key, Different) {
  EXPECT_FALSE(Key{kTestString} == Key{kTestString2});
  EXPECT_TRUE(Key{kTestString} != Key{kTestString2});
}

TEST(Key, DifferentWithSameLength) {
  // Start second test string offset by one.
  EXPECT_FALSE(
      (Key{kTestString} == Key{kTestString2 + 1, kTestStringView.size()}));
  EXPECT_TRUE(
      (Key{kTestString} != Key{kTestString2 + 1, kTestStringView.size()}));
}

TEST(Key, ConvertToStringView) {
  std::string_view view = Key{kTestString};
  EXPECT_TRUE(view == kTestStringView);
}

}  // namespace pw::kvs