-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path09-6-testing.rkt
executable file
·158 lines (109 loc) · 3.26 KB
/
09-6-testing.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
#lang racket
(require "extras.rkt")
(require rackunit)
;; A StupidRobot represents a robot moving along a one-dimensional line,
;; starting at position 0.
(define StupidRobot<%>
(interface ()
;; a new StupidRobot<%> is required to start at position 0
;; -> StupidRobot<%>
;; RETURNS: a Robot just like this one, except moved one
;; position to the right
move-right
;; -> Integer
;; RETURNS: the current x-position of this robot
get-pos
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Client Code of Interface
;; move-right-by-distance : Robot<%> Nat -> Robot<%>
(define (move-right-by-distance r n)
(cond
[(zero? n) r]
[else (move-right-by-distance
(send r move-right)
(- n 1))]))
;;; tests
(begin-for-test
(local
((define r0 (new-robot))
;; move r0 right twice
(define r1 (send (send r0 move-right) move-right)))
;; get-pos should then return 2
(check-equal?
(send r1 get-pos)
2)))
(begin-for-test
(local
((define r0 (new-robot))
(define r1 (move-right-by-distance r0 3)))
(check-equal?
(send r1 get-pos)
3)))
;; let's create a bunch of robots and see if our tests pass for all of them
(begin-for-test
(let ((the-robots (map (lambda (n) (new-robot)) '(1 2 3 4 5 6 7 8 9 10))))
(map
(lambda (r) (let ((r1 (move-right-by-distance r 13)))
(check-equal?
(send r1 get-pos)
13)))
the-robots)))
(begin-for-test
(let ((r1 (new Robot1%))
(r2 (new Robot1%)))
(check-equal? r1 r2 "This is an expected failure")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Implementations
(define Robot1%
(class* object% (StupidRobot<%>)
(init-field [x 0])
;; interp: the position of the robot, initially 0
(super-new)
(define/public (move-right)
(new Robot1% [x (+ x 1)]))
(define/public (get-pos)
x)
))
(define Robot2%
(class* object% (StupidRobot<%>)
(init-field [blerch 0])
;; interp: the position of the robot.
(super-new)
(define/public (move-right)
(new Robot2% [blerch (+ blerch 1)]))
(define/public (get-pos)
blerch)
))
(define Robot3%
(class* object% (StupidRobot<%>)
(init-field [y 0])
;; interp: the negative of the position of the robot.
(super-new)
(define/public (move-right)
(new Robot3% [y (- y 1)]))
;; RETURNS: the x-position of the robot
(define/public (get-pos)
(- y))
))
(define Robot4%
(class* object% (StupidRobot<%>)
(init-field [x empty])
;; Interp:
;; a list whose length is equal to the position of the robot
(super-new)
(define/public (move-right)
(new Robot4% [x (cons 99 x)]))
;; RETURNS: the x-position of the robot
(define/public (get-pos)
(length x))
))
;; -> StupidRobot<%>
(define (new-robot)
(local
((define i (random 3)))
(cond
[(= i 0) (new Robot1%)]
[(= i 1) (new Robot2%)]
[(= i 2) (new Robot3%)]
[(= i 3) (new Robot4%)])))