1 // Copyright (C) 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "icing/index/lite/term-id-hit-pair.h"
16
17 #include <algorithm>
18 #include <cstdint>
19 #include <vector>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "icing/index/hit/hit.h"
24 #include "icing/schema/section.h"
25 #include "icing/store/document-id.h"
26 #include "icing/testing/common-matchers.h"
27
28 namespace icing {
29 namespace lib {
30 namespace {
31
32 using ::testing::ElementsAre;
33 using ::testing::Eq;
34
35 static constexpr DocumentId kSomeDocumentId = 24;
36 static constexpr SectionId kSomeSectionid = 5;
37 static constexpr Hit::TermFrequency kSomeTermFrequency = 57;
38 static constexpr uint32_t kSomeTermId = 129;
39 static constexpr uint32_t kSomeSmallerTermId = 1;
40 static constexpr uint32_t kSomeLargerTermId = 0b101010101111111100000001;
41
TEST(TermIdHitPairTest,Accessors)42 TEST(TermIdHitPairTest, Accessors) {
43 Hit hit1(kSomeSectionid, kSomeDocumentId, kSomeTermFrequency,
44 /*is_in_prefix_section=*/false, /*is_prefix_hit=*/false,
45 /*is_stemmed_hit=*/false);
46 Hit hit2(kSomeSectionid, kSomeDocumentId, kSomeTermFrequency,
47 /*is_in_prefix_section=*/true, /*is_prefix_hit=*/true,
48 /*is_stemmed_hit=*/false);
49 Hit hit3(kSomeSectionid, kSomeDocumentId, kSomeTermFrequency,
50 /*is_in_prefix_section=*/false, /*is_prefix_hit=*/false,
51 /*is_stemmed_hit=*/false);
52 Hit hit4(kSomeSectionid, kSomeDocumentId, kSomeTermFrequency,
53 /*is_in_prefix_section=*/true, /*is_prefix_hit=*/true,
54 /*is_stemmed_hit=*/true);
55 Hit invalid_hit(Hit::kInvalidValue);
56
57 TermIdHitPair term_id_hit_pair_1(kSomeTermId, hit1);
58 EXPECT_THAT(term_id_hit_pair_1.term_id(), Eq(kSomeTermId));
59 EXPECT_THAT(term_id_hit_pair_1.hit(), EqualsHit(hit1));
60
61 TermIdHitPair term_id_hit_pair_2(kSomeLargerTermId, hit2);
62 EXPECT_THAT(term_id_hit_pair_2.term_id(), Eq(kSomeLargerTermId));
63 EXPECT_THAT(term_id_hit_pair_2.hit(), EqualsHit(hit2));
64
65 TermIdHitPair term_id_hit_pair_3(kSomeTermId, invalid_hit);
66 EXPECT_THAT(term_id_hit_pair_3.term_id(), Eq(kSomeTermId));
67 EXPECT_THAT(term_id_hit_pair_3.hit(), EqualsHit(invalid_hit));
68
69 TermIdHitPair term_id_hit_pair_4(kSomeTermId, hit4);
70 EXPECT_THAT(term_id_hit_pair_4.term_id(), Eq(kSomeTermId));
71 EXPECT_THAT(term_id_hit_pair_4.hit(), EqualsHit(hit4));
72 }
73
TEST(TermIdHitPairTest,Comparison)74 TEST(TermIdHitPairTest, Comparison) {
75 Hit hit(kSomeSectionid, kSomeDocumentId, kSomeTermFrequency,
76 /*is_in_prefix_section=*/false, /*is_prefix_hit=*/false,
77 /*is_stemmed_hit=*/false);
78 Hit smaller_hit(/*section_id=*/1, /*document_id=*/100, /*term_frequency=*/1,
79 /*is_in_prefix_section=*/false, /*is_prefix_hit=*/false,
80 /*is_stemmed_hit=*/false);
81
82 TermIdHitPair term_id_hit_pair(kSomeTermId, hit);
83 TermIdHitPair term_id_hit_pair_equal(kSomeTermId, hit);
84 TermIdHitPair term_id_hit_pair_smaller_hit(kSomeTermId, smaller_hit);
85 TermIdHitPair term_id_hit_pair_smaller_term_id(kSomeSmallerTermId, hit);
86 TermIdHitPair term_id_hit_pair_larger_term_id(kSomeLargerTermId, hit);
87 TermIdHitPair term_id_hit_pair_smaller_term_id_and_hit(kSomeSmallerTermId,
88 smaller_hit);
89
90 std::vector<TermIdHitPair> term_id_hit_pairs{
91 term_id_hit_pair,
92 term_id_hit_pair_equal,
93 term_id_hit_pair_smaller_hit,
94 term_id_hit_pair_smaller_term_id,
95 term_id_hit_pair_larger_term_id,
96 term_id_hit_pair_smaller_term_id_and_hit};
97 std::sort(term_id_hit_pairs.begin(), term_id_hit_pairs.end());
98 EXPECT_THAT(term_id_hit_pairs,
99 ElementsAre(term_id_hit_pair_smaller_term_id_and_hit,
100 term_id_hit_pair_smaller_term_id,
101 term_id_hit_pair_smaller_hit, term_id_hit_pair_equal,
102 term_id_hit_pair, term_id_hit_pair_larger_term_id));
103 }
104
105 } // namespace
106
107 } // namespace lib
108 } // namespace icing
109