Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: components/omnibox/browser/in_memory_url_index_unittest.cc

Issue 2702413007: Cleaning up test only variables. (Closed)
Patch Set: Review, round 1. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <fstream> 9 #include <fstream>
10 10
(...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 ASCIIToUTF16("atdmt view"), base::string16::npos, kMaxMatches); 703 ASCIIToUTF16("atdmt view"), base::string16::npos, kMaxMatches);
704 EXPECT_EQ(1U, matches.size()); 704 EXPECT_EQ(1U, matches.size());
705 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt.view"), 705 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt.view"),
706 base::string16::npos, kMaxMatches); 706 base::string16::npos, kMaxMatches);
707 EXPECT_EQ(0U, matches.size()); 707 EXPECT_EQ(0U, matches.size());
708 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("view.atdmt"), 708 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("view.atdmt"),
709 base::string16::npos, kMaxMatches); 709 base::string16::npos, kMaxMatches);
710 EXPECT_EQ(1U, matches.size()); 710 EXPECT_EQ(1U, matches.size());
711 } 711 }
712 712
713 TEST_F(InMemoryURLIndexTest, TrimHistortIds) {
714 // Constants ---------------------------------------------------------------
715
716 constexpr size_t kItemsToScoreLimit = 500;
717 constexpr size_t kAlmostLimit = kItemsToScoreLimit - 5;
718
719 constexpr int kLowTypedCount = 2;
720 constexpr int kHighTypedCount = 100;
721
722 constexpr int kLowVisitCount = 20;
723 constexpr int kHighVisitCount = 200;
724
725 constexpr base::TimeDelta kOld = base::TimeDelta::FromDays(15);
726 constexpr base::TimeDelta kNew = base::TimeDelta::FromDays(2);
727
728 constexpr int kMinRowId = 5000;
729
730 // Helpers -----------------------------------------------------------------
731
732 struct ItemGroup {
733 history::URLID min_id;
734 history::URLID max_id;
735 int typed_count;
736 int visit_count;
737 base::TimeDelta days_ago;
738 };
739
740 auto GetHistoryIdsUpTo = [](HistoryID max) {
741 HistoryIDSet res;
742 // All ids are inserted in the end so the implicit hint would work.
743 for (HistoryID id = kMinRowId; id < max; ++id)
744 res.insert(id);
745 return res;
746 };
747
748 auto CountGroupElementsInIds = [](const ItemGroup& group,
749 const HistoryIDSet& items) {
750 return std::count_if(items.begin(), items.end(), [&](history::URLID id) {
751 return group.min_id <= id && id < group.max_id;
752 });
753 };
754
755 // Test body --------------------------------------------------------------
756
757 // Groups of items ordered by increasing priority.
758 ItemGroup item_groups[] = {
759 {0, 0, kLowTypedCount, kLowVisitCount, kOld},
760 {0, 0, kLowTypedCount, kLowVisitCount, kNew},
761 {0, 0, kLowTypedCount, kHighVisitCount, kOld},
762 {0, 0, kLowTypedCount, kHighVisitCount, kNew},
763 {0, 0, kHighTypedCount, kLowVisitCount, kOld},
764 {0, 0, kHighTypedCount, kLowVisitCount, kNew},
765 {0, 0, kHighTypedCount, kHighVisitCount, kOld},
766 {0, 0, kHighTypedCount, kHighVisitCount, kNew},
767 };
768
769 // Initialize groups.
770 {
Peter Kasting 2017/02/24 01:37:41 Nit: This outer brace not needed (the win of sayin
dyaroshev 2017/02/24 02:14:25 Done.
771 history::URLID row_id = kMinRowId;
772 for (auto& group : item_groups) {
773 group.min_id = row_id;
774 for (size_t i = 0; i < kAlmostLimit; ++i) {
775 history::URLRow new_row(
776 GURL("http://www.fake_url" + std::to_string(row_id) + ".com"),
777 row_id);
778 new_row.set_typed_count(group.typed_count);
779 new_row.set_visit_count(group.visit_count);
780 new_row.set_last_visit(base::Time::Now() - group.days_ago);
781 UpdateURL(std::move(new_row));
782 ++row_id;
783 }
784 group.max_id = row_id;
785 }
786 }
787
788 // First group. Because number of entries is small enough no trimming occurs.
789 {
790 auto ids = GetHistoryIdsUpTo(item_groups[0].max_id);
791 EXPECT_FALSE(GetPrivateData()->TrimHistoryIdsPool(&ids));
792 EXPECT_EQ(kAlmostLimit, ids.size());
793 }
794
795 // Each next group should fill almost everything, while the previous group
796 // should occupy what's left.
797 auto error_position = std::adjacent_find(
798 std::begin(item_groups), std::end(item_groups),
799 [&](const ItemGroup& previous, const ItemGroup& current) {
800 auto ids = GetHistoryIdsUpTo(current.max_id);
801 EXPECT_TRUE(GetPrivateData()->TrimHistoryIdsPool(&ids));
802
803 size_t current_count = CountGroupElementsInIds(current, ids);
804 EXPECT_EQ(current_count, kAlmostLimit);
Peter Kasting 2017/02/24 01:37:41 Nit: (expected, actual)
dyaroshev 2017/02/24 02:14:25 Done.
805 if (current_count != kAlmostLimit)
806 return true;
807
808 size_t previous_count = CountGroupElementsInIds(previous, ids);
809 EXPECT_EQ(previous_count, kItemsToScoreLimit - kAlmostLimit);
810 return previous_count != kItemsToScoreLimit - kAlmostLimit;
811 });
812
813 EXPECT_TRUE(error_position == std::end(item_groups))
814 << "broken after: " << error_position - std::begin(item_groups);
815 }
816
713 TEST_F(InMemoryURLIndexTest, HugeResultSet) { 817 TEST_F(InMemoryURLIndexTest, HugeResultSet) {
714 // Create a huge set of qualifying history items. 818 // Create a huge set of qualifying history items.
715 for (history::URLID row_id = 5000; row_id < 6000; ++row_id) { 819 for (history::URLID row_id = 5000; row_id < 6000; ++row_id) {
716 history::URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), 820 history::URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"),
717 row_id); 821 row_id);
718 new_row.set_last_visit(base::Time::Now()); 822 new_row.set_last_visit(base::Time::Now());
719 EXPECT_TRUE(UpdateURL(new_row)); 823 EXPECT_TRUE(UpdateURL(new_row));
720 } 824 }
721 825
722 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms( 826 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(
723 ASCIIToUTF16("b"), base::string16::npos, kMaxMatches); 827 ASCIIToUTF16("b"), base::string16::npos, kMaxMatches);
724 URLIndexPrivateData& private_data(*GetPrivateData());
725 EXPECT_EQ(kMaxMatches, matches.size()); 828 EXPECT_EQ(kMaxMatches, matches.size());
726 // There are 7 matches already in the database.
727 EXPECT_EQ(1008U, private_data.pre_filter_item_count_);
728 EXPECT_EQ(500U, private_data.post_filter_item_count_);
729 EXPECT_EQ(kMaxMatches, private_data.post_scoring_item_count_);
730 } 829 }
731 830
732 TEST_F(InMemoryURLIndexTest, TitleSearch) { 831 TEST_F(InMemoryURLIndexTest, TitleSearch) {
733 // Signal if someone has changed the test DB. 832 // Signal if someone has changed the test DB.
734 EXPECT_EQ(30U, GetPrivateData()->history_info_map_.size()); 833 EXPECT_EQ(30U, GetPrivateData()->history_info_map_.size());
735 834
736 // Ensure title is being searched. 835 // Ensure title is being searched.
737 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms( 836 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(
738 ASCIIToUTF16("MORTGAGE RATE DROPS"), base::string16::npos, kMaxMatches); 837 ASCIIToUTF16("MORTGAGE RATE DROPS"), base::string16::npos, kMaxMatches);
739 ASSERT_EQ(1U, matches.size()); 838 ASSERT_EQ(1U, matches.size());
(...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after
1345 ASSERT_TRUE(GetCacheFilePath(&full_file_path)); 1444 ASSERT_TRUE(GetCacheFilePath(&full_file_path));
1346 std::vector<base::FilePath::StringType> actual_parts; 1445 std::vector<base::FilePath::StringType> actual_parts;
1347 full_file_path.GetComponents(&actual_parts); 1446 full_file_path.GetComponents(&actual_parts);
1348 ASSERT_EQ(expected_parts.size(), actual_parts.size()); 1447 ASSERT_EQ(expected_parts.size(), actual_parts.size());
1349 size_t count = expected_parts.size(); 1448 size_t count = expected_parts.size();
1350 for (size_t i = 0; i < count; ++i) 1449 for (size_t i = 0; i < count; ++i)
1351 EXPECT_EQ(expected_parts[i], actual_parts[i]); 1450 EXPECT_EQ(expected_parts[i], actual_parts[i]);
1352 // Must clear the history_dir_ to satisfy the dtor's DCHECK. 1451 // Must clear the history_dir_ to satisfy the dtor's DCHECK.
1353 set_history_dir(base::FilePath()); 1452 set_history_dir(base::FilePath());
1354 } 1453 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698