Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(538)

Side by Side Diff: tests/SkPEGTest.cpp

Issue 2271743002: Reland: Experimental parsing expression grammar (PEG) template library (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: review comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« include/core/SkTLazy.h ('K') | « include/core/SkTLazy.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkPEG.h"
9 #include "Test.h"
10
11 using namespace skpeg;
12
13 namespace {
14
15 void test_EOS(skiatest::Reporter* r) {
16 static const struct {
17 const char* fInput;
18 bool fMatch;
19 } gTests[] = {
20 { "" , true },
21 { " " , false },
22 { "\0" , true },
23 { "foo", false },
24 };
25
26 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
27 const auto match = EOS::Match(gTests[i].fInput);
28 REPORTER_ASSERT(r, match == gTests[i].fMatch);
29 REPORTER_ASSERT(r, match.fNext == (match ? gTests[i].fInput : nullptr));
30 }
31 }
32
33 void test_LIT(skiatest::Reporter* r) {
34 static const struct {
35 const char* fInput;
36 bool fMatch;
37 } gTests[] = {
38 { "" , false },
39 { " " , false },
40 { "x" , false },
41 { "X" , true },
42 { "xX", false },
43 { "Xx", true },
44 };
45
46 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
47 const auto match = LIT<'X'>::Match(gTests[i].fInput);
48 REPORTER_ASSERT(r, match == gTests[i].fMatch);
49 REPORTER_ASSERT(r, match.fNext == (match ? gTests[i].fInput + 1 : nullpt r));
50 }
51
52 REPORTER_ASSERT(r, !(LIT<'F', 'o', 'o'>::Match("")));
53 REPORTER_ASSERT(r, !(LIT<'F', 'o', 'o'>::Match("Fo")));
54 REPORTER_ASSERT(r, !(LIT<'F', 'o', 'o'>::Match("FoO")));
55 REPORTER_ASSERT(r, (LIT<'F', 'o', 'o'>::Match("Foo")));
56 REPORTER_ASSERT(r, (LIT<'F', 'o', 'o'>::Match("Foobar")));
57 }
58
59 void test_Alpha(skiatest::Reporter* r) {
60 static const struct {
61 const char* fInput;
62 bool fMatch;
63 char fMatchValue;
64 } gTests[] = {
65 { "" , false, 0 },
66 { "\r", false, 0 },
67 { "\n", false, 0 },
68 { "\t", false, 0 },
69 { "0" , false, 0 },
70 { "9" , false, 0 },
71 { "a" , true , 'a' },
72 { "a" , true , 'a' },
73 { "z" , true , 'z' },
74 { "A" , true , 'A' },
75 { "Z" , true , 'Z' },
76 { "az", true , 'a' },
77 { "a0", true , 'a' },
78 { "0a", false, 0 },
79 };
80
81 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
82 const auto match = Alpha::Match(gTests[i].fInput);
83 REPORTER_ASSERT(r, match == gTests[i].fMatch);
84 REPORTER_ASSERT(r, match.fNext == (match ? gTests[i].fInput + 1 : nullpt r));
85 if (match) {
86 REPORTER_ASSERT(r, *match == gTests[i].fMatchValue);
87 }
88 }
89 }
90
91 void test_Digit(skiatest::Reporter* r) {
92 static const struct {
93 const char* fInput;
94 bool fMatch;
95 uint8_t fMatchValue;
96 } gTests[] = {
97 { "" , false, 0 },
98 { "/" , false, 0 },
99 { ":" , false, 0 },
100 { "x" , false, 0 },
101 { "x0" , false, 0 },
102 { "0" , true , 0 },
103 { "1x" , true , 1 },
104 { "9 a", true , 9 },
105 };
106
107 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
108 const auto match = Digit::Match(gTests[i].fInput);
109 REPORTER_ASSERT(r, match == gTests[i].fMatch);
110 REPORTER_ASSERT(r, match.fNext == (match ? gTests[i].fInput + 1 : nullpt r));
111 if (match) {
112 REPORTER_ASSERT(r, *match == gTests[i].fMatchValue);
113 }
114 }
115 }
116
117 void test_Opt(skiatest::Reporter* r) {
118 static const struct {
119 const char* fInput;
120 bool fMatch;
121 } gTests[] = {
122 { "" , false },
123 { "fo" , false },
124 { " foo" , false },
125 { "foo" , true },
126 { "foobar" , true },
127 };
128
129 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
130 const auto m = Opt<LIT<'f', 'o', 'o'>>::Match(gTests[i].fInput);
131 REPORTER_ASSERT(r, m);
132 REPORTER_ASSERT(r, m->fValue.isValid() == gTests[i].fMatch);
133 }
134 }
135
136 void test_Seq(skiatest::Reporter* r) {
137 REPORTER_ASSERT(r, (Seq<LIT<'X'>, EOS>::Match("X")));
138 REPORTER_ASSERT(r, !(Seq<LIT<'X'>, EOS>::Match("x")));
139 REPORTER_ASSERT(r, !(Seq<LIT<'X'>, EOS>::Match("xX")));
140 REPORTER_ASSERT(r, !(Seq<LIT<'X'>, EOS>::Match("XX")));
141 REPORTER_ASSERT(r, (Seq<LIT<'X'>, Seq<LIT<'X'>, EOS>>::Match("XX")));
142 REPORTER_ASSERT(r, (Seq<LIT<'X'>, Seq<LIT<'X'>, EOS>>::Match("XX")));
143
144 REPORTER_ASSERT(r, !(Seq<LIT<'F', 'o', 'o'>, EOS>::Match("FooBar")));
145 REPORTER_ASSERT(r, (Seq<LIT<'F', 'o', 'o'>, EOS>::Match("Foo")));
146
147 {
148 const auto m = Seq<LIT<'x'>, Digit>::Match("x5");
149 REPORTER_ASSERT(r, m);
150 REPORTER_ASSERT(r, m->get<1>() == 5);
151 }
152 {
153 const auto m = Seq<Digit, Digit>::Match("42");
154 REPORTER_ASSERT(r, m);
155 REPORTER_ASSERT(r, m->get<0>() == 4);
156 REPORTER_ASSERT(r, m->get<1>() == 2);
157 }
158 }
159
160 void test_Choice(skiatest::Reporter* r) {
161 REPORTER_ASSERT(r, !(Choice<Digit,Alpha>::Match("")));
162 REPORTER_ASSERT(r, !(Choice<Digit,Alpha>::Match("\t")));
163 REPORTER_ASSERT(r, !(Choice<Digit,Alpha>::Match(" ")));
164 REPORTER_ASSERT(r, (Choice<Digit,Alpha>::Match("a")));
165 REPORTER_ASSERT(r, (Choice<Digit,Alpha>::Match("3")));
166 REPORTER_ASSERT(r, (Choice<Digit,Alpha>::Match("a ")));
167 REPORTER_ASSERT(r, (Choice<Digit,Alpha>::Match("3 ")));
168 REPORTER_ASSERT(r, !(Choice<Digit,Alpha>::Match(" a ")));
169 REPORTER_ASSERT(r, !(Choice<Digit,Alpha>::Match(" 3 ")));
170
171 {
172 const auto m = Choice<Alpha, Digit>::Match("x");
173 REPORTER_ASSERT(r, m);
174 REPORTER_ASSERT(r, m->v1.isValid());
175 REPORTER_ASSERT(r, !m->v2.isValid());
176 REPORTER_ASSERT(r, *m->v1.get() == 'x');
177 }
178
179 {
180 const auto m = Choice<Alpha, Digit>::Match("7");
181 REPORTER_ASSERT(r, m);
182 REPORTER_ASSERT(r, !m->v1.isValid());
183 REPORTER_ASSERT(r, m->v2.isValid());
184 REPORTER_ASSERT(r, *m->v2.get() == 7);
185 }
186 }
187
188 void test_AnySome(skiatest::Reporter* r) {
189 static const struct {
190 const char* fInput;
191 int fCount;
192 } gTests[] = {
193 { "" , 0 },
194 { "fo" , 0 },
195 { "Foo" , 0 },
196 { "foo" , 1 },
197 { "foofoo", 2 },
198 };
199
200 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
201 const auto matchAny = Any<LIT<'f', 'o', 'o'>>::Match(gTests[i].fInput);
202 REPORTER_ASSERT(r, matchAny);
203 REPORTER_ASSERT(r, matchAny->fValues.count() == gTests[i].fCount);
204
205 const auto matchSome = Some<LIT<'f', 'o', 'o'>>::Match(gTests[i].fInput) ;
206 REPORTER_ASSERT(r, matchSome == (gTests[i].fCount > 0));
207 REPORTER_ASSERT(r, !matchSome ||
208 matchSome->get<1>().fValues.count() == gTests[i].fCo unt - 1);
209 }
210
211 {
212 const auto m = Any<Digit>::Match("0123456789foo");
213 REPORTER_ASSERT(r, m);
214 REPORTER_ASSERT(r, m->fValues.count() == 10);
215 for (int i = 0; i < m->fValues.count(); ++i) {
216 REPORTER_ASSERT(r, m->fValues[i] == i);
217 }
218 }
219 }
220
221 void test_Complex(skiatest::Reporter* r) {
222 // [0-9]+(,[0-9]+)?$
223 using P0 =
224 Seq<
225 Some<Digit>,
226 Opt<Seq<
227 LIT<','>,
228 Some<Digit>>>,
229 EOS>;
230
231 REPORTER_ASSERT(r, !P0::Match(""));
232 REPORTER_ASSERT(r, !P0::Match(","));
233 REPORTER_ASSERT(r, !P0::Match("1,"));
234 REPORTER_ASSERT(r, !P0::Match(",1"));
235 REPORTER_ASSERT(r, P0::Match("1"));
236 REPORTER_ASSERT(r, P0::Match("1,2"));
237 REPORTER_ASSERT(r, !P0::Match("1,2 "));
238 REPORTER_ASSERT(r, P0::Match("123,456"));
239
240 // [ ]*[Ff]oo([Bb]ar)+[Bb]az[ ]*$
241 using P1 =
242 Seq<
243 Any<LIT<' '>>,
244 Choice<LIT<'F'>, LIT<'f'>>,
245 LIT<'o', 'o'>,
246 Some<Seq<
247 Choice<LIT<'B'>, LIT<'b'>>,
248 LIT<'a', 'r'>>>,
249 Choice<LIT<'B'>, LIT<'b'>>,
250 LIT<'a', 'z'>,
251 Any<LIT<' '>>,
252 EOS>;
253
254 REPORTER_ASSERT(r, !P1::Match(""));
255 REPORTER_ASSERT(r, !P1::Match("FooBar"));
256 REPORTER_ASSERT(r, !P1::Match("FooBaz"));
257 REPORTER_ASSERT(r, P1::Match("FooBarBaz"));
258 REPORTER_ASSERT(r, P1::Match("foobarbaz"));
259 REPORTER_ASSERT(r, P1::Match(" FooBarbaz "));
260 REPORTER_ASSERT(r, P1::Match(" FooBarbarbarBaz "));
261 }
262
263 } // anonymous ns
264
265 DEF_TEST(SkPEG, r) {
266 test_EOS(r);
267 test_LIT(r);
268 test_Alpha(r);
269 test_Digit(r);
270 test_Opt(r);
271 test_Seq(r);
272 test_Choice(r);
273 test_AnySome(r);
274 test_Complex(r);
275 }
OLDNEW
« include/core/SkTLazy.h ('K') | « include/core/SkTLazy.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698