File: inverted_index_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (578 lines) | stat: -rw-r--r-- 21,663 bytes parent folder | download | duplicates (7)
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
571
572
573
574
575
576
577
578
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/local_search_service/inverted_index.h"

#include <cmath>
#include <string>
#include <unordered_map>
#include <vector>

#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/local_search_service/shared_structs.h"
#include "chromeos/ash/components/local_search_service/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::local_search_service {

namespace {

std::vector<float> GetScoresFromTfidfResult(
    const std::vector<TfidfResult>& results) {
  std::vector<float> scores;
  for (const auto& item : results) {
    scores.push_back(std::roundf(std::get<2>(item) * 100) / 100.0);
  }
  return scores;
}

constexpr double kDefaultWeight = 1.0;

}  // namespace

class InvertedIndexTest : public ::testing::Test {
 public:
  void SetUp() override {
    // All content weights are initialized to |kDefaultWeight|.
    index_.doc_length_ =
        std::unordered_map<std::string, uint32_t>({{"doc1", 8}, {"doc2", 6}});

    index_.dictionary_[u"A"] = PostingList(
        {{"doc1",
          Posting({WeightedPosition(kDefaultWeight, Position("header", 1, 1)),
                   WeightedPosition(kDefaultWeight, Position("header", 3, 1)),
                   WeightedPosition(kDefaultWeight, Position("body", 5, 1)),
                   WeightedPosition(kDefaultWeight, Position("body", 7, 1))})},
         {"doc2",
          Posting(
              {WeightedPosition(kDefaultWeight, Position("header", 2, 1)),
               WeightedPosition(kDefaultWeight, Position("header", 4, 1))})}});

    index_.dictionary_[u"B"] = PostingList(
        {{"doc1",
          Posting(
              {WeightedPosition(kDefaultWeight, Position("header", 2, 1)),
               WeightedPosition(kDefaultWeight, Position("body", 4, 1)),
               WeightedPosition(kDefaultWeight, Position("header", 6, 1)),
               WeightedPosition(kDefaultWeight, Position("body", 8, 1))})}});

    index_.dictionary_[u"C"] = PostingList(
        {{"doc2",
          Posting(
              {WeightedPosition(kDefaultWeight, Position("header", 1, 1)),
               WeightedPosition(kDefaultWeight, Position("body", 3, 1)),
               WeightedPosition(kDefaultWeight, Position("header", 5, 1)),
               WeightedPosition(kDefaultWeight, Position("body", 7, 1))})}});
    index_.terms_to_be_updated_.insert(u"A");
    index_.terms_to_be_updated_.insert(u"B");
    index_.terms_to_be_updated_.insert(u"C");

    // Manually set |is_index_built_| below because the docs above were not
    // added to the index using the AddOrUpdate method.
    index_.is_index_built_ = false;
  }

  PostingList FindTerm(const std::u16string& term) {
    return index_.FindTerm(term);
  }

  std::vector<Result> FindMatchingDocumentsApproximately(
      const std::unordered_set<std::u16string>& terms,
      double prefix_threshold,
      double block_threshold) {
    return index_.FindMatchingDocumentsApproximately(terms, prefix_threshold,
                                                     block_threshold);
  }

  void AddDocumentsAndCheck(const DocumentToUpdate& documents) {
    bool callback_done = false;
    index_.AddDocuments(
        documents,
        base::BindOnce([](bool* callback_done) { *callback_done = true; },
                       &callback_done));
    Wait();
    ASSERT_TRUE(callback_done);
  }

  void RemoveDocumentsAndCheck(const std::vector<std::string>& doc_ids,
                               uint32_t expect_num_deleted) {
    bool callback_done = false;
    uint32_t num_deleted = 0u;
    index_.RemoveDocuments(doc_ids,
                           base::BindOnce(
                               [](bool* callback_done, uint32_t* num_deleted,
                                  uint32_t num_deleted_callback) {
                                 *callback_done = true;
                                 *num_deleted = num_deleted_callback;
                               },
                               &callback_done, &num_deleted));
    Wait();
    ASSERT_TRUE(callback_done);
    EXPECT_EQ(num_deleted, expect_num_deleted);
  }

