xref: /aosp_15_r20/external/libtextclassifier/native/utils/grammar/parsing/chart_test.cc (revision 993b0882672172b81d12fad7a7ac0c3e5c824a12)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "utils/grammar/parsing/chart.h"
18 
19 #include "annotator/types.h"
20 #include "utils/base/arena.h"
21 #include "utils/grammar/parsing/parse-tree.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 
25 namespace libtextclassifier3::grammar {
26 namespace {
27 
28 using ::testing::IsEmpty;
29 
30 class ChartTest : public testing::Test {
31  protected:
ChartTest()32   explicit ChartTest() : arena_(/*block_size=*/16 << 10) {}
33   UnsafeArena arena_;
34 };
35 
TEST_F(ChartTest,IsEmptyByDefault)36 TEST_F(ChartTest, IsEmptyByDefault) {
37   Chart<> chart;
38 
39   EXPECT_THAT(chart.derivations(), IsEmpty());
40   EXPECT_TRUE(chart.MatchesEndingAt(0).Done());
41 }
42 
TEST_F(ChartTest,IteratesThroughCell)43 TEST_F(ChartTest, IteratesThroughCell) {
44   Chart<> chart;
45   ParseTree* m0 = arena_.AllocAndInit<ParseTree>(/*lhs=*/0, CodepointSpan{0, 1},
46                                                  /*match_offset=*/0,
47                                                  ParseTree::Type::kDefault);
48   chart.Add(m0);
49   ParseTree* m1 = arena_.AllocAndInit<ParseTree>(/*lhs=*/1, CodepointSpan{0, 2},
50                                                  /*match_offset=*/0,
51                                                  ParseTree::Type::kDefault);
52   chart.Add(m1);
53   ParseTree* m2 = arena_.AllocAndInit<ParseTree>(/*lhs=*/2, CodepointSpan{0, 2},
54                                                  /*match_offset=*/0,
55                                                  ParseTree::Type::kDefault);
56   chart.Add(m2);
57 
58   // Position 0 should be empty.
59   EXPECT_TRUE(chart.MatchesEndingAt(0).Done());
60 
61   // Position 1 should contain m0.
62   {
63     Chart<>::Iterator it = chart.MatchesEndingAt(1);
64     ASSERT_FALSE(it.Done());
65     EXPECT_EQ(it.Item(), m0);
66     it.Next();
67     EXPECT_TRUE(it.Done());
68   }
69 
70   // Position 2 should contain m1 and m2.
71   {
72     Chart<>::Iterator it = chart.MatchesEndingAt(2);
73     ASSERT_FALSE(it.Done());
74     EXPECT_EQ(it.Item(), m2);
75     it.Next();
76     ASSERT_FALSE(it.Done());
77     EXPECT_EQ(it.Item(), m1);
78     it.Next();
79     EXPECT_TRUE(it.Done());
80   }
81 }
82 
TEST_F(ChartTest,ChecksExistingMatches)83 TEST_F(ChartTest, ChecksExistingMatches) {
84   Chart<> chart;
85   ParseTree* m0 = arena_.AllocAndInit<ParseTree>(/*lhs=*/0, CodepointSpan{0, 1},
86                                                  /*match_offset=*/0,
87                                                  ParseTree::Type::kDefault);
88   chart.Add(m0);
89   ParseTree* m1 = arena_.AllocAndInit<ParseTree>(/*lhs=*/1, CodepointSpan{0, 2},
90                                                  /*match_offset=*/0,
91                                                  ParseTree::Type::kDefault);
92   chart.Add(m1);
93   ParseTree* m2 = arena_.AllocAndInit<ParseTree>(/*lhs=*/2, CodepointSpan{0, 2},
94                                                  /*match_offset=*/0,
95                                                  ParseTree::Type::kDefault);
96   chart.Add(m2);
97 
98   EXPECT_TRUE(chart.HasMatch(0, CodepointSpan{0, 1}));
99   EXPECT_FALSE(chart.HasMatch(0, CodepointSpan{0, 2}));
100   EXPECT_TRUE(chart.HasMatch(1, CodepointSpan{0, 2}));
101   EXPECT_TRUE(chart.HasMatch(2, CodepointSpan{0, 2}));
102   EXPECT_FALSE(chart.HasMatch(0, CodepointSpan{0, 2}));
103 }
104 
105 }  // namespace
106 }  // namespace libtextclassifier3::grammar
107