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 <algorithm> 6 7 #include "base/strings/string16.h" 8 #include "base/strings/utf_string_conversions.h" 9 #include "chrome/browser/history/in_memory_url_index_types.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace history { 13 14 // Helper function for verifying that the contents of a C++ iterable container 15 // of ints matches a C array ints. 16 template <typename T> 17 bool IntArraysEqual(const size_t* expected, 18 size_t expected_size, 19 const T& actual) { 20 if (expected_size != actual.size()) 21 return false; 22 for (size_t i = 0; i < expected_size; ++i) 23 if (expected[i] != actual[i]) 24 return false; 25 return true; 26 } 27 28 class InMemoryURLIndexTypesTest : public testing::Test { 29 }; 30 31 TEST_F(InMemoryURLIndexTypesTest, StaticFunctions) { 32 // Test String16VectorFromString16 33 string16 string_a(ASCIIToUTF16("http://www.google.com/ frammy the brammy")); 34 WordStarts actual_starts_a; 35 String16Vector string_vec = 36 String16VectorFromString16(string_a, false, &actual_starts_a); 37 ASSERT_EQ(7U, string_vec.size()); 38 // See if we got the words we expected. 39 EXPECT_EQ(UTF8ToUTF16("http"), string_vec[0]); 40 EXPECT_EQ(UTF8ToUTF16("www"), string_vec[1]); 41 EXPECT_EQ(UTF8ToUTF16("google"), string_vec[2]); 42 EXPECT_EQ(UTF8ToUTF16("com"), string_vec[3]); 43 EXPECT_EQ(UTF8ToUTF16("frammy"), string_vec[4]); 44 EXPECT_EQ(UTF8ToUTF16("the"), string_vec[5]); 45 EXPECT_EQ(UTF8ToUTF16("brammy"), string_vec[6]); 46 // Verify the word starts. 47 size_t expected_starts_a[] = {0, 7, 11, 18, 23, 31, 35}; 48 EXPECT_TRUE(IntArraysEqual(expected_starts_a, arraysize(expected_starts_a), 49 actual_starts_a)); 50 51 WordStarts actual_starts_b; 52 string_vec = String16VectorFromString16(string_a, true, &actual_starts_b); 53 ASSERT_EQ(5U, string_vec.size()); 54 EXPECT_EQ(UTF8ToUTF16("http://"), string_vec[0]); 55 EXPECT_EQ(UTF8ToUTF16("www.google.com/"), string_vec[1]); 56 EXPECT_EQ(UTF8ToUTF16("frammy"), string_vec[2]); 57 EXPECT_EQ(UTF8ToUTF16("the"), string_vec[3]); 58 EXPECT_EQ(UTF8ToUTF16("brammy"), string_vec[4]); 59 size_t expected_starts_b[] = {0, 7, 23, 31, 35}; 60 EXPECT_TRUE(IntArraysEqual(expected_starts_b, arraysize(expected_starts_b), 61 actual_starts_b)); 62 63 string16 string_c(ASCIIToUTF16( 64 " funky%20string-with=@strange sequences, intended(to exceed)")); 65 WordStarts actual_starts_c; 66 string_vec = String16VectorFromString16(string_c, false, &actual_starts_c); 67 ASSERT_EQ(8U, string_vec.size()); 68 // Note that we stop collecting words and word starts at kMaxSignificantChars. 69 size_t expected_starts_c[] = {1, 7, 16, 22, 32, 43}; 70 EXPECT_TRUE(IntArraysEqual(expected_starts_c, arraysize(expected_starts_c), 71 actual_starts_c)); 72 73 // Test String16SetFromString16 74 string16 string_d(ASCIIToUTF16( 75 "http://web.google.com/search Google Web Search")); 76 WordStarts actual_starts_d; 77 String16Set string_set = String16SetFromString16(string_d, &actual_starts_d); 78 EXPECT_EQ(5U, string_set.size()); 79 // See if we got the words we expected. 80 EXPECT_TRUE(string_set.find(UTF8ToUTF16("com")) != string_set.end()); 81 EXPECT_TRUE(string_set.find(UTF8ToUTF16("google")) != string_set.end()); 82 EXPECT_TRUE(string_set.find(UTF8ToUTF16("http")) != string_set.end()); 83 EXPECT_TRUE(string_set.find(UTF8ToUTF16("search")) != string_set.end()); 84 EXPECT_TRUE(string_set.find(UTF8ToUTF16("web")) != string_set.end()); 85 size_t expected_starts_d[] = {0, 7, 11, 18, 22, 29, 36, 40}; 86 EXPECT_TRUE(IntArraysEqual(expected_starts_d, arraysize(expected_starts_d), 87 actual_starts_d)); 88 89 // Test SortAndDeoverlapMatches 90 TermMatches matches_e; 91 matches_e.push_back(TermMatch(1, 13, 10)); 92 matches_e.push_back(TermMatch(2, 23, 10)); 93 matches_e.push_back(TermMatch(3, 3, 10)); 94 matches_e.push_back(TermMatch(4, 40, 5)); 95 TermMatches matches_f = SortAndDeoverlapMatches(matches_e); 96 // Nothing should have been eliminated. 97 EXPECT_EQ(matches_e.size(), matches_f.size()); 98 // The order should now be 3, 1, 2, 4. 99 EXPECT_EQ(3, matches_f[0].term_num); 100 EXPECT_EQ(1, matches_f[1].term_num); 101 EXPECT_EQ(2, matches_f[2].term_num); 102 EXPECT_EQ(4, matches_f[3].term_num); 103 matches_e.push_back(TermMatch(5, 18, 10)); 104 matches_e.push_back(TermMatch(6, 38, 5)); 105 matches_f = SortAndDeoverlapMatches(matches_e); 106 // Two matches should have been eliminated. 107 EXPECT_EQ(matches_e.size() - 2, matches_f.size()); 108 // The order should now be 3, 1, 2, 6. 109 EXPECT_EQ(3, matches_f[0].term_num); 110 EXPECT_EQ(1, matches_f[1].term_num); 111 EXPECT_EQ(2, matches_f[2].term_num); 112 EXPECT_EQ(6, matches_f[3].term_num); 113 114 // Test MatchTermInString 115 TermMatches matches_g = MatchTermInString( 116 UTF8ToUTF16("x"), UTF8ToUTF16("axbxcxdxex fxgx/hxixjx.kx"), 123); 117 const size_t expected_offsets[] = { 1, 3, 5, 7, 9, 12, 14, 17, 19, 21, 24 }; 118 ASSERT_EQ(arraysize(expected_offsets), matches_g.size()); 119 for (size_t i = 0; i < arraysize(expected_offsets); ++i) 120 EXPECT_EQ(expected_offsets[i], matches_g[i].offset); 121 } 122 123 TEST_F(InMemoryURLIndexTypesTest, OffsetsAndTermMatches) { 124 // Test OffsetsFromTermMatches 125 history::TermMatches matches_a; 126 matches_a.push_back(history::TermMatch(1, 1, 2)); 127 matches_a.push_back(history::TermMatch(2, 4, 3)); 128 matches_a.push_back(history::TermMatch(3, 9, 1)); 129 matches_a.push_back(history::TermMatch(3, 10, 1)); 130 matches_a.push_back(history::TermMatch(4, 14, 5)); 131 std::vector<size_t> offsets = OffsetsFromTermMatches(matches_a); 132 const size_t expected_offsets_a[] = {1, 4, 9, 10, 14}; 133 ASSERT_EQ(offsets.size(), arraysize(expected_offsets_a)); 134 for (size_t i = 0; i < offsets.size(); ++i) 135 EXPECT_EQ(expected_offsets_a[i], offsets[i]); 136 137 // Test ReplaceOffsetsInTermMatches 138 offsets[2] = string16::npos; 139 history::TermMatches matches_b = 140 ReplaceOffsetsInTermMatches(matches_a, offsets); 141 const size_t expected_offsets_b[] = {1, 4, 10, 14}; 142 ASSERT_EQ(arraysize(expected_offsets_b), matches_b.size()); 143 for (size_t i = 0; i < matches_b.size(); ++i) 144 EXPECT_EQ(expected_offsets_b[i], matches_b[i].offset); 145 } 146 147 } // namespace history 148