  void UpdateDocumentsAndCheck(const DocumentToUpdate& documents,
                               uint32_t expect_num_deleted) {
    bool callback_done = false;
    uint32_t num_deleted = 0u;
    index_.UpdateDocuments(documents,
                           base::BindOnce(
                               [](bool* callback_done, uint32_t* num_deleted,
                                  uint32_t num_deleted_callback) {
                                 *callback_done = true;
                                 *num_deleted = num_deleted_callback;
                               },
                               &callback_done, &num_deleted));
    Wait();
    ASSERT_TRUE(callback_done);
    EXPECT_EQ(num_deleted, expect_num_deleted);
  }

  void ClearInvertedIndexAndCheck() {
    bool callback_done = false;
    index_.ClearInvertedIndex(base::BindOnce(
        [](bool* callback_done) { *callback_done = true; }, &callback_done));
    Wait();
    ASSERT_TRUE(callback_done);
  }

  std::vector<TfidfResult> GetTfidf(const std::u16string& term) {
    return index_.GetTfidf(term);
  }

  bool GetTfidfForDocId(const std::u16string& term,
                        const std::string& docid,
                        float* tfidf,
                        size_t* number_positions) {
    const auto& posting = GetTfidf(term);
    if (posting.empty()) {
      return false;
    }

    for (const auto& tfidf_result : posting) {
      if (std::get<0>(tfidf_result) == docid) {
        *number_positions = std::get<1>(tfidf_result).size();
        *tfidf = std::get<2>(tfidf_result);
        return true;
      }
    }
    return false;
  }

  void BuildInvertedIndexAndCheck() {
    bool callback_done = false;
    index_.BuildInvertedIndex(base::BindOnce(
        [](bool* callback_done) { *callback_done = true; }, &callback_done));
    Wait();
    ASSERT_TRUE(callback_done);
  }

  bool IsInvertedIndexBuilt() { return index_.IsInvertedIndexBuilt(); }

  std::unordered_map<std::u16string, PostingList> GetDictionary() {
    return index_.dictionary_;
  }

  std::unordered_map<std::string, uint32_t> GetDocLength() {
    return index_.doc_length_;
  }

  std::unordered_map<std::u16string, std::vector<TfidfResult>> GetTfidfCache() {
    return index_.tfidf_cache_;
  }

  DocumentToUpdate GetDocumentsToUpdate() {
    return index_.documents_to_update_;
  }

  TermSet GetTermToBeUpdated() { return index_.terms_to_be_updated_; }

  void Wait() { task_environment_.RunUntilIdle(); }

 protected:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::MainThreadType::DEFAULT,
      base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED};

 private:
  InvertedIndex index_;
};

TEST_F(InvertedIndexTest, FindTermTest) {
  PostingList result = FindTerm(u"A");
  ASSERT_EQ(result.size(), 2u);
  EXPECT_EQ(result["doc1"][0].weight, kDefaultWeight);
  EXPECT_EQ(result["doc1"][0].position.start, 1u);
  EXPECT_EQ(result["doc1"][1].weight, kDefaultWeight);
  EXPECT_EQ(result["doc1"][1].position.start, 3u);
  EXPECT_EQ(result["doc1"][2].weight, kDefaultWeight);
  EXPECT_EQ(result["doc1"][2].position.start, 5u);
  EXPECT_EQ(result["doc1"][3].weight, kDefaultWeight);
  EXPECT_EQ(result["doc1"][3].position.start, 7u);

  EXPECT_EQ(result["doc2"][0].weight, kDefaultWeight);
  EXPECT_EQ(result["doc2"][0].position.start, 2u);
  EXPECT_EQ(result["doc2"][1].weight, kDefaultWeight);
  EXPECT_EQ(result["doc2"][1].position.start, 4u);
}

