| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "blimp/net/delta_encoding.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 using testing::ElementsAre; | |
| 16 | |
| 17 namespace blimp { | |
| 18 namespace { | |
| 19 | |
| 20 // Provides an example of field elision across structs. | |
| 21 // For each CompoundStruct element in a sequence: | |
| 22 // |str| overrides the previous element's |str|, if set. | |
| 23 // |str| inherits the previous element's |str|, if empty. | |
| 24 // |number| is computed as a delta from the previous element's |number|. | |
| 25 struct CompoundStruct { | |
| 26 static CompoundStruct Difference(const CompoundStruct& lhs, | |
| 27 const CompoundStruct& rhs) { | |
| 28 CompoundStruct output; | |
| 29 output.number = lhs.number - rhs.number; | |
| 30 if (lhs.str != rhs.str) { | |
| 31 output.str = lhs.str; | |
| 32 } | |
| 33 return output; | |
| 34 } | |
| 35 | |
| 36 static CompoundStruct Merge(const CompoundStruct& lhs, | |
| 37 const CompoundStruct& rhs) { | |
| 38 CompoundStruct output; | |
| 39 output.number = lhs.number + rhs.number; | |
| 40 output.str = (rhs.str.empty() ? lhs.str : rhs.str); | |
| 41 return output; | |
| 42 } | |
| 43 | |
| 44 // Sortable by |number|. | |
| 45 friend bool operator<(const CompoundStruct& lhs, const CompoundStruct& rhs) { | |
| 46 return lhs.number < rhs.number; | |
| 47 } | |
| 48 | |
| 49 int number; | |
| 50 std::string str; | |
| 51 }; | |
| 52 | |
| 53 class TestDeltaEncoding : public testing::Test { | |
| 54 public: | |
| 55 TestDeltaEncoding() {} | |
| 56 | |
| 57 private: | |
| 58 DISALLOW_COPY_AND_ASSIGN(TestDeltaEncoding); | |
| 59 }; | |
| 60 | |
| 61 TEST_F(TestDeltaEncoding, DeltaEncode) { | |
| 62 std::vector<int> test{5, 1, 3}; | |
| 63 DeltaEncode(test.begin(), test.end()); | |
| 64 EXPECT_THAT(test, ElementsAre(5, -4, 2)); | |
| 65 } | |
| 66 | |
| 67 TEST_F(TestDeltaEncoding, DeltaEncodeEmpty) { | |
| 68 std::vector<int> test; | |
| 69 DeltaEncode(test.begin(), test.end()); | |
| 70 EXPECT_TRUE(test.empty()); | |
| 71 } | |
| 72 | |
| 73 TEST_F(TestDeltaEncoding, DeltaEncodeSingleValue) { | |
| 74 std::vector<int> test; | |
| 75 test.push_back(1); | |
| 76 DeltaEncode(test.begin(), test.end()); | |
| 77 EXPECT_THAT(test, ElementsAre(1)); | |
| 78 } | |
| 79 | |
| 80 TEST_F(TestDeltaEncoding, DeltaDecode) { | |
| 81 std::vector<int> test{1, 1, 2}; | |
| 82 DeltaDecode(test.begin(), test.end()); | |
| 83 EXPECT_THAT(test, ElementsAre(1, 2, 4)); | |
| 84 } | |
| 85 | |
| 86 TEST_F(TestDeltaEncoding, DeltaDecodeEmpty) { | |
| 87 std::vector<int> test; | |
| 88 DeltaDecode(test.begin(), test.end()); | |
| 89 EXPECT_TRUE(test.empty()); | |
| 90 } | |
| 91 | |
| 92 TEST_F(TestDeltaEncoding, DeltaDecodeSingleValue) { | |
| 93 std::vector<int> test{1}; | |
| 94 DeltaEncode(test.begin(), test.end()); | |
| 95 EXPECT_THAT(test, ElementsAre(1)); | |
| 96 } | |
| 97 | |
| 98 TEST_F(TestDeltaEncoding, SortAndDeltaEncodeRoundTrip) { | |
| 99 std::vector<int> test{8, 3, 5}; | |
| 100 SortAndDeltaEncode(test.begin(), test.end()); | |
| 101 EXPECT_THAT(test, ElementsAre(3, 2, 3)); | |
| 102 DeltaDecode(test.begin(), test.end()); | |
| 103 EXPECT_THAT(test, ElementsAre(3, 5, 8)); | |
| 104 } | |
| 105 | |
| 106 TEST_F(TestDeltaEncoding, DeltaEncodeStruct) { | |
| 107 std::vector<CompoundStruct> test{{2, "foo"}, {3, "foo"}, {0, "bar"}}; | |
| 108 DeltaEncode(test.begin(), test.end(), &CompoundStruct::Difference); | |
| 109 | |
| 110 EXPECT_EQ(2, test[0].number); | |
| 111 EXPECT_EQ(1, test[1].number); | |
| 112 EXPECT_EQ(-3, test[2].number); | |
| 113 | |
| 114 EXPECT_EQ("foo", test[0].str); | |
| 115 EXPECT_EQ("", test[1].str); | |
| 116 EXPECT_EQ("bar", test[2].str); | |
| 117 } | |
| 118 | |
| 119 TEST_F(TestDeltaEncoding, DeltaDecodeStruct) { | |
| 120 std::vector<CompoundStruct> test{{2, "foo"}, {-1, ""}, {0, "bar"}}; | |
| 121 DeltaDecode(test.begin(), test.end(), &CompoundStruct::Merge); | |
| 122 | |
| 123 EXPECT_EQ(2, test[0].number); | |
| 124 EXPECT_EQ(1, test[1].number); | |
| 125 EXPECT_EQ(1, test[2].number); | |
| 126 | |
| 127 EXPECT_EQ("foo", test[0].str); | |
| 128 EXPECT_EQ("foo", test[1].str); | |
| 129 EXPECT_EQ("bar", test[2].str); | |
| 130 } | |
| 131 | |
| 132 TEST_F(TestDeltaEncoding, SortAndDeltaEncodeStructRoundTrip) { | |
| 133 std::vector<CompoundStruct> test{{2, "foo"}, {3, "foo"}, {0, "bar"}}; | |
| 134 SortAndDeltaEncode(test.begin(), test.end(), &CompoundStruct::Difference); | |
| 135 | |
| 136 EXPECT_EQ(0, test[0].number); | |
| 137 EXPECT_EQ(2, test[1].number); | |
| 138 EXPECT_EQ(1, test[2].number); | |
| 139 | |
| 140 EXPECT_EQ("bar", test[0].str); | |
| 141 EXPECT_EQ("foo", test[1].str); | |
| 142 EXPECT_EQ("", test[2].str); | |
| 143 | |
| 144 DeltaDecode(test.begin(), test.end(), &CompoundStruct::Merge); | |
| 145 EXPECT_EQ(0, test[0].number); | |
| 146 EXPECT_EQ(2, test[1].number); | |
| 147 EXPECT_EQ(3, test[2].number); | |
| 148 | |
| 149 EXPECT_EQ("bar", test[0].str); | |
| 150 EXPECT_EQ("foo", test[1].str); | |
| 151 EXPECT_EQ("foo", test[2].str); | |
| 152 } | |
| 153 | |
| 154 } // namespace | |
| 155 } // namespace blimp | |
| OLD | NEW |