-
Notifications
You must be signed in to change notification settings - Fork 16
/
test-check.rkt
71 lines (59 loc) · 1.86 KB
/
test-check.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
#lang racket/base
(provide record-bench
test
time-test
todo
*test-result-same?*)
(require "private/internals.rkt"
(for-syntax racket/base syntax/parse))
(define (tree-contains tree atom)
(cond
((null? tree) #f)
((pair? tree)
(or (tree-contains (car tree) atom)
(tree-contains (cdr tree) atom)))
(else (equal? tree atom))))
(define (tree-count tree atom)
(cond
((null? tree) 0)
((pair? tree)
(+ (tree-count(car tree) atom)
(tree-count (cdr tree) atom)))
((equal? tree atom) 1)
(else 0)))
(define (record-bench phase name . args)
(when (generated-code)
(printf "generated code u-eval-expo count: ~a~%"
(tree-count (generated-code) 'invoke-fallback)))
(if (null? args)
(printf "BENCH ~a ~a\n" phase name)
(printf "BENCH ~a ~a ~a\n" phase name (car args)))
(reset-generated-code!))
(define test-failed #f)
(define (set-test-failed!)
(set! test-failed #t))
(define *test-result-same?* (make-parameter equal?))
(define-syntax test
(syntax-parser
((~and test-case (_ tested-expression expected-result))
#'(begin
(printf "Testing ~a\n" 'tested-expression)
(let* ((expected expected-result)
(produced tested-expression))
(unless ((*test-result-same?*) expected produced)
(set-test-failed!)
(raise-syntax-error
'test
(format "Failed: ~a~%Expected: ~a~%Computed: ~a~%"
'tested-expression expected produced)
#'test-case)))))))
(define-syntax time-test
(syntax-rules ()
((_ tested-expression expected-result)
(test
(time tested-expression)
expected-result))))
(define-syntax todo
(syntax-rules ()
((_ title tested-expression expected-result)
(printf "TODO ~s\n" title))))