| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "sync/internal_api/public/util/immutable.h" | 5 #include "components/sync/base/immutable.h" | 
| 6 | 6 | 
| 7 #include <algorithm> | 7 #include <algorithm> | 
| 8 #include <cstddef> | 8 #include <cstddef> | 
| 9 #include <deque> | 9 #include <deque> | 
| 10 #include <list> | 10 #include <list> | 
| 11 #include <set> | 11 #include <set> | 
| 12 #include <string> | 12 #include <string> | 
| 13 #include <vector> | 13 #include <vector> | 
| 14 | 14 | 
| 15 #include "base/macros.h" | 15 #include "base/macros.h" | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 58   TokenBase(const TokenBase& other) : core_(other.core_) { | 58   TokenBase(const TokenBase& other) : core_(other.core_) { | 
| 59     core_->RecordCopy(); | 59     core_->RecordCopy(); | 
| 60   } | 60   } | 
| 61 | 61 | 
| 62   TokenBase& operator=(const TokenBase& other) { | 62   TokenBase& operator=(const TokenBase& other) { | 
| 63     core_ = other.core_; | 63     core_ = other.core_; | 
| 64     core_->RecordCopy(); | 64     core_->RecordCopy(); | 
| 65     return *this; | 65     return *this; | 
| 66   } | 66   } | 
| 67 | 67 | 
| 68   const char* GetToken() const { | 68   const char* GetToken() const { return core_->GetToken(); } | 
| 69     return core_->GetToken(); |  | 
| 70   } |  | 
| 71 | 69 | 
| 72   int GetCopyCount() const { | 70   int GetCopyCount() const { return core_->GetCopyCount(); } | 
| 73     return core_->GetCopyCount(); |  | 
| 74   } |  | 
| 75 | 71 | 
| 76   // For associative containers. | 72   // For associative containers. | 
| 77   bool operator<(const TokenBase& other) const { | 73   bool operator<(const TokenBase& other) const { | 
| 78     return std::string(GetToken()) < std::string(other.GetToken()); | 74     return std::string(GetToken()) < std::string(other.GetToken()); | 
| 79   } | 75   } | 
| 80 | 76 | 
| 81   // STL-style swap. | 77   // STL-style swap. | 
| 82   void swap(TokenBase& other) { | 78   void swap(TokenBase& other) { | 
| 83     using std::swap; | 79     using std::swap; | 
| 84     swap(other.core_, core_); | 80     swap(other.core_, core_); | 
| (...skipping 16 matching lines...) Expand all  Loading... | 
| 101 void swap(ADLToken& t1, ADLToken& t2) { | 97 void swap(ADLToken& t1, ADLToken& t2) { | 
| 102   t1.Swap(&t2); | 98   t1.Swap(&t2); | 
| 103 } | 99 } | 
| 104 | 100 | 
| 105 }  // namespace syncer | 101 }  // namespace syncer | 
| 106 | 102 | 
| 107 // Allowed by the standard (17.4.3.1/1). | 103 // Allowed by the standard (17.4.3.1/1). | 
| 108 namespace std { | 104 namespace std { | 
| 109 | 105 | 
| 110 template <> | 106 template <> | 
| 111 void swap(syncer::SpecializationToken& t1, | 107 void swap(syncer::SpecializationToken& t1, syncer::SpecializationToken& t2) { | 
| 112           syncer::SpecializationToken& t2) { |  | 
| 113   t1.Swap(&t2); | 108   t1.Swap(&t2); | 
| 114 } | 109 } | 
| 115 | 110 | 
| 116 }  // namespace std | 111 }  // namespace std | 
| 117 | 112 | 
| 118 namespace syncer { | 113 namespace syncer { | 
| 119 namespace { | 114 namespace { | 
| 120 | 115 | 
| 121 class ImmutableTest : public ::testing::Test {}; | 116 class ImmutableTest : public ::testing::Test {}; | 
| 122 | 117 | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 158   EXPECT_EQ(0, t.GetCopyCount()); | 153   EXPECT_EQ(0, t.GetCopyCount()); | 
| 159 | 154 | 
| 160   ImmutableT immutable_t(&t); | 155   ImmutableT immutable_t(&t); | 
| 161   EXPECT_EQ(token, immutable_t.Get().GetToken()); | 156   EXPECT_EQ(token, immutable_t.Get().GetToken()); | 
| 162   EXPECT_EQ(kEmptyToken, t.GetToken()); | 157   EXPECT_EQ(kEmptyToken, t.GetToken()); | 
| 163   EXPECT_EQ(expect_copies, immutable_t.Get().GetCopyCount() > 0); | 158   EXPECT_EQ(expect_copies, immutable_t.Get().GetCopyCount() > 0); | 
| 164   EXPECT_EQ(expect_copies, t.GetCopyCount() > 0); | 159   EXPECT_EQ(expect_copies, t.GetCopyCount() > 0); | 
| 165 } | 160 } | 
| 166 | 161 | 
| 167 TEST_F(ImmutableTest, Token) { | 162 TEST_F(ImmutableTest, Token) { | 
| 168   RunTokenTest<Token, Immutable<Token> >("Token", true /* expect_copies */); | 163   RunTokenTest<Token, Immutable<Token>>("Token", true /* expect_copies */); | 
| 169 } | 164 } | 
| 170 | 165 | 
| 171 TEST_F(ImmutableTest, TokenSwapMemFnByRef) { | 166 TEST_F(ImmutableTest, TokenSwapMemFnByRef) { | 
| 172   RunTokenTest<Token, Immutable<Token, HasSwapMemFnByRef<Token> > >( | 167   RunTokenTest<Token, Immutable<Token, HasSwapMemFnByRef<Token>>>( | 
| 173       "TokenSwapMemFnByRef", false /* expect_copies */); | 168       "TokenSwapMemFnByRef", false /* expect_copies */); | 
| 174 } | 169 } | 
| 175 | 170 | 
| 176 TEST_F(ImmutableTest, TokenSwapMemFnByPtr) { | 171 TEST_F(ImmutableTest, TokenSwapMemFnByPtr) { | 
| 177   RunTokenTest<Token, Immutable<Token, HasSwapMemFnByPtr<Token> > >( | 172   RunTokenTest<Token, Immutable<Token, HasSwapMemFnByPtr<Token>>>( | 
| 178       "TokenSwapMemFnByPtr", false /* expect_copies */); | 173       "TokenSwapMemFnByPtr", false /* expect_copies */); | 
| 179 } | 174 } | 
| 180 | 175 | 
| 181 TEST_F(ImmutableTest, ADLToken) { | 176 TEST_F(ImmutableTest, ADLToken) { | 
| 182   RunTokenTest<ADLToken, Immutable<ADLToken> >( | 177   RunTokenTest<ADLToken, Immutable<ADLToken>>("ADLToken", | 
| 183       "ADLToken", false /* expect_copies */); | 178                                               false /* expect_copies */); | 
| 184 } | 179 } | 
| 185 | 180 | 
| 186 TEST_F(ImmutableTest, SpecializationToken) { | 181 TEST_F(ImmutableTest, SpecializationToken) { | 
| 187   RunTokenTest<SpecializationToken, Immutable<SpecializationToken> >( | 182   RunTokenTest<SpecializationToken, Immutable<SpecializationToken>>( | 
| 188       "SpecializationToken", false /* expect_copies */); | 183       "SpecializationToken", false /* expect_copies */); | 
| 189 } | 184 } | 
| 190 | 185 | 
| 191 template <typename C, typename ImmutableC> | 186 template <typename C, typename ImmutableC> | 
| 192 void RunTokenContainerTest(const char* token) { | 187 void RunTokenContainerTest(const char* token) { | 
| 193   SCOPED_TRACE(token); | 188   SCOPED_TRACE(token); | 
| 194   const Token tokens[] = { Token(), Token(token) }; | 189   const Token tokens[] = {Token(), Token(token)}; | 
| 195   const size_t token_count = arraysize(tokens); | 190   const size_t token_count = arraysize(tokens); | 
| 196   C c(tokens, tokens + token_count); | 191   C c(tokens, tokens + token_count); | 
| 197   const int copy_count = c.begin()->GetCopyCount(); | 192   const int copy_count = c.begin()->GetCopyCount(); | 
| 198   EXPECT_GT(copy_count, 0); | 193   EXPECT_GT(copy_count, 0); | 
| 199   for (typename C::const_iterator it = c.begin(); it != c.end(); ++it) { | 194   for (typename C::const_iterator it = c.begin(); it != c.end(); ++it) { | 
| 200     EXPECT_EQ(copy_count, it->GetCopyCount()); | 195     EXPECT_EQ(copy_count, it->GetCopyCount()); | 
| 201   } | 196   } | 
| 202 | 197 | 
| 203   // Make sure that making the container immutable doesn't incur any | 198   // Make sure that making the container immutable doesn't incur any | 
| 204   // copies of the tokens. | 199   // copies of the tokens. | 
| 205   ImmutableC immutable_c(&c); | 200   ImmutableC immutable_c(&c); | 
| 206   EXPECT_TRUE(c.empty()); | 201   EXPECT_TRUE(c.empty()); | 
| 207   ASSERT_EQ(token_count, immutable_c.Get().size()); | 202   ASSERT_EQ(token_count, immutable_c.Get().size()); | 
| 208   int i = 0; | 203   int i = 0; | 
| 209   for (typename C::const_iterator it = c.begin(); it != c.end(); ++it) { | 204   for (typename C::const_iterator it = c.begin(); it != c.end(); ++it) { | 
| 210     EXPECT_EQ(tokens[i].GetToken(), it->GetToken()); | 205     EXPECT_EQ(tokens[i].GetToken(), it->GetToken()); | 
| 211     EXPECT_EQ(copy_count, it->GetCopyCount()); | 206     EXPECT_EQ(copy_count, it->GetCopyCount()); | 
| 212     ++i; | 207     ++i; | 
| 213   } | 208   } | 
| 214 } | 209 } | 
| 215 | 210 | 
| 216 TEST_F(ImmutableTest, Vector) { | 211 TEST_F(ImmutableTest, Vector) { | 
| 217   RunTokenContainerTest<std::vector<Token>, Immutable<std::vector<Token> > >( | 212   RunTokenContainerTest<std::vector<Token>, Immutable<std::vector<Token>>>( | 
| 218       "Vector"); | 213       "Vector"); | 
| 219 } | 214 } | 
| 220 | 215 | 
| 221 TEST_F(ImmutableTest, VectorSwapMemFnByRef) { | 216 TEST_F(ImmutableTest, VectorSwapMemFnByRef) { | 
| 222   RunTokenContainerTest< | 217   RunTokenContainerTest< | 
| 223     std::vector<Token>, | 218       std::vector<Token>, | 
| 224     Immutable<std::vector<Token>, HasSwapMemFnByRef<std::vector<Token> > > >( | 219       Immutable<std::vector<Token>, HasSwapMemFnByRef<std::vector<Token>>>>( | 
| 225         "VectorSwapMemFnByRef"); | 220       "VectorSwapMemFnByRef"); | 
| 226 } | 221 } | 
| 227 | 222 | 
| 228 TEST_F(ImmutableTest, Deque) { | 223 TEST_F(ImmutableTest, Deque) { | 
| 229   RunTokenContainerTest<std::deque<Token>, Immutable<std::deque<Token> > >( | 224   RunTokenContainerTest<std::deque<Token>, Immutable<std::deque<Token>>>( | 
| 230       "Deque"); | 225       "Deque"); | 
| 231 } | 226 } | 
| 232 | 227 | 
| 233 TEST_F(ImmutableTest, List) { | 228 TEST_F(ImmutableTest, List) { | 
| 234   RunTokenContainerTest<std::list<Token>, Immutable<std::list<Token> > >( | 229   RunTokenContainerTest<std::list<Token>, Immutable<std::list<Token>>>("List"); | 
| 235       "List"); |  | 
| 236 } | 230 } | 
| 237 | 231 | 
| 238 TEST_F(ImmutableTest, Set) { | 232 TEST_F(ImmutableTest, Set) { | 
| 239   RunTokenContainerTest<std::set<Token>, Immutable<std::set<Token> > >( | 233   RunTokenContainerTest<std::set<Token>, Immutable<std::set<Token>>>("Set"); | 
| 240       "Set"); |  | 
| 241 } | 234 } | 
| 242 | 235 | 
| 243 }  // namespace | 236 }  // namespace | 
| 244 }  // namespace syncer | 237 }  // namespace syncer | 
| OLD | NEW | 
|---|