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

Unified Diff: blimp/net/delta_encoding_unittest.cc

Issue 2275763002: Blimp: add delta encoding family of functions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean up includes Created 4 years, 4 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
« blimp/net/delta_encoding.h ('K') | « blimp/net/delta_encoding.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: blimp/net/delta_encoding_unittest.cc
diff --git a/blimp/net/delta_encoding_unittest.cc b/blimp/net/delta_encoding_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1ccbda194e8c9edfce169f93dec5f69de97b23b3
--- /dev/null
+++ b/blimp/net/delta_encoding_unittest.cc
@@ -0,0 +1,123 @@
+// 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/net/delta_encoding.h"
+
+#include <string>
+#include <vector>
+
+#include "base/logging.h"
+#include "base/macros.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::ElementsAre;
+
+namespace blimp {
+namespace {
+
+struct CompoundStruct {
nyquist 2016/08/26 20:43:42 Could you add a clarifying comment as to how strin
Wez 2016/08/29 23:25:24 +1 :)
Kevin M 2016/08/30 19:06:33 Done.
+ static CompoundStruct Difference(const CompoundStruct& lhs,
+ const CompoundStruct& rhs) {
+ CompoundStruct output;
+ output.number = lhs.number - rhs.number;
+ if (lhs.str != rhs.str) {
+ output.str = lhs.str;
+ }
+ return output;
+ }
+
+ static CompoundStruct Merge(const CompoundStruct& lhs,
+ const CompoundStruct& rhs) {
+ CompoundStruct output;
+ output.number = lhs.number + rhs.number;
+ output.str = (rhs.str.empty() ? lhs.str : rhs.str);
+ return output;
+ }
+
+ int number;
+ std::string str;
+};
+
+class TestDeltaEncoding : public testing::Test {
+ public:
+ TestDeltaEncoding() {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestDeltaEncoding);
+};
+
+TEST_F(TestDeltaEncoding, DeltaEncode) {
+ std::vector<int> test{5, 1, 3};
+ DeltaEncode(test.begin(), test.end());
+ EXPECT_THAT(test, ElementsAre(5, -4, 2));
+}
+
+TEST_F(TestDeltaEncoding, DeltaEncodeEmpty) {
+ std::vector<int> test;
+ DeltaEncode(test.begin(), test.end());
+ EXPECT_TRUE(test.empty());
+}
+
+TEST_F(TestDeltaEncoding, DeltaEncodeSingleValue) {
+ std::vector<int> test;
+ test.push_back(1);
+ DeltaEncode(test.begin(), test.end());
+ EXPECT_THAT(test, ElementsAre(1));
+}
+
+TEST_F(TestDeltaEncoding, DeltaDecode) {
+ std::vector<int> test{1, 1, 2};
+ DeltaDecode(test.begin(), test.end());
+ EXPECT_THAT(test, ElementsAre(1, 2, 4));
+}
+
+TEST_F(TestDeltaEncoding, DeltaDecodeEmpty) {
+ std::vector<int> test;
+ DeltaDecode(test.begin(), test.end());
+ EXPECT_TRUE(test.empty());
+}
+
+TEST_F(TestDeltaEncoding, DeltaDecodeSingleValue) {
+ std::vector<int> test{1};
+ DeltaEncode(test.begin(), test.end());
+ EXPECT_THAT(test, ElementsAre(1));
+}
+
+TEST_F(TestDeltaEncoding, SortAndDeltaEncodeRoundTrip) {
+ std::vector<int> test{8, 3, 5};
+ SortAndDeltaEncode(test.begin(), test.end());
+ EXPECT_THAT(test, ElementsAre(3, 2, 3));
+ DeltaDecode(test.begin(), test.end());
+ EXPECT_THAT(test, ElementsAre(3, 5, 8));
+}
+
+TEST_F(TestDeltaEncoding, DeltaEncodeStruct) {
+ std::vector<CompoundStruct> test{{2, "foo"}, {3, "foo"}, {0, "bar"}};
+ DeltaEncode(test.begin(), test.end(), &CompoundStruct::Difference);
+
+ EXPECT_EQ(2, test[0].number);
+ EXPECT_EQ(1, test[1].number);
+ EXPECT_EQ(-3, test[2].number);
+
+ EXPECT_EQ("foo", test[0].str);
+ EXPECT_EQ("", test[1].str);
+ EXPECT_EQ("bar", test[2].str);
+}
+
+TEST_F(TestDeltaEncoding, DeltaDecodeStruct) {
nyquist 2016/08/26 20:43:42 How does a sortable compound struct work? Could yo
Kevin M 2016/08/30 19:06:33 You would pick some kind of key field and sort usi
+ std::vector<CompoundStruct> test{{2, "foo"}, {-1, ""}, {0, "bar"}};
+ DeltaDecode(test.begin(), test.end(), &CompoundStruct::Merge);
+
+ EXPECT_EQ(2, test[0].number);
+ EXPECT_EQ(1, test[1].number);
+ EXPECT_EQ(1, test[2].number);
+
+ EXPECT_EQ("foo", test[0].str);
+ EXPECT_EQ("foo", test[1].str);
+ EXPECT_EQ("bar", test[2].str);
+}
+
+} // namespace
+} // namespace blimp
« blimp/net/delta_encoding.h ('K') | « blimp/net/delta_encoding.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698