-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_native.lspy
87 lines (79 loc) · 3.33 KB
/
test_native.lspy
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
; vim: ft=lisp
;;;
;;; Test cases for native functions
;;;
; Functions that are not 'properly' tested but should be fine :
; - load
; - print
; - show (although running the tests should show the results)
; - error
; - fun (although being able to run the tests show it somewhat works)
; - def (although fun is actually syntactic sugar over def)
; - put (it will be tested in the std lib, since it's used for closures)
; - lambda (although fun is actually syntactic sugar over lambda)
; Helper functions
(fun {unpack func l} {
eval (join (list func) l)
})
(fun {test name func expected & args}
{cond (== (unpack func args) expected)
{show name " : Ok"}
{show name " : Not Ok -> Got " (unpack func args) ", Expected " expected}})
;; Arithmetic tests
(show "Arithmetic tests\n============================\n")
(test "Addition of 2 numbers " + 2 1 1)
(test "Addition of 2 decimal numbers " + -0.125 -1.125 1)
(test "Addition of 3 numbers " + 15 10 2 3)
(test "Substraction of 2 numbers " - 0 1 1)
(test "Substraction of 2 decimal numbers " - -2.125 -1.125 1)
(test "Substraction of 3 numbers " - 5 10 2 3)
(test "Division " / 2.5 5 2)
(test "Multiplication " * 22 11 2)
(test "Multiplication of negative numbers" * -12 4 -3)
(test "Modulo " % 2 8 3)
(test "Modulo with 3 numbers " % 1 10 7 2)
(test "Floor " floor 10 10.2)
(test "Floor with negative " floor -11 -10.2)
(show "\n")
;; List tests
(show "List tests\n============================\n")
(test "Head with arguments " head {2} {2 4 1 6})
(test "Head on nil " head nil nil)
(test "Tail with arguments " tail {4 1 6} {2 4 1 6})
(test "Tail on nil " tail nil nil)
(test "Join 2 arguments " join {2 3} {2} {3})
(test "Join 2 lists " join {1 4 2 6} {1 4} {2 6})
(test "Join multiple lists " join {1 4 23 5 6 89 7} {1} {4 23} {5} {6 89 7})
(test "Eval " eval 4 {4})
(test "List " list {1 3 4} 1 3 4)
(test "Length of empty list" len 0 nil)
(test "Length of real list " len 3 { 2 3 5 })
(test "Length of singleton " len 1 {"hello"})
(test "Cons on nil " cons {"test"} "test" nil)
(test "Cons on real list " cons {"alpha" 2 "gamma"} "alpha" {2 "gamma"})
(test "Init on real list " init {"alpha" 2} {"alpha" 2 "gamma"})
(show "\n")
;; Logical tests
(show "Logical tests\n============================\n")
(test "Equality true " == t 1 1)
(test "Equality false " == f 1 0)
(test "not equal true " != f "test" "test")
(test "not equal false " != t "1" 1)
(test "geq true " >= t 1 1)
(test "geq true " >= t 2 1)
(test "geq false " >= f -1 1)
(test "ge true " > t 2 1)
(test "ge false " > f 2 2)
(test "ge false " > f -1 1)
(test "leq true " <= t 1 1)
(test "leq true " <= t -1 1)
(test "leq false " <= f 1 0)
(test "le true " < t -1 1)
(test "le false " < f 1 1)
(test "le false " < f 1 0)
(test "Or true " || t f f t f f)
(test "Or false " || f f f f f)
(test "And true " && t t t t)
(test "And false " && f t t f t)
(test "Cond first branch " cond 0 (t) {0} {"second"})
(test "Cond second branch" cond "second" (f) {0} {"second"})