/
suite-with-random-tests.lua
183 lines (156 loc) · 5.65 KB
/
suite-with-random-tests.lua
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
-- @module suite_with_random_tests
local suite_with_random_tests = {}
-- Several random tests, with arguments specified both
-- via functions and via consts.
local seed = os.time()
local lunatest = package.loaded.lunatest
lunatest.set_seed(seed)
print("Seed is ", seed)
local random_bool= lunatest.random_bool
local random_int = lunatest.random_int
local random_float = lunatest.random_float
local random_string = lunatest.random_string
local assert_random = lunatest.assert_random
local assert_len = lunatest.assert_len
local assert_match = lunatest.assert_match
local assert_boolean = lunatest.assert_boolean
local assert_lt, assert_lte = lunatest.assert_lt, lunatest.assert_lte
local assert_gt, assert_gte = lunatest.assert_gt, lunatest.assert_gte
local assert_number= lunatest.assert_number
function suite_with_random_tests.test_random_bool()
assert_random("bool is t or f",
function (b)
return b == true or b == false
end, true)
end
function suite_with_random_tests.test_random_bool2()
assert_random("bool is t or f",
function ()
return random_bool()
end, true)
end
function suite_with_random_tests.test_random_int()
assert_random({name="pos int", count=1000 },
function ()
local ri = random_int(1, 10)
return ri >= 1 and ri <= 10
end)
end
function suite_with_random_tests.test_random_int2()
assert_random({ name="pos int", count=1000 },
function (ri)
return ri >= 0 and ri <= 10
end, 10)
end
function suite_with_random_tests.test_neg_int()
assert_random({ name="neg int", count=1000 },
function ()
local ni = random_int(-math.huge, 0)
return ni < 0
end)
end
function suite_with_random_tests.test_neg_or_pos_int()
assert_random({ name="neg int", count=1000 },
function (i)
assert_number(i)
assert_gte(-400, i)
assert_lte(400, i)
end, -400)
end
function suite_with_random_tests.test_random_int_trio()
assert_random({ name="three ints", count=1000 },
function ()
local ri = random_int
local x, y, z = ri(0, 5), ri(10, 50), ri(50, 100)
assert_gt(x, y)
assert_gte(y, z)
assert_lt(z, x)
end)
end
function suite_with_random_tests.test_random_float()
assert_random({ name="float", count=1000 },
function ()
local rf = random_float
assert_gt(21.5, rf(30, 40))
end)
end
function suite_with_random_tests.test_random_float2()
assert_random({ name="float", count=1000 },
function (x)
assert_gt(0, x)
end, 55.5)
end
function suite_with_random_tests.test_random_string()
assert_random("vowels",
function (x)
local len = string.len(x)
assert_lte(10, len)
assert_match("[aeiou]+", x)
end, "10 aeiou")
end
function suite_with_random_tests.test_random_string2()
-- In the first argument, .always={...} is a list of seeds
-- that will be run every trial.
assert_random( { name="alpha",
always={ 6483877599982, 9948212639558 }},
function (x)
local len = string.len(x)
assert_gte(10, len)
assert_lte(30, len)
assert_match(".+", x)
end, "10,30 %a")
end
function suite_with_random_tests.test_coinflip()
local function rbool() return random_bool() end
-- typically, this would go in a metatable...
local coin = { __random = rbool }
assert_random("flip",
function(flip)
assert_boolean(flip)
end, coin)
end
function suite_with_random_tests.test_random_int_bounds()
for run, pair in ipairs{ {1, 2}, {2, 10}, {-1, 1},
{-100, 100}, {-1, 0}, {0, 1} } do
assert_random("int_bounds",
function()
local ri = random_int(pair[1], pair[2])
assert_gte(pair[1], ri)
assert_lte(pair[2], ri)
end)
end
end
function suite_with_random_tests.test_random_int_bounds()
for run, pair in ipairs{ {1, 2}, {2, 10}, {-1, 1},
{-100, 100}, {-1, 0}, {0, 1} } do
assert_random("float_bounds",
function()
local ri = random_float(pair[1], pair[2])
assert_gte(pair[1], ri)
assert_lt(pair[2], ri)
end)
end
end
function suite_with_random_tests.test_random_str_len()
local fmt = string.format
for _,l in ipairs{ 1, 5, 10, 15, 30 } do
assert_random("strlen",
function()
local pat = fmt("%d %%l", l)
local rs = random_string(pat)
assert_len(l, rs)
assert_match("^%l+$", rs)
end)
end
end
function suite_with_random_tests.test_random_str_range()
local fmt = string.format
assert_random("strlen2",
function()
local pat = fmt("%d a-z", 15)
local rs = random_string(pat)
assert_len(15, rs)
assert_match("^%l+$", rs)
end)
end
return suite_with_random_tests