-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun-ut.scm
executable file
·181 lines (147 loc) · 5.09 KB
/
run-ut.scm
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
175
176
177
178
179
180
181
#! /usr/bin/env gsi
;;;============================================================================
;;; File: "run-unit-tests.scm"
;;; Copyright (c) 2012-2014 by Marc Feeley, All Rights Reserved.
;;;----------------------------------------------------------------------------
;;; This is a modified version of the run-unit-tests.scm
;;; file from Gambit Scheme (http://gambitscheme.org/)
(define nb-good 0)
(define nb-fail 0)
(define nb-other 0)
(define nb-total 0)
(define start 0)
(define PATH "")
(define EXEC "")
(define ARGS '())
(define (num->string num w d) ; w = total width, d = decimals
(let ((n (floor (inexact->exact (round (* (abs num) (expt 10 d)))))))
(let ((i (quotient n (expt 10 d)))
(f (modulo n (expt 10 d))))
(let ((si (string-append
(if (< num 0) "-" "")
(if (and (= i 0) (> d 0)) "" (number->string i 10))))
(sf (number->string (+ f (expt 10 d)) 10)))
(if (> d 0)
(string-set! sf 0 #\.)
(set! sf ""))
(let ((lsi (string-length si))
(lsf (string-length sf)))
(let ((blanks (- w (+ lsi lsf))))
(string-append (make-string (max blanks 0) #\space) si sf)))))))
(define (show-bar nb-good nb-fail nb-other nb-total elapsed)
(define (ratio n)
(quotient (* n (+ nb-good nb-fail nb-other)) nb-total))
(if (tty? (current-output-port))
(let* ((bar-width 42)
(bar-length (ratio bar-width)))
(define (esc x)
x)
(print "\r"
"["
(esc "\33[32;1m") (num->string nb-good 4 0) (esc "\33[0m")
"|"
(esc "\33[31;1m") (num->string nb-fail 4 0) (esc "\33[0m")
"|"
(esc "\33[34;1m") (num->string nb-other 4 0) (esc "\33[0m")
"] "
(num->string (ratio 100) 3 0)
"% "
(make-string bar-length #\#)
(make-string (- bar-width bar-length) #\.)
(num->string elapsed 6 1)
"s"
(esc "\33[K")))))
(define (run path . args)
(let* ((port
(open-process (list path: path
arguments: args
;;stderr-redirection: #t
)))
(output
(read-line port #f))
(status
(process-status port)))
(close-port port)
(cons status output)))
(define (run-lc file . args)
(let* ((run-args (append (list "./lc" file) args))
(x (apply run run-args)))
(if (= (car x) 0)
(let ((y (apply run run-args)))
(if (= (car y) 0)
(if (equal? (cdr y)
(get-expected-output file))
y
(cons 1 (cdr y)))))
x)))
(define (get-expected-output filename)
(call-with-input-file
filename
(lambda (port)
(let ((lines (read-all port (lambda (p) (read-line p #\newline #t)))))
(let loop ((rev-lines (reverse lines))
(output '()))
(if (and (pair? rev-lines)
(>= (string-length (car rev-lines)) 1)
(char=? (string-ref (car rev-lines) 0) #\;))
(loop (cdr rev-lines)
(cons (substring (car rev-lines)
1
(string-length (car rev-lines)))
output))
(apply string-append output)))))))
(define (test file)
(let ((result (apply run-lc (cons file ARGS))))
(if (= 0 (car result))
(set! nb-good (+ nb-good 1))
(begin
(set! nb-fail (+ nb-fail 1))
(print "\n")
(print "*********************** FAILED TEST " file "\n")
(print (cdr result))))
(show-bar nb-good
nb-fail
nb-other
nb-total
(- (time->seconds (current-time)) start))))
(define (run-tests files)
(set! nb-good 0)
(set! nb-fail 0)
(set! nb-other 0)
(set! nb-total (length files))
(set! start (time->seconds (current-time)))
(for-each test files)
(print "\n")
(if (= nb-good nb-total)
(begin
(print "PASSED ALL " nb-total " UNIT TESTS\n")
(exit 0))
(begin
(print "FAILED " nb-fail " UNIT TESTS OUT OF " nb-total " (" (num->string (* 100. (/ nb-fail nb-total)) 0 1) "%)\n")
(exit 1))))
(define (find-files file-or-dir filter)
(if (eq? (file-type file-or-dir) 'directory)
(apply
append
(map
(lambda (f)
(find-files (path-expand f file-or-dir) filter))
(directory-files file-or-dir)))
(if (filter file-or-dir)
(list file-or-dir)
(list))))
(define (list-of-scm-files args)
(apply
append
(map
(lambda (f)
(find-files f
(lambda (filename)
(equal? (path-extension filename) ".scm"))))
args)))
(define (main . args)
(set! PATH "unit-tests")
(set! EXEC "lc")
(set! ARGS args)
(run-tests (list-of-scm-files (list PATH))))
;;;============================================================================