SkPEGTest.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include "tests/Test.h"
  8. #if defined(SK_XML)
  9. #include "experimental/svg/model/SkPEG.h"
  10. #include "include/private/SkTo.h"
  11. using namespace skpeg;
  12. namespace {
  13. struct Alpha {
  14. using V = char;
  15. using MatchT = MatchResult<V>;
  16. static MatchT Match(const char* in) {
  17. static constexpr unsigned kAlphaRange = 'z' - 'a';
  18. return static_cast<unsigned>(*in - 'a') <= kAlphaRange
  19. || static_cast<unsigned>(*in - 'A') <= kAlphaRange
  20. ? MatchT(in + 1, *in)
  21. : nullptr;
  22. }
  23. };
  24. struct Digit {
  25. using V = uint8_t;
  26. using MatchT = MatchResult<V>;
  27. static MatchT Match(const char* in) {
  28. static constexpr unsigned kDigitRange = '9' - '0';
  29. return static_cast<unsigned>(*in - '0') <= kDigitRange
  30. ? MatchT(in + 1, SkTo<uint8_t>(*in - '0'))
  31. : nullptr;
  32. }
  33. };
  34. void test_EOS(skiatest::Reporter* r) {
  35. static const struct {
  36. const char* fInput;
  37. bool fMatch;
  38. } gTests[] = {
  39. { "" , true },
  40. { " " , false },
  41. { "\0" , true },
  42. { "foo", false },
  43. };
  44. for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
  45. const auto match = EOS::Match(gTests[i].fInput);
  46. REPORTER_ASSERT(r, match == gTests[i].fMatch);
  47. REPORTER_ASSERT(r, match.fNext == (match ? gTests[i].fInput : nullptr));
  48. }
  49. }
  50. void test_LIT(skiatest::Reporter* r) {
  51. static const struct {
  52. const char* fInput;
  53. bool fMatch;
  54. } gTests[] = {
  55. { "" , false },
  56. { " " , false },
  57. { "x" , false },
  58. { "X" , true },
  59. { "xX", false },
  60. { "Xx", true },
  61. };
  62. for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
  63. const auto match = LIT<'X'>::Match(gTests[i].fInput);
  64. REPORTER_ASSERT(r, match == gTests[i].fMatch);
  65. REPORTER_ASSERT(r, match.fNext == (match ? gTests[i].fInput + 1 : nullptr));
  66. }
  67. REPORTER_ASSERT(r, !(LIT<'F', 'o', 'o'>::Match("")));
  68. REPORTER_ASSERT(r, !(LIT<'F', 'o', 'o'>::Match("Fo")));
  69. REPORTER_ASSERT(r, !(LIT<'F', 'o', 'o'>::Match("FoO")));
  70. REPORTER_ASSERT(r, (LIT<'F', 'o', 'o'>::Match("Foo")));
  71. REPORTER_ASSERT(r, (LIT<'F', 'o', 'o'>::Match("Foobar")));
  72. }
  73. void test_Alpha(skiatest::Reporter* r) {
  74. static const struct {
  75. const char* fInput;
  76. bool fMatch;
  77. char fMatchValue;
  78. } gTests[] = {
  79. { "" , false, 0 },
  80. { "\r", false, 0 },
  81. { "\n", false, 0 },
  82. { "\t", false, 0 },
  83. { "0" , false, 0 },
  84. { "9" , false, 0 },
  85. { "a" , true , 'a' },
  86. { "a" , true , 'a' },
  87. { "z" , true , 'z' },
  88. { "A" , true , 'A' },
  89. { "Z" , true , 'Z' },
  90. { "az", true , 'a' },
  91. { "a0", true , 'a' },
  92. { "0a", false, 0 },
  93. };
  94. for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
  95. const auto match = Alpha::Match(gTests[i].fInput);
  96. REPORTER_ASSERT(r, match == gTests[i].fMatch);
  97. REPORTER_ASSERT(r, match.fNext == (match ? gTests[i].fInput + 1 : nullptr));
  98. if (match) {
  99. REPORTER_ASSERT(r, *match == gTests[i].fMatchValue);
  100. }
  101. }
  102. }
  103. void test_Digit(skiatest::Reporter* r) {
  104. static const struct {
  105. const char* fInput;
  106. bool fMatch;
  107. uint8_t fMatchValue;
  108. } gTests[] = {
  109. { "" , false, 0 },
  110. { "/" , false, 0 },
  111. { ":" , false, 0 },
  112. { "x" , false, 0 },
  113. { "x0" , false, 0 },
  114. { "0" , true , 0 },
  115. { "1x" , true , 1 },
  116. { "9 a", true , 9 },
  117. };
  118. for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
  119. const auto match = Digit::Match(gTests[i].fInput);
  120. REPORTER_ASSERT(r, match == gTests[i].fMatch);
  121. REPORTER_ASSERT(r, match.fNext == (match ? gTests[i].fInput + 1 : nullptr));
  122. if (match) {
  123. REPORTER_ASSERT(r, *match == gTests[i].fMatchValue);
  124. }
  125. }
  126. }
  127. void test_Opt(skiatest::Reporter* r) {
  128. static const struct {
  129. const char* fInput;
  130. bool fMatch;
  131. } gTests[] = {
  132. { "" , false },
  133. { "fo" , false },
  134. { " foo" , false },
  135. { "foo" , true },
  136. { "foobar" , true },
  137. };
  138. for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
  139. const auto m = Opt<LIT<'f', 'o', 'o'>>::Match(gTests[i].fInput);
  140. REPORTER_ASSERT(r, m);
  141. REPORTER_ASSERT(r, m->fValue.isValid() == gTests[i].fMatch);
  142. }
  143. }
  144. void test_Seq(skiatest::Reporter* r) {
  145. REPORTER_ASSERT(r, (Seq<LIT<'X'>, EOS>::Match("X")));
  146. REPORTER_ASSERT(r, !(Seq<LIT<'X'>, EOS>::Match("x")));
  147. REPORTER_ASSERT(r, !(Seq<LIT<'X'>, EOS>::Match("xX")));
  148. REPORTER_ASSERT(r, !(Seq<LIT<'X'>, EOS>::Match("XX")));
  149. REPORTER_ASSERT(r, (Seq<LIT<'X'>, Seq<LIT<'X'>, EOS>>::Match("XX")));
  150. REPORTER_ASSERT(r, (Seq<LIT<'X'>, Seq<LIT<'X'>, EOS>>::Match("XX")));
  151. REPORTER_ASSERT(r, !(Seq<LIT<'F', 'o', 'o'>, EOS>::Match("FooBar")));
  152. REPORTER_ASSERT(r, (Seq<LIT<'F', 'o', 'o'>, EOS>::Match("Foo")));
  153. {
  154. const auto m = Seq<LIT<'x'>, Digit>::Match("x5");
  155. REPORTER_ASSERT(r, m);
  156. REPORTER_ASSERT(r, m->get<1>() == 5);
  157. }
  158. {
  159. const auto m = Seq<Digit, Digit>::Match("42");
  160. REPORTER_ASSERT(r, m);
  161. REPORTER_ASSERT(r, m->get<0>() == 4);
  162. REPORTER_ASSERT(r, m->get<1>() == 2);
  163. }
  164. }
  165. void test_Choice(skiatest::Reporter* r) {
  166. REPORTER_ASSERT(r, !(Choice<Digit,Alpha>::Match("")));
  167. REPORTER_ASSERT(r, !(Choice<Digit,Alpha>::Match("\t")));
  168. REPORTER_ASSERT(r, !(Choice<Digit,Alpha>::Match(" ")));
  169. REPORTER_ASSERT(r, (Choice<Digit,Alpha>::Match("a")));
  170. REPORTER_ASSERT(r, (Choice<Digit,Alpha>::Match("3")));
  171. REPORTER_ASSERT(r, (Choice<Digit,Alpha>::Match("a ")));
  172. REPORTER_ASSERT(r, (Choice<Digit,Alpha>::Match("3 ")));
  173. REPORTER_ASSERT(r, !(Choice<Digit,Alpha>::Match(" a ")));
  174. REPORTER_ASSERT(r, !(Choice<Digit,Alpha>::Match(" 3 ")));
  175. {
  176. const auto m = Choice<Alpha, Digit>::Match("x");
  177. REPORTER_ASSERT(r, m);
  178. REPORTER_ASSERT(r, m->v1.isValid());
  179. REPORTER_ASSERT(r, !m->v2.isValid());
  180. REPORTER_ASSERT(r, *m->v1.get() == 'x');
  181. }
  182. {
  183. const auto m = Choice<Alpha, Digit>::Match("7");
  184. REPORTER_ASSERT(r, m);
  185. REPORTER_ASSERT(r, !m->v1.isValid());
  186. REPORTER_ASSERT(r, m->v2.isValid());
  187. REPORTER_ASSERT(r, *m->v2.get() == 7);
  188. }
  189. }
  190. void test_AnySome(skiatest::Reporter* r) {
  191. static const struct {
  192. const char* fInput;
  193. int fCount;
  194. } gTests[] = {
  195. { "" , 0 },
  196. { "fo" , 0 },
  197. { "Foo" , 0 },
  198. { "foo" , 1 },
  199. { "foofoo", 2 },
  200. };
  201. for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
  202. const auto matchAny = Any<LIT<'f', 'o', 'o'>>::Match(gTests[i].fInput);
  203. REPORTER_ASSERT(r, matchAny);
  204. REPORTER_ASSERT(r, matchAny->fValues.count() == gTests[i].fCount);
  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].fCount - 1);
  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. void test_Complex(skiatest::Reporter* r) {
  220. // [0-9]+(,[0-9]+)?$
  221. using P0 =
  222. Seq<
  223. Some<Digit>,
  224. Opt<Seq<
  225. LIT<','>,
  226. Some<Digit>>>,
  227. EOS>;
  228. REPORTER_ASSERT(r, !P0::Match(""));
  229. REPORTER_ASSERT(r, !P0::Match(","));
  230. REPORTER_ASSERT(r, !P0::Match("1,"));
  231. REPORTER_ASSERT(r, !P0::Match(",1"));
  232. REPORTER_ASSERT(r, P0::Match("1"));
  233. REPORTER_ASSERT(r, P0::Match("1,2"));
  234. REPORTER_ASSERT(r, !P0::Match("1,2 "));
  235. REPORTER_ASSERT(r, P0::Match("123,456"));
  236. // [ ]*[Ff]oo([Bb]ar)+[Bb]az[ ]*$
  237. using P1 =
  238. Seq<
  239. Any<LIT<' '>>,
  240. Choice<LIT<'F'>, LIT<'f'>>,
  241. LIT<'o', 'o'>,
  242. Some<Seq<
  243. Choice<LIT<'B'>, LIT<'b'>>,
  244. LIT<'a', 'r'>>>,
  245. Choice<LIT<'B'>, LIT<'b'>>,
  246. LIT<'a', 'z'>,
  247. Any<LIT<' '>>,
  248. EOS>;
  249. REPORTER_ASSERT(r, !P1::Match(""));
  250. REPORTER_ASSERT(r, !P1::Match("FooBar"));
  251. REPORTER_ASSERT(r, !P1::Match("FooBaz"));
  252. REPORTER_ASSERT(r, P1::Match("FooBarBaz"));
  253. REPORTER_ASSERT(r, P1::Match("foobarbaz"));
  254. REPORTER_ASSERT(r, P1::Match(" FooBarbaz "));
  255. REPORTER_ASSERT(r, P1::Match(" FooBarbarbarBaz "));
  256. }
  257. } // anonymous ns
  258. DEF_TEST(SkPEG, r) {
  259. test_EOS(r);
  260. test_LIT(r);
  261. test_Alpha(r);
  262. test_Digit(r);
  263. test_Opt(r);
  264. test_Seq(r);
  265. test_Choice(r);
  266. test_AnySome(r);
  267. test_Complex(r);
  268. }
  269. #endif // SK_XML