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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "google/protobuf/compiler/hpb/tests/child_model.hpb.h"
#include "google/protobuf/compiler/hpb/tests/test_extension.hpb.h"
#include "google/protobuf/compiler/hpb/tests/test_model.hpb.h"
#include "google/protobuf/hpb/requires.h"
namespace {
using ::hpb::internal::Requires;
using ::hpb_unittest::protos::ChildModel1;
using ::hpb_unittest::protos::TestModel;
using ::testing::ElementsAre;
const char kTestStr1[] = "abcdefg";
const char kTestStr2[] = "just another test string";
TEST(CppGeneratedCode, RepeatedMessages) {
::hpb::Arena arena;
auto test_model = ::hpb::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.child_models_size());
// Should be able to clear repeated field when empty.
test_model.mutable_child_models()->clear();
EXPECT_EQ(0, test_model.child_models_size());
// Add 2 children.
auto new_child = test_model.add_child_models();
EXPECT_EQ(true, new_child.ok());
new_child.value()->set_child_str1(kTestStr1);
new_child = test_model.add_child_models();
EXPECT_EQ(true, new_child.ok());
new_child.value()->set_child_str1(kTestStr2);
EXPECT_EQ(2, test_model.child_models_size());
// Mutable access.
auto mutable_first = test_model.mutable_child_models(0);
EXPECT_EQ(mutable_first->child_str1(), kTestStr1);
mutable_first->set_child_str1("change1");
auto mutable_second = test_model.mutable_child_models(1);
EXPECT_EQ(mutable_second->child_str1(), kTestStr2);
mutable_second->set_child_str1("change2");
// Check mutations using views.
auto view_first = test_model.child_models(0);
EXPECT_EQ(view_first->child_str1(), "change1");
auto view_second = test_model.child_models(1);
EXPECT_EQ(view_second->child_str1(), "change2");
}
TEST(CppGeneratedCode, RepeatedScalar) {
::hpb::Arena arena;
auto test_model = ::hpb::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.value_array_size());
// Should be able to clear repeated field when empty.
test_model.mutable_value_array()->clear();
EXPECT_EQ(0, test_model.value_array_size());
// Add 2 children.
EXPECT_EQ(true, test_model.add_value_array(5));
EXPECT_EQ(true, test_model.add_value_array(6));
EXPECT_EQ(2, test_model.value_array_size());
EXPECT_EQ(5, test_model.value_array(0));
EXPECT_EQ(6, test_model.value_array(1));
EXPECT_EQ(true, test_model.resize_value_array(3));
EXPECT_EQ(3, test_model.value_array_size());
test_model.set_value_array(2, 7);
EXPECT_EQ(5, test_model.value_array(0));
EXPECT_EQ(6, test_model.value_array(1));
EXPECT_EQ(7, test_model.value_array(2));
}
TEST(CppGeneratedCode, RepeatedFieldClear) {
::hpb::Arena arena;
auto test_model = ::hpb::CreateMessage<TestModel>(arena);
test_model.mutable_value_array()->push_back(5);
test_model.mutable_value_array()->push_back(16);
test_model.mutable_value_array()->push_back(27);
ASSERT_EQ(test_model.mutable_value_array()->size(), 3);
test_model.mutable_value_array()->clear();
EXPECT_EQ(test_model.mutable_value_array()->size(), 0);
}
TEST(CppGeneratedCode, RepeatedFieldProxyForScalars) {
::hpb::Arena arena;
auto test_model = ::hpb::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.value_array().size());
EXPECT_EQ(0, test_model.mutable_value_array()->size());
test_model.mutable_value_array()->push_back(5);
test_model.mutable_value_array()->push_back(16);
test_model.mutable_value_array()->push_back(27);
ASSERT_EQ(test_model.mutable_value_array()->size(), 3);
EXPECT_EQ((*test_model.mutable_value_array())[0], 5);
EXPECT_EQ((*test_model.mutable_value_array())[1], 16);
EXPECT_EQ((*test_model.mutable_value_array())[2], 27);
const auto value_array = test_model.value_array();
ASSERT_EQ(value_array.size(), 3);
EXPECT_EQ(value_array[0], 5);
EXPECT_EQ(value_array[1], 16);
EXPECT_EQ(value_array[2], 27);
EXPECT_THAT(value_array, ElementsAre(5, 16, 27));
EXPECT_THAT(std::vector(value_array.begin(), value_array.end()),
ElementsAre(5, 16, 27));
EXPECT_THAT(std::vector(value_array.cbegin(), value_array.cend()),
ElementsAre(5, 16, 27));
EXPECT_THAT(std::vector(value_array.rbegin(), value_array.rend()),
ElementsAre(27, 16, 5));
EXPECT_THAT(std::vector(value_array.crbegin(), value_array.crend()),
ElementsAre(27, 16, 5));
}
TEST(CppGeneratedCode, RepeatedScalarIterator) {
::hpb::Arena arena;
auto test_model = ::hpb::CreateMessage<TestModel>(arena);
test_model.mutable_value_array()->push_back(5);
test_model.mutable_value_array()->push_back(16);
test_model.mutable_value_array()->push_back(27);
int sum = 0;
// Access by value.
const ::hpb::RepeatedField<int32_t>::CProxy rep1 = test_model.value_array();
for (auto i : rep1) {
sum += i;
}
EXPECT_EQ(sum, 5 + 16 + 27);
// Access by const reference.
sum = 0;
for (const auto& i : *test_model.mutable_value_array()) {
sum += i;
}
EXPECT_EQ(sum, 5 + 16 + 27);
// Access by forwarding reference.
sum = 0;
for (auto&& i : *test_model.mutable_value_array()) {
sum += i;
}
EXPECT_EQ(sum, 5 + 16 + 27);
// Test iterator operators.
auto begin = test_model.value_array().begin();
auto end = test_model.value_array().end();
sum = 0;
for (auto it = begin; it != end; ++it) {
sum += *it;
}
EXPECT_EQ(sum, 5 + 16 + 27);
auto it = begin;
++it;
EXPECT_TRUE(begin < it);
EXPECT_TRUE(begin <= it);
it = end;
EXPECT_TRUE(it == end);
EXPECT_TRUE(it > begin);
EXPECT_TRUE(it >= begin);
EXPECT_TRUE(it != begin);
// difference type
it = end;
--it;
--it;
EXPECT_EQ(end - it, 2);
it = begin;
EXPECT_EQ(it[0], 5);
EXPECT_EQ(it[1], 16);
EXPECT_EQ(it[2], 27);
// ValueProxy.
sum = 0;
for (::hpb::RepeatedField<int32_t>::ValueCProxy c :
test_model.value_array()) {
sum += c;
}
EXPECT_EQ(sum, 5 + 16 + 27);
sum = 0;
for (::hpb::RepeatedField<int32_t>::ValueProxy c :
*test_model.mutable_value_array()) {
sum += c;
}
EXPECT_EQ(sum, 5 + 16 + 27);
}
TEST(CppGeneratedCode, RepeatedFieldProxyForStrings) {
::hpb::Arena arena;
auto test_model = ::hpb::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.repeated_string().size());
EXPECT_EQ(0, test_model.mutable_repeated_string()->size());
test_model.mutable_repeated_string()->push_back("a");
test_model.mutable_repeated_string()->push_back("b");
test_model.mutable_repeated_string()->push_back("c");
ASSERT_EQ(test_model.repeated_string().size(), 3);
EXPECT_EQ(test_model.repeated_string()[0], "a");
EXPECT_EQ(test_model.repeated_string()[1], "b");
EXPECT_EQ(test_model.repeated_string()[2], "c");
EXPECT_THAT(test_model.repeated_string(), ElementsAre("a", "b", "c"));
EXPECT_THAT(*test_model.mutable_repeated_string(),
ElementsAre("a", "b", "c"));
ASSERT_EQ(test_model.mutable_repeated_string()->size(), 3);
EXPECT_EQ((*test_model.mutable_repeated_string())[0], "a");
EXPECT_EQ((*test_model.mutable_repeated_string())[1], "b");
EXPECT_EQ((*test_model.mutable_repeated_string())[2], "c");
// The const accessor can't be used to modify the element
EXPECT_FALSE((std::is_assignable<decltype(test_model.repeated_string()[1]),
absl::string_view>::value));
// But the mutable one is fine.
(*test_model.mutable_repeated_string())[1] = "other";
EXPECT_THAT(test_model.repeated_string(), ElementsAre("a", "other", "c"));
test_model.mutable_repeated_string()->clear();
EXPECT_EQ(test_model.mutable_repeated_string()->size(), 0);
}
TEST(CppGeneratedCode, RepeatedFieldProxyForMessages) {
::hpb::Arena arena;
auto test_model = ::hpb::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.child_models().size());
ChildModel1 child1;
child1.set_child_str1(kTestStr1);
test_model.mutable_child_models()->push_back(child1);
ChildModel1 child2;
child2.set_child_str1(kTestStr2);
test_model.mutable_child_models()->push_back(std::move(child2));
int i = 0;
for (auto child : test_model.child_models()) {
EXPECT_FALSE(Requires<decltype(child)>(
[](auto&& x) -> decltype(x.set_child_str1("")) {}));
if (i++ == 0) {
EXPECT_EQ(child.child_str1(), kTestStr1);
} else {
EXPECT_EQ(child.child_str1(), kTestStr2);
}
}
i = 0;
for (const auto& child : *test_model.mutable_child_models()) {
if (i++ == 0) {
EXPECT_EQ(child.child_str1(), kTestStr1);
} else {
EXPECT_EQ(child.child_str1(), kTestStr2);
}
}
using testing::_;
EXPECT_THAT(test_model.child_models(), ElementsAre(_, _));
EXPECT_EQ(test_model.child_models().size(), 2);
EXPECT_EQ(test_model.child_models()[0].child_str1(), kTestStr1);
EXPECT_EQ(test_model.child_models()[1].child_str1(), kTestStr2);
EXPECT_EQ((*test_model.mutable_child_models())[0].child_str1(), kTestStr1);
EXPECT_EQ((*test_model.mutable_child_models())[1].child_str1(), kTestStr2);
(*test_model.mutable_child_models())[0].set_child_str1("change1");
EXPECT_EQ((*test_model.mutable_child_models())[0].child_str1(), "change1");
test_model.mutable_child_models()->clear();
EXPECT_EQ(test_model.mutable_child_models()->size(), 0);
}
TEST(CppGeneratedCode, EmptyRepeatedFieldProxyForMessages) {
::hpb::Arena arena;
auto test_model = ::hpb::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.child_models().size());
ChildModel1 child1;
child1.set_child_str1(kTestStr1);
EXPECT_EQ(test_model.child_models().size(), 0);
EXPECT_EQ(std::distance(test_model.child_models().begin(),
test_model.child_models().end()),
0);
}
TEST(CppGeneratedCode, RepeatedFieldProxyForMessagesIndexOperator) {
::hpb::Arena arena;
auto test_model = ::hpb::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.child_models().size());
ChildModel1 child1;
child1.set_child_str1(kTestStr1);
test_model.mutable_child_models()->push_back(child1);
ChildModel1 child2;
child2.set_child_str1(kTestStr2);
test_model.mutable_child_models()->push_back(std::move(child2));
ASSERT_EQ(test_model.child_models().size(), 2);
// test_model.child_models()[0].set_child_str1("change1");
(*test_model.mutable_child_models())[0].set_child_str1("change1");
EXPECT_EQ((*test_model.mutable_child_models())[0].child_str1(), "change1");
}
TEST(CppGeneratedCode, RepeatedStrings) {
::hpb::Arena arena;
auto test_model = ::hpb::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.repeated_string_size());
// Should be able to clear repeated field when empty.
test_model.mutable_repeated_string()->clear();
EXPECT_EQ(0, test_model.repeated_string_size());
// Add 2 children.
EXPECT_EQ(true, test_model.add_repeated_string("Hello"));
EXPECT_EQ(true, test_model.add_repeated_string("World"));
EXPECT_EQ(2, test_model.repeated_string_size());
EXPECT_EQ("Hello", test_model.repeated_string(0));
EXPECT_EQ("World", test_model.repeated_string(1));
EXPECT_EQ(true, test_model.resize_repeated_string(3));
EXPECT_EQ(3, test_model.repeated_string_size());
test_model.set_repeated_string(2, "Test");
EXPECT_EQ("Hello", test_model.repeated_string(0));
EXPECT_EQ("World", test_model.repeated_string(1));
EXPECT_EQ("Test", test_model.repeated_string(2));
}
} // namespace
|