| 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/base/unique_position.h" | 5 #include "sync/internal_api/public/base/unique_position.h" |
| 6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 7 #include <algorithm> | 9 #include <algorithm> |
| 8 #include <string> | 10 #include <string> |
| 9 | 11 |
| 10 #include "base/base64.h" | 12 #include "base/base64.h" |
| 11 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/sha1.h" | 16 #include "base/sha1.h" |
| 15 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 16 #include "sync/protocol/unique_position.pb.h" | 18 #include "sync/protocol/unique_position.pb.h" |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 // suffix format is a base64'ed SHA1 hash, which should be fairly close to | 393 // suffix format is a base64'ed SHA1 hash, which should be fairly close to |
| 392 // random anyway. | 394 // random anyway. |
| 393 std::string input = cache_guid_ + base::Int64ToString(next_id_--); | 395 std::string input = cache_guid_ + base::Int64ToString(next_id_--); |
| 394 std::string output; | 396 std::string output; |
| 395 base::Base64Encode(base::SHA1HashString(input), &output); | 397 base::Base64Encode(base::SHA1HashString(input), &output); |
| 396 return output; | 398 return output; |
| 397 } | 399 } |
| 398 | 400 |
| 399 private: | 401 private: |
| 400 const std::string cache_guid_; | 402 const std::string cache_guid_; |
| 401 int64 next_id_; | 403 int64_t next_id_; |
| 402 }; | 404 }; |
| 403 | 405 |
| 404 // Cache guids generated in the same style as real clients. | 406 // Cache guids generated in the same style as real clients. |
| 405 static const char kCacheGuidStr1[] = "tuiWdG8hV+8y4RT9N5Aikg=="; | 407 static const char kCacheGuidStr1[] = "tuiWdG8hV+8y4RT9N5Aikg=="; |
| 406 static const char kCacheGuidStr2[] = "yaKb7zHtY06aue9a0vlZgw=="; | 408 static const char kCacheGuidStr2[] = "yaKb7zHtY06aue9a0vlZgw=="; |
| 407 | 409 |
| 408 class PositionScenariosTest : public UniquePositionTest { | 410 class PositionScenariosTest : public UniquePositionTest { |
| 409 public: | 411 public: |
| 410 PositionScenariosTest() | 412 PositionScenariosTest() |
| 411 : generator1_(std::string(kCacheGuidStr1, arraysize(kCacheGuidStr1)-1)), | 413 : generator1_(std::string(kCacheGuidStr1, arraysize(kCacheGuidStr1)-1)), |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 INSTANTIATE_TEST_CASE_P(NormalSuffix, PositionInsertTest, | 496 INSTANTIATE_TEST_CASE_P(NormalSuffix, PositionInsertTest, |
| 495 ::testing::Values(kNormalSuffix)); | 497 ::testing::Values(kNormalSuffix)); |
| 496 | 498 |
| 497 class PositionFromIntTest : public UniquePositionTest { | 499 class PositionFromIntTest : public UniquePositionTest { |
| 498 public: | 500 public: |
| 499 PositionFromIntTest() | 501 PositionFromIntTest() |
| 500 : generator_(std::string(kCacheGuidStr1, arraysize(kCacheGuidStr1)-1)) { | 502 : generator_(std::string(kCacheGuidStr1, arraysize(kCacheGuidStr1)-1)) { |
| 501 } | 503 } |
| 502 | 504 |
| 503 protected: | 505 protected: |
| 504 static const int64 kTestValues[]; | 506 static const int64_t kTestValues[]; |
| 505 static const size_t kNumTestValues; | 507 static const size_t kNumTestValues; |
| 506 | 508 |
| 507 std::string NextSuffix() { | 509 std::string NextSuffix() { |
| 508 return generator_.NextSuffix(); | 510 return generator_.NextSuffix(); |
| 509 } | 511 } |
| 510 | 512 |
| 511 private: | 513 private: |
| 512 SuffixGenerator generator_; | 514 SuffixGenerator generator_; |
| 513 }; | 515 }; |
| 514 | 516 |
| 515 const int64 PositionFromIntTest::kTestValues[] = { | 517 const int64_t PositionFromIntTest::kTestValues[] = {0LL, |
| 516 0LL, | 518 1LL, |
| 517 1LL, -1LL, | 519 -1LL, |
| 518 2LL, -2LL, | 520 2LL, |
| 519 3LL, -3LL, | 521 -2LL, |
| 520 0x79LL, -0x79LL, | 522 3LL, |
| 521 0x80LL, -0x80LL, | 523 -3LL, |
| 522 0x81LL, -0x81LL, | 524 0x79LL, |
| 523 0xFELL, -0xFELL, | 525 -0x79LL, |
| 524 0xFFLL, -0xFFLL, | 526 0x80LL, |
| 525 0x100LL, -0x100LL, | 527 -0x80LL, |
| 526 0x101LL, -0x101LL, | 528 0x81LL, |
| 527 0xFA1AFELL, -0xFA1AFELL, | 529 -0x81LL, |
| 528 0xFFFFFFFELL, -0xFFFFFFFELL, | 530 0xFELL, |
| 529 0xFFFFFFFFLL, -0xFFFFFFFFLL, | 531 -0xFELL, |
| 530 0x100000000LL, -0x100000000LL, | 532 0xFFLL, |
| 531 0x100000001LL, -0x100000001LL, | 533 -0xFFLL, |
| 532 0xFFFFFFFFFFLL, -0xFFFFFFFFFFLL, | 534 0x100LL, |
| 533 0x112358132134LL, -0x112358132134LL, | 535 -0x100LL, |
| 534 0xFEFFBEEFABC1234LL, -0xFEFFBEEFABC1234LL, | 536 0x101LL, |
| 535 kint64max, | 537 -0x101LL, |
| 536 kint64min, | 538 0xFA1AFELL, |
| 537 kint64min + 1, | 539 -0xFA1AFELL, |
| 538 kint64max - 1 | 540 0xFFFFFFFELL, |
| 539 }; | 541 -0xFFFFFFFELL, |
| 542 0xFFFFFFFFLL, |
| 543 -0xFFFFFFFFLL, |
| 544 0x100000000LL, |
| 545 -0x100000000LL, |
| 546 0x100000001LL, |
| 547 -0x100000001LL, |
| 548 0xFFFFFFFFFFLL, |
| 549 -0xFFFFFFFFFFLL, |
| 550 0x112358132134LL, |
| 551 -0x112358132134LL, |
| 552 0xFEFFBEEFABC1234LL, |
| 553 -0xFEFFBEEFABC1234LL, |
| 554 INT64_MAX, |
| 555 INT64_MIN, |
| 556 INT64_MIN + 1, |
| 557 INT64_MAX - 1}; |
| 540 | 558 |
| 541 const size_t PositionFromIntTest::kNumTestValues = | 559 const size_t PositionFromIntTest::kNumTestValues = |
| 542 arraysize(PositionFromIntTest::kTestValues); | 560 arraysize(PositionFromIntTest::kTestValues); |
| 543 | 561 |
| 544 TEST_F(PositionFromIntTest, IsValid) { | 562 TEST_F(PositionFromIntTest, IsValid) { |
| 545 for (size_t i = 0; i < kNumTestValues; ++i) { | 563 for (size_t i = 0; i < kNumTestValues; ++i) { |
| 546 const UniquePosition pos = | 564 const UniquePosition pos = |
| 547 UniquePosition::FromInt64(kTestValues[i], NextSuffix()); | 565 UniquePosition::FromInt64(kTestValues[i], NextSuffix()); |
| 548 EXPECT_TRUE(pos.IsValid()) << "i = " << i << "; " << pos.ToDebugString(); | 566 EXPECT_TRUE(pos.IsValid()) << "i = " << i << "; " << pos.ToDebugString(); |
| 549 } | 567 } |
| 550 } | 568 } |
| 551 | 569 |
| 552 TEST_F(PositionFromIntTest, RoundTripConversion) { | 570 TEST_F(PositionFromIntTest, RoundTripConversion) { |
| 553 for (size_t i = 0; i < kNumTestValues; ++i) { | 571 for (size_t i = 0; i < kNumTestValues; ++i) { |
| 554 const int64 expected_value = kTestValues[i]; | 572 const int64_t expected_value = kTestValues[i]; |
| 555 const UniquePosition pos = | 573 const UniquePosition pos = |
| 556 UniquePosition::FromInt64(kTestValues[i], NextSuffix()); | 574 UniquePosition::FromInt64(kTestValues[i], NextSuffix()); |
| 557 const int64 value = pos.ToInt64(); | 575 const int64_t value = pos.ToInt64(); |
| 558 EXPECT_EQ(expected_value, value) << "i = " << i; | 576 EXPECT_EQ(expected_value, value) << "i = " << i; |
| 559 } | 577 } |
| 560 } | 578 } |
| 561 | 579 |
| 562 template <typename T, typename LessThan = std::less<T> > | 580 template <typename T, typename LessThan = std::less<T> > |
| 563 class IndexedLessThan { | 581 class IndexedLessThan { |
| 564 public: | 582 public: |
| 565 IndexedLessThan(const T* values) : values_(values) {} | 583 IndexedLessThan(const T* values) : values_(values) {} |
| 566 | 584 |
| 567 bool operator()(int i1, int i2) { | 585 bool operator()(int i1, int i2) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 578 std::vector<int> original_ordering(kNumTestValues); | 596 std::vector<int> original_ordering(kNumTestValues); |
| 579 std::vector<int> int64_ordering(kNumTestValues); | 597 std::vector<int> int64_ordering(kNumTestValues); |
| 580 std::vector<int> position_ordering(kNumTestValues); | 598 std::vector<int> position_ordering(kNumTestValues); |
| 581 for (size_t i = 0; i < kNumTestValues; ++i) { | 599 for (size_t i = 0; i < kNumTestValues; ++i) { |
| 582 positions[i] = UniquePosition::FromInt64( | 600 positions[i] = UniquePosition::FromInt64( |
| 583 kTestValues[i], NextSuffix()); | 601 kTestValues[i], NextSuffix()); |
| 584 original_ordering[i] = int64_ordering[i] = position_ordering[i] = i; | 602 original_ordering[i] = int64_ordering[i] = position_ordering[i] = i; |
| 585 } | 603 } |
| 586 | 604 |
| 587 std::sort(int64_ordering.begin(), int64_ordering.end(), | 605 std::sort(int64_ordering.begin(), int64_ordering.end(), |
| 588 IndexedLessThan<int64>(kTestValues)); | 606 IndexedLessThan<int64_t>(kTestValues)); |
| 589 std::sort(position_ordering.begin(), position_ordering.end(), | 607 std::sort(position_ordering.begin(), position_ordering.end(), |
| 590 IndexedLessThan<UniquePosition, PositionLessThan>(positions)); | 608 IndexedLessThan<UniquePosition, PositionLessThan>(positions)); |
| 591 EXPECT_NE(original_ordering, int64_ordering); | 609 EXPECT_NE(original_ordering, int64_ordering); |
| 592 EXPECT_EQ(int64_ordering, position_ordering); | 610 EXPECT_EQ(int64_ordering, position_ordering); |
| 593 } | 611 } |
| 594 | 612 |
| 595 class CompressedPositionTest : public UniquePositionTest { | 613 class CompressedPositionTest : public UniquePositionTest { |
| 596 public: | 614 public: |
| 597 CompressedPositionTest() { | 615 CompressedPositionTest() { |
| 598 positions_.push_back( | 616 positions_.push_back( |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 // This requires values in the test harness to be hard-coded in ascending order. | 689 // This requires values in the test harness to be hard-coded in ascending order. |
| 672 TEST_F(CompressedPositionTest, OrderingTest) { | 690 TEST_F(CompressedPositionTest, OrderingTest) { |
| 673 for (size_t i = 0; i < positions_.size()-1; ++i) { | 691 for (size_t i = 0; i < positions_.size()-1; ++i) { |
| 674 EXPECT_PRED_FORMAT2(LessThan, positions_[i], positions_[i+1]); | 692 EXPECT_PRED_FORMAT2(LessThan, positions_[i], positions_[i+1]); |
| 675 } | 693 } |
| 676 } | 694 } |
| 677 | 695 |
| 678 } // namespace | 696 } // namespace |
| 679 | 697 |
| 680 } // namespace syncer | 698 } // namespace syncer |
| OLD | NEW |