Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2614)

Unified Diff: blimp/helium/syncable_primitive_serializer_unittest.cc

Issue 2400303002: Add LwwRegister CRDT (Closed)
Patch Set: Rebase with Syncable interface changes + code review feedback Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: blimp/helium/syncable_primitive_serializer_unittest.cc
diff --git a/blimp/helium/syncable_primitive_serializer_unittest.cc b/blimp/helium/syncable_primitive_serializer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..12b2eac5b094ef82604ad23a956d17415c7ae20d
--- /dev/null
+++ b/blimp/helium/syncable_primitive_serializer_unittest.cc
@@ -0,0 +1,120 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "blimp/helium/syncable_primitive_serializer.h"
+
+#include <string>
+#include <vector>
+
+#include "base/macros.h"
+#include "blimp/helium/version_vector.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
+#include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h"
+
+namespace blimp {
+namespace helium {
+namespace {
+
+class SyncablePrimitiveSerializerTest : public testing::Test {
+ public:
+ SyncablePrimitiveSerializerTest() {}
+ ~SyncablePrimitiveSerializerTest() override = default;
+
+ protected:
+ template <class T>
+ void SerializeAndDeserialize(const std::vector<T>& input) {
+ std::string changeset;
+ google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
+ google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
+
+ for (size_t i = 0; i < input.size(); ++i) {
+ SyncablePrimitiveSerializer::Serialize(input[i], &output_stream);
+ }
+
+ google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
+ changeset.size());
+ google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
+
+ for (size_t i = 0; i < input.size(); ++i) {
+ CheckCorrectDeserialization<T>(&input_stream, input[i]);
+ }
+ }
+
+ template <class T>
+ void CheckCorrectDeserialization(
+ google::protobuf::io::CodedInputStream* input_stream,
+ T expected) {
+ T output;
+ EXPECT_TRUE(
+ SyncablePrimitiveSerializer::Deserialize(input_stream, &output));
+ EXPECT_EQ(expected, output);
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(SyncablePrimitiveSerializerTest);
+};
+
+template <>
+void SyncablePrimitiveSerializerTest::CheckCorrectDeserialization<
+ VersionVector>(google::protobuf::io::CodedInputStream* input_stream,
+ VersionVector expected) {
+ VersionVector output;
+ EXPECT_TRUE(SyncablePrimitiveSerializer::Deserialize(input_stream, &output));
+ EXPECT_EQ(VersionVector::Comparison::EqualTo, expected.CompareTo(output));
+}
+
+TEST_F(SyncablePrimitiveSerializerTest, SerializesMixedTypes) {
+ std::string changeset;
+ google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
+ google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
+
+ SyncablePrimitiveSerializer::Serialize(345, &output_stream);
+ SyncablePrimitiveSerializer::Serialize("some string", &output_stream);
+ SyncablePrimitiveSerializer::Serialize(VersionVector(12, 13), &output_stream);
+
+ google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
+ changeset.size());
+ google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
+
+ CheckCorrectDeserialization<int32_t>(&input_stream, 345);
+ CheckCorrectDeserialization<std::string>(&input_stream, "some string");
+ CheckCorrectDeserialization<VersionVector>(&input_stream,
+ VersionVector(12, 13));
+}
+
+TEST_F(SyncablePrimitiveSerializerTest, SerializesInt32) {
+ SerializeAndDeserialize<int32_t>({123, 0, -456, 987});
+}
+
+TEST_F(SyncablePrimitiveSerializerTest, SerializesString) {
+ SerializeAndDeserialize<std::string>({"Hello, World", "", "Goodbye, World!"});
+}
+
+TEST_F(SyncablePrimitiveSerializerTest,
+ FailsToDeserializeStringGivenOutrageousLength) {
+ std::string changeset;
+ google::protobuf::io::StringOutputStream raw_output_stream(&changeset);
+ google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
+ output_stream.WriteVarint32(20 << 20); // 20MB
Kevin M 2016/10/20 00:38:25 "20MB, equal to kMaxStringLength"
steimel 2016/10/20 00:57:38 Done, but with "greater than", since kMax is 10MB
+ output_stream.WriteString("This string is 20MB, I swear!");
+
+ google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
+ changeset.size());
+ google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
+
+ std::string output;
+ EXPECT_FALSE(
+ SyncablePrimitiveSerializer::Deserialize(&input_stream, &output));
+}
+
+TEST_F(SyncablePrimitiveSerializerTest, SerializesVersionVector) {
+ SerializeAndDeserialize<VersionVector>(
+ {VersionVector(0, 0), VersionVector(3, 1), VersionVector(0, 4),
+ VersionVector(3, 0)});
+}
+
+} // namespace
+} // namespace helium
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698