Debian Codegraph @

extra/protobuf/protobuf-24.4/src/google/protobuf/map_test.cc

raw
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #include <array>
32 #include <string>
33 #include <type_traits>
34 #include <utility>
35 #include <vector>
36
37 #include "absl/container/flat_hash_set.h"
38 #include "google/protobuf/arena_test_util.h"
39 #include "google/protobuf/internal_visibility_for_testing.h"
40 #include "google/protobuf/map_proto2_unittest.pb.h"
41 #include "google/protobuf/map_unittest.pb.h"
42 #include "google/protobuf/reflection_tester.h"
43 #include "google/protobuf/unittest.pb.h"
44 #include "google/protobuf/unittest_import.pb.h"
45
46
47 #define BRIDGE_UNITTEST ::google::protobuf::bridge_unittest
48 #define UNITTEST ::protobuf_unittest
49 #define UNITTEST_IMPORT ::protobuf_unittest_import
50 #define UNITTEST_PACKAGE_NAME "protobuf_unittest"
51
52 // Must include after defining UNITTEST, etc.
53 // clang-format off
54 #include "google/protobuf/test_util.inc"
55 #include "google/protobuf/map_test_util.inc"
56 #include "google/protobuf/map_test.inc"
57 // clang-format on
58
59 // Must be included last.
60 #include "google/protobuf/port_def.inc"
61
62 namespace google {
63 namespace protobuf {
64 namespace internal {
65
66 struct AlignedAsDefault {
67 int x;
68 };
69 struct alignas(8) AlignedAs8 {
70 int x;
71 };
72
73 template <>
74 struct is_internal_map_value_type<AlignedAsDefault> : std::true_type {};
75 template <>
76 struct is_internal_map_value_type<AlignedAs8> : std::true_type {};
77
78 namespace {
79
80 using ::testing::FieldsAre;
81 using ::testing::UnorderedElementsAre;
82
83
84 TEST(MapTest, CopyConstructIntegers) {
85 auto token = internal::InternalVisibilityForTesting{};
86 using MapType = Map<int32_t, int32_t>;
87 MapType original;
88 original[1] = 2;
89 original[2] = 3;
90
91 MapType map1(original);
92 ASSERT_EQ(map1.size(), 2);
93 EXPECT_EQ(map1[1], 2);
94 EXPECT_EQ(map1[2], 3);
95
96 MapType map2(token, nullptr, original);
97 ASSERT_EQ(map2.size(), 2);
98 EXPECT_EQ(map2[1], 2);
99 EXPECT_EQ(map2[2], 3);
100 }
101
102 TEST(MapTest, CopyConstructStrings) {
103 auto token = internal::InternalVisibilityForTesting{};
104 using MapType = Map<std::string, std::string>;
105 MapType original;
106 original["1"] = "2";
107 original["2"] = "3";
108
109 MapType map1(original);
110 ASSERT_EQ(map1.size(), 2);
111 EXPECT_EQ(map1["1"], "2");
112 EXPECT_EQ(map1["2"], "3");
113
114 MapType map2(token, nullptr, original);
115 ASSERT_EQ(map2.size(), 2);
116 EXPECT_EQ(map2["1"], "2");
117 EXPECT_EQ(map2["2"], "3");
118 }
119
120 TEST(MapTest, CopyConstructMessages) {
121 auto token = internal::InternalVisibilityForTesting{};
122 using MapType = Map<std::string, TestAllTypes>;
123 MapType original;
124 original["1"].set_optional_int32(1);
125 original["2"].set_optional_int32(2);
126
127 MapType map1(original);
128 ASSERT_EQ(map1.size(), 2);
129 EXPECT_EQ(map1["1"].optional_int32(), 1);
130 EXPECT_EQ(map1["2"].optional_int32(), 2);
131
132 MapType map2(token, nullptr, original);
133 ASSERT_EQ(map2.size(), 2);
134 EXPECT_EQ(map2["1"].optional_int32(), 1);
135 EXPECT_EQ(map2["2"].optional_int32(), 2);
136 }
137
138 TEST(MapTest, CopyConstructIntegersWithArena) {
139 auto token = internal::InternalVisibilityForTesting{};
140 using MapType = Map<int32_t, int32_t>;
141 MapType original;
142 original[1] = 2;
143 original[2] = 3;
144
145 Arena arena;
146 alignas(MapType) char mem1[sizeof(MapType)];
147 MapType& map1 = *new (mem1) MapType(token, &arena, original);
148 ASSERT_EQ(map1.size(), 2);
149 EXPECT_EQ(map1[1], 2);
150 EXPECT_EQ(map1[2], 3);
151 EXPECT_EQ(map1[2], 3);
152 }
153
154 TEST(MapTest, CopyConstructStringsWithArena) {
155 auto token = internal::InternalVisibilityForTesting{};
156 using MapType = Map<std::string, std::string>;
157 MapType original;
158 original["1"] = "2";
159 original["2"] = "3";
160
161 Arena arena;
162 alignas(MapType) char mem1[sizeof(MapType)];
163 MapType& map1 = *new (mem1) MapType(token, &arena, original);
164 ASSERT_EQ(map1.size(), 2);
165 EXPECT_EQ(map1["1"], "2");
166 EXPECT_EQ(map1["2"], "3");
167 }
168
169 TEST(MapTest, CopyConstructMessagesWithArena) {
170 auto token = internal::InternalVisibilityForTesting{};
171 using MapType = Map<std::string, TestAllTypes>;
172 MapType original;
173 original["1"].set_optional_int32(1);
174 original["2"].set_optional_int32(2);
175
176 Arena arena;
177 alignas(MapType) char mem1[sizeof(MapType)];
178 MapType& map1 = *new (mem1) MapType(token, &arena, original);
179 ASSERT_EQ(map1.size(), 2);
180 EXPECT_EQ(map1["1"].optional_int32(), 1);
181 EXPECT_EQ(map1["1"].GetArena(), &arena);
182 EXPECT_EQ(map1["2"].optional_int32(), 2);
183 EXPECT_EQ(map1["2"].GetArena(), &arena);
184 }
185
186 template <typename Aligned, bool on_arena = false>
187 void MapTest_Aligned() {
188 Arena arena;
189 constexpr size_t align_mask = alignof(Aligned) - 1;
190 Map<int, Aligned> map(on_arena ? &arena : nullptr);
191 map.insert({1, Aligned{}});
192 auto it = map.find(1);
193 ASSERT_NE(it, map.end());
194 ASSERT_EQ(reinterpret_cast<intptr_t>(&it->second) & align_mask, 0);
195 map.clear();
196 }
197
198 TEST(MapTest, Aligned) { MapTest_Aligned<AlignedAsDefault>(); }
199 TEST(MapTest, AlignedOnArena) { MapTest_Aligned<AlignedAsDefault, true>(); }
200 TEST(MapTest, Aligned8) { MapTest_Aligned<AlignedAs8>(); }
201 TEST(MapTest, Aligned8OnArena) { MapTest_Aligned<AlignedAs8, true>(); }
202
203
204
205 } // namespace
206 } // namespace internal
207 } // namespace protobuf
208 } // namespace google
209
210 #include "google/protobuf/port_undef.inc"