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

Side by Side Diff: courgette/third_party/bsdiff/qsufsort_unittest.cc

Issue 1961963003: Move //courgette/third_party to subfolder. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed compilation Created 4 years, 7 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/qsufsort.h" 5 #include "courgette/third_party/bsdiff/qsufsort.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cstring> 10 #include <cstring>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 TEST(QSufSortTest, Sort) { 17 TEST(QSufSortTest, Sort) {
18 const char* test_cases[] = { 18 const char* test_cases[] = {
19 "", 19 "",
huangs 2016/05/10 18:17:03 Is this recommended by formatter? I'd think 2 spa
altimin 2016/05/11 17:48:47 I've just run "git cl format" :)
20 "a", 20 "a",
21 "za", 21 "za",
22 "CACAO", 22 "CACAO",
23 "banana", 23 "banana",
24 "tobeornottobe", 24 "tobeornottobe",
25 "The quick brown fox jumps over the lazy dog.", 25 "The quick brown fox jumps over the lazy dog.",
26 "elephantelephantelephantelephantelephant", 26 "elephantelephantelephantelephantelephant",
27 "-------------------------", 27 "-------------------------",
28 "011010011001011010010110011010010", 28 "011010011001011010010110011010010",
29 "3141592653589793238462643383279502884197169399375105", 29 "3141592653589793238462643383279502884197169399375105",
30 "\xFF\xFE\xFF\xFE\xFD\x80\x30\x31\x32\x80\x30\xFF\x01\xAB\xCD", 30 "\xFF\xFE\xFF\xFE\xFD\x80\x30\x31\x32\x80\x30\xFF\x01\xAB\xCD",
31 }; 31 };
32 32
33 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) { 33 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) {
34 int len = static_cast<int>(::strlen(test_cases[idx])); 34 int len = static_cast<int>(::strlen(test_cases[idx]));
35 const unsigned char* s = 35 const unsigned char* s =
36 reinterpret_cast<const unsigned char*>(test_cases[idx]); 36 reinterpret_cast<const unsigned char*>(test_cases[idx]);
37 37
38 // Generate the suffix array as I. 38 // Generate the suffix array as I.
39 std::vector<int> I(len + 1); 39 std::vector<int> I(len + 1);
40 std::vector<int> V(len + 1); 40 std::vector<int> V(len + 1);
(...skipping 20 matching lines...) Expand all
61 } 61 }
62 } 62 }
63 63
64 TEST(QSufSortTest, Search) { 64 TEST(QSufSortTest, Search) {
65 // Initialize main string and the suffix array. 65 // Initialize main string and the suffix array.
66 // Positions: 00000000001111111111122222222233333333334444 66 // Positions: 00000000001111111111122222222233333333334444
67 // 01234567890123456789012345678901234567890123 67 // 01234567890123456789012345678901234567890123
68 const char* old_str = "the quick brown fox jumps over the lazy dog."; 68 const char* old_str = "the quick brown fox jumps over the lazy dog.";
69 int old_size = static_cast<int>(::strlen(old_str)); 69 int old_size = static_cast<int>(::strlen(old_str));
70 const unsigned char* old_buf = 70 const unsigned char* old_buf =
71 reinterpret_cast<const unsigned char*>(old_str); 71 reinterpret_cast<const unsigned char*>(old_str);
72 std::vector<int> I(old_size + 1); 72 std::vector<int> I(old_size + 1);
73 std::vector<int> V(old_size + 1); 73 std::vector<int> V(old_size + 1);
74 courgette::qsuf::qsufsort<int*>(&I[0], &V[0], old_buf, old_size); 74 courgette::qsuf::qsufsort<int*>(&I[0], &V[0], old_buf, old_size);
75 75
76 // Test queries. 76 // Test queries.
77 const struct { 77 const struct {
78 int exp_pos; // -1 means "don't care". 78 int exp_pos; // -1 means "don't care".
79 int exp_match_len; 79 int exp_match_len;
80 const char* query_str; 80 const char* query_str;
81 } test_cases[] = { 81 } test_cases[] = {
82 // Entire string. 82 // Entire string.
huangs 2016/05/10 18:17:03 Same here re. spacing.
altimin 2016/05/11 17:48:47 I'm not a big fan of deviating from what "git cl f
83 {0, 44, "the quick brown fox jumps over the lazy dog."}, 83 {0, 44, "the quick brown fox jumps over the lazy dog."},
84 // Empty string. 84 // Empty string.
85 {-1, 0, ""}, // Current algorithm does not enforce |pos| == 0. 85 {-1, 0, ""}, // Current algorithm does not enforce |pos| == 0.
86 // Exact and unique suffix match. 86 // Exact and unique suffix match.
87 {43, 1, "."}, 87 {43, 1, "."},
88 {31, 13, "the lazy dog."}, 88 {31, 13, "the lazy dog."},
89 // Exact and unique non-suffix match. 89 // Exact and unique non-suffix match.
90 {4, 5, "quick"}, 90 {4, 5, "quick"},
91 {0, 9, "the quick"}, // Unique prefix. 91 {0, 9, "the quick"}, // Unique prefix.
92 // Entire word match with mutiple results: take lexicographical first. 92 // Entire word match with mutiple results: take lexicographical first.
93 {31, 3, "the"}, // Non-unique prefix: "the l"... < "the q"... 93 {31, 3, "the"}, // Non-unique prefix: "the l"... < "the q"...
94 {9, 1, " "}, // " brown"... wins. 94 {9, 1, " "}, // " brown"... wins.
95 // Partial and unique match of query prefix. 95 // Partial and unique match of query prefix.
96 {16, 10, "fox jumps with the hosps"}, 96 {16, 10, "fox jumps with the hosps"},
97 // Partial and multiple match of query prefix: no guarantees on |pos|. 97 // Partial and multiple match of query prefix: no guarantees on |pos|.
98 // Take lexicographical first for matching portion *only*, so same results: 98 // Take lexicographical first for matching portion *only*, so same
99 {-1, 4, "the apple"}, // query < "the l"... < "the q"... 99 // results:
100 {-1, 4, "the opera"}, // "the l"... < query < "the q"... 100 {-1, 4, "the apple"}, // query < "the l"... < "the q"...
101 {-1, 4, "the zebra"}, // "the l"... < "the q"... < query 101 {-1, 4, "the opera"}, // "the l"... < query < "the q"...
102 // Prefix match dominates suffix match. 102 {-1, 4, "the zebra"}, // "the l"... < "the q"... < query
103 {26, 5, "over quick brown fox"}, 103 // Prefix match dominates suffix match.
104 // No match. 104 {26, 5, "over quick brown fox"},
105 {-1, 0, ","}, 105 // No match.
106 {-1, 0, "1234"}, 106 {-1, 0, ","},
107 {-1, 0, "THE QUICK BROWN FOX"}, 107 {-1, 0, "1234"},
108 {-1, 0, "(the"}, 108 {-1, 0, "THE QUICK BROWN FOX"},
109 {-1, 0, "(the"},
109 }; 110 };
110 111
111 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) { 112 for (size_t idx = 0; idx < arraysize(test_cases); ++idx) {
112 const auto& test_case = test_cases[idx]; 113 const auto& test_case = test_cases[idx];
113 int new_size = static_cast<int>(::strlen(test_case.query_str)); 114 int new_size = static_cast<int>(::strlen(test_case.query_str));
114 const unsigned char* new_buf = 115 const unsigned char* new_buf =
115 reinterpret_cast<const unsigned char*>(test_case.query_str); 116 reinterpret_cast<const unsigned char*>(test_case.query_str);
116 117
117 // Perform the search. 118 // Perform the search.
118 int pos = 0; 119 int pos = 0;
119 int match_len = courgette::qsuf::search( 120 int match_len = courgette::qsuf::search(&I[0], old_buf, old_size, new_buf,
120 &I[0], old_buf, old_size, new_buf, new_size, &pos); 121 new_size, &pos);
121 122
122 // Check basic properties and match with expected values. 123 // Check basic properties and match with expected values.
123 EXPECT_GE(match_len, 0) << "test_case[" << idx << "]"; 124 EXPECT_GE(match_len, 0) << "test_case[" << idx << "]";
124 EXPECT_LE(match_len, new_size) << "test_case[" << idx << "]"; 125 EXPECT_LE(match_len, new_size) << "test_case[" << idx << "]";
125 if (match_len > 0) { 126 if (match_len > 0) {
126 EXPECT_GE(pos, 0) << "test_case[" << idx << "]"; 127 EXPECT_GE(pos, 0) << "test_case[" << idx << "]";
127 EXPECT_LE(pos, old_size - match_len) << "test_case[" << idx << "]"; 128 EXPECT_LE(pos, old_size - match_len) << "test_case[" << idx << "]";
128 EXPECT_EQ(0, ::memcmp(old_buf + pos, new_buf, match_len)) 129 EXPECT_EQ(0, ::memcmp(old_buf + pos, new_buf, match_len)) << "test_case["
129 << "test_case[" << idx << "]"; 130 << idx << "]";
130 } 131 }
131 if (test_case.exp_pos >= 0) { 132 if (test_case.exp_pos >= 0) {
132 EXPECT_EQ(test_case.exp_pos, pos) << "test_case[" << idx << "]"; 133 EXPECT_EQ(test_case.exp_pos, pos) << "test_case[" << idx << "]";
133 } 134 }
134 EXPECT_EQ(test_case.exp_match_len, match_len) << "test_case[" << idx << "]"; 135 EXPECT_EQ(test_case.exp_match_len, match_len) << "test_case[" << idx << "]";
135 } 136 }
136 } 137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698