Debian Codegraph @

test/include/srfi-178-tests.scm

raw
1 ;;; Copyright (C) 2020 Wolfgang Corcoran-Mathe
2
3 ;;; Permission is hereby granted, free of charge, to any person obtaining a
4 ;;; copy of this software and associated documentation files (the
5 ;;; "Software"), to deal in the Software without restriction, including
6 ;;; without limitation the rights to use, copy, modify, merge, publish,
7 ;;; distribute, sublicense, and/or sell copies of the Software, and to
8 ;;; permit persons to whom the Software is furnished to do so, subject to
9 ;;; the following conditions:
10
11 ;;; The above copyright notice and this permission notice shall be included
12 ;;; in all copies or substantial portions of the Software.
13
14 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 ;;; OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ;;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 ;;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 ;;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 ;;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 (import (scheme base))
23 (import (scheme write))
24 (import (srfi 178))
25
26 (cond-expand
27 ((library (srfi 78))
28 (import (srfi 78)))
29 (else
30 (begin
31 (define *tests-failed* 0)
32 (define-syntax check
33 (syntax-rules (=>)
34 ((check expr => expected)
35 (if (equal? expr expected)
36 (begin
37 (display 'expr)
38 (display " => ")
39 (display expected)
40 (display " ; correct")
41 (newline))
42 (begin
43 (set! *tests-failed* (+ *tests-failed* 1))
44 (display "FAILED: for ")
45 (display 'expr)
46 (display " expected ")
47 (display expected)
48 (display " but got ")
49 (display expr)
50 (newline))))))
51 (define (check-report)
52 (if (zero? *tests-failed*)
53 (begin
54 (display "All tests passed.")
55 (newline))
56 (begin
57 (display "TESTS FAILED: ")
58 (display *tests-failed*)
59 (newline)))))))
60
61 (cond-expand
62 ((library (srfi 158))
63 (import (only (srfi 158) generator-for-each generator->list)))
64 (else
65 (begin
66 (define (generator-for-each proc g)
67 (let ((v (g)))
68 (unless (eof-object? v)
69 (proc v)
70 (generator-for-each proc g))))
71
72 (define (generator->list g)
73 (let ((v (g)))
74 (if (eof-object? v)
75 '()
76 (cons v (generator->list g))))))))
77
78 (define (print-header message)
79 (newline)
80 (display ";;; ")
81 (display message)
82 (newline))
83
84 ;;;; Utility
85
86 (define (proc-or a b) (or a b))
87
88 (define (constantly x) (lambda (_) x))
89
90 (define bitvector= bitvector=?)
91
92 (define (check-bit-conversions)
93 (print-header "Checking bit conversions...")
94
95 (check (bit->integer 0) => 0)
96 (check (bit->integer 1) => 1)
97 (check (bit->integer #f) => 0)
98 (check (bit->integer #t) => 1)
99 (check (bit->boolean 0) => #f)
100 (check (bit->boolean 1) => #t)
101 (check (bit->boolean #f) => #f)
102 (check (bit->boolean #t) => #t))
103
104 (define (check-predicates)
105 (print-header "Checking predicates...")
106
107 (check (bitvector? (bitvector)) => #t)
108 (check (bitvector? (make-bitvector 1)) => #t)
109
110 (check (bitvector-empty? (bitvector)) => #t)
111 (check (bitvector-empty? (bitvector 1)) => #f)
112
113 (check (bitvector= (bitvector) (bitvector)) => #t)
114 (check (bitvector= (bitvector 1 0 0) (bitvector 1 0 0)) => #t)
115 (check (bitvector= (bitvector 1 0 0) (bitvector 1 0 1)) => #f)
116 (check (bitvector= (bitvector 1 0 0) (bitvector 1 0)) => #f)
117 (check (bitvector= (bitvector 1 0 0)
118 (bitvector 1 0 0)
119 (bitvector 1 0 0))
120 => #t)
121 (check (bitvector= (bitvector 1 0 0)
122 (bitvector 1 0 1)
123 (bitvector 1 0 0))
124 => #f))
125
126 (include "test/constructors.scm")
127 (include "test/iterators.scm")
128 (include "test/selectors.scm")
129 (include "test/conversions.scm")
130 (include "test/mutators.scm")
131 (include "test/quasi-string.scm")
132 (include "test/gen-accum.scm")
133 (include "test/logic-ops.scm")
134 (include "test/quasi-ints.scm")
135 (include "test/fields.scm")
136
137 (define (check-all)
138 ;; Check predicates, bitvector conversions, and selectors first,
139 ;; since they're used extensively in later tests.
140 (check-predicates)
141 (check-bitvector-conversions)
142 (check-selectors)
143 (check-bit-conversions)
144 (check-constructors)
145 (check-iterators)
146 (check-quasi-string-ops)
147 (check-mutators)
148 (check-bitwise-operations)
149 (check-quasi-integer-operations)
150 (check-bit-field-operations)
151
152 (newline)
153 (check-report))
154
155 (check-all)