Chromium Code Reviews| OLD | NEW |
|---|---|
| (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->v2 == 5); | |
| 151 } | |
| 152 { | |
| 153 const auto m = Seq<Digit, Digit>::Match("42"); | |
| 154 REPORTER_ASSERT(r, m); | |
| 155 REPORTER_ASSERT(r, m->v1 == 4); | |
| 156 REPORTER_ASSERT(r, m->v2 == 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 || matchSome->fValues.count() == gTests[i] .fCount); | |
| 208 } | |
| 209 | |
| 210 { | |
| 211 const auto m = Any<Digit>::Match("0123456789foo"); | |
| 212 REPORTER_ASSERT(r, m); | |
| 213 REPORTER_ASSERT(r, m->fValues.count() == 10); | |
| 214 for (int i = 0; i < m->fValues.count(); ++i) { | |
| 215 REPORTER_ASSERT(r, m->fValues[i] == i); | |
| 216 } | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 void test_Complex(skiatest::Reporter* r) { | |
| 221 // [0-9]+(,[0-9]+)? | |
|
bungeman-skia
2016/08/23 20:49:18
Doesn't this need '$' at the end?
f(malita)
2016/08/24 13:47:29
Done.
| |
| 222 using P0 = | |
| 223 Seq< | |
| 224 Seq< | |
| 225 Some<Digit>, | |
| 226 Opt<Seq< | |
| 227 LIT<','>, | |
| 228 Some<Digit> | |
| 229 >> | |
| 230 >, | |
| 231 EOS | |
| 232 >; | |
| 233 REPORTER_ASSERT(r, !P0::Match("")); | |
| 234 REPORTER_ASSERT(r, !P0::Match(",")); | |
| 235 REPORTER_ASSERT(r, !P0::Match("1,")); | |
| 236 REPORTER_ASSERT(r, !P0::Match(",1")); | |
| 237 REPORTER_ASSERT(r, P0::Match("1")); | |
| 238 REPORTER_ASSERT(r, P0::Match("1,2")); | |
| 239 REPORTER_ASSERT(r, P0::Match("123,456")); | |
|
bungeman-skia
2016/08/23 20:49:18
For example P0::Match("1,2 ") should be false.
f(malita)
2016/08/24 13:47:29
Ack. Added as a test.
| |
| 240 | |
| 241 // [ ]*[Ff]oo([Bb]ar)+[Bb]az[ ]* | |
|
bungeman-skia
2016/08/23 20:49:18
Needs a '$' to match the EOS below?
f(malita)
2016/08/24 13:47:29
Done.
| |
| 242 using P1 = | |
| 243 Seq< | |
| 244 Any<LIT<' '>>, | |
| 245 Seq< | |
| 246 Choice<LIT<'F'>, LIT<'f'>>, | |
| 247 Seq< | |
| 248 LIT<'o', 'o'>, | |
| 249 Seq< | |
| 250 Some<Seq< | |
| 251 Choice<LIT<'B'>, LIT<'b'>>, | |
| 252 LIT<'a', 'r'> | |
| 253 >>, | |
| 254 Seq< | |
| 255 Choice<LIT<'B'>, LIT<'b'>>, | |
| 256 Seq< | |
| 257 LIT<'a', 'z'>, | |
| 258 Seq< | |
| 259 Any<LIT<' '>>, | |
| 260 EOS | |
|
bungeman-skia
2016/08/23 20:49:18
This seems a strange grouping for EOS. Properly pa
f(malita)
2016/08/24 13:47:29
Mostly because of two-param Seq impl, trying out t
| |
| 261 > | |
| 262 > | |
| 263 > | |
| 264 > | |
| 265 > | |
| 266 > | |
| 267 >; | |
| 268 | |
| 269 REPORTER_ASSERT(r, !P1::Match("")); | |
| 270 REPORTER_ASSERT(r, !P1::Match("FooBar")); | |
| 271 REPORTER_ASSERT(r, !P1::Match("FooBaz")); | |
| 272 REPORTER_ASSERT(r, P1::Match("FooBarBaz")); | |
| 273 REPORTER_ASSERT(r, P1::Match("foobarbaz")); | |
| 274 REPORTER_ASSERT(r, P1::Match(" FooBarbaz ")); | |
| 275 REPORTER_ASSERT(r, P1::Match(" FooBarbarbarBaz ")); | |
| 276 } | |
| 277 | |
| 278 } // anonymous ns | |
| 279 | |
| 280 DEF_TEST(SkPEG, r) { | |
| 281 test_EOS(r); | |
| 282 test_LIT(r); | |
| 283 test_Alpha(r); | |
| 284 test_Digit(r); | |
| 285 test_Opt(r); | |
| 286 test_Seq(r); | |
| 287 test_Choice(r); | |
| 288 test_AnySome(r); | |
| 289 test_Complex(r); | |
| 290 } | |
| OLD | NEW |