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