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 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.
| |
21 static CompoundStruct Difference(const CompoundStruct& lhs, | |
22 const CompoundStruct& rhs) { | |
23 CompoundStruct output; | |
24 output.number = lhs.number - rhs.number; | |
25 if (lhs.str != rhs.str) { | |
26 output.str = lhs.str; | |
27 } | |
28 return output; | |
29 } | |
30 | |
31 static CompoundStruct Merge(const CompoundStruct& lhs, | |
32 const CompoundStruct& rhs) { | |
33 CompoundStruct output; | |
34 output.number = lhs.number + rhs.number; | |
35 output.str = (rhs.str.empty() ? lhs.str : rhs.str); | |
36 return output; | |
37 } | |
38 | |
39 int number; | |
40 std::string str; | |
41 }; | |
42 | |
43 class TestDeltaEncoding : public testing::Test { | |
44 public: | |
45 TestDeltaEncoding() {} | |
46 | |
47 private: | |
48 DISALLOW_COPY_AND_ASSIGN(TestDeltaEncoding); | |
49 }; | |
50 | |
51 TEST_F(TestDeltaEncoding, DeltaEncode) { | |
52 std::vector<int> test{5, 1, 3}; | |
53 DeltaEncode(test.begin(), test.end()); | |
54 EXPECT_THAT(test, ElementsAre(5, -4, 2)); | |
55 } | |
56 | |
57 TEST_F(TestDeltaEncoding, DeltaEncodeEmpty) { | |
58 std::vector<int> test; | |
59 DeltaEncode(test.begin(), test.end()); | |
60 EXPECT_TRUE(test.empty()); | |
61 } | |
62 | |
63 TEST_F(TestDeltaEncoding, DeltaEncodeSingleValue) { | |
64 std::vector<int> test; | |
65 test.push_back(1); | |
66 DeltaEncode(test.begin(), test.end()); | |
67 EXPECT_THAT(test, ElementsAre(1)); | |
68 } | |
69 | |
70 TEST_F(TestDeltaEncoding, DeltaDecode) { | |
71 std::vector<int> test{1, 1, 2}; | |
72 DeltaDecode(test.begin(), test.end()); | |
73 EXPECT_THAT(test, ElementsAre(1, 2, 4)); | |
74 } | |
75 | |
76 TEST_F(TestDeltaEncoding, DeltaDecodeEmpty) { | |
77 std::vector<int> test; | |
78 DeltaDecode(test.begin(), test.end()); | |
79 EXPECT_TRUE(test.empty()); | |
80 } | |
81 | |
82 TEST_F(TestDeltaEncoding, DeltaDecodeSingleValue) { | |
83 std::vector<int> test{1}; | |
84 DeltaEncode(test.begin(), test.end()); | |
85 EXPECT_THAT(test, ElementsAre(1)); | |
86 } | |
87 | |
88 TEST_F(TestDeltaEncoding, SortAndDeltaEncodeRoundTrip) { | |
89 std::vector<int> test{8, 3, 5}; | |
90 SortAndDeltaEncode(test.begin(), test.end()); | |
91 EXPECT_THAT(test, ElementsAre(3, 2, 3)); | |
92 DeltaDecode(test.begin(), test.end()); | |
93 EXPECT_THAT(test, ElementsAre(3, 5, 8)); | |
94 } | |
95 | |
96 TEST_F(TestDeltaEncoding, DeltaEncodeStruct) { | |
97 std::vector<CompoundStruct> test{{2, "foo"}, {3, "foo"}, {0, "bar"}}; | |
98 DeltaEncode(test.begin(), test.end(), &CompoundStruct::Difference); | |
99 | |
100 EXPECT_EQ(2, test[0].number); | |
101 EXPECT_EQ(1, test[1].number); | |
102 EXPECT_EQ(-3, test[2].number); | |
103 | |
104 EXPECT_EQ("foo", test[0].str); | |
105 EXPECT_EQ("", test[1].str); | |
106 EXPECT_EQ("bar", test[2].str); | |
107 } | |
108 | |
109 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
| |
110 std::vector<CompoundStruct> test{{2, "foo"}, {-1, ""}, {0, "bar"}}; | |
111 DeltaDecode(test.begin(), test.end(), &CompoundStruct::Merge); | |
112 | |
113 EXPECT_EQ(2, test[0].number); | |
114 EXPECT_EQ(1, test[1].number); | |
115 EXPECT_EQ(1, test[2].number); | |
116 | |
117 EXPECT_EQ("foo", test[0].str); | |
118 EXPECT_EQ("foo", test[1].str); | |
119 EXPECT_EQ("bar", test[2].str); | |
120 } | |
121 | |
122 } // namespace | |
123 } // namespace blimp | |
OLD | NEW |