-
Notifications
You must be signed in to change notification settings - Fork 4
/
cache-test.ss
102 lines (83 loc) · 2.56 KB
/
cache-test.ss
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
#lang scheme/base
(require "test-base.ss"
"cache.ss")
; Helpers ----------------------------------------
(define loads null)
(define saves null)
(define counter 0)
(define expiries null)
(define cache0
(make-cacheeq
; Load
(lambda (key)
(set! loads (cons key loads))
(set! counter (add1 counter))
counter)
; Store
(lambda (key value)
(set! saves (cons (cons key value) saves)))))
(define cache1
(make-cache
; Load
(lambda (key)
(set! loads (cons key loads))
(set! counter (add1 counter))
counter)
; Store
(lambda (key value)
(set! saves (cons (cons key value) saves)))
#:expire
(lambda (c k v)
(set! expiries (cons k expiries)))
#:lifetime 1))
(define (clean-up)
(cache-clear! cache0)
(cache-clear! cache1)
(set! loads null)
(set! saves null)
(set! counter 0)
(set! expiries null))
(define-syntax cache-test-case
(syntax-rules ()
[(cache-test-case name expr0 expr1 ...)
(test-case name (after expr0 expr1 ... (clean-up)))]))
; Tests ------------------------------------------
(define/provide-test-suite cache-tests
#:before (lambda ()
(clean-up)
(printf "Starting tests for cache.ss (these will take a few seconds).~n"))
#:after (lambda ()
(clean-up)
(printf "Finished tests for cache.ss.~n"))
(cache-test-case "Empty cache calls load"
(check-equal? (cache-ref cache0 'a) 1)
(check-equal? loads '(a)))
(cache-test-case "Populated cache doesn't call load"
(cache-ref cache0 'a)
(check-equal? (cache-ref cache0 'a) 1)
(check-equal? loads '(a)))
(cache-test-case "Store to cache writes through"
(cache-set! cache0 'a 'foo)
(check-equal? saves '((a . foo)))
(check-equal? (cache-ref cache0 'a) 'foo))
(cache-test-case "Empty 'equal cache calls load"
(set! loads null)
(check-equal? (cache-ref cache1 "a") 1)
(check-equal? loads '("a")))
(cache-test-case "Populated 'equal cache doesn't call load"
; Store a then reset
(cache-set! cache1 "a" 1)
(set! loads '())
(cache-ref cache1 "a")
(check-equal? (cache-ref cache1 "a") 1)
(check-equal? loads '()))
(cache-test-case "Store to 'equal cache writes through"
(cache-set! cache1 "a" 'foo)
(check-equal? saves '(("a" . foo)))
(check-equal? (cache-ref cache1 "a") 'foo))
(cache-test-case "Expire function is called when lifetime exceeded"
(cache-set! cache1 "a" 'foo)
(sleep 2)
(check-equal? (cache-ref cache1 "a") 1)
(check-equal? expiries '("a")))
)