1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/history_fuzzy_provider.h"
#include <algorithm>
#include <vector>
#include "base/logging.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
struct TestCase {
int tolerance;
std::u16string input;
bool expect_found;
std::vector<std::u16string> corrected_inputs;
};
template <typename Container, typename Item>
void SwapRemoveElement(Container& container, const Item& item) {
typename Container::iterator it = std::ranges::find(container, item);
if (it == container.end()) {
return;
}
typename Container::iterator last = container.end() - 1;
if (it != last) {
std::iter_swap(it, last);
}
container.pop_back();
}
// These operator implementations are for debugging.
std::ostream& operator<<(std::ostream& os, const fuzzy::Edit& edit) {
os << '{';
switch (edit.kind) {
case fuzzy::Edit::Kind::KEEP: {
os << 'K';
break;
}
case fuzzy::Edit::Kind::DELETE: {
os << 'D';
break;
}
case fuzzy::Edit::Kind::INSERT: {
os << 'I';
break;
}
case fuzzy::Edit::Kind::REPLACE: {
os << 'R';
break;
}
case fuzzy::Edit::Kind::TRANSPOSE: {
os << 'T';
break;
}
default: {
NOTREACHED();
}
}
os << "," << edit.at << "," << static_cast<char>(edit.new_char) << "}";
return os;
}
std::ostream& operator<<(std::ostream& os,
const fuzzy::Correction& correction) {
os << '[';
for (size_t i = 0; i < correction.edit_count; i++) {
os << correction.edits[i];
os << " <- ";
}
os << ']';
return os;
}
// Note: The `test_case` is destroyed in place as it is checked.
void VerifyTestCase(fuzzy::Node* node,
TestCase& test_case,
fuzzy::ToleranceSchedule tolerance_schedule) {
std::vector<fuzzy::Correction> corrections;
bool found =
node->FindCorrections(test_case.input, tolerance_schedule, corrections);
for (const fuzzy::Correction& correction : corrections) {
std::u16string corrected_input = test_case.input;
correction.ApplyTo(corrected_input);
DVLOG(1) << correction << " -> " << corrected_input;
}
CHECK_EQ(found, test_case.expect_found)
<< " input(" << test_case.tolerance << "): " << test_case.input;
CHECK_EQ(test_case.corrected_inputs.size(), corrections.size())
<< " input(" << test_case.tolerance << "): " << test_case.input;
for (const fuzzy::Correction& correction : corrections) {
std::u16string corrected_input = test_case.input;
correction.ApplyTo(corrected_input);
SwapRemoveElement(test_case.corrected_inputs, corrected_input);
}
CHECK_EQ(test_case.corrected_inputs.size(), size_t{0})
<< " input(" << test_case.tolerance << "): " << test_case.input;
}
// Verifies a vector of `cases`, destroying them in the process.
void VerifyCases(fuzzy::Node* node, std::vector<TestCase>& cases) {
for (TestCase& test_case : cases) {
VerifyTestCase(
node, test_case,
{.start_index = 0, .step_length = 0, .limit = test_case.tolerance});
}
}
// This is just like `VerifyCases` but uses a specified `tolerance_schedule`
// for all cases instead of each TestCase `tolerance` value.
void VerifyCasesWithSchedule(fuzzy::Node* node,
std::vector<TestCase>& cases,
fuzzy::ToleranceSchedule tolerance_schedule) {
for (TestCase& test_case : cases) {
VerifyTestCase(node, test_case, tolerance_schedule);
}
}
} // namespace
class HistoryFuzzyProviderTest : public testing::Test {
public:
HistoryFuzzyProviderTest() = default;
HistoryFuzzyProviderTest(const HistoryFuzzyProviderTest&) = delete;
HistoryFuzzyProviderTest& operator=(const HistoryFuzzyProviderTest&) = delete;
void SetUp() override {}
};
TEST_F(HistoryFuzzyProviderTest, AlgorithmIsNotGreedy) {
fuzzy::Node node;
node.Insert(u"wind", 0);
node.Insert(u"wash", 0);
std::vector<TestCase> cases = {
{
1,
u"wand",
false,
{
u"wind",
},
},
{
1,
u"wish",
false,
{
u"wash",
},
},
{
2,
u"xasx",
false,
{
u"wash",
},
},
{
3,
u"xaxsx",
false,
{
u"wash",
},
},
{
2,
u"want",
false,
{
u"wind",
u"wash",
},
},
};
VerifyCases(&node, cases);
}
TEST_F(HistoryFuzzyProviderTest, ReplacementWorksAnywhere) {
fuzzy::Node node;
node.Insert(u"abcdefg", 0);
node.Insert(u"abcdxyz", 0);
node.Insert(u"tuvwxyz", 0);
node.Insert(u"tuvabcd", 0);
// A few things to note about these cases:
// They don't complete to full strings; minimal corrections are supplied.
// Tolerance consumption is currently naive. This should likely change,
// as we may want to start with low (or even no) tolerance and then allow
// incremental tolerance gains as more of the input string is scanned.
// A stepping tolerance schedule like this would greatly increase efficiency
// and allow more tolerance for longer strings without risking odd matches.
std::vector<TestCase> cases = {
{
0,
u"abcdefg",
true,
{},
},
{
0,
u"abc_efg",
false,
{},
},
{
1,
u"abc_efg",
false,
{
u"abcdefg",
},
},
{
1,
u"abc_ef",
false,
{
u"abcdef",
},
},
{
2,
u"abc_e_g",
false,
{
u"abcdefg",
},
},
{
2,
u"a_c_e_g",
false,
{},
},
{
3,
u"a_c_e_",
false,
{
u"abcdef",
},
},
};
VerifyCases(&node, cases);
}
TEST_F(HistoryFuzzyProviderTest, InsertionWorksAnywhereExceptEnd) {
fuzzy::Node node;
node.Insert(u"abc", 0);
std::vector<TestCase> cases = {
{
1,
u"bc",
false,
{
u"abc",
},
},
{
1,
u"ac",
false,
{
u"abc",
},
},
{
1,
u"ab",
true,
{
// Note, we are NOT expecting "abc" here because insertion at
// end of input is generally predictive, and that is the job
// of the whole autocomplete system, not the fuzzy input
// correction algorithm, which seeks only to get inputs back
// on track for good suggestions.
},
},
{
2,
u"b",
false,
{
u"ab",
},
},
};
VerifyCases(&node, cases);
}
TEST_F(HistoryFuzzyProviderTest, DeletionWorksAnywhere) {
fuzzy::Node node;
node.Insert(u"abc", 0);
std::vector<TestCase> cases = {
{
1,
u"xabc",
false,
{
u"abc",
},
},
{
1,
u"abxc",
false,
{
u"abc",
},
},
{
1,
u"abcx",
false,
{
u"abc",
},
},
{
2,
u"axbxc",
false,
{
u"abc",
},
},
};
VerifyCases(&node, cases);
}
// This test ensures a preference for longer results when edit distances are
// equal. This isn't an absolute requirement, and some relevance or probability
// guidance might be better, but this simple heuristic avoids creating shorter
// substring corrections, for example both "was" and "wash".
TEST_F(HistoryFuzzyProviderTest, LongerResultsArePreferred) {
fuzzy::Node node;
node.Insert(u"ao", 0);
node.Insert(u"eeo", 0);
std::vector<TestCase> cases = {
{
1,
u"eo",
false,
{
u"eeo",
},
},
};
VerifyCases(&node, cases);
}
TEST_F(HistoryFuzzyProviderTest, EmptyTrieRespectsToleranceSchedule) {
fuzzy::Node node;
TestCase test_case;
// Blank is produced by deleting at index zero.
test_case = {
0,
u"x",
false,
{
u"",
},
};
VerifyTestCase(&node, test_case, {.start_index = 0, .limit = 1});
// But this is not allowed when `start_index` is one.
test_case = {
0,
u"x",
false,
{},
};
VerifyTestCase(&node, test_case, {.start_index = 1, .limit = 1});
}
TEST_F(HistoryFuzzyProviderTest, ToleranceScheduleIsEnforced) {
fuzzy::Node node;
node.Insert(u"abcdefghijklmnopqrstuv", 0);
std::vector<TestCase> cases = {
{
0,
u"axcdef",
false,
{},
},
{
0,
u"abxdef",
false,
{
u"abcdef",
},
},
{
0,
u"abxxdef",
false,
{},
},
{
0,
u"abxdexghi",
false,
{},
},
{
0,
u"abxdefxhi",
false,
{
u"abcdefghi",
},
},
{
0,
u"abxdefxhijxlmnop",
false,
{
u"abcdefghijklmnop",
},
},
{
0,
u"abxdefxhijxlmnopqx",
false,
{},
},
};
VerifyCasesWithSchedule(&node, cases,
{.start_index = 2, .step_length = 4, .limit = 3});
}
// This test ensures that transposition swaps two adjacent characters with
// a single operation at edit distance one. Only directly adjacent characters
// can be transposed and nonadjacent character swaps still require two edits.
TEST_F(HistoryFuzzyProviderTest, TransposeIsEditDistanceOne) {
fuzzy::Node node;
node.Insert(u"transpose", 0);
std::vector<TestCase> cases = {
{
// Direct transposition 'op' -> 'po'. Finding the correction
// with tolerance 1 implies a single transposition edit was enough.
1,
u"transopse",
false,
{
u"transpose",
},
},
{
// Not a direct transposition, as the 's' is in between,
// so this case requires insert + delete pair (tolerance 2).
2,
u"transpeso",
false,
{
u"transpose",
},
},
};
VerifyCases(&node, cases);
}
// This test covers a subtlety in the algorithm. It ensures we don't take
// replacements at the same position of a previous insertion because we
// only want one of {I,1,e}~{R,0,t} (ler ter) | {R,0,e}~{I,0,t} (er ter).
TEST_F(HistoryFuzzyProviderTest, DoesNotProduceDuplicate) {
fuzzy::Node node;
node.Insert(u"ter", 0);
std::vector<TestCase> cases = {
{
2,
u"lr",
false,
{
u"ter",
},
},
};
VerifyCases(&node, cases);
}
TEST_F(HistoryFuzzyProviderTest, NodesDeleteAndPreserveStructure) {
fuzzy::Node node;
const auto checked_delete = [&](const std::u16string& s) {
node.Delete(s, 0);
return node.TerminalCount() == 0;
};
node.Insert(u"abc", 0);
CHECK(checked_delete(u"abc"));
CHECK(node.next.empty());
node.Insert(u"def", 0);
CHECK(!checked_delete(u"de"));
CHECK(!node.next.empty());
CHECK(checked_delete(u"def"));
node.Insert(u"ghi", 0);
node.Insert(u"ghost", 0);
CHECK(!checked_delete(u"gh"));
CHECK(!node.next.empty());
CHECK(!checked_delete(u"ghi"));
CHECK(checked_delete(u"ghost"));
CHECK(node.next.empty());
}
TEST_F(HistoryFuzzyProviderTest, NodesMaintainRelevanceTotalTerminalCount) {
fuzzy::Node node;
// Start with no terminals.
CHECK_EQ(node.TerminalCount(), 0);
// Support including the empty string as terminal.
node.Insert(u"", 0);
CHECK_EQ(node.TerminalCount(), 1);
// Ensure repeated insertions don't cause growth.
node.Insert(u"abc", 0);
CHECK_EQ(node.TerminalCount(), 2);
node.Insert(u"abc", 0);
CHECK_EQ(node.TerminalCount(), 2);
// Check further additions on different, same, and partially same paths.
node.Insert(u"def", 0);
CHECK_EQ(node.TerminalCount(), 3);
node.Insert(u"abcd", 0);
CHECK_EQ(node.TerminalCount(), 4);
node.Insert(u"ab", 0);
CHECK_EQ(node.next[u'a']->TerminalCount(), 3);
CHECK_EQ(node.next[u'd']->TerminalCount(), 1);
CHECK_EQ(node.TerminalCount(), 5);
// Check deletion, including no-op and empty string deletion.
node.Delete(u"a", 0);
CHECK_EQ(node.TerminalCount(), 5);
node.Delete(u"x", 0);
CHECK_EQ(node.TerminalCount(), 5);
node.Delete(u"", 0);
CHECK_EQ(node.TerminalCount(), 4);
node.Delete(u"abc", 0);
CHECK_EQ(node.TerminalCount(), 3);
node.Delete(u"abcd", 0);
CHECK_EQ(node.TerminalCount(), 2);
node.Delete(u"ab", 0);
CHECK_EQ(node.TerminalCount(), 1);
node.Delete(u"defx", 0);
CHECK_EQ(node.TerminalCount(), 1);
node.Delete(u"def", 0);
CHECK_EQ(node.TerminalCount(), 0);
}
|