-
Notifications
You must be signed in to change notification settings - Fork 2
/
cl-string-generator-tests.lisp
74 lines (67 loc) · 2.83 KB
/
cl-string-generator-tests.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(defpackage #:cl-string-generator-tests
(:use :cl :cl-string-generator :rove :alexandria))
(in-package #:cl-string-generator-tests)
(defun ensure-char (string)
(assert (and (stringp string)
(= 1 (length string))))
(char string 0))
(defun simple-test (regex test expected-num)
(loop :with table := (make-array 128 :initial-element nil)
:repeat 1000
:for string := (generate regex)
:always (and (stringp string) (= 1 (length string)) (funcall test (char string 0)))
:do (setf (aref table (char-code (char string 0))) t)
:finally (return (= expected-num (count t table)))))
(deftest simple-regex-tests
(ok (string= "" (generate "")))
(ok (string= "a" (generate #\a)))
(ok (string= "a" (generate "a")))
(ok (string= "abc" (generate "abc")))
(ok (simple-test "." #'graphic-char-p 95))
(ok (simple-test "\\d" #'digit-char-p 10))
(ok (simple-test "\\w" #'ppcre::word-char-p 63))
(ok (simple-test "\\s" #'ppcre::whitespacep 5))
(ok (simple-test "\\D" (complement #'digit-char-p) 118))
(ok (simple-test "\\S" (complement #'ppcre::whitespacep) 123))
(ok (simple-test "\\W" (complement #'ppcre::word-char-p) 65)))
(deftest complex-regex-tests
(ok (string= "ab" (generate '(:sequence "a" "b"))))
(ok (loop :with chars := '()
:repeat 100
:for c := (ensure-char (generate "a|b"))
:do (pushnew c chars)
:finally (return (length= chars 2))))
(ok (loop :with chars := '()
:repeat 100
:for c := (ensure-char (generate "a|b|c"))
:do (pushnew c chars)
:finally (return (length= chars 3))))
(ok (string= "ab" (generate "(?:ab)")))
(ok (string= "abcd" (generate '(:group "ab" "cd"))))
(ok (loop :with chars := '()
:repeat 100
:for c := (ensure-char (generate "[a-c]"))
:do (pushnew c chars)
:finally (return (length= chars 3))))
(ok (loop :with chars := '()
:repeat 100
:for c := (ensure-char (generate "[a-c0-2_]"))
:do (pushnew c chars)
:finally (return (length= chars 7))))
(ok (loop :with chars := '()
:repeat 100
:for c := (ensure-char (generate "[abc]"))
:do (pushnew c chars)
:finally (return (length= chars 3))))
(ok (loop :repeat 10 :always (ppcre:scan "^a*$" (generate "a*"))))
(ok (loop :repeat 100 :always (ppcre:scan "^([0-9]*)\\1$" (generate "([0-9]*)\\1")))))
(deftest repetition-tests
(ok (loop :repeat 100
:always (string= (generate "a{4}") "aaaa"))))
(deftest max-length-tests
(ok (loop :repeat 100
:always (<= (length (generate "a*b*" :max-length 3)) 3))))
(deftest random-tests
(ok (loop :repeat 100
:always (ppcre:scan "^[a-zA-Z0-9-]*$"
(generate "^[a-zA-Z0-9-]*$")))))