-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisp-tests.lisp
531 lines (433 loc) · 17.7 KB
/
lisp-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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
;;; lisp-tests.lisp - A simple testing framework for our primitives.
;;; About
;;
;; This file contains a bunch of simple test-cases which demonstrate
;; that our lisp-implemented functions work as expected.
;;
;; The file will attempt to output the results in a TAP format, such
;; that it can be processed via automated tools.
;;
;; For example the "tapview" shell-script can consume our output and
;; will present something like this;
;;
;; $ yal tests.lisp | tapview
;; ....
;; 4 tests, 0 failures.
;;
;; When a test fails it will be shown:
;;
;; $ yal tests.lisp | tapview
;; not ok add:mult failed %!s(int=40) != %!s(int=10)
;; 4 tests, 1 failures.
;;
;; tapview can be found here:
;;
;; https://gitlab.com/esr/tapview
;;
;;
;;; Note
;;
;; Of course the results can also be expected manually, the tapview is
;; just one of the many available TAP-protocol helpers.
;;
;; $ yal tests.lisp | grep "not ok"
;;
;;
;;
;;; Details
;;
;; In terms of our implementation we use a macro to register
;; test functions. Test functions are expected to return a list
;; of two elements - a test passes if those elements are identical,
;; and fails otherwise.
;;
;; The macro which defines a test-case will store the details in the
;; global *tests* hash:
;;
;; key -> name of the test
;; val -> The lambda body
;;
;; When we come to execute the tests we'll just iterate over the key/val
;; pairs appropriately.
;;
;;
;; A hash of all known test-cases.
;;
;; This is updated via the `deftest` macro, and iterated over by the
;; `run-tests` function.
;;
(set! *tests* {} )
;;
;; Define a new test.
;;
(defmacro! deftest (fn* (name body)
"Create a new test, storing details in the global *tests* hash.
If the name of the test is not unique then that will cause an error to be printed."
`(if (get *tests* `~name)
(print "not ok - name is not unique %s" `~name)
(set *tests* `~name (lambda () (do ~body))))
))
;;
;; Some data for testing-purposes
;;
;; Define a "person" object
(struct person forename surname)
;; Create some people
(set! people (list (person "Ralph" "Wiggum")
(person "Lisa" "Simpson")
(person "Apu" "Nahasapeemapetilon")
(person "Marge" "Bouvier")
(person "Artie" "Ziff")
(person "Edna", "Krabappel")
(person "Homer" "Simpson")))
;;
;; Test cases now follow, defined with the macro above.
;;
;;
;; Each test-case should return a list of two values:
;;
;; 1. If the two values are equal we have a pass.
;; 2. If the two values are not equal the test fails.
;;
;; If the test case returns anything other than a two-element
;; list it is also a failure, as is a non-unique test-name.
;;
;; +
(deftest add:simple (list (+ 3 4) 7))
(deftest add:mult (list (+ 1 2 3 4) 10))
;; /
(deftest div:1 (list (/ 2 ) 0.5)) ; "/ x" == "1/x"
(deftest div:2 (list (/ 9 3) 3))
(deftest div:3 (list (/ 8 2) 4))
;; *
(deftest mul:1 (list (* 2 ) 2)) ; "* x" == "1 * x"
(deftest mul:2 (list (* 2 2 ) 4))
(deftest mul:3 (list (* 2 2 2 ) 8))
(deftest mul:4 (list (* 2 2 2 3) 24))
;; -
(deftest minus:1 (list (- 1 2 ) -1))
(deftest minus:2 (list (- 10 2 ) 8))
(deftest minus:3 (list (- 10 2 3) 5))
;; sqrt
(deftest sqrt:1 (list (sqrt 100) 10))
(deftest sqrt:2 (list (sqrt 9) 3))
;; power
(deftest pow:1 (list (# 10 2) 100))
(deftest pow:2 (list (# 2 3) 8))
;; neg
(deftest neg:1 (list (neg 100) -100))
(deftest neg:2 (list (neg -33) 33))
;; abs
(deftest abs:1 (list (abs 100) 100))
(deftest abs:2 (list (abs -33) 33))
(deftest abs:3 (list (abs 0) 0))
;; sign
(deftest sign:1 (list (sign 100) 1))
(deftest sign:2 (list (sign -33) -1))
(deftest sign:3 (list (sign 0) 1))
;; neg?
(deftest neg?:1 (list (neg? 100) false))
(deftest neg?:2 (list (neg? -33) true))
(deftest neg:3 (list (neg? 0.1) false))
(deftest neg:4 (list (neg? -0.1) true))
;; pos?
(deftest pos:1 (list (pos? 100) true))
(deftest pos:2 (list (pos? -33) false))
(deftest pos:3 (list (pos? 0.1) true))
(deftest pos:4 (list (pos? -0.1) false))
;; inc
(deftest inc:1 (list (inc 1) 2))
(deftest inc:2 (list (inc -1) 0))
(deftest inc:3 (list (inc 1.3) 2.3))
;; dec
(deftest dec:1 (list (dec 1) 0))
(deftest dec:2 (list (dec -1) -2))
(deftest dec:3 (list (dec 1.5) 0.5))
;; and
(deftest and:1 (list (and (list false)) false))
(deftest and:2 (list (and (list true)) true))
(deftest and:3 (list (and (list true true)) true))
(deftest and:4 (list (and (list true false)) false))
;; not
(deftest not:1 (list (not true) false))
(deftest not:2 (list (not false) true))
(deftest not:3 (list (not "steve") false))
(deftest not:4 (list (not 3) false))
(deftest not:5 (list (not ()) false))
(deftest not:6 (list (not nil) true)) ; not nil -> true is expected
;; or
(deftest or:1 (list (or (list false)) false))
(deftest or:2 (list (or (list true)) true))
(deftest or:3 (list (or (list true true)) true))
(deftest or:4 (list (or (list true false)) true))
(deftest or:5 (list (or (list false false)) false))
;; numeric parsing
(deftest parse:int:1 (list 0b1111 15))
(deftest parse:int:2 (list 0xff 255))
(deftest parse:int:3 (list 332.2 332.2))
;; Upper-case a string
(deftest string:upper:ascii (list (upper "steve") "STEVE"))
(deftest string:upper:utf (list (upper "π!狐犬") "π!狐犬"))
(deftest string:upper:mixed (list (upper "π-steve") "π-STEVE"))
;; Lower-case a string
(deftest string:lower:ascii (list (lower "STEVE") "steve"))
(deftest string:lower:utf (list (lower "π!狐犬") "π!狐犬"))
(deftest string:lower:mixed (list (lower "π-STEVE") "π-steve"))
;; Left-pad
(deftest string:pad:left:ascii (list (pad:left "me" "x" 4) "xxme"))
(deftest string:pad:left:utf (list (pad:left "狐犬π" "x" 4) "x狐犬π"))
(deftest string:pad:left:mixed (list (pad:left "fπ" "x" 4) "xxfπ"))
;; Right-pad
(deftest string:pad:right:ascii (list (pad:right "me" "x" 8) "mexxxxxx"))
(deftest string:pad:right:utf (list (pad:right "狐犬π" "x" 8) "狐犬πxxxxx"))
(deftest string:pad:right:mixed (list (pad:right "fπ" "x" 8) "fπxxxxxx"))
;; Time should have two-digit length HH, MM, SS fields.
(deftest time:hms:len (list (strlen (hms)) 8))
;; Year should be four digits, always.
(deftest year:len (list (strlen (str (date:year))) 4))
;; < test
(deftest cmp:lt:1 (list (< 1 10) true))
(deftest cmp:lt:2 (list (< -1 0) true))
(deftest cmp:lt:3 (list (< 10 0) false))
;; > test
(deftest cmp:gt:1 (list (> 1 10) false))
(deftest cmp:gt:2 (list (> 1 0) true))
(deftest cmp:gt:3 (list (> 10 -10) true))
;; <= test
(deftest cmp:lte:1 (list (<= 1 10) true))
(deftest cmp:lte:2 (list (<= -1 0) true))
(deftest cmp:lte:3 (list (<= 10 0) false))
(deftest cmp:lte:4 (list (<= 10 10) true))
;; >= test
(deftest cmp:gte:1 (list (>= 1 10) false))
(deftest cmp:gte:2 (list (>= 1 0) true))
(deftest cmp:gte:3 (list (>= 10 -10) true))
(deftest cmp:gte:4 (list (>= 10 10) true))
;; eq test
(deftest cmp:eq:1 (list (eq 1 10) false))
(deftest cmp:eq:2 (list (eq 1 1) true))
(deftest cmp:eq:3 (list (eq 10 -10) false))
(deftest cmp:eq:4 (list (eq "steve" "steve") true))
(deftest cmp:eq:5 (list (eq "steve" "kemp") false))
(deftest cmp:eq:6 (list (eq 32 "steve") false))
(deftest cmp:eq:7 (list (eq () nil ) false))
(deftest cmp:eq:8 (list (eq () () ) true))
(deftest cmp:eq:9 (list (eq nil nil ) true))
;; = test
(deftest cmp:=:1 (list (eq 1 1) true))
(deftest cmp:=:2 (list (eq 1 (- 3 2)) true))
(deftest cmp:=:3 (list (eq 1 -1) false))
(deftest cmp:=:4 (list (eq .5 (/ 1 2)) true))
;; char<
(deftest char<:1 (list (char< #\a #\b ) true))
(deftest char<:2 (list (char< #\b #\a ) false))
;; char<=
(deftest char<=:1 (list (char<= #\a #\b ) true))
(deftest char<=:2 (list (char<= #\b #\a ) false))
(deftest char<=:3 (list (char<= #\b #\b ) true))
;; char>
(deftest char>:1 (list (char> #\a #\b ) false))
(deftest char>:2 (list (char> #\b #\a ) true))
;; char>=
(deftest char>=:1 (list (char>= #\a #\b ) false))
(deftest char>=:2 (list (char>= #\b #\a ) true))
(deftest char>=:3 (list (char>= #\b #\b ) true))
;; zero? test
(deftest tst:zero:1 (list (zero? 0) true))
(deftest tst:zero:2 (list (zero? 10) false))
;; one? test
(deftest tst:one:1 (list (one? 1) true))
(deftest tst:one:2 (list (one? 10) false))
;; even? test
(deftest tst:even:1 (list (even? 1) false))
(deftest tst:even:2 (list (even? 2) true))
(deftest tst:even:3 (list (even? 3) false))
;; odd? test
(deftest tst:odd:1 (list (odd? 1) true))
(deftest tst:odd:2 (list (odd? 2) false))
(deftest tst:odd:3 (list (odd? 3) true))
;; true? test
(deftest tst:true:1 (list (true? true) true))
(deftest tst:true:2 (list (true? nil) false))
(deftest tst:true:3 (list (true? false) false))
(deftest tst:true:4 (list (true? 32111) false))
(deftest tst:true:5 (list (true? ()) false))
;; false? test
(deftest tst:false:1 (list (false? false) true))
(deftest tst:false:2 (list (false? nil) false))
(deftest tst:false:3 (list (false? true) false))
(deftest tst:false:4 (list (false? 32111) false))
(deftest tst:false:5 (list (false? ()) false))
;; nil? test
(deftest tst:nil:1 (list (nil? false) false))
(deftest tst:nil:2 (list (nil? ()) true))
(deftest tst:nil:3 (list (nil? nil) true))
(deftest tst:nil:4 (list (nil? "steve") false))
(deftest tst:nil:5 (list (nil? 3223232) false))
;; member test
(deftest member:1 (list (member "foo" (list "foo" "bar" "baz")) true))
(deftest member:2 (list (member "luv" (list "foo" "bar" "baz")) false))
;; union test
(deftest union:1 (list (union (list "foo") (list "foo" "bar" "baz")) (list "foo" "bar" "baz")))
(deftest union:2 (list (union (list "foo") (list "bar" "baz")) (list "foo" "bar" "baz")))
;; intersection
(deftest intersection:1 (list (intersection (list "foo") (list "foo" "bar" "baz")) (list "foo")))
(deftest intersection:2 (list (intersection (list 1 2 3) (list 2 3 4 )) (list 2 3)))
;; TODO / FIXME / BUG - should intersection return nil if there are no common elements?
(deftest intersection:3 (list (intersection (list 1) (list 2 3 4 )) nil))
;; reverse
(deftest reverse:1 (list (reverse (list "m" "e")) (list "e" "m")))
(deftest reverse:2 (list (reverse (list "狐" "犬" "π")) (list "π" "犬" "狐")))
;; seq
(deftest seq:0 (list (seq 0) (list 0)))
(deftest seq:1 (list (seq 1) (list 0 1)))
(deftest seq:2 (list (seq 2) (list 0 1 2)))
;; nat
(deftest nat:0 (list (nat 0) (list )))
(deftest nat:1 (list (nat 1) (list 1)))
(deftest nat:2 (list (nat 2) (list 1 2)))
;; take
(deftest take:1 (list (take 0 (list 0 1 2 3)) nil))
(deftest take:2 (list (take 1 (list 0 1 2 3)) (list 0)))
(deftest take:3 (list (take 2 (list 0 1 2 3)) (list 0 1)))
;; drop
(deftest drop:1 (list (drop 0 (list 0 1 2 3)) (list 0 1 2 3)))
(deftest drop:2 (list (drop 1 (list 0 1 2 3)) (list 1 2 3)))
(deftest drop:3 (list (drop 2 (list 0 1 2 3)) (list 2 3)))
;; butlast
(deftest butlast:1 (list (butlast (list 0 1 2 3)) (list 0 1 2)))
(deftest butlast:2 (list (butlast (list 0)) nil))
(deftest butlast:3 (list (butlast nil) nil))
;; append
(deftest append:1 (list (append () "2") "2"))
(deftest append:2 (list (append (list 2) "2") (list 2 "2")))
(deftest append:3 (list (append (list 2 3) 5) (list 2 3 5)))
;; string<
(deftest string<:1 (list (string< "a" "b") true))
(deftest string<:2 (list (string< "b" "a") false))
;; string<=
(deftest string<=:1 (list (string<= "a" "b") true))
(deftest string<=:2 (list (string<= "b" "a") false))
(deftest string<=:3 (list (string<= "b" "b") true))
;; string>
(deftest string>:1 (list (string> "a" "b") false))
(deftest string>:2 (list (string> "B" "A") true))
;; string>=
(deftest string>=:1 (list (string>= "a" "b") false))
(deftest string>=:2 (list (string>= "b" "a") true))
(deftest string>=:3 (list (string>= "b" "b") true))
;; strlen
(deftest strlen:1 (list (strlen "") 0))
(deftest strlen:2 (list (strlen "steve") 5))
(deftest strlen:3 (list (strlen "狐犬π") 3))
;; repeated
(deftest repeated:0 (list (repeated 0 "x") nil))
(deftest repeated:1 (list (repeated 1 "x") (list "x")))
(deftest repeated:2 (list (repeated 2 "x") (list "x" "x")))
(deftest repeated:3 (list (repeated 3 "x") (list "x" "x" "x")))
;; hex
(deftest hex:1 (list (dec2hex 255) "ff"))
(deftest hex:2 (list (dec2hex 10) "a"))
;; binary - note that the shortest form will be returned
(deftest binary:1 (list (dec2bin 3) "11"))
(deftest binary:2 (list (dec2bin 4) "100"))
;; stacks
;;
;; uses "foof" as a variable/stack but that doesn't persist between runs.
;;
(deftest stack:1 (list (do (stack:push 3 foof) (stack:push 4 foof) foof) (list 4 3)))
(deftest stack:2 (list (stack:empty? foof) true))
(deftest stack:3 (list (stack:size foof) 0))
(deftest stack:4 (list (do (stack:push 3 foof) (stack:push 4 foof) (stack:empty? foof)) false))
(deftest stack:5 (list (do (stack:push 3 foof) (stack:push 4 foof) (stack:size foof)) 2))
;; popping from an empty stack returns nil
(deftest stack:6 (list (do (stack:pop foof) (stack:pop foof)) nil))
(deftest stack:7 (list (do (stack:pop "") (stack:pop "")) nil))
;; structures
(deftest struct:1 (list (do (struct person name) (type (person "me")))
"person"))
(deftest struct:2 (list (do (struct person name) (person? (person "me")))
true))
(deftest struct:3 (list (do (struct person name) (person.name (person "me")))
"me"))
;; sum and mean
(deftest sum:1 (list (sum (list 1)) 1))
(deftest sum:2 (list (sum (list 1 2 3)) 6))
(deftest sum:3 (list (sum (list 100 21 32)) 153))
(deftest mean:1 (list (mean (list 3 3 3)) 3))
(deftest mean:2 (list (mean (list 10 6)) 8))
;; counting matches in a list
(deftest occur:1 (list (occurrences 3 '(1 2 3)) 1))
(deftest occur:2 (list (occurrences 3 '(3 3 3)) 3))
(deftest occur:3 (list (occurrences 3 '()) 0))
(deftest occur:4 (list (occurrences "me" '("me" "you" 3 "me")) 2))
;; finding values in a list
(deftest find:1 (list (find 3 '(1 2)) nil))
(deftest find:2 (list (find 3 '(1 2 3)) '(2)))
(deftest find:3 (list (find 3 '(1 2 3 3 2 1 )) '(2 3 )))
(deftest find:4 (list (find 3 '()) nil))
;; defaults
(set! addy (fn* ( (a 10) (b 11) ) (+ a b) ))
(deftest defaults:1 (list (addy) 21))
(deftest defaults:2 (list (addy 1) 12))
(deftest defaults:3 (list (addy 1 2) 3))
(deftest defaults:4 (list (addy 1 2 3) 3))
;; Define two helpers for sorting, by one/other field.
(set! people-surname-sort (fn* (a b) (string< (person.surname a) (person.surname b))))
(set! people-forename-sort (fn* (a b) (string< (person.forename a) (person.forename b))))
;; sort-by
(deftest sort-by:1 (list (type (person "foo" "bar")) "person"))
(deftest sort-by:2 (list (type (car people)) "person"))
(deftest sort-by:3 (list (type people-surname-sort) "procedure(lisp)"))
(deftest sort-by:4 (list (type people-forename-sort) "procedure(lisp)"))
;; forename-sort, first and last
(deftest sort-by:5 (list (let* (sorted (sort-by people-forename-sort people))
(sprintf "%s %s"
(person.forename (car sorted))
(person.surname (car sorted))))
"Apu Nahasapeemapetilon"))
(deftest sort-by:6 (list (let* (sorted (sort-by people-forename-sort people))
(sprintf "%s %s"
(person.forename (last sorted))
(person.surname (last sorted))))
"Ralph Wiggum"))
;; surname-sort, first and last
(deftest sort-by:7 (list (let* (sorted (sort-by people-surname-sort people))
(sprintf "%s %s"
(person.forename (car sorted))
(person.surname (car sorted))))
"Marge Bouvier"))
(deftest sort-by:8 (list (let* (sorted (sort-by people-surname-sort people))
(sprintf "%s %s"
(person.forename (last sorted))
(person.surname (last sorted))))
"Artie Ziff"))
;;
;; Define a function to run all the tests, by iterating over the
;; contents of the global hash, to which the tests were stored via
;; the deftest macro.
;;
(set! run-tests (fn* (hsh)
"Run all the tests, by iterating over the supplied hash:
- The hash will contain a key naming the test.
- The value of the hash will be a function to invoke to run the test.
If the result is not a list containing two entries it is an immediate failure.
If the two values don't match then we fail, otherwise it is a pass."
(do
(print "TAP version 14")
(apply-hash hsh (lambda (test fun)
(let* (out (fun))
(if (! (list? out))
(print "not ok %s should have returned a list, instead got %v" test out)
(if (! (= (count out) 2 ))
(print "not ok %s should have been a list of 2 elements, instead got %s" test out)
(let* (a (car out)
b (car (cdr out)))
(if (! (eq a b))
(print "not ok %s failed %s != %s" test a b)
(print "ok %s" test))))))))
(print "1..%d" (count (keys hsh))))))
;;
;; Now run the tests.
;;
(run-tests *tests*)