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