| 1 |
(local fennel (require :fennel)) |
| 2 |
(local faith (require :test.faith)) |
| 3 |
(local load (or _G.loadstring load)) |
| 4 |
|
| 5 |
(fn lua-string-compiler [str] |
| 6 |
"Baseline implementation we're comparing against." |
| 7 |
((assert (load (.. "return " str))))) |
| 8 |
|
| 9 |
(fn fennel-string-compiler [str] |
| 10 |
"Our implementation. Round trip through the fennel compiler." |
| 11 |
((assert (load (pick-values 1 (fennel.compile-string str)))))) |
| 12 |
|
| 13 |
(fn fennel-parser [str] |
| 14 |
"Our implementation. Going just through the parser" |
| 15 |
(let [[_ok loaded] [((fennel.parser str))]] |
| 16 |
loaded)) |
| 17 |
|
| 18 |
(fn into-single-line-string [str] |
| 19 |
"Gives a string that meets the following conditions: |
| 20 |
* The string is surrounded by \" marks |
| 21 |
* The string has no newlines \\n or carriage returns \\r |
| 22 |
* The string has no unescaped \" marks" |
| 23 |
(.. "\"" |
| 24 |
(-> str |
| 25 |
;; no newlines (so that lua can parse it) |
| 26 |
(: :gsub "[\r\n]" "") |
| 27 |
;; all quotes must be escaped (so that it doesn't break out early) |
| 28 |
(: :gsub "\\*\"" |
| 29 |
;; make the number of slashes even |
| 30 |
#(when (= (% (length $) 2) 1) |
| 31 |
($:sub 2)))) |
| 32 |
"\"")) |
| 33 |
|
| 34 |
(fn generate-random-string [] |
| 35 |
"generate a random single-line string to be parsed by lua or fennel" |
| 36 |
(into-single-line-string |
| 37 |
(table.concat |
| 38 |
(fcollect [_ 1 (math.random 20)] |
| 39 |
(if (< (math.random) 0.5) |
| 40 |
(let [c "{}\t'\"uxabfnrtv0123456789 " |
| 41 |
n (math.random (length c))] |
| 42 |
(c:sub n n)) |
| 43 |
(< (math.random) 0.5) |
| 44 |
(string.char (math.random 0 255)) |
| 45 |
(< (math.random) 0.5) |
| 46 |
(.. "\\u{" (if (< (math.random) 0.9) |
| 47 |
;; valid |
| 48 |
(string.format "%x" |
| 49 |
;; weighted toward 0 |
| 50 |
;; you have to do this with floats because lua5.1 can't handle math.random calls with 0xFFFFFFFF |
| 51 |
(math.floor (* (math.random) (math.random) 0xFFFFFFFE))) |
| 52 |
;; invalid |
| 53 |
(table.concat |
| 54 |
(fcollect [_ 0 (math.random 10)] |
| 55 |
(string.char (math.random 0 255))))) |
| 56 |
"}") |
| 57 |
(< (math.random) 0.5) |
| 58 |
(.. "\\" (math.random 300)) |
| 59 |
(< (math.random) 0.5) |
| 60 |
" " |
| 61 |
"\\"))))) |
| 62 |
|
| 63 |
(local remove-size [8 2 1]) |
| 64 |
(fn minimize [str still-has-property?] |
| 65 |
"repeatedly find smaller and smaller `str` where (still-has-property? str)" |
| 66 |
(case (faccumulate [reduced nil |
| 67 |
i 2 (- (length str) 1) |
| 68 |
&until reduced] |
| 69 |
(accumulate [reduced nil |
| 70 |
_ num-to-remove (ipairs remove-size) |
| 71 |
&until reduced] |
| 72 |
(let [reduced (into-single-line-string |
| 73 |
(.. (str:sub 2 (- i 1)) |
| 74 |
(str:sub (+ i num-to-remove) -2)))] |
| 75 |
(if (still-has-property? reduced) reduced)))) |
| 76 |
better (minimize better still-has-property?) |
| 77 |
_ str)) |
| 78 |
|
| 79 |
(fn get-string-parse-error [string-to-parse] |
| 80 |
(let [(old-success? old-out-str) (pcall lua-string-compiler string-to-parse) |
| 81 |
(new-success? new-out-str) (pcall fennel-parser string-to-parse)] |
| 82 |
(when (or (not= old-success? new-success?) ;; one accepts string, other rejects |
| 83 |
(and old-success? (not= old-out-str new-out-str))) ;; they both accept, but with different answers |
| 84 |
(.. "discrepancy parsing string: " string-to-parse "\n" |
| 85 |
"LUA: print(fennel.view(" string-to-parse ")) -- " |
| 86 |
(if old-success? (fennel.view old-out-str) |
| 87 |
old-out-str) "\n" |
| 88 |
"FENNEL: (print (fennel.view " string-to-parse ")) ;; " |
| 89 |
(if new-success? (fennel.view new-out-str) |
| 90 |
new-out-str) "\n")))) |
| 91 |
|
| 92 |
|
| 93 |
(fn test-fuzz-string-1 [] |
| 94 |
;; Comparing Fennel's parser to Lua's. |
| 95 |
;; We want the same string features as Lua 5.3+/LuaJIT |
| 96 |
;; Lua 5.2 and 5.1 don't support all the string escape codes, |
| 97 |
;; so they're not useful as a baseline for this fuzz test. |
| 98 |
(if (or (and (= _VERSION "Lua 5.1") (not (pcall require :jit))) |
| 99 |
(= _VERSION "Lua 5.2")) |
| 100 |
(faith.skip) |
| 101 |
(let [verbose? (os.getenv "VERBOSE") |
| 102 |
seed (os.time)] |
| 103 |
(math.randomseed seed) |
| 104 |
(for [_ 1 (tonumber (or (os.getenv "FUZZ_COUNT") 256))] |
| 105 |
(let [s (generate-random-string)] |
| 106 |
(when verbose? (print s)) |
| 107 |
(when (get-string-parse-error s) |
| 108 |
(local minimized (minimize s get-string-parse-error)) |
| 109 |
(error (get-string-parse-error minimized)))))))) |
| 110 |
|
| 111 |
(fn get-string-compile-error [string-to-parse] |
| 112 |
(let [(old-success? old-out-str) (pcall fennel-string-compiler string-to-parse) |
| 113 |
(new-success? new-out-str) (pcall fennel-parser string-to-parse)] |
| 114 |
(when (or (not= old-success? new-success?) ;; one accepts string, other rejects |
| 115 |
(and old-success? (not= old-out-str new-out-str))) ;; they both accept, but with different answers |
| 116 |
(.. "discrepancy parsing string: " string-to-parse "\n" |
| 117 |
"LUA: print(fennel.view(" string-to-parse ")) -- " |
| 118 |
(if old-success? (fennel.view old-out-str) |
| 119 |
old-out-str) "\n" |
| 120 |
"FENNEL: (print (fennel.view " string-to-parse ")) ;; " |
| 121 |
(if new-success? (fennel.view new-out-str) |
| 122 |
new-out-str) "\n")))) |
| 123 |
|
| 124 |
(fn test-fuzz-string-2 [] |
| 125 |
;; Comparing Fennel's parser to Fennel. |
| 126 |
;; In Fennel, a string is supposed to evaluate to itself. |
| 127 |
;; This should work, regardless of Lua version |
| 128 |
(let [verbose? (os.getenv "VERBOSE") |
| 129 |
seed (os.time)] |
| 130 |
(math.randomseed seed) |
| 131 |
(for [_ 1 (tonumber (or (os.getenv "FUZZ_COUNT") 256))] |
| 132 |
(let [s (generate-random-string)] |
| 133 |
(when verbose? (print s)) |
| 134 |
(when (get-string-compile-error s) |
| 135 |
(local minimized (minimize s get-string-compile-error)) |
| 136 |
(error (get-string-compile-error minimized))))))) |
| 137 |
|
| 138 |
|
| 139 |
{: test-fuzz-string-1 |
| 140 |
: test-fuzz-string-2} |