regextest.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "unittest.h"
  15. #include "rapidjson/internal/regex.h"
  16. using namespace rapidjson::internal;
  17. TEST(Regex, Single) {
  18. Regex re("a");
  19. ASSERT_TRUE(re.IsValid());
  20. RegexSearch rs(re);
  21. EXPECT_TRUE(rs.Match("a"));
  22. EXPECT_FALSE(rs.Match(""));
  23. EXPECT_FALSE(rs.Match("b"));
  24. }
  25. TEST(Regex, Concatenation) {
  26. Regex re("abc");
  27. ASSERT_TRUE(re.IsValid());
  28. RegexSearch rs(re);
  29. EXPECT_TRUE(rs.Match("abc"));
  30. EXPECT_FALSE(rs.Match(""));
  31. EXPECT_FALSE(rs.Match("a"));
  32. EXPECT_FALSE(rs.Match("b"));
  33. EXPECT_FALSE(rs.Match("ab"));
  34. EXPECT_FALSE(rs.Match("abcd"));
  35. }
  36. TEST(Regex, Alternation1) {
  37. Regex re("abab|abbb");
  38. ASSERT_TRUE(re.IsValid());
  39. RegexSearch rs(re);
  40. EXPECT_TRUE(rs.Match("abab"));
  41. EXPECT_TRUE(rs.Match("abbb"));
  42. EXPECT_FALSE(rs.Match(""));
  43. EXPECT_FALSE(rs.Match("ab"));
  44. EXPECT_FALSE(rs.Match("ababa"));
  45. EXPECT_FALSE(rs.Match("abb"));
  46. EXPECT_FALSE(rs.Match("abbbb"));
  47. }
  48. TEST(Regex, Alternation2) {
  49. Regex re("a|b|c");
  50. ASSERT_TRUE(re.IsValid());
  51. RegexSearch rs(re);
  52. EXPECT_TRUE(rs.Match("a"));
  53. EXPECT_TRUE(rs.Match("b"));
  54. EXPECT_TRUE(rs.Match("c"));
  55. EXPECT_FALSE(rs.Match(""));
  56. EXPECT_FALSE(rs.Match("aa"));
  57. EXPECT_FALSE(rs.Match("ab"));
  58. }
  59. TEST(Regex, Parenthesis1) {
  60. Regex re("(ab)c");
  61. ASSERT_TRUE(re.IsValid());
  62. RegexSearch rs(re);
  63. EXPECT_TRUE(rs.Match("abc"));
  64. EXPECT_FALSE(rs.Match(""));
  65. EXPECT_FALSE(rs.Match("a"));
  66. EXPECT_FALSE(rs.Match("b"));
  67. EXPECT_FALSE(rs.Match("ab"));
  68. EXPECT_FALSE(rs.Match("abcd"));
  69. }
  70. TEST(Regex, Parenthesis2) {
  71. Regex re("a(bc)");
  72. ASSERT_TRUE(re.IsValid());
  73. RegexSearch rs(re);
  74. EXPECT_TRUE(rs.Match("abc"));
  75. EXPECT_FALSE(rs.Match(""));
  76. EXPECT_FALSE(rs.Match("a"));
  77. EXPECT_FALSE(rs.Match("b"));
  78. EXPECT_FALSE(rs.Match("ab"));
  79. EXPECT_FALSE(rs.Match("abcd"));
  80. }
  81. TEST(Regex, Parenthesis3) {
  82. Regex re("(a|b)(c|d)");
  83. ASSERT_TRUE(re.IsValid());
  84. RegexSearch rs(re);
  85. EXPECT_TRUE(rs.Match("ac"));
  86. EXPECT_TRUE(rs.Match("ad"));
  87. EXPECT_TRUE(rs.Match("bc"));
  88. EXPECT_TRUE(rs.Match("bd"));
  89. EXPECT_FALSE(rs.Match(""));
  90. EXPECT_FALSE(rs.Match("ab"));
  91. EXPECT_FALSE(rs.Match("cd"));
  92. }
  93. TEST(Regex, ZeroOrOne1) {
  94. Regex re("a?");
  95. ASSERT_TRUE(re.IsValid());
  96. RegexSearch rs(re);
  97. EXPECT_TRUE(rs.Match(""));
  98. EXPECT_TRUE(rs.Match("a"));
  99. EXPECT_FALSE(rs.Match("aa"));
  100. }
  101. TEST(Regex, ZeroOrOne2) {
  102. Regex re("a?b");
  103. ASSERT_TRUE(re.IsValid());
  104. RegexSearch rs(re);
  105. EXPECT_TRUE(rs.Match("b"));
  106. EXPECT_TRUE(rs.Match("ab"));
  107. EXPECT_FALSE(rs.Match("a"));
  108. EXPECT_FALSE(rs.Match("aa"));
  109. EXPECT_FALSE(rs.Match("bb"));
  110. EXPECT_FALSE(rs.Match("ba"));
  111. }
  112. TEST(Regex, ZeroOrOne3) {
  113. Regex re("ab?");
  114. ASSERT_TRUE(re.IsValid());
  115. RegexSearch rs(re);
  116. EXPECT_TRUE(rs.Match("a"));
  117. EXPECT_TRUE(rs.Match("ab"));
  118. EXPECT_FALSE(rs.Match("b"));
  119. EXPECT_FALSE(rs.Match("aa"));
  120. EXPECT_FALSE(rs.Match("bb"));
  121. EXPECT_FALSE(rs.Match("ba"));
  122. }
  123. TEST(Regex, ZeroOrOne4) {
  124. Regex re("a?b?");
  125. ASSERT_TRUE(re.IsValid());
  126. RegexSearch rs(re);
  127. EXPECT_TRUE(rs.Match(""));
  128. EXPECT_TRUE(rs.Match("a"));
  129. EXPECT_TRUE(rs.Match("b"));
  130. EXPECT_TRUE(rs.Match("ab"));
  131. EXPECT_FALSE(rs.Match("aa"));
  132. EXPECT_FALSE(rs.Match("bb"));
  133. EXPECT_FALSE(rs.Match("ba"));
  134. EXPECT_FALSE(rs.Match("abc"));
  135. }
  136. TEST(Regex, ZeroOrOne5) {
  137. Regex re("a(ab)?b");
  138. ASSERT_TRUE(re.IsValid());
  139. RegexSearch rs(re);
  140. EXPECT_TRUE(rs.Match("ab"));
  141. EXPECT_TRUE(rs.Match("aabb"));
  142. EXPECT_FALSE(rs.Match("aab"));
  143. EXPECT_FALSE(rs.Match("abb"));
  144. }
  145. TEST(Regex, ZeroOrMore1) {
  146. Regex re("a*");
  147. ASSERT_TRUE(re.IsValid());
  148. RegexSearch rs(re);
  149. EXPECT_TRUE(rs.Match(""));
  150. EXPECT_TRUE(rs.Match("a"));
  151. EXPECT_TRUE(rs.Match("aa"));
  152. EXPECT_FALSE(rs.Match("b"));
  153. EXPECT_FALSE(rs.Match("ab"));
  154. }
  155. TEST(Regex, ZeroOrMore2) {
  156. Regex re("a*b");
  157. ASSERT_TRUE(re.IsValid());
  158. RegexSearch rs(re);
  159. EXPECT_TRUE(rs.Match("b"));
  160. EXPECT_TRUE(rs.Match("ab"));
  161. EXPECT_TRUE(rs.Match("aab"));
  162. EXPECT_FALSE(rs.Match(""));
  163. EXPECT_FALSE(rs.Match("bb"));
  164. }
  165. TEST(Regex, ZeroOrMore3) {
  166. Regex re("a*b*");
  167. ASSERT_TRUE(re.IsValid());
  168. RegexSearch rs(re);
  169. EXPECT_TRUE(rs.Match(""));
  170. EXPECT_TRUE(rs.Match("a"));
  171. EXPECT_TRUE(rs.Match("aa"));
  172. EXPECT_TRUE(rs.Match("b"));
  173. EXPECT_TRUE(rs.Match("bb"));
  174. EXPECT_TRUE(rs.Match("ab"));
  175. EXPECT_TRUE(rs.Match("aabb"));
  176. EXPECT_FALSE(rs.Match("ba"));
  177. }
  178. TEST(Regex, ZeroOrMore4) {
  179. Regex re("a(ab)*b");
  180. ASSERT_TRUE(re.IsValid());
  181. RegexSearch rs(re);
  182. EXPECT_TRUE(rs.Match("ab"));
  183. EXPECT_TRUE(rs.Match("aabb"));
  184. EXPECT_TRUE(rs.Match("aababb"));
  185. EXPECT_FALSE(rs.Match(""));
  186. EXPECT_FALSE(rs.Match("aa"));
  187. }
  188. TEST(Regex, OneOrMore1) {
  189. Regex re("a+");
  190. ASSERT_TRUE(re.IsValid());
  191. RegexSearch rs(re);
  192. EXPECT_TRUE(rs.Match("a"));
  193. EXPECT_TRUE(rs.Match("aa"));
  194. EXPECT_FALSE(rs.Match(""));
  195. EXPECT_FALSE(rs.Match("b"));
  196. EXPECT_FALSE(rs.Match("ab"));
  197. }
  198. TEST(Regex, OneOrMore2) {
  199. Regex re("a+b");
  200. ASSERT_TRUE(re.IsValid());
  201. RegexSearch rs(re);
  202. EXPECT_TRUE(rs.Match("ab"));
  203. EXPECT_TRUE(rs.Match("aab"));
  204. EXPECT_FALSE(rs.Match(""));
  205. EXPECT_FALSE(rs.Match("b"));
  206. }
  207. TEST(Regex, OneOrMore3) {
  208. Regex re("a+b+");
  209. ASSERT_TRUE(re.IsValid());
  210. RegexSearch rs(re);
  211. EXPECT_TRUE(rs.Match("ab"));
  212. EXPECT_TRUE(rs.Match("aab"));
  213. EXPECT_TRUE(rs.Match("abb"));
  214. EXPECT_TRUE(rs.Match("aabb"));
  215. EXPECT_FALSE(rs.Match(""));
  216. EXPECT_FALSE(rs.Match("b"));
  217. EXPECT_FALSE(rs.Match("ba"));
  218. }
  219. TEST(Regex, OneOrMore4) {
  220. Regex re("a(ab)+b");
  221. ASSERT_TRUE(re.IsValid());
  222. RegexSearch rs(re);
  223. EXPECT_TRUE(rs.Match("aabb"));
  224. EXPECT_TRUE(rs.Match("aababb"));
  225. EXPECT_FALSE(rs.Match(""));
  226. EXPECT_FALSE(rs.Match("ab"));
  227. }
  228. TEST(Regex, QuantifierExact1) {
  229. Regex re("ab{3}c");
  230. ASSERT_TRUE(re.IsValid());
  231. RegexSearch rs(re);
  232. EXPECT_TRUE(rs.Match("abbbc"));
  233. EXPECT_FALSE(rs.Match("ac"));
  234. EXPECT_FALSE(rs.Match("abc"));
  235. EXPECT_FALSE(rs.Match("abbc"));
  236. EXPECT_FALSE(rs.Match("abbbbc"));
  237. }
  238. TEST(Regex, QuantifierExact2) {
  239. Regex re("a(bc){3}d");
  240. ASSERT_TRUE(re.IsValid());
  241. RegexSearch rs(re);
  242. EXPECT_TRUE(rs.Match("abcbcbcd"));
  243. EXPECT_FALSE(rs.Match("ad"));
  244. EXPECT_FALSE(rs.Match("abcd"));
  245. EXPECT_FALSE(rs.Match("abcbcd"));
  246. EXPECT_FALSE(rs.Match("abcbcbcbcd"));
  247. }
  248. TEST(Regex, QuantifierExact3) {
  249. Regex re("a(b|c){3}d");
  250. ASSERT_TRUE(re.IsValid());
  251. RegexSearch rs(re);
  252. EXPECT_TRUE(rs.Match("abbbd"));
  253. EXPECT_TRUE(rs.Match("acccd"));
  254. EXPECT_TRUE(rs.Match("abcbd"));
  255. EXPECT_FALSE(rs.Match("ad"));
  256. EXPECT_FALSE(rs.Match("abbd"));
  257. EXPECT_FALSE(rs.Match("accccd"));
  258. EXPECT_FALSE(rs.Match("abbbbd"));
  259. }
  260. TEST(Regex, QuantifierMin1) {
  261. Regex re("ab{3,}c");
  262. ASSERT_TRUE(re.IsValid());
  263. RegexSearch rs(re);
  264. EXPECT_TRUE(rs.Match("abbbc"));
  265. EXPECT_TRUE(rs.Match("abbbbc"));
  266. EXPECT_TRUE(rs.Match("abbbbbc"));
  267. EXPECT_FALSE(rs.Match("ac"));
  268. EXPECT_FALSE(rs.Match("abc"));
  269. EXPECT_FALSE(rs.Match("abbc"));
  270. }
  271. TEST(Regex, QuantifierMin2) {
  272. Regex re("a(bc){3,}d");
  273. ASSERT_TRUE(re.IsValid());
  274. RegexSearch rs(re);
  275. EXPECT_TRUE(rs.Match("abcbcbcd"));
  276. EXPECT_TRUE(rs.Match("abcbcbcbcd"));
  277. EXPECT_FALSE(rs.Match("ad"));
  278. EXPECT_FALSE(rs.Match("abcd"));
  279. EXPECT_FALSE(rs.Match("abcbcd"));
  280. }
  281. TEST(Regex, QuantifierMin3) {
  282. Regex re("a(b|c){3,}d");
  283. ASSERT_TRUE(re.IsValid());
  284. RegexSearch rs(re);
  285. EXPECT_TRUE(rs.Match("abbbd"));
  286. EXPECT_TRUE(rs.Match("acccd"));
  287. EXPECT_TRUE(rs.Match("abcbd"));
  288. EXPECT_TRUE(rs.Match("accccd"));
  289. EXPECT_TRUE(rs.Match("abbbbd"));
  290. EXPECT_FALSE(rs.Match("ad"));
  291. EXPECT_FALSE(rs.Match("abbd"));
  292. }
  293. TEST(Regex, QuantifierMinMax1) {
  294. Regex re("ab{3,5}c");
  295. ASSERT_TRUE(re.IsValid());
  296. RegexSearch rs(re);
  297. EXPECT_TRUE(rs.Match("abbbc"));
  298. EXPECT_TRUE(rs.Match("abbbbc"));
  299. EXPECT_TRUE(rs.Match("abbbbbc"));
  300. EXPECT_FALSE(rs.Match("ac"));
  301. EXPECT_FALSE(rs.Match("abc"));
  302. EXPECT_FALSE(rs.Match("abbc"));
  303. EXPECT_FALSE(rs.Match("abbbbbbc"));
  304. }
  305. TEST(Regex, QuantifierMinMax2) {
  306. Regex re("a(bc){3,5}d");
  307. ASSERT_TRUE(re.IsValid());
  308. RegexSearch rs(re);
  309. EXPECT_TRUE(rs.Match("abcbcbcd"));
  310. EXPECT_TRUE(rs.Match("abcbcbcbcd"));
  311. EXPECT_TRUE(rs.Match("abcbcbcbcbcd"));
  312. EXPECT_FALSE(rs.Match("ad"));
  313. EXPECT_FALSE(rs.Match("abcd"));
  314. EXPECT_FALSE(rs.Match("abcbcd"));
  315. EXPECT_FALSE(rs.Match("abcbcbcbcbcbcd"));
  316. }
  317. TEST(Regex, QuantifierMinMax3) {
  318. Regex re("a(b|c){3,5}d");
  319. ASSERT_TRUE(re.IsValid());
  320. RegexSearch rs(re);
  321. EXPECT_TRUE(rs.Match("abbbd"));
  322. EXPECT_TRUE(rs.Match("acccd"));
  323. EXPECT_TRUE(rs.Match("abcbd"));
  324. EXPECT_TRUE(rs.Match("accccd"));
  325. EXPECT_TRUE(rs.Match("abbbbd"));
  326. EXPECT_TRUE(rs.Match("acccccd"));
  327. EXPECT_TRUE(rs.Match("abbbbbd"));
  328. EXPECT_FALSE(rs.Match("ad"));
  329. EXPECT_FALSE(rs.Match("abbd"));
  330. EXPECT_FALSE(rs.Match("accccccd"));
  331. EXPECT_FALSE(rs.Match("abbbbbbd"));
  332. }
  333. // Issue538
  334. TEST(Regex, QuantifierMinMax4) {
  335. Regex re("a(b|c){0,3}d");
  336. ASSERT_TRUE(re.IsValid());
  337. RegexSearch rs(re);
  338. EXPECT_TRUE(rs.Match("ad"));
  339. EXPECT_TRUE(rs.Match("abd"));
  340. EXPECT_TRUE(rs.Match("acd"));
  341. EXPECT_TRUE(rs.Match("abbd"));
  342. EXPECT_TRUE(rs.Match("accd"));
  343. EXPECT_TRUE(rs.Match("abcd"));
  344. EXPECT_TRUE(rs.Match("abbbd"));
  345. EXPECT_TRUE(rs.Match("acccd"));
  346. EXPECT_FALSE(rs.Match("abbbbd"));
  347. EXPECT_FALSE(rs.Match("add"));
  348. EXPECT_FALSE(rs.Match("accccd"));
  349. EXPECT_FALSE(rs.Match("abcbcd"));
  350. }
  351. // Issue538
  352. TEST(Regex, QuantifierMinMax5) {
  353. Regex re("a(b|c){0,}d");
  354. ASSERT_TRUE(re.IsValid());
  355. RegexSearch rs(re);
  356. EXPECT_TRUE(rs.Match("ad"));
  357. EXPECT_TRUE(rs.Match("abd"));
  358. EXPECT_TRUE(rs.Match("acd"));
  359. EXPECT_TRUE(rs.Match("abbd"));
  360. EXPECT_TRUE(rs.Match("accd"));
  361. EXPECT_TRUE(rs.Match("abcd"));
  362. EXPECT_TRUE(rs.Match("abbbd"));
  363. EXPECT_TRUE(rs.Match("acccd"));
  364. EXPECT_TRUE(rs.Match("abbbbd"));
  365. EXPECT_TRUE(rs.Match("accccd"));
  366. EXPECT_TRUE(rs.Match("abcbcd"));
  367. EXPECT_FALSE(rs.Match("add"));
  368. EXPECT_FALSE(rs.Match("aad"));
  369. }
  370. #define EURO "\xE2\x82\xAC" // "\xE2\x82\xAC" is UTF-8 rsquence of Euro sign U+20AC
  371. TEST(Regex, Unicode) {
  372. Regex re("a" EURO "+b");
  373. ASSERT_TRUE(re.IsValid());
  374. RegexSearch rs(re);
  375. EXPECT_TRUE(rs.Match("a" EURO "b"));
  376. EXPECT_TRUE(rs.Match("a" EURO EURO "b"));
  377. EXPECT_FALSE(rs.Match("a?b"));
  378. EXPECT_FALSE(rs.Match("a" EURO "\xAC" "b")); // unaware of UTF-8 will match
  379. }
  380. TEST(Regex, AnyCharacter) {
  381. Regex re(".");
  382. ASSERT_TRUE(re.IsValid());
  383. RegexSearch rs(re);
  384. EXPECT_TRUE(rs.Match("a"));
  385. EXPECT_TRUE(rs.Match("b"));
  386. EXPECT_TRUE(rs.Match(EURO));
  387. EXPECT_FALSE(rs.Match(""));
  388. EXPECT_FALSE(rs.Match("aa"));
  389. }
  390. TEST(Regex, CharacterRange1) {
  391. Regex re("[abc]");
  392. ASSERT_TRUE(re.IsValid());
  393. RegexSearch rs(re);
  394. EXPECT_TRUE(rs.Match("a"));
  395. EXPECT_TRUE(rs.Match("b"));
  396. EXPECT_TRUE(rs.Match("c"));
  397. EXPECT_FALSE(rs.Match(""));
  398. EXPECT_FALSE(rs.Match("`"));
  399. EXPECT_FALSE(rs.Match("d"));
  400. EXPECT_FALSE(rs.Match("aa"));
  401. }
  402. TEST(Regex, CharacterRange2) {
  403. Regex re("[^abc]");
  404. ASSERT_TRUE(re.IsValid());
  405. RegexSearch rs(re);
  406. EXPECT_TRUE(rs.Match("`"));
  407. EXPECT_TRUE(rs.Match("d"));
  408. EXPECT_FALSE(rs.Match("a"));
  409. EXPECT_FALSE(rs.Match("b"));
  410. EXPECT_FALSE(rs.Match("c"));
  411. EXPECT_FALSE(rs.Match(""));
  412. EXPECT_FALSE(rs.Match("aa"));
  413. }
  414. TEST(Regex, CharacterRange3) {
  415. Regex re("[a-c]");
  416. ASSERT_TRUE(re.IsValid());
  417. RegexSearch rs(re);
  418. EXPECT_TRUE(rs.Match("a"));
  419. EXPECT_TRUE(rs.Match("b"));
  420. EXPECT_TRUE(rs.Match("c"));
  421. EXPECT_FALSE(rs.Match(""));
  422. EXPECT_FALSE(rs.Match("`"));
  423. EXPECT_FALSE(rs.Match("d"));
  424. EXPECT_FALSE(rs.Match("aa"));
  425. }
  426. TEST(Regex, CharacterRange4) {
  427. Regex re("[^a-c]");
  428. ASSERT_TRUE(re.IsValid());
  429. RegexSearch rs(re);
  430. EXPECT_TRUE(rs.Match("`"));
  431. EXPECT_TRUE(rs.Match("d"));
  432. EXPECT_FALSE(rs.Match("a"));
  433. EXPECT_FALSE(rs.Match("b"));
  434. EXPECT_FALSE(rs.Match("c"));
  435. EXPECT_FALSE(rs.Match(""));
  436. EXPECT_FALSE(rs.Match("aa"));
  437. }
  438. TEST(Regex, CharacterRange5) {
  439. Regex re("[-]");
  440. ASSERT_TRUE(re.IsValid());
  441. RegexSearch rs(re);
  442. EXPECT_TRUE(rs.Match("-"));
  443. EXPECT_FALSE(rs.Match(""));
  444. EXPECT_FALSE(rs.Match("a"));
  445. }
  446. TEST(Regex, CharacterRange6) {
  447. Regex re("[a-]");
  448. ASSERT_TRUE(re.IsValid());
  449. RegexSearch rs(re);
  450. EXPECT_TRUE(rs.Match("a"));
  451. EXPECT_TRUE(rs.Match("-"));
  452. EXPECT_FALSE(rs.Match(""));
  453. EXPECT_FALSE(rs.Match("`"));
  454. EXPECT_FALSE(rs.Match("b"));
  455. }
  456. TEST(Regex, CharacterRange7) {
  457. Regex re("[-a]");
  458. ASSERT_TRUE(re.IsValid());
  459. RegexSearch rs(re);
  460. EXPECT_TRUE(rs.Match("a"));
  461. EXPECT_TRUE(rs.Match("-"));
  462. EXPECT_FALSE(rs.Match(""));
  463. EXPECT_FALSE(rs.Match("`"));
  464. EXPECT_FALSE(rs.Match("b"));
  465. }
  466. TEST(Regex, CharacterRange8) {
  467. Regex re("[a-zA-Z0-9]*");
  468. ASSERT_TRUE(re.IsValid());
  469. RegexSearch rs(re);
  470. EXPECT_TRUE(rs.Match("Milo"));
  471. EXPECT_TRUE(rs.Match("MT19937"));
  472. EXPECT_TRUE(rs.Match("43"));
  473. EXPECT_FALSE(rs.Match("a_b"));
  474. EXPECT_FALSE(rs.Match("!"));
  475. }
  476. TEST(Regex, Search) {
  477. Regex re("abc");
  478. ASSERT_TRUE(re.IsValid());
  479. RegexSearch rs(re);
  480. EXPECT_TRUE(rs.Search("abc"));
  481. EXPECT_TRUE(rs.Search("_abc"));
  482. EXPECT_TRUE(rs.Search("abc_"));
  483. EXPECT_TRUE(rs.Search("_abc_"));
  484. EXPECT_TRUE(rs.Search("__abc__"));
  485. EXPECT_TRUE(rs.Search("abcabc"));
  486. EXPECT_FALSE(rs.Search("a"));
  487. EXPECT_FALSE(rs.Search("ab"));
  488. EXPECT_FALSE(rs.Search("bc"));
  489. EXPECT_FALSE(rs.Search("cba"));
  490. }
  491. TEST(Regex, Search_BeginAnchor) {
  492. Regex re("^abc");
  493. ASSERT_TRUE(re.IsValid());
  494. RegexSearch rs(re);
  495. EXPECT_TRUE(rs.Search("abc"));
  496. EXPECT_TRUE(rs.Search("abc_"));
  497. EXPECT_TRUE(rs.Search("abcabc"));
  498. EXPECT_FALSE(rs.Search("_abc"));
  499. EXPECT_FALSE(rs.Search("_abc_"));
  500. EXPECT_FALSE(rs.Search("a"));
  501. EXPECT_FALSE(rs.Search("ab"));
  502. EXPECT_FALSE(rs.Search("bc"));
  503. EXPECT_FALSE(rs.Search("cba"));
  504. }
  505. TEST(Regex, Search_EndAnchor) {
  506. Regex re("abc$");
  507. ASSERT_TRUE(re.IsValid());
  508. RegexSearch rs(re);
  509. EXPECT_TRUE(rs.Search("abc"));
  510. EXPECT_TRUE(rs.Search("_abc"));
  511. EXPECT_TRUE(rs.Search("abcabc"));
  512. EXPECT_FALSE(rs.Search("abc_"));
  513. EXPECT_FALSE(rs.Search("_abc_"));
  514. EXPECT_FALSE(rs.Search("a"));
  515. EXPECT_FALSE(rs.Search("ab"));
  516. EXPECT_FALSE(rs.Search("bc"));
  517. EXPECT_FALSE(rs.Search("cba"));
  518. }
  519. TEST(Regex, Search_BothAnchor) {
  520. Regex re("^abc$");
  521. ASSERT_TRUE(re.IsValid());
  522. RegexSearch rs(re);
  523. EXPECT_TRUE(rs.Search("abc"));
  524. EXPECT_FALSE(rs.Search(""));
  525. EXPECT_FALSE(rs.Search("a"));
  526. EXPECT_FALSE(rs.Search("b"));
  527. EXPECT_FALSE(rs.Search("ab"));
  528. EXPECT_FALSE(rs.Search("abcd"));
  529. }
  530. TEST(Regex, Escape) {
  531. const char* s = "\\^\\$\\|\\(\\)\\?\\*\\+\\.\\[\\]\\{\\}\\\\\\f\\n\\r\\t\\v[\\b][\\[][\\]]";
  532. Regex re(s);
  533. ASSERT_TRUE(re.IsValid());
  534. RegexSearch rs(re);
  535. EXPECT_TRUE(rs.Match("^$|()?*+.[]{}\\\x0C\n\r\t\x0B\b[]"));
  536. EXPECT_FALSE(rs.Match(s)); // Not escaping
  537. }
  538. TEST(Regex, Invalid) {
  539. #define TEST_INVALID(s) \
  540. {\
  541. Regex re(s);\
  542. EXPECT_FALSE(re.IsValid());\
  543. }
  544. TEST_INVALID("");
  545. TEST_INVALID("a|");
  546. TEST_INVALID("()");
  547. TEST_INVALID(")");
  548. TEST_INVALID("(a))");
  549. TEST_INVALID("(a|)");
  550. TEST_INVALID("(a||b)");
  551. TEST_INVALID("(|b)");
  552. TEST_INVALID("?");
  553. TEST_INVALID("*");
  554. TEST_INVALID("+");
  555. TEST_INVALID("{");
  556. TEST_INVALID("{}");
  557. TEST_INVALID("a{a}");
  558. TEST_INVALID("a{0}");
  559. TEST_INVALID("a{-1}");
  560. TEST_INVALID("a{}");
  561. // TEST_INVALID("a{0,}"); // Support now
  562. TEST_INVALID("a{,0}");
  563. TEST_INVALID("a{1,0}");
  564. TEST_INVALID("a{-1,0}");
  565. TEST_INVALID("a{-1,1}");
  566. TEST_INVALID("a{4294967296}"); // overflow of unsigned
  567. TEST_INVALID("a{1a}");
  568. TEST_INVALID("[");
  569. TEST_INVALID("[]");
  570. TEST_INVALID("[^]");
  571. TEST_INVALID("[\\a]");
  572. TEST_INVALID("\\a");
  573. #undef TEST_INVALID
  574. }
  575. TEST(Regex, Issue538) {
  576. Regex re("^[0-9]+(\\\\.[0-9]+){0,2}");
  577. EXPECT_TRUE(re.IsValid());
  578. }
  579. TEST(Regex, Issue583) {
  580. Regex re("[0-9]{99999}");
  581. ASSERT_TRUE(re.IsValid());
  582. }
  583. #undef EURO