blob: e0b014387fbb86d5f2ae39e18627429fd64da4c7 [file] [log] [blame]
[email protected]6085ccd2012-02-29 03:57:291// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]5852edc1b2009-09-10 06:05:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]c1c32c82012-03-15 09:35:425#include "sync/syncable/syncable_id.h"
[email protected]5852edc1b2009-09-10 06:05:276
7#include <vector>
8
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/scoped_ptr.h"
[email protected]6085ccd2012-02-29 03:57:2910#include "base/test/values_test_util.h"
[email protected]4b0749102011-03-25 19:46:4111#include "base/values.h"
[email protected]c1c32c82012-03-15 09:35:4212#include "sync/test/engine/test_id_factory.h"
[email protected]5852edc1b2009-09-10 06:05:2713#include "testing/gtest/include/gtest/gtest.h"
14
[email protected]9cfc7c702012-07-02 22:54:1715namespace syncer {
[email protected]5852edc1b2009-09-10 06:05:2716namespace syncable {
17
[email protected]d45f0d92012-07-20 17:25:4118using std::vector;
[email protected]5852edc1b2009-09-10 06:05:2719
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
[email protected]aa39be82011-03-03 03:18:4746TEST(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
[email protected]4b0749102011-03-25 19:46:4186TEST(SyncableIdTest, ToValue) {
[email protected]6085ccd2012-02-29 03:57:2987 base::ExpectStringValue("r", Id::CreateFromServerId("0").ToValue());
88 base::ExpectStringValue("svalue", Id::CreateFromServerId("value").ToValue());
[email protected]4b0749102011-03-25 19:46:4189
[email protected]6085ccd2012-02-29 03:57:2990 base::ExpectStringValue("r", Id::CreateFromClientString("0").ToValue());
91 base::ExpectStringValue("cvalue",
[email protected]3d86d2f2011-04-26 01:02:0892 Id::CreateFromClientString("value").ToValue());
[email protected]4b0749102011-03-25 19:46:4193}
94
[email protected]5852edc1b2009-09-10 06:05:2795} // namespace syncable
[email protected]9cfc7c702012-07-02 22:54:1796} // namespace syncer