• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
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 
16 #include <string>
17 
18 #include <gtest/gtest.h>
19 
20 #include "util/parser/parser.h"
21 
22 namespace ark::parser::test {
23 namespace {
24 struct Context {};
25 struct smth;
26 
27 // NOLINTNEXTLINE (cppcoreguidelines-interfaces-global-init)
__anon255a47c70202(Action act, Context &, auto &it) 28 const auto FOO = [](Action act, [[maybe_unused]] Context &, auto &it) {
29     switch (act) {
30         case Action::START:
31             if (*it != 'f') {
32                 return false;
33             }
34             ++it;
35             if (*it != 'o') {
36                 return false;
37             }
38             ++it;
39             if (*it != 'o') {
40                 return false;
41             }
42             ++it;
43             return true;
44 
45         case Action::CANCEL:
46             return true;
47 
48         case Action::PARSED:
49             return true;
50         default:
51             UNREACHABLE();
52             return false;
53     }
54 };
55 
56 // NOLINTNEXTLINE (cppcoreguidelines-interfaces-global-init)
__anon255a47c70302(Action act, Context &, auto &it) 57 const auto BAR = [](Action act, [[maybe_unused]] Context &, auto &it) {
58     switch (act) {
59         case Action::START:
60             if (*it != 'b') {
61                 return false;
62             }
63             ++it;
64             if (*it != 'a') {
65                 return false;
66             }
67             ++it;
68             if (*it != 'r') {
69                 return false;
70             }
71             ++it;
72             return true;
73 
74         case Action::CANCEL:
75             return true;
76 
77         case Action::PARSED:
78             return true;
79         default:
80             UNREACHABLE();
81             return false;
82     }
83 };
84 
85 using P = typename Parser<Context, const char, const char *>::template Next<smth>;
86 using P1 = typename P::P;
87 using P2 = typename P1::P;
88 using P3 = typename P2::P;
89 using P4 = typename P3::P;
90 using P5 = typename P4::P;
91 using P6 = typename P5::P;
92 
93 using It = const char *;
94 }  // namespace
95 
96 // NOLINTBEGIN(fuchsia-statically-constructed-objects,readability-identifier-naming)
97 const std::string aBc("aBc");
98 const std::string string("string");
99 const std::string d("d");
100 const std::string acstring("ACstring");
101 const std::string fooAcB("fooAcB");
102 const auto ABCP = P::OfCharset(Charset("abcABC"));
103 const auto DEFP = P1::OfCharset(Charset("defDEF"));
104 const auto STRINGP = P2::OfString("string");
105 const auto ENDP = P3::End();
106 // NOLINTEND(fuchsia-statically-constructed-objects,readability-identifier-naming)
107 
108 // NOLINTBEGIN(readability-magic-numbers)
VerifierParserTest1(Context & cont,It & start,It & end)109 static void VerifierParserTest1(Context &cont, It &start, It &end)
110 {
111     start = &(string[0U]);
112     end = &(string[0U]);
113     EXPECT_TRUE(ENDP(cont, start, end));
114     end = &(string[2U]);
115     EXPECT_FALSE(ENDP(cont, start, end));
116 
117     static const auto ACSTRINGP = ~ABCP >> STRINGP;
118     start = &(string[0U]);
119     end = &(string[6U]);
120     EXPECT_TRUE(ACSTRINGP(cont, start, end));
121     start = &(acstring[0U]);
122     end = &(acstring[8U]);
123     EXPECT_TRUE(ACSTRINGP(cont, start, end));
124     end = &(acstring[7U]);
125     EXPECT_FALSE(ACSTRINGP(cont, start, end));
126 
127     static const auto FOOABCP = ABCP |= FOO;
128     static const auto BARABCP = ABCP |= BAR;
129     start = &(string[0U]);
130     end = &(string[6U]);
131     EXPECT_FALSE(FOOABCP(cont, start, end));
132     start = &(fooAcB[0U]);
133     end = &(fooAcB[6U]);
134     EXPECT_TRUE(FOOABCP(cont, start, end));
135     start = &(fooAcB[0U]);
136     EXPECT_FALSE(BARABCP(cont, start, end));
137 
138     static const auto ABCDEFP = ABCP | DEFP;
139     start = &(aBc[0U]);
140     end = &(aBc[3U]);
141     EXPECT_TRUE(ABCDEFP(cont, start, end));
142     start = &(string[0U]);
143     end = &(string[6U]);
144     EXPECT_FALSE(ABCDEFP(cont, start, end));
145     start = &(d[0U]);
146     end = &(d[1U]);
147     EXPECT_TRUE(ABCDEFP(cont, start, end));
148 }
149 
VerifierParserTest2(Context & cont,It & start,It & end)150 static void VerifierParserTest2(Context &cont, It &start, It &end)
151 {
152     static const auto EMPTYP = ABCP & DEFP;
153     start = &(aBc[0U]);
154     end = &(aBc[3U]);
155     EXPECT_FALSE(EMPTYP(cont, start, end));
156     start = &(string[0U]);
157     end = &(string[6U]);
158     EXPECT_FALSE(EMPTYP(cont, start, end));
159     start = &(d[0U]);
160     end = &(d[1U]);
161     EXPECT_FALSE(EMPTYP(cont, start, end));
162 
163     static const auto ABC2P = ABCP << STRINGP >> STRINGP;
164     start = &(acstring[0U]);
165     end = &(acstring[8U]);
166     EXPECT_TRUE(ABC2P(cont, start, end));
167     start = &(string[0U]);
168     end = &(string[6U]);
169     EXPECT_FALSE(ABC2P(cont, start, end));
170     start = &(d[0U]);
171     end = &(d[1U]);
172     EXPECT_FALSE(ABC2P(cont, start, end));
173 
174     static const auto NOABCP = !ABCP;
175     start = &(aBc[0U]);
176     end = &(aBc[3U]);
177     EXPECT_FALSE(NOABCP(cont, start, end));
178     start = &(string[0U]);
179     end = &(string[6U]);
180     EXPECT_TRUE(NOABCP(cont, start, end));
181     start = &(d[0U]);
182     end = &(d[1U]);
183     EXPECT_TRUE(NOABCP(cont, start, end));
184 
185     static const auto STRINGSTRINGENDP = *STRINGP >> ENDP;
186     static const auto STRINGENDP = STRINGP >> ENDP;
187     std::string stringstring {"stringstring"};
188     start = &(stringstring[0U]);
189     end = &(stringstring[12U]);
190     EXPECT_FALSE(STRINGENDP(cont, start, end));
191     start = &(stringstring[0U]);
192     EXPECT_TRUE(STRINGSTRINGENDP(cont, start, end));
193 }
194 
TEST(VerifierParserTest,Parser)195 TEST(VerifierParserTest, Parser)
196 {
197     Context cont;
198     It start = &(aBc[0U]);
199     It end = &(aBc[3U]);
200 
201     EXPECT_TRUE(ABCP(cont, start, end));
202     start = &(aBc[1U]);
203     EXPECT_TRUE(ABCP(cont, start, end));
204     start = &(aBc[0U]);
205     EXPECT_FALSE(DEFP(cont, start, end));
206     start = &(aBc[0U]);
207     EXPECT_FALSE(STRINGP(cont, start, end));
208     start = &(string[0U]);
209     end = &(string[6U]);
210     EXPECT_FALSE(ABCP(cont, start, end));
211     start = &(string[0U]);
212     EXPECT_FALSE(DEFP(cont, start, end));
213     start = &(string[0U]);
214     EXPECT_TRUE(STRINGP(cont, start, end));
215     start = &(d[0U]);
216     end = &(d[1U]);
217     EXPECT_FALSE(ABCP(cont, start, end));
218     start = &(d[0U]);
219     EXPECT_TRUE(DEFP(cont, start, end));
220     start = &(d[0U]);
221     EXPECT_FALSE(STRINGP(cont, start, end));
222     start = &(string[0U]);
223     end = &(string[3U]);
224     EXPECT_FALSE(ABCP(cont, start, end));
225     start = &(string[0U]);
226     EXPECT_FALSE(DEFP(cont, start, end));
227     start = &(string[0U]);
228     EXPECT_FALSE(STRINGP(cont, start, end));
229 
230     VerifierParserTest1(cont, start, end);
231     VerifierParserTest2(cont, start, end);
232 }
233 // NOLINTEND(readability-magic-numbers)
234 }  // namespace ark::parser::test