-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.scm
129 lines (119 loc) · 3.2 KB
/
tests.scm
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
(import (scheme base)
(scheme write)
(chibi test)
(lexer)
(parser)
(utils))
(define (eval-math parse-fn str)
(eval-ast (parse-fn (tokenize str))))
(define (test-basic fn)
(test-group "basic binary operations"
(test "100"
100 (eval-math fn "100"))
(test "-1"
-1 (eval-math fn "-1"))
(test "+2"
2 (eval-math fn "+2"))
(test "1 + 2"
3 (eval-math fn "1 + 2"))
(test "(1 - 2)"
-1 (eval-math fn "(1 - 2)"))
(test "1 * 2"
2 (eval-math fn "1 * 2"))
(test "1 / 2"
0.5 (eval-math fn "1 / 2"))
(test "(1 % 2)"
1 (eval-math fn "(1 % 2)"))
(test "1 ^ 2"
1 (eval-math fn "1 ^ 2"))
(test "(20)"
20 (eval-math fn "(20)"))
(test "(-2)"
-2 (eval-math fn "(-2)"))
(test "(((192)))"
192 (eval-math fn "(((192)))"))))
(define (test-unary fn)
(test-group "unary operations"
(test "----1"
1 (eval-math fn "----1"))
(test "+++2"
2 (eval-math fn "+++2"))
(test "-+-+-2"
-2 (eval-math fn "-+-+-2"))
(test "+(--2)"
2 (eval-math fn "+(--2)"))
(test "+(-(+(-(+1))))"
1 (eval-math fn "+(-(+(-(+1))))"))
(test "3 + -2"
1 (eval-math fn "3 + -2"))
(test "3 * -2"
-6 (eval-math fn "3 * -2"))
(test "2 ^ -2"
0.25 (eval-math fn "2 ^ -2"))
(test "-3 ^ 2"
9 (eval-math fn "-3 ^ 2"))))
(define (test-associativity fn)
(test-group "associativity of operators"
(test "1 + 2 + 3 + 4"
10 (eval-math fn "1 + 2 + 3 + 4"))
(test "1 - 2 - 3 - 4"
-8 (eval-math fn "1 - 2 - 3 - 4"))
(test "1 * 2 * 3 * 4"
24 (eval-math fn "1 * 2 * 3 * 4"))
(test "4 / 2 / 2"
1 (eval-math fn "4 / 2 / 2"))
(test "4 % 3 % 2"
1 (eval-math fn "4 % 3 % 2"))
(test "2 ^ 3 ^ 2"
512 (eval-math fn "2 ^ 3 ^ 2"))))
(define (test-precedence fn)
(test-group "precedence of operators"
(test "1 + 2 - 3"
0 (eval-math fn "1 + 2 - 3"))
(test "1 + 2 * 3"
7 (eval-math fn "1 + 2 * 3"))
(test "1 / 2 - 3"
-2.5 (eval-math fn "1 / 2 - 3"))
(test "3 * 4 % 5"
2 (eval-math fn "3 * 4 % 5"))
(test "2 * (3 + 4)"
14 (eval-math fn "2 * (3 + 4)"))
(test "3 - (3 + 4)"
-4 (eval-math fn "3 - (3 + 4)"))
(test "(10 - 4) / 2"
3 (eval-math fn "(10 - 4) / 2"))
(test "13 % 2 ^ 4"
13 (eval-math fn "13 % 2 ^ 4"))
(test "2 ^ (2 * 5)"
1024 (eval-math fn "2 ^ (2 * 5)"))
(test "2 ^ 3 * 3 + 1"
25 (eval-math fn "2 ^ 3 * 3 + 1"))))
(define (test-err fn)
(test-group "common errors"
(test-error "1 2"
(eval-math fn "1 2"))
(test-error "1 +"
(eval-math fn "1 +"))
(test-error "2 * %"
(eval-math fn "2 * %"))
(test-error "^"
(eval-math fn "^"))
(test-error "(1 + 2"
(eval-math fn "(1 + 2"))
(test-error ")21"
(eval-math fn ")21"))))
(define (test-all fn)
(test-basic fn)
(test-unary fn)
(test-associativity fn)
(test-precedence fn)
(test-err fn))
(display "testing recursive descent parser\n")
(test-all rec-desc-parse)
(newline)
(display "testing recursive ascent parser\n")
(test-all rec-asc-parse)
(newline)
(display "testing pratt parser\n")
(test-all pratt-parse)
(newline)