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/memory/scoped_ptr.h" 10 #include "base/test/values_test_util.h" 11 #include "base/values.h" 12 #include "sync/test/engine/test_id_factory.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace syncer { 16 namespace syncable { 17 18 using std::vector; 19 20 class SyncableIdTest : public testing::Test { }; 21 22 TEST(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 46 TEST(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 86 TEST(SyncableIdTest, ToValue) { 87 base::ExpectStringValue("r", Id::CreateFromServerId("0").ToValue()); 88 base::ExpectStringValue("svalue", Id::CreateFromServerId("value").ToValue()); 89 90 base::ExpectStringValue("r", Id::CreateFromClientString("0").ToValue()); 91 base::ExpectStringValue("cvalue", 92 Id::CreateFromClientString("value").ToValue()); 93 } 94 95 } // namespace syncable 96 } // namespace syncer 97