syncable_id_unittest.cc revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1// Copyright (c) 2011 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 "chrome/browser/sync/syncable/syncable_id.h"
6
7#include <vector>
8
9#include "base/memory/scoped_ptr.h"
10#include "base/values.h"
11#include "chrome/test/sync/engine/test_id_factory.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14using std::vector;
15
16namespace syncable {
17
18using browser_sync::TestIdFactory;
19
20class SyncableIdTest : public testing::Test { };
21
22TEST(SyncableIdTest, TestIDCreation) {
23  vector<Id> v;
24  v.push_back(TestIdFactory::FromNumber(5));
25  v.push_back(TestIdFactory::FromNumber(1));
26  v.push_back(TestIdFactory::FromNumber(-5));
27  v.push_back(TestIdFactory::MakeLocal("A"));
28  v.push_back(TestIdFactory::MakeLocal("B"));
29  v.push_back(TestIdFactory::MakeServer("A"));
30  v.push_back(TestIdFactory::MakeServer("B"));
31  v.push_back(Id::CreateFromServerId("-5"));
32  v.push_back(Id::CreateFromClientString("A"));
33  v.push_back(Id::CreateFromServerId("A"));
34
35  for (vector<Id>::iterator i = v.begin(); i != v.end(); ++i) {
36    for (vector<Id>::iterator j = v.begin(); j != i; ++j) {
37      ASSERT_NE(*i, *j) << "mis equated two distinct ids";
38    }
39    ASSERT_EQ(*i, *i) << "self-equality failed";
40    Id copy1 = *i;
41    Id copy2 = *i;
42    ASSERT_EQ(copy1, copy2) << "equality after copy failed";
43  }
44}
45
46TEST(SyncableIdTest, GetLeastIdForLexicographicComparison) {
47  vector<Id> v;
48  v.push_back(Id::CreateFromServerId("z5"));
49  v.push_back(Id::CreateFromServerId("z55"));
50  v.push_back(Id::CreateFromServerId("z6"));
51  v.push_back(Id::CreateFromClientString("zA-"));
52  v.push_back(Id::CreateFromClientString("zA--"));
53  v.push_back(Id::CreateFromServerId("zA--"));
54
55  for (int i = 0; i <= 255; ++i) {
56    std::string one_character_id;
57    one_character_id.push_back(i);
58    v.push_back(Id::CreateFromClientString(one_character_id));
59  }
60
61  for (vector<Id>::iterator i = v.begin(); i != v.end(); ++i) {
62    // The following looks redundant, but we're testing a custom operator<.
63    ASSERT_LT(Id::GetLeastIdForLexicographicComparison(), *i);
64    ASSERT_NE(*i, i->GetLexicographicSuccessor());
65    ASSERT_NE(i->GetLexicographicSuccessor(), *i);
66    ASSERT_LT(*i, i->GetLexicographicSuccessor());
67    ASSERT_GT(i->GetLexicographicSuccessor(), *i);
68    for (vector<Id>::iterator j = v.begin(); j != v.end(); ++j) {
69      if (j == i)
70        continue;
71      if (*j < *i) {
72        ASSERT_LT(j->GetLexicographicSuccessor(), *i);
73        ASSERT_LT(j->GetLexicographicSuccessor(),
74            i->GetLexicographicSuccessor());
75        ASSERT_LT(*j, i->GetLexicographicSuccessor());
76      } else {
77        ASSERT_GT(j->GetLexicographicSuccessor(), *i);
78        ASSERT_GT(j->GetLexicographicSuccessor(),
79            i->GetLexicographicSuccessor());
80        ASSERT_GT(*j, i->GetLexicographicSuccessor());
81      }
82    }
83  }
84}
85
86namespace {
87
88// TODO(akalin): Move this to values_test_util.h.
89
90// Takes ownership of |actual|.
91void ExpectStringValue(const std::string& expected_str,
92                       StringValue* actual) {
93  scoped_ptr<StringValue> scoped_actual(actual);
94  std::string actual_str;
95  EXPECT_TRUE(scoped_actual->GetAsString(&actual_str));
96  EXPECT_EQ(expected_str, actual_str);
97}
98
99}  // namespace
100
101TEST(SyncableIdTest, ToValue) {
102  ExpectStringValue("r", Id::CreateFromServerId("0").ToValue());
103  ExpectStringValue("svalue", Id::CreateFromServerId("value").ToValue());
104
105  ExpectStringValue("r", Id::CreateFromClientString("0").ToValue());
106  ExpectStringValue("cvalue", Id::CreateFromClientString("value").ToValue());
107}
108
109}  // namespace syncable
110