size: 1 KiB

1package.path = "./?.lua;" .. package.path
2-- Full coverage test
3--
4-- This test is similar to fuzz.lua, but, rather than generating
5-- random strings, we exhaustively enumerate short strings.
6
7local djot = require("djot")
8local to_html = function(s)
9 local doc = djot.parse(s)
10 return djot.render_html(doc)
11end
12
13local function combinations(alpha, n)
14 if n == 0 then
15 return {""}
16 end
17 if alpha == "" then
18 return {}
19 end
20 local res = {}
21 local first = alpha:sub(1, 1)
22 local rest = alpha:sub(2)
23 for _, s in ipairs(combinations(rest, n)) do
24 res[#res + 1] = s
25 end
26 for _, s in ipairs(combinations(rest, n - 1)) do
27 res[#res + 1] = first .. s
28 end
29 return res
30end
31
32
33local n = 4 -- We select n interesting characters
34local m = 6 -- and generate every string of length m.
35local swarm = combinations(" -*|[]{}()_`:ai\n", n)
36
37local iter = 0
38for _, alphabet in ipairs(swarm) do
39 iter = iter + 1
40 if iter % 10 == 0 then
41 print(iter, "of", #swarm)
42 end
43
44 -- Tricky bit: we essentially want to write m nested
45 -- 'for i=1,m' loops. We can't do that, so instead we
46 -- track `m` loop variables in `ii` manually.
47 --
48 -- That is, `ii` is "vector of `i`s".
49 local ii = {}
50 for i=1,m do
51 ii[i] = 1
52 end
53
54 local done = false
55 while not done do
56 local s = ""
57 for i=1,m do
58 s = s .. alphabet:sub(ii[i], ii[i])
59 end
60
61 to_html(s)
62
63 -- Increment the innermost index, reset others to 1.
64 done = true
65 for i=m,1,-1 do
66 if ii[i] ~= #alphabet then
67 ii[i] = ii[i] + 1
68 for j=i+1,m do
69 ii[j] = 1
70 end
71 done = false
72 break
73 end
74 end
75
76 end
77end