TEST_F(InvertedIndexTest, AddNewDocumentTest) {
  const std::u16string a_utf16(u"A");
  const std::u16string d_utf16(u"D");

  AddDocumentsAndCheck({{"doc3",
                         {{a_utf16,
                           {{kDefaultWeight, {"header", 1, 1}},
                            {kDefaultWeight / 2, {"body", 2, 1}},
                            {kDefaultWeight, {"header", 4, 1}}}},
                          {d_utf16,
                           {{kDefaultWeight, {"header", 3, 1}},
                            {kDefaultWeight / 2, {"body", 5, 1}}}}}}});

  EXPECT_EQ(GetDocLength()["doc3"], 5u);

  // Find "A"
  PostingList result = FindTerm(a_utf16);
  ASSERT_EQ(result.size(), 3u);
  EXPECT_EQ(result["doc3"][0].weight, kDefaultWeight);
  EXPECT_EQ(result["doc3"][0].position.start, 1u);
  EXPECT_EQ(result["doc3"][1].weight, kDefaultWeight / 2);
  EXPECT_EQ(result["doc3"][1].position.start, 2u);
  EXPECT_EQ(result["doc3"][2].weight, kDefaultWeight);
  EXPECT_EQ(result["doc3"][2].position.start, 4u);

  // Find "D"
  result = FindTerm(d_utf16);
  ASSERT_EQ(result.size(), 1u);
  EXPECT_EQ(result["doc3"][0].weight, kDefaultWeight);
  EXPECT_EQ(result["doc3"][0].position.start, 3u);
  EXPECT_EQ(result["doc3"][1].weight, kDefaultWeight / 2);
  EXPECT_EQ(result["doc3"][1].position.start, 5u);

  // Add multiple documents
  AddDocumentsAndCheck({{"doc4",
                         {{u"E",
                           {{kDefaultWeight, {"header", 1, 1}},
                            {kDefaultWeight / 2, {"body", 2, 1}},
                            {kDefaultWeight, {"header", 4, 1}}}},
                          {u"F",
                           {{kDefaultWeight, {"header", 3, 1}},
                            {kDefaultWeight / 2, {"body", 5, 1}}}}}},
                        {"doc5",
                         {{u"E",
                           {{kDefaultWeight, {"header", 1, 1}},
                            {kDefaultWeight / 2, {"body", 2, 1}},
                            {kDefaultWeight, {"header", 4, 1}}}},
                          {u"G",
                           {{kDefaultWeight, {"header", 3, 1}},
                            {kDefaultWeight / 2, {"body", 5, 1}}}}}}});

  // Find "E"
  result = FindTerm(u"E");
  ASSERT_EQ(result.size(), 2u);

  // Find "F"
  result = FindTerm(u"F");
  ASSERT_EQ(result.size(), 1u);
}

TEST_F(InvertedIndexTest, ReplaceDocumentTest) {
  const std::u16string a_utf16(u"A");
  const std::u16string d_utf16(u"D");

  AddDocumentsAndCheck({{"doc1",
                         {{a_utf16,
                           {{kDefaultWeight, {"header", 1, 1}},
                            {kDefaultWeight / 4, {"body", 2, 1}},
                            {kDefaultWeight / 2, {"header", 4, 1}}}},
                          {d_utf16,
                           {{kDefaultWeight / 3, {"header", 3, 1}},
                            {kDefaultWeight / 5, {"body", 5, 1}}}}}}});

  EXPECT_EQ(GetDocLength()["doc1"], 5u);
  EXPECT_EQ(GetDocLength()["doc2"], 6u);

  // Find "A"
  PostingList result = FindTerm(a_utf16);
  ASSERT_EQ(result.size(), 2u);
  EXPECT_EQ(result["doc1"][0].weight, kDefaultWeight);
  EXPECT_EQ(result["doc1"][0].position.start, 1u);
  EXPECT_EQ(result["doc1"][1].weight, kDefaultWeight / 4);
  EXPECT_EQ(result["doc1"][1].position.start, 2u);
  EXPECT_EQ(result["doc1"][2].weight, kDefaultWeight / 2);
  EXPECT_EQ(result["doc1"][2].position.start, 4u);

  // Find "B"
  result = FindTerm(u"B");
  ASSERT_EQ(result.size(), 0u);

  // Find "D"
  result = FindTerm(d_utf16);
  ASSERT_EQ(result.size(), 1u);
  EXPECT_EQ(result["doc1"][0].weight, kDefaultWeight / 3);
  EXPECT_EQ(result["doc1"][0].position.start, 3u);
  EXPECT_EQ(result["doc1"][1].weight, kDefaultWeight / 5);
  EXPECT_EQ(result["doc1"][1].position.start, 5u);
}

