-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-utils.rkt
39 lines (33 loc) · 1.2 KB
/
test-utils.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
#lang racket
(require rackunit)
(provide (all-defined-out))
(define-syntax check-values-equal?
(syntax-rules ()
[(_ a b) (check-equal? (call-with-values (thunk a) list)
b)]))
(define (lists-approximately-equal? l1 l2 epsilon)
(define (approximately-equal? value1 value2 epsilon)
(<= (abs (- (abs value1)
(abs value2)))
epsilon))
(define (check-elements l1 l2 epsilon)
(cond [(and (empty? l1) (empty? l2)) true]
[(approximately-equal? (vector-ref (first l1) 0)
(vector-ref (first l2) 0)
epsilon)
(check-elements (rest l1)
(rest l2)
epsilon)]
[else false]))
(if (= (length l1) (length l2))
(check-elements l1 l2 epsilon)
false))
;; updates a vector in a functional way,
;; returning a new vector which is the same as the given vector,
;; but has at position pos the value value.
(define (vector-set a-vector pos value)
(vector->immutable-vector
(for/vector ([index (in-range (vector-length a-vector))])
(if (= pos index)
value
(vector-ref a-vector index)))))