OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | |
6 #include "sync/internal_api/public/base/node_ordinal.h" | 5 #include "sync/internal_api/public/base/node_ordinal.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 6 |
7 #include <stdint.h> | |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cstddef> | 10 #include <cstddef> |
11 #include <limits> | |
12 | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
11 | 14 |
12 namespace syncer { | 15 namespace syncer { |
13 | 16 |
14 namespace { | 17 namespace { |
15 | 18 |
16 const int64 kTestValues[] = { | 19 const int64 kTestValues[] = {0LL, |
pavely
2015/11/25 18:24:36
Could you change int64 to int64_t here as well?
Avi (use Gerrit)
2015/11/25 18:30:12
Oops, missed that! Thanks.
| |
17 0LL, | 20 1LL, |
18 1LL, -1LL, | 21 -1LL, |
19 2LL, -2LL, | 22 2LL, |
20 3LL, -3LL, | 23 -2LL, |
21 0x79LL, -0x79LL, | 24 3LL, |
22 0x80LL, -0x80LL, | 25 -3LL, |
23 0x81LL, -0x81LL, | 26 0x79LL, |
24 0xFELL, -0xFELL, | 27 -0x79LL, |
25 0xFFLL, -0xFFLL, | 28 0x80LL, |
26 0x100LL, -0x100LL, | 29 -0x80LL, |
27 0x101LL, -0x101LL, | 30 0x81LL, |
28 0xFA1AFELL, -0xFA1AFELL, | 31 -0x81LL, |
29 0xFFFFFFFELL, -0xFFFFFFFELL, | 32 0xFELL, |
30 0xFFFFFFFFLL, -0xFFFFFFFFLL, | 33 -0xFELL, |
31 0x100000000LL, -0x100000000LL, | 34 0xFFLL, |
32 0x100000001LL, -0x100000001LL, | 35 -0xFFLL, |
33 0xFFFFFFFFFFLL, -0xFFFFFFFFFFLL, | 36 0x100LL, |
34 0x112358132134LL, -0x112358132134LL, | 37 -0x100LL, |
35 0xFEFFBEEFABC1234LL, -0xFEFFBEEFABC1234LL, | 38 0x101LL, |
36 kint64max, | 39 -0x101LL, |
37 kint64min, | 40 0xFA1AFELL, |
38 kint64min + 1, | 41 -0xFA1AFELL, |
39 kint64max - 1 | 42 0xFFFFFFFELL, |
40 }; | 43 -0xFFFFFFFELL, |
44 0xFFFFFFFFLL, | |
45 -0xFFFFFFFFLL, | |
46 0x100000000LL, | |
47 -0x100000000LL, | |
48 0x100000001LL, | |
49 -0x100000001LL, | |
50 0xFFFFFFFFFFLL, | |
51 -0xFFFFFFFFFFLL, | |
52 0x112358132134LL, | |
53 -0x112358132134LL, | |
54 0xFEFFBEEFABC1234LL, | |
55 -0xFEFFBEEFABC1234LL, | |
56 INT64_MAX, | |
57 INT64_MIN, | |
58 INT64_MIN + 1, | |
59 INT64_MAX - 1}; | |
41 | 60 |
42 const size_t kNumTestValues = arraysize(kTestValues); | 61 const size_t kNumTestValues = arraysize(kTestValues); |
43 | 62 |
44 // Convert each test value to an ordinal. All ordinals should be | 63 // Convert each test value to an ordinal. All ordinals should be |
45 // valid. | 64 // valid. |
46 TEST(NodeOrdinalTest, IsValid) { | 65 TEST(NodeOrdinalTest, IsValid) { |
47 for (size_t i = 0; i < kNumTestValues; ++i) { | 66 for (size_t i = 0; i < kNumTestValues; ++i) { |
48 const NodeOrdinal ordinal = Int64ToNodeOrdinal(kTestValues[i]); | 67 const NodeOrdinal ordinal = Int64ToNodeOrdinal(kTestValues[i]); |
49 EXPECT_TRUE(ordinal.IsValid()) << "i = " << i; | 68 EXPECT_TRUE(ordinal.IsValid()) << "i = " << i; |
50 } | 69 } |
51 } | 70 } |
52 | 71 |
53 // Convert each test value to an ordinal. All ordinals should have | 72 // Convert each test value to an ordinal. All ordinals should have |
54 // 8-byte strings, except for kint64min, which should have a 9-byte | 73 // 8-byte strings, except for kint64min, which should have a 9-byte |
55 // string. | 74 // string. |
56 TEST(NodeOrdinalTest, Size) { | 75 TEST(NodeOrdinalTest, Size) { |
57 EXPECT_EQ(9U, Int64ToNodeOrdinal(kint64min).ToInternalValue().size()); | 76 EXPECT_EQ(9U, Int64ToNodeOrdinal(std::numeric_limits<int64_t>::min()) |
77 .ToInternalValue() | |
78 .size()); | |
58 | 79 |
59 for (size_t i = 0; i < kNumTestValues; ++i) { | 80 for (size_t i = 0; i < kNumTestValues; ++i) { |
60 if (kTestValues[i] == kint64min) { | 81 if (kTestValues[i] == std::numeric_limits<int64_t>::min()) { |
61 continue; | 82 continue; |
62 } | 83 } |
63 const NodeOrdinal ordinal = Int64ToNodeOrdinal(kTestValues[i]); | 84 const NodeOrdinal ordinal = Int64ToNodeOrdinal(kTestValues[i]); |
64 EXPECT_EQ(8U, ordinal.ToInternalValue().size()) << "i = " << i; | 85 EXPECT_EQ(8U, ordinal.ToInternalValue().size()) << "i = " << i; |
65 } | 86 } |
66 } | 87 } |
67 | 88 |
68 // Convert each test value to an ordinal and back. That resulting | 89 // Convert each test value to an ordinal and back. That resulting |
69 // value should be equal to the original value. | 90 // value should be equal to the original value. |
70 TEST(NodeOrdinalTest, PositionToOrdinalToPosition) { | 91 TEST(NodeOrdinalTest, PositionToOrdinalToPosition) { |
71 for (size_t i = 0; i < kNumTestValues; ++i) { | 92 for (size_t i = 0; i < kNumTestValues; ++i) { |
72 const int64 expected_value = kTestValues[i]; | 93 const int64_t expected_value = kTestValues[i]; |
73 const NodeOrdinal ordinal = Int64ToNodeOrdinal(expected_value); | 94 const NodeOrdinal ordinal = Int64ToNodeOrdinal(expected_value); |
74 const int64 value = NodeOrdinalToInt64(ordinal); | 95 const int64_t value = NodeOrdinalToInt64(ordinal); |
75 EXPECT_EQ(expected_value, value) << "i = " << i; | 96 EXPECT_EQ(expected_value, value) << "i = " << i; |
76 } | 97 } |
77 } | 98 } |
78 | 99 |
79 template <typename T, typename LessThan = std::less<T> > | 100 template <typename T, typename LessThan = std::less<T> > |
80 class IndexedLessThan { | 101 class IndexedLessThan { |
81 public: | 102 public: |
82 IndexedLessThan(const T* values) : values_(values) {} | 103 IndexedLessThan(const T* values) : values_(values) {} |
83 | 104 |
84 bool operator()(int i1, int i2) { | 105 bool operator()(int i1, int i2) { |
85 return less_than_(values_[i1], values_[i2]); | 106 return less_than_(values_[i1], values_[i2]); |
86 } | 107 } |
87 | 108 |
88 private: | 109 private: |
89 const T* values_; | 110 const T* values_; |
90 LessThan less_than_; | 111 LessThan less_than_; |
91 }; | 112 }; |
92 | 113 |
93 // Sort kTestValues by int64 value and then sort it by NodeOrdinal | 114 // Sort kTestValues by int64_t value and then sort it by NodeOrdinal |
94 // value. kTestValues should not already be sorted (by either | 115 // value. kTestValues should not already be sorted (by either |
95 // comparator) and the two orderings should be the same. | 116 // comparator) and the two orderings should be the same. |
96 TEST(NodeOrdinalTest, ConsistentOrdering) { | 117 TEST(NodeOrdinalTest, ConsistentOrdering) { |
97 NodeOrdinal ordinals[kNumTestValues]; | 118 NodeOrdinal ordinals[kNumTestValues]; |
98 std::vector<int> original_ordering(kNumTestValues); | 119 std::vector<int> original_ordering(kNumTestValues); |
99 std::vector<int> int64_ordering(kNumTestValues); | 120 std::vector<int> int64_ordering(kNumTestValues); |
100 std::vector<int> ordinal_ordering(kNumTestValues); | 121 std::vector<int> ordinal_ordering(kNumTestValues); |
101 for (size_t i = 0; i < kNumTestValues; ++i) { | 122 for (size_t i = 0; i < kNumTestValues; ++i) { |
102 ordinals[i] = Int64ToNodeOrdinal(kTestValues[i]); | 123 ordinals[i] = Int64ToNodeOrdinal(kTestValues[i]); |
103 original_ordering[i] = int64_ordering[i] = ordinal_ordering[i] = i; | 124 original_ordering[i] = int64_ordering[i] = ordinal_ordering[i] = i; |
104 } | 125 } |
105 | 126 |
106 std::sort(int64_ordering.begin(), int64_ordering.end(), | 127 std::sort(int64_ordering.begin(), int64_ordering.end(), |
107 IndexedLessThan<int64>(kTestValues)); | 128 IndexedLessThan<int64_t>(kTestValues)); |
108 std::sort(ordinal_ordering.begin(), ordinal_ordering.end(), | 129 std::sort(ordinal_ordering.begin(), ordinal_ordering.end(), |
109 IndexedLessThan<NodeOrdinal, NodeOrdinal::LessThanFn>(ordinals)); | 130 IndexedLessThan<NodeOrdinal, NodeOrdinal::LessThanFn>(ordinals)); |
110 EXPECT_NE(original_ordering, int64_ordering); | 131 EXPECT_NE(original_ordering, int64_ordering); |
111 EXPECT_EQ(int64_ordering, ordinal_ordering); | 132 EXPECT_EQ(int64_ordering, ordinal_ordering); |
112 } | 133 } |
113 | 134 |
114 // Create two NodeOrdinals and create another one between them. It | 135 // Create two NodeOrdinals and create another one between them. It |
115 // should lie halfway between them. | 136 // should lie halfway between them. |
116 TEST(NodeOrdinalTest, CreateBetween) { | 137 TEST(NodeOrdinalTest, CreateBetween) { |
117 const NodeOrdinal ordinal1("\1\1\1\1\1\1\1\1"); | 138 const NodeOrdinal ordinal1("\1\1\1\1\1\1\1\1"); |
118 const NodeOrdinal ordinal2("\1\1\1\1\1\1\1\3"); | 139 const NodeOrdinal ordinal2("\1\1\1\1\1\1\1\3"); |
119 EXPECT_EQ("\1\1\1\1\1\1\1\2", | 140 EXPECT_EQ("\1\1\1\1\1\1\1\2", |
120 ordinal1.CreateBetween(ordinal2).ToInternalValue()); | 141 ordinal1.CreateBetween(ordinal2).ToInternalValue()); |
121 } | 142 } |
122 | 143 |
123 } // namespace | 144 } // namespace |
124 | 145 |
125 } // namespace syncer | 146 } // namespace syncer |
OLD | NEW |