TEST_F(InvertedIndexTest, RemoveDocumentTest) {
  EXPECT_EQ(GetDictionary().size(), 3u);
  EXPECT_EQ(GetDocLength().size(), 2u);

  RemoveDocumentsAndCheck({"doc1"}, 1u);

  EXPECT_EQ(GetDictionary().size(), 2u);
  EXPECT_EQ(GetDocLength().size(), 1u);
  EXPECT_EQ(GetDocLength()["doc2"], 6u);

  // Find "A"
  PostingList result = FindTerm(u"A");
  ASSERT_EQ(result.size(), 1u);
  EXPECT_EQ(result["doc2"][0].weight, kDefaultWeight);
  EXPECT_EQ(result["doc2"][0].position.start, 2u);
  EXPECT_EQ(result["doc2"][1].weight, kDefaultWeight);
  EXPECT_EQ(result["doc2"][1].position.start, 4u);

  // Find "B"
  result = FindTerm(u"B");
  ASSERT_EQ(result.size(), 0u);

  // Find "C"
  result = FindTerm(u"C");
  ASSERT_EQ(result.size(), 1u);
  EXPECT_EQ(result["doc2"][0].weight, kDefaultWeight);
  EXPECT_EQ(result["doc2"][0].position.start, 1u);
  EXPECT_EQ(result["doc2"][1].weight, kDefaultWeight);
  EXPECT_EQ(result["doc2"][1].position.start, 3u);
  EXPECT_EQ(result["doc2"][2].weight, kDefaultWeight);
  EXPECT_EQ(result["doc2"][2].position.start, 5u);
  EXPECT_EQ(result["doc2"][3].weight, kDefaultWeight);
  EXPECT_EQ(result["doc2"][3].position.start, 7u);

  // Removes multiple documents, but only "doc2" is actually removed since
  // "doc1" and "doc3" don't exist.
  RemoveDocumentsAndCheck({"doc1", "doc2", "doc3"}, 1u);
  EXPECT_EQ(GetDictionary().size(), 0u);
  EXPECT_EQ(GetDocLength().size(), 0u);
}

TEST_F(InvertedIndexTest, UpdateIndexTest) {
  EXPECT_EQ(GetTfidfCache().size(), 0u);
  BuildInvertedIndexAndCheck();
  EXPECT_EQ(GetTfidfCache().size(), 3u);

  // Replaces "doc1"
  AddDocumentsAndCheck({{"doc1",
                         {{u"A",
                           {{kDefaultWeight / 2, {"header", 1, 1}},
                            {kDefaultWeight / 4, {"body", 2, 1}},
                            {kDefaultWeight / 2, {"header", 4, 1}}}},
                          {u"D",
                           {{kDefaultWeight, {"header", 3, 1}},
                            {kDefaultWeight, {"body", 5, 1}}}}}}});

  EXPECT_FALSE(IsInvertedIndexBuilt());
  BuildInvertedIndexAndCheck();

  EXPECT_EQ(GetTfidfCache().size(), 3u);

  std::vector<TfidfResult> results = GetTfidf(u"A");
  const double expected_tfidf_A_doc1 =
      std::roundf(
          TfIdfScore(
              /*num_docs=*/2,
              /*num_docs_with_term=*/2,
              /*weighted_num_term_occurrence_in_doc=*/kDefaultWeight / 2 +
                  kDefaultWeight / 4 + kDefaultWeight / 2,
              /*doc_length=*/5) *
          100) /
      100;
  const double expected_tfidf_A_doc2 =
      std::roundf(
          TfIdfScore(/*num_docs=*/2,
                     /*num_docs_with_term=*/2,
                     /*weighted_num_term_occurrence_in_doc=*/kDefaultWeight * 2,
                     /*doc_length=*/6) *
          100) /
      100;
  EXPECT_THAT(GetScoresFromTfidfResult(results),
              testing::UnorderedElementsAre(expected_tfidf_A_doc1,
                                            expected_tfidf_A_doc2));

  results = GetTfidf(u"B");
  EXPECT_THAT(GetScoresFromTfidfResult(results),
              testing::UnorderedElementsAre());

  results = GetTfidf(u"C");
  const double expected_tfidf_C_doc2 =
      std::roundf(
          TfIdfScore(/*num_docs=*/2,
                     /*num_docs_with_term=*/1,
                     /*weighted_num_term_occurrence_in_doc=*/kDefaultWeight * 4,
                     /*doc_length=*/6) *
          100) /
      100;
  EXPECT_THAT(GetScoresFromTfidfResult(results),
              testing::UnorderedElementsAre(expected_tfidf_C_doc2));

  results = GetTfidf(u"D");
  const double expected_tfidf_D_doc1 =
      std::roundf(
          TfIdfScore(/*num_docs=*/2,
                     /*num_docs_with_term=*/1,
                     /*weighted_num_term_occurrence_in_doc=*/kDefaultWeight * 2,
                     /*doc_length=*/5) *
          100) /
      100;
  EXPECT_THAT(GetScoresFromTfidfResult(results),
              testing::UnorderedElementsAre(expected_tfidf_D_doc1));
}

