-
Notifications
You must be signed in to change notification settings - Fork 2
/
rhea-tests.rkt
174 lines (140 loc) · 4.51 KB
/
rhea-tests.rkt
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
#lang racket/base
(require rackunit rackunit/text-ui
"rhea.rkt")
(provide rhea-tests)
(define cassowary-epsilon 1e-5)
(define (approx? a b)
(and (>= a (- b cassowary-epsilon))
(<= a (+ b cassowary-epsilon))))
(define (sat? solver) (check-equal? #t (send solver solve)))
(define (add-delete1-test)
(test-case
"add-delete1-test"
(define solver (new cassowary%))
(define x (new cassowary-variable% [initial-value 100.0]))
(send solver add-constraint (send x = 100.0) (send solver weak-strength))
(define c10 (send x <= 10))
(define c20 (send x <= 20))
(send solver add-constraint c10)
(send solver add-constraint c20)
(sat? solver)
(check approx? (send x value) 10)
(send solver remove-constraint c10)
(sat? solver)
(check approx? (send x value) 20)
(send solver remove-constraint c20)
(sat? solver)
(check approx? (send x value) 100)
(define c10again (send x <= 10))
(send solver add-constraint c10)
(send solver add-constraint c10again)
(sat? solver)
(check approx? (send x value) 10)
(send solver remove-constraint c10)
(sat? solver)
(check approx? (send x value) 10)
(send solver remove-constraint c10again)
(sat? solver)
(check approx? (send x value) 100)))
(define (add-delete2-test)
(test-case
"add-delete2-test"
(define solver (new cassowary%))
(define x (new cassowary-variable% [initial-value 100.0]))
(define y (new cassowary-variable% [initial-value 120.0]))
(send solver add-constraint (send x = 100.0) (send solver weak-strength))
(send solver add-constraint (send y = 120.0) (send solver strong-strength))
(define c10 (send x <= 10))
(define c20 (send x <= 20))
(send solver add-constraint c10)
(send solver add-constraint c20)
(sat? solver)
(check approx? (send x value) 10)
(check approx? (send y value) 120)
(send solver remove-constraint c10)
(sat? solver)
(check approx? (send x value) 20)
(check approx? (send y value) 120)
(define cxy (send (send x * 2) = y))
(send solver add-constraint cxy)
(sat? solver)
(check approx? (send x value) 20)
(check approx? (send y value) 40)
(send solver remove-constraint c20)
(sat? solver)
(check approx? (send x value) 60)
(check approx? (send y value) 120)
(send solver remove-constraint cxy)
(sat? solver)
(check approx? (send x value) 100)
(check approx? (send y value) 120)
(define cxy2 (send (send x * 1) = 1000))
(send solver add-constraint cxy2)
(sat? solver)
(check approx? (send x value) 1000)
(check approx? (send y value) 120)
))
(define (test-edit2vars)
(test-case
"test-edit2vars"
(define x (make-object cassowary-variable% 20))
(define y (make-object cassowary-variable% 30))
(define z (make-object cassowary-variable% 120))
(define solver (new cassowary%))
(send solver add-stay x)
(send solver add-stay z)
(send solver add-constraint (send z = (send (send x * 2) + y)))
(sat? solver)
(check approx? (send x value) 20)
(check approx? (send y value) 80)
(check approx? (send z value) 120)
(send solver add-edit-var x)
(send solver add-edit-var y)
(send solver begin-edit)
(send solver edit-value x 10)
(send solver edit-value y 5)
(send solver resolve)
(check approx? (send x value) 10)
(check approx? (send y value) 5)
(check approx? (send z value) 25)
(send solver edit-value x -10)
(send solver edit-value y 15)
(send solver resolve)
(check approx? (send x value) -10)
(check approx? (send y value) 15)
(check approx? (send z value) -5)
(send solver end-edit)
(check approx? (send x value) -10)
(check approx? (send y value) 15)
(check approx? (send z value) -5)
))
(define (test-inconsistent1)
(test-case
"test-inconsistent1"
(define x (new cassowary-variable%))
(define solver (new cassowary%))
(send solver add-constraint (send x = 10))
(sat? solver)
(check-equal? #f (send solver add-constraint (send x = 5)))
))
(define (test-suggest)
(test-case
"test-suggest"
(define x (new cassowary-variable%))
(define solver (new cassowary%))
(send solver add-constraint (send x <= 10))
(sat? solver)
(check approx? (send x value) 10)
(check-equal? (send solver suggest-value x 4) #t)
(check approx? (send x value) 4)
))
(define rhea-tests
(test-suite
"run all rhea tests"
(add-delete1-test)
(add-delete2-test)
(test-edit2vars)
(test-inconsistent1)
(test-suggest)
))
(time (run-tests rhea-tests))