| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "courgette/third_party/bsdiff/bsdiff_search.h" | 5 #include "courgette/third_party/bsdiff/bsdiff_search.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "courgette/third_party/bsdiff/qsufsort.h" | 11 #include "courgette/third_party/bsdiff/paged_array.h" |
| 12 #include "courgette/third_party/divsufsort/divsufsort.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 TEST(BSDiffSearchTest, Search) { | 15 TEST(BSDiffSearchTest, Search) { |
| 15 // Initialize main string and the suffix array. | 16 // Initialize main string and the suffix array. |
| 16 // Positions: 000000000011111111111222222222333333333344444 | 17 // Positions: 000000000011111111111222222222333333333344444 |
| 17 // 012345678901234567890123456789012345678901234 | 18 // 012345678901234567890123456789012345678901234 |
| 18 const char* str = "the quick brown fox jumps over the lazy dog."; | 19 const char* str = "the quick brown fox jumps over the lazy dog."; |
| 19 int size = static_cast<int>(::strlen(str)); | 20 int size = static_cast<int>(::strlen(str)); |
| 20 const unsigned char* buf = reinterpret_cast<const unsigned char*>(str); | 21 const unsigned char* buf = reinterpret_cast<const unsigned char*>(str); |
| 21 std::vector<int> I(size + 1); | 22 courgette::PagedArray<divsuf::saidx_t> I; |
| 22 std::vector<int> V(size + 1); | 23 ASSERT_TRUE(I.Allocate(size + 1)); |
| 23 courgette::qsuf::qsufsort<int*>(&I[0], &V[0], buf, size); | 24 I[0] = size; |
| 25 divsuf::divsufsort(buf, I.begin() + 1, size); |
| 24 | 26 |
| 25 // Specific queries. | 27 // Specific queries. |
| 26 const struct { | 28 const struct { |
| 27 int exp_pos; // -1 means "don't care". | 29 int exp_pos; // -1 means "don't care". |
| 28 int exp_match_size; | 30 int exp_match_size; |
| 29 const char* query_str; | 31 const char* query_str; |
| 30 } test_cases[] = { | 32 } test_cases[] = { |
| 31 // Entire string: exact and unique. | 33 // Entire string: exact and unique. |
| 32 {0, 44, "the quick brown fox jumps over the lazy dog."}, | 34 {0, 44, "the quick brown fox jumps over the lazy dog."}, |
| 33 // Empty string: exact and non-unique. | 35 // Empty string: exact and non-unique. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 59 | 61 |
| 60 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) { | 62 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) { |
| 61 const auto& test_case = test_cases[idx]; | 63 const auto& test_case = test_cases[idx]; |
| 62 int query_size = static_cast<int>(::strlen(test_case.query_str)); | 64 int query_size = static_cast<int>(::strlen(test_case.query_str)); |
| 63 const unsigned char* query_buf = | 65 const unsigned char* query_buf = |
| 64 reinterpret_cast<const unsigned char*>(test_case.query_str); | 66 reinterpret_cast<const unsigned char*>(test_case.query_str); |
| 65 | 67 |
| 66 // Perform the search. | 68 // Perform the search. |
| 67 int pos = 0; | 69 int pos = 0; |
| 68 int match_size = | 70 int match_size = |
| 69 courgette::search(&I[0], buf, size, query_buf, query_size, &pos); | 71 bsdiff::search(&I[0], buf, size, query_buf, query_size, &pos); |
| 70 | 72 |
| 71 // Check basic properties and match with expected values. | 73 // Check basic properties and match with expected values. |
| 72 EXPECT_GE(match_size, 0); | 74 EXPECT_GE(match_size, 0); |
| 73 EXPECT_LE(match_size, query_size); | 75 EXPECT_LE(match_size, query_size); |
| 74 if (match_size > 0) { | 76 if (match_size > 0) { |
| 75 EXPECT_GE(pos, 0); | 77 EXPECT_GE(pos, 0); |
| 76 EXPECT_LE(pos, size - match_size); | 78 EXPECT_LE(pos, size - match_size); |
| 77 EXPECT_EQ(0, ::memcmp(buf + pos, query_buf, match_size)); | 79 EXPECT_EQ(0, ::memcmp(buf + pos, query_buf, match_size)); |
| 78 } | 80 } |
| 79 if (test_case.exp_pos >= 0) { | 81 if (test_case.exp_pos >= 0) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 94 "banana", | 96 "banana", |
| 95 "tobeornottobe", | 97 "tobeornottobe", |
| 96 "the quick brown fox jumps over the lazy dog.", | 98 "the quick brown fox jumps over the lazy dog.", |
| 97 "elephantelephantelephantelephantelephant", | 99 "elephantelephantelephantelephantelephant", |
| 98 "011010011001011010010110011010010", | 100 "011010011001011010010110011010010", |
| 99 }; | 101 }; |
| 100 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) { | 102 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) { |
| 101 int size = static_cast<int>(::strlen(test_cases[idx])); | 103 int size = static_cast<int>(::strlen(test_cases[idx])); |
| 102 const unsigned char* buf = | 104 const unsigned char* buf = |
| 103 reinterpret_cast<const unsigned char*>(test_cases[idx]); | 105 reinterpret_cast<const unsigned char*>(test_cases[idx]); |
| 104 std::vector<int> I(size + 1); | 106 courgette::PagedArray<divsuf::saidx_t> I; |
| 105 std::vector<int> V(size + 1); | 107 ASSERT_TRUE(I.Allocate(size + 1)); |
| 106 courgette::qsuf::qsufsort<int*>(&I[0], &V[0], buf, size); | 108 I[0] = size; |
| 109 divsuf::divsufsort(buf, I.begin() + 1, size); |
| 107 | 110 |
| 108 // Test exact matches for every non-empty substring. | 111 // Test exact matches for every non-empty substring. |
| 109 for (int lo = 0; lo < size; ++lo) { | 112 for (int lo = 0; lo < size; ++lo) { |
| 110 for (int hi = lo + 1; hi <= size; ++hi) { | 113 for (int hi = lo + 1; hi <= size; ++hi) { |
| 111 std::string query(buf + lo, buf + hi); | 114 std::string query(buf + lo, buf + hi); |
| 112 int query_size = static_cast<int>(query.length()); | 115 int query_size = static_cast<int>(query.length()); |
| 113 ASSERT_EQ(query_size, hi - lo); | 116 ASSERT_EQ(query_size, hi - lo); |
| 114 const unsigned char* query_buf = | 117 const unsigned char* query_buf = |
| 115 reinterpret_cast<const unsigned char*>(query.c_str()); | 118 reinterpret_cast<const unsigned char*>(query.c_str()); |
| 116 int pos = 0; | 119 int pos = 0; |
| 117 int match_size = | 120 int match_size = |
| 118 courgette::search(&I[0], buf, size, query_buf, query_size, &pos); | 121 bsdiff::search(&I[0], buf, size, query_buf, query_size, &pos); |
| 119 | 122 |
| 120 EXPECT_EQ(query_size, match_size); | 123 EXPECT_EQ(query_size, match_size); |
| 121 EXPECT_GE(pos, 0); | 124 EXPECT_GE(pos, 0); |
| 122 EXPECT_LE(pos, size - match_size); | 125 EXPECT_LE(pos, size - match_size); |
| 123 std::string suffix(buf + pos, buf + size); | 126 std::string suffix(buf + pos, buf + size); |
| 124 EXPECT_EQ(suffix.substr(0, query_size), query); | 127 EXPECT_EQ(suffix.substr(0, query_size), query); |
| 125 } | 128 } |
| 126 } | 129 } |
| 127 } | 130 } |
| 128 } | 131 } |
| OLD | NEW |