|
| 1 | +// Copyright 2025 Google LLC |
| 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 | +// 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, |
| 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 "google/cloud/bigtable/bytes.h" |
| 16 | +#include <gmock/gmock.h> |
| 17 | +#include <cstdint> |
| 18 | +#include <deque> |
| 19 | +#include <limits> |
| 20 | +#include <sstream> |
| 21 | +#include <string> |
| 22 | +#include <utility> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace google { |
| 26 | +namespace cloud { |
| 27 | +namespace bigtable { |
| 28 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 29 | +namespace { |
| 30 | + |
| 31 | +TEST(Bytes, RoundTrip) { |
| 32 | + char c = std::numeric_limits<char>::min(); |
| 33 | + std::string chars(1, c); |
| 34 | + while (c != std::numeric_limits<char>::max()) { |
| 35 | + chars.push_back(++c); |
| 36 | + } |
| 37 | + |
| 38 | + // Empty string. |
| 39 | + std::string data; |
| 40 | + Bytes bytes(data); |
| 41 | + EXPECT_EQ(data, bytes.get<std::string>()); |
| 42 | + |
| 43 | + // All 1-char strings. |
| 44 | + data.resize(1); |
| 45 | + for (auto c : chars) { |
| 46 | + data[0] = c; |
| 47 | + Bytes bytes(data); |
| 48 | + EXPECT_EQ(data, bytes.get<std::string>()); |
| 49 | + } |
| 50 | + |
| 51 | + // All 2-char strings. |
| 52 | + data.resize(2); |
| 53 | + for (auto c0 : chars) { |
| 54 | + data[0] = c0; |
| 55 | + for (auto c1 : chars) { |
| 56 | + data[1] = c1; |
| 57 | + Bytes bytes(data); |
| 58 | + EXPECT_EQ(data, bytes.get<std::string>()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Some 3-char strings. |
| 63 | + data.resize(3); |
| 64 | + for (auto c0 : {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}) { |
| 65 | + data[0] = c0; |
| 66 | + for (auto c1 : chars) { |
| 67 | + data[1] = c1; |
| 68 | + for (auto c2 : chars) { |
| 69 | + data[2] = c2; |
| 70 | + Bytes bytes(data); |
| 71 | + EXPECT_EQ(data, bytes.get<std::string>()); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +TEST(Bytes, RelationalOperators) { |
| 78 | + std::string const s_plain = "The quick brown fox jumps over the lazy dog."; |
| 79 | + std::deque<char> const d_plain(s_plain.begin(), s_plain.end()); |
| 80 | + std::vector<std::uint8_t> const v_plain(s_plain.begin(), s_plain.end()); |
| 81 | + |
| 82 | + auto s_bytes = Bytes(s_plain.begin(), s_plain.end()); |
| 83 | + auto d_bytes = Bytes(d_plain.begin(), d_plain.end()); |
| 84 | + auto v_bytes = Bytes(v_plain.begin(), v_plain.end()); |
| 85 | + EXPECT_EQ(s_bytes, d_bytes); |
| 86 | + EXPECT_EQ(d_bytes, v_bytes); |
| 87 | + EXPECT_EQ(v_bytes, s_bytes); |
| 88 | + |
| 89 | + auto x_bytes = Bytes(s_plain + " How vexingly quick daft zebras jump!"); |
| 90 | + EXPECT_NE(x_bytes, s_bytes); |
| 91 | + EXPECT_NE(x_bytes, d_bytes); |
| 92 | + EXPECT_NE(x_bytes, v_bytes); |
| 93 | +} |
| 94 | + |
| 95 | +TEST(Bytes, OutputStream) { |
| 96 | + struct TestCase { |
| 97 | + Bytes bytes; |
| 98 | + std::string expected; |
| 99 | + }; |
| 100 | + |
| 101 | + std::vector<TestCase> test_cases = { |
| 102 | + {Bytes(std::string("")), R"(B"")"}, |
| 103 | + {Bytes(std::string("foo")), R"(B"foo")"}, |
| 104 | + {Bytes(std::string("a\011B")), R"(B"a\011B")"}, |
| 105 | + {Bytes(std::string("a\377B")), R"(B"a\377B")"}, |
| 106 | + {Bytes(std::string("!@#$%^&*()-.")), R"(B"!@#$%^&*()-.")"}, |
| 107 | + {Bytes(std::string(3, '\0')), R"(B"\000\000\000")"}, |
| 108 | + {Bytes(""), R"(B"\000")"}, |
| 109 | + {Bytes("foo"), R"(B"foo\000")"}, |
| 110 | + }; |
| 111 | + |
| 112 | + for (auto const& tc : test_cases) { |
| 113 | + std::ostringstream ss; |
| 114 | + ss << tc.bytes; |
| 115 | + EXPECT_EQ(ss.str(), tc.expected); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +TEST(Bytes, OutputStreamEscapingCannotFail) { |
| 120 | + using ByteType = unsigned char; |
| 121 | + for (int i = 0; i < std::numeric_limits<ByteType>::max() + 1; ++i) { |
| 122 | + auto const b = static_cast<ByteType>(i); |
| 123 | + std::ostringstream ss; |
| 124 | + ss << Bytes(std::array<ByteType, 1>{b}); |
| 125 | + EXPECT_NE(ss.str(), R"(B"\?")") << "i=" << i; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace |
| 130 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 131 | +} // namespace bigtable |
| 132 | +} // namespace cloud |
| 133 | +} // namespace google |
0 commit comments