TEST_F(InvertedIndexTest, UpdateDocumentsTest) {
  EXPECT_EQ(GetTfidfCache().size(), 0u);
  BuildInvertedIndexAndCheck();
  EXPECT_EQ(GetTfidfCache().size(), 3u);

  // Replaces "doc1" and remove "doc2"
  UpdateDocumentsAndCheck({{"doc1",
                            {{u"A",
                              {{kDefaultWeight / 2, {"header", 1, 1}},
                               {kDefaultWeight / 4, {"body", 2, 1}},
                               {kDefaultWeight / 2, {"header", 4, 1}}}},
                             {u"D",
                              {{kDefaultWeight, {"header", 3, 1}},
                               {kDefaultWeight, {"body", 5, 1}}}}}},
                           {"doc2", {}}},
                          1u);
  BuildInvertedIndexAndCheck();

  EXPECT_EQ(GetTfidfCache().size(), 2u);

  std::vector<TfidfResult> results = GetTfidf(u"C");
  EXPECT_EQ(results.size(), 0u);
}

TEST_F(InvertedIndexTest, ClearInvertedIndexTest) {
  EXPECT_EQ(GetTfidfCache().size(), 0u);
  BuildInvertedIndexAndCheck();
  EXPECT_EQ(GetTfidfCache().size(), 3u);

  // Add a document and clear the index simultaneously.
  const std::u16string a_utf16(u"A");
  const std::u16string d_utf16(u"D");
  AddDocumentsAndCheck({{"doc3",
                         {{a_utf16,
                           {{kDefaultWeight, {"header", 1, 1}},
                            {kDefaultWeight / 2, {"body", 2, 1}},
                            {kDefaultWeight, {"header", 4, 1}}}},
                          {d_utf16,
                           {{kDefaultWeight, {"header", 3, 1}},
                            {kDefaultWeight / 2, {"body", 5, 1}}}}}}});
  ClearInvertedIndexAndCheck();

  EXPECT_EQ(GetTfidfCache().size(), 0u);
  EXPECT_EQ(GetTermToBeUpdated().size(), 0u);
  EXPECT_EQ(GetDocLength().size(), 0u);
  EXPECT_EQ(GetDictionary().size(), 0u);
  EXPECT_EQ(GetDocumentsToUpdate().size(), 0u);
}

