-
Notifications
You must be signed in to change notification settings - Fork 444
Expand file tree
/
Copy pathbytes_test.cc
More file actions
133 lines (118 loc) · 3.6 KB
/
bytes_test.cc
File metadata and controls
133 lines (118 loc) · 3.6 KB
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
129
130
131
132
133
// Copyright 2025 Google LLC
//
// 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 "google/cloud/bigtable/bytes.h"
#include <gmock/gmock.h>
#include <cstdint>
#include <deque>
#include <limits>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
namespace google {
namespace cloud {
namespace bigtable {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {
TEST(Bytes, RoundTrip) {
char c = std::numeric_limits<char>::min();
std::string chars(1, c);
while (c != std::numeric_limits<char>::max()) {
chars.push_back(++c);
}
// Empty string.
std::string data;
Bytes bytes(data);
EXPECT_EQ(data, bytes.get<std::string>());
// All 1-char strings.
data.resize(1);
for (auto c : chars) {
data[0] = c;
Bytes bytes(data);
EXPECT_EQ(data, bytes.get<std::string>());
}
// All 2-char strings.
data.resize(2);
for (auto c0 : chars) {
data[0] = c0;
for (auto c1 : chars) {
data[1] = c1;
Bytes bytes(data);
EXPECT_EQ(data, bytes.get<std::string>());
}
}
// Some 3-char strings.
data.resize(3);
for (auto c0 : {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}) {
data[0] = c0;
for (auto c1 : chars) {
data[1] = c1;
for (auto c2 : chars) {
data[2] = c2;
Bytes bytes(data);
EXPECT_EQ(data, bytes.get<std::string>());
}
}
}
}
TEST(Bytes, RelationalOperators) {
std::string const s_plain = "The quick brown fox jumps over the lazy dog.";
std::deque<char> const d_plain(s_plain.begin(), s_plain.end());
std::vector<std::uint8_t> const v_plain(s_plain.begin(), s_plain.end());
auto s_bytes = Bytes(s_plain.begin(), s_plain.end());
auto d_bytes = Bytes(d_plain.begin(), d_plain.end());
auto v_bytes = Bytes(v_plain.begin(), v_plain.end());
EXPECT_EQ(s_bytes, d_bytes);
EXPECT_EQ(d_bytes, v_bytes);
EXPECT_EQ(v_bytes, s_bytes);
auto x_bytes = Bytes(s_plain + " How vexingly quick daft zebras jump!");
EXPECT_NE(x_bytes, s_bytes);
EXPECT_NE(x_bytes, d_bytes);
EXPECT_NE(x_bytes, v_bytes);
}
TEST(Bytes, OutputStream) {
struct TestCase {
Bytes bytes;
std::string expected;
};
std::vector<TestCase> test_cases = {
{Bytes(std::string("")), R"(B"")"},
{Bytes(std::string("foo")), R"(B"foo")"},
{Bytes(std::string("a\011B")), R"(B"a\011B")"},
{Bytes(std::string("a\377B")), R"(B"a\377B")"},
{Bytes(std::string("!@#$%^&*()-.")), R"(B"!@#$%^&*()-.")"},
{Bytes(std::string(3, '\0')), R"(B"\000\000\000")"},
{Bytes(""), R"(B"")"},
{Bytes("foo"), R"(B"foo")"},
};
for (auto const& tc : test_cases) {
std::ostringstream ss;
ss << tc.bytes;
EXPECT_EQ(ss.str(), tc.expected);
}
}
TEST(Bytes, OutputStreamEscapingCannotFail) {
using ByteType = unsigned char;
for (int i = 0; i < std::numeric_limits<ByteType>::max() + 1; ++i) {
auto const b = static_cast<ByteType>(i);
std::ostringstream ss;
ss << Bytes(std::array<ByteType, 1>{b});
EXPECT_NE(ss.str(), R"(B"\?")") << "i=" << i;
}
}
} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigtable
} // namespace cloud
} // namespace google