summaryrefslogtreecommitdiffstats
path: root/sync/internal_api/public/base/node_ordinal_unittest.cc
blob: 3ade983f3937970fb1ade37564eabb2077e49e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2012 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 "sync/internal_api/public/base/node_ordinal.h"

#include <stdint.h>

#include <algorithm>
#include <cstddef>
#include <functional>
#include <limits>
#include <vector>

#include "base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace syncer {

namespace {

const int64_t kTestValues[] = {0LL,
                               1LL,
                               -1LL,
                               2LL,
                               -2LL,
                               3LL,
                               -3LL,
                               0x79LL,
                               -0x79LL,
                               0x80LL,
                               -0x80LL,
                               0x81LL,
                               -0x81LL,
                               0xFELL,
                               -0xFELL,
                               0xFFLL,
                               -0xFFLL,
                               0x100LL,
                               -0x100LL,
                               0x101LL,
                               -0x101LL,
                               0xFA1AFELL,
                               -0xFA1AFELL,
                               0xFFFFFFFELL,
                               -0xFFFFFFFELL,
                               0xFFFFFFFFLL,
                               -0xFFFFFFFFLL,
                               0x100000000LL,
                               -0x100000000LL,
                               0x100000001LL,
                               -0x100000001LL,
                               0xFFFFFFFFFFLL,
                               -0xFFFFFFFFFFLL,
                               0x112358132134LL,
                               -0x112358132134LL,
                               0xFEFFBEEFABC1234LL,
                               -0xFEFFBEEFABC1234LL,
                               INT64_MAX,
                               INT64_MIN,
                               INT64_MIN + 1,
                               INT64_MAX - 1};

const size_t kNumTestValues = arraysize(kTestValues);

// Convert each test value to an ordinal.  All ordinals should be
// valid.
TEST(NodeOrdinalTest, IsValid) {
  for (size_t i = 0; i < kNumTestValues; ++i) {
    const NodeOrdinal ordinal = Int64ToNodeOrdinal(kTestValues[i]);
    EXPECT_TRUE(ordinal.IsValid()) << "i = " << i;
  }
}

// Convert each test value to an ordinal.  All ordinals should have
// 8-byte strings, except for kint64min, which should have a 9-byte
// string.
TEST(NodeOrdinalTest, Size) {
  EXPECT_EQ(9U, Int64ToNodeOrdinal(std::numeric_limits<int64_t>::min())
                    .ToInternalValue()
                    .size());

  for (size_t i = 0; i < kNumTestValues; ++i) {
    if (kTestValues[i] == std::numeric_limits<int64_t>::min()) {
      continue;
    }
    const NodeOrdinal ordinal = Int64ToNodeOrdinal(kTestValues[i]);
    EXPECT_EQ(8U, ordinal.ToInternalValue().size()) << "i = " << i;
  }
}

// Convert each test value to an ordinal and back.  That resulting
// value should be equal to the original value.
TEST(NodeOrdinalTest, PositionToOrdinalToPosition) {
  for (size_t i = 0; i < kNumTestValues; ++i) {
    const int64_t expected_value = kTestValues[i];
    const NodeOrdinal ordinal = Int64ToNodeOrdinal(expected_value);
    const int64_t value = NodeOrdinalToInt64(ordinal);
    EXPECT_EQ(expected_value, value) << "i = " << i;
  }
}

template <typename T, typename LessThan = std::less<T> >
class IndexedLessThan {
 public:
  explicit IndexedLessThan(const T* values) : values_(values) {}

  bool operator()(int i1, int i2) {
    return less_than_(values_[i1], values_[i2]);
  }

 private:
  const T* values_;
  LessThan less_than_;
};

// Sort kTestValues by int64_t value and then sort it by NodeOrdinal
// value.  kTestValues should not already be sorted (by either
// comparator) and the two orderings should be the same.
TEST(NodeOrdinalTest, ConsistentOrdering) {
  NodeOrdinal ordinals[kNumTestValues];
  std::vector<int> original_ordering(kNumTestValues);
  std::vector<int> int64_ordering(kNumTestValues);
  std::vector<int> ordinal_ordering(kNumTestValues);
  for (size_t i = 0; i < kNumTestValues; ++i) {
    ordinals[i] = Int64ToNodeOrdinal(kTestValues[i]);
    original_ordering[i] = int64_ordering[i] = ordinal_ordering[i] = i;
  }

  std::sort(int64_ordering.begin(), int64_ordering.end(),
            IndexedLessThan<int64_t>(kTestValues));
  std::sort(ordinal_ordering.begin(), ordinal_ordering.end(),
            IndexedLessThan<NodeOrdinal, NodeOrdinal::LessThanFn>(ordinals));
  EXPECT_NE(original_ordering, int64_ordering);
  EXPECT_EQ(int64_ordering, ordinal_ordering);
}

// Create two NodeOrdinals and create another one between them.  It
// should lie halfway between them.
TEST(NodeOrdinalTest, CreateBetween) {
  const NodeOrdinal ordinal1("\1\1\1\1\1\1\1\1");
  const NodeOrdinal ordinal2("\1\1\1\1\1\1\1\3");
  EXPECT_EQ("\1\1\1\1\1\1\1\2",
            ordinal1.CreateBetween(ordinal2).ToInternalValue());
}

}  // namespace

}  // namespace syncer