TEST_F(InvertedIndexTest, FindMatchingDocumentsApproximatelyTest) {
  const double prefix_threshold = 1.0;
  const double block_threshold = 1.0;
  const std::u16string a_utf16(u"A");
  const std::u16string b_utf16(u"B");
  const std::u16string c_utf16(u"C");
  const std::u16string d_utf16(u"D");

  // Replace doc1, same occurrences, just different weights.
  AddDocumentsAndCheck({{"doc1",
                         {{a_utf16,
                           {{kDefaultWeight, {"header", 1, 1}},
                            {kDefaultWeight, {"header", 3, 1}},
                            {kDefaultWeight / 2, {"body", 5, 1}},
                            {kDefaultWeight / 2, {"body", 7, 1}}}},
                          {b_utf16,
                           {{kDefaultWeight / 2, {"header", 2, 1}},
                            {kDefaultWeight / 2, {"header", 6, 1}},
                            {kDefaultWeight / 3, {"body", 4, 1}},
                            {kDefaultWeight / 3, {"body", 5, 1}}}}}}});

  {
    // "A" exists in "doc1" and "doc2". The score of each document is simply A's
    // TF-IDF score.
    const std::vector<Result> matching_docs =
        FindMatchingDocumentsApproximately({a_utf16}, prefix_threshold,
                                           block_threshold);

    EXPECT_EQ(matching_docs.size(), 2u);
    std::vector<std::string> expected_docids = {"doc1", "doc2"};
    // TODO(jiameng): for testing, we should  use another independent method to
    // verify scores.
    std::vector<float> actual_scores;
    for (size_t i = 0; i < 2; ++i) {
      const auto& docid = expected_docids[i];
      float expected_score = 0;
      size_t expected_num_pos = 0u;
      EXPECT_TRUE(
          GetTfidfForDocId(a_utf16, docid, &expected_score, &expected_num_pos));
      CheckResult(matching_docs[i], docid, expected_score, expected_num_pos);
      actual_scores.push_back(expected_score);
    }

    // Check scores are non-increasing.
    EXPECT_GE(actual_scores[0], actual_scores[1]);
  }

  {
    // "D" does not exist in the index.
    const std::vector<Result> matching_docs =
        FindMatchingDocumentsApproximately({d_utf16}, prefix_threshold,
                                           block_threshold);

    EXPECT_TRUE(matching_docs.empty());
  }

  {
    // Query is {"A", "B", "C"}, which matches all documents.
    const std::vector<Result> matching_docs =
        FindMatchingDocumentsApproximately({a_utf16, b_utf16, c_utf16},
                                           prefix_threshold, block_threshold);
    EXPECT_EQ(matching_docs.size(), 2u);

    // Ranked documents are {"doc2", "doc1"}.
    // "doc2" score comes from sum of TF-IDF of "A" and "C"
    float expected_score2_A = 0;
    size_t expected_num_pos2_A = 0u;
    EXPECT_TRUE(GetTfidfForDocId(a_utf16, "doc2", &expected_score2_A,
                                 &expected_num_pos2_A));
    float expected_score2_C = 0;
    size_t expected_num_pos2_C = 0u;
    EXPECT_TRUE(GetTfidfForDocId(c_utf16, "doc2", &expected_score2_C,
                                 &expected_num_pos2_C));
    const float expected_score2 = expected_score2_A + expected_score2_C;
    const size_t expected_num_pos2 = expected_num_pos2_A + expected_num_pos2_C;
    CheckResult(matching_docs[0], "doc2", expected_score2, expected_num_pos2);

    // "doc1" score comes from sum of TF-IDF of "A" and "B".
    float expected_score1_A = 0;
    size_t expected_num_pos1_A = 0u;
    EXPECT_TRUE(GetTfidfForDocId(a_utf16, "doc1", &expected_score1_A,
                                 &expected_num_pos1_A));
    float expected_score1_B = 0;
    size_t expected_num_pos1_B = 0u;
    EXPECT_TRUE(GetTfidfForDocId(b_utf16, "doc1", &expected_score1_B,
                                 &expected_num_pos1_B));
    const float expected_score1 = expected_score1_A + expected_score1_B;
    const size_t expected_num_pos1 = expected_num_pos1_B + expected_num_pos1_B;
    CheckResult(matching_docs[1], "doc1", expected_score1, expected_num_pos1);

    EXPECT_GE(expected_score2, expected_score1);
  }
}

}  // namespace ash::local_search_service