| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "courgette/third_party/bsdiff/bsdiff_search.h" |
| 6 |
| 7 #include <cstring> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "courgette/third_party/bsdiff/qsufsort.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 TEST(BSDiffSearchTest, Search) { |
| 15 // Initialize main string and the suffix array. |
| 16 // Positions: 000000000011111111111222222222333333333344444 |
| 17 // 012345678901234567890123456789012345678901234 |
| 18 const char* str = "the quick brown fox jumps over the lazy dog."; |
| 19 int size = static_cast<int>(::strlen(str)); |
| 20 const unsigned char* buf = reinterpret_cast<const unsigned char*>(str); |
| 21 std::vector<int> I(size + 1); |
| 22 std::vector<int> V(size + 1); |
| 23 courgette::qsuf::qsufsort<int*>(&I[0], &V[0], buf, size); |
| 24 |
| 25 // Specific queries. |
| 26 const struct { |
| 27 int exp_pos; // -1 means "don't care". |
| 28 int exp_match_size; |
| 29 const char* query_str; |
| 30 } test_cases[] = { |
| 31 // Entire string: exact and unique. |
| 32 {0, 44, "the quick brown fox jumps over the lazy dog."}, |
| 33 // Empty string: exact and non-unique. |
| 34 {-1, 0, ""}, |
| 35 // Exact and unique suffix matches. |
| 36 {43, 1, "."}, |
| 37 {31, 13, "the lazy dog."}, |
| 38 // Exact and unique non-suffix matches. |
| 39 {4, 5, "quick"}, |
| 40 {0, 9, "the quick"}, // Unique prefix. |
| 41 // Partial and unique matches. |
| 42 {16, 10, "fox jumps with the hosps"}, // Unique prefix. |
| 43 {18, 1, "xyz"}, |
| 44 // Exact and non-unique match: take lexicographical first. |
| 45 {-1, 3, "the"}, // Non-unique prefix. |
| 46 {-1, 1, " "}, |
| 47 // Partial and non-unique match: no guarantees on |pos|! |
| 48 {-1, 4, "the apple"}, // query < "the l"... < "the q"... |
| 49 {-1, 4, "the opera"}, // "the l"... < query < "the q"... |
| 50 {-1, 4, "the zebra"}, // "the l"... < "the q"... < query |
| 51 // Prefix match dominates suffix match (unique). |
| 52 {26, 5, "over quick brown fox"}, |
| 53 // Empty matchs. |
| 54 {-1, 0, ","}, |
| 55 {-1, 0, "1234"}, |
| 56 {-1, 0, "THE QUICK BROWN FOX"}, |
| 57 {-1, 0, "(the"}, |
| 58 }; |
| 59 |
| 60 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) { |
| 61 const auto& test_case = test_cases[idx]; |
| 62 int query_size = static_cast<int>(::strlen(test_case.query_str)); |
| 63 const unsigned char* query_buf = |
| 64 reinterpret_cast<const unsigned char*>(test_case.query_str); |
| 65 |
| 66 // Perform the search. |
| 67 int pos = 0; |
| 68 int match_size = |
| 69 courgette::search(&I[0], buf, size, query_buf, query_size, &pos); |
| 70 |
| 71 // Check basic properties and match with expected values. |
| 72 EXPECT_GE(match_size, 0); |
| 73 EXPECT_LE(match_size, query_size); |
| 74 if (match_size > 0) { |
| 75 EXPECT_GE(pos, 0); |
| 76 EXPECT_LE(pos, size - match_size); |
| 77 EXPECT_EQ(0, ::memcmp(buf + pos, query_buf, match_size)); |
| 78 } |
| 79 if (test_case.exp_pos >= 0) { |
| 80 EXPECT_EQ(test_case.exp_pos, pos); |
| 81 } |
| 82 EXPECT_EQ(test_case.exp_match_size, match_size); |
| 83 } |
| 84 } |
| 85 |
| 86 TEST(BSDiffSearchTest, SearchExact) { |
| 87 const char* test_cases[] = { |
| 88 "a", |
| 89 "aa", |
| 90 "az", |
| 91 "za", |
| 92 "aaaaa", |
| 93 "CACAO", |
| 94 "banana", |
| 95 "tobeornottobe", |
| 96 "the quick brown fox jumps over the lazy dog.", |
| 97 "elephantelephantelephantelephantelephant", |
| 98 "011010011001011010010110011010010", |
| 99 }; |
| 100 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) { |
| 101 int size = static_cast<int>(::strlen(test_cases[idx])); |
| 102 const unsigned char* buf = |
| 103 reinterpret_cast<const unsigned char*>(test_cases[idx]); |
| 104 std::vector<int> I(size + 1); |
| 105 std::vector<int> V(size + 1); |
| 106 courgette::qsuf::qsufsort<int*>(&I[0], &V[0], buf, size); |
| 107 |
| 108 // Test exact matches for every non-empty substring. |
| 109 for (int lo = 0; lo < size; ++lo) { |
| 110 for (int hi = lo + 1; hi <= size; ++hi) { |
| 111 std::string query(buf + lo, buf + hi); |
| 112 int query_size = static_cast<int>(query.length()); |
| 113 ASSERT_EQ(query_size, hi - lo); |
| 114 const unsigned char* query_buf = |
| 115 reinterpret_cast<const unsigned char*>(query.c_str()); |
| 116 int pos = 0; |
| 117 int match_size = |
| 118 courgette::search(&I[0], buf, size, query_buf, query_size, &pos); |
| 119 |
| 120 EXPECT_EQ(query_size, match_size); |
| 121 EXPECT_GE(pos, 0); |
| 122 EXPECT_LE(pos, size - match_size); |
| 123 std::string suffix(buf + pos, buf + size); |
| 124 EXPECT_EQ(suffix.substr(0, query_size), query); |
| 125 } |
| 126 } |
| 127 } |
| 128 } |
| OLD | NEW |