forked from tpapp/data-omnivore
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.lisp
243 lines (196 loc) · 8.95 KB
/
tests.lisp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
;;; -*- Mode: LISP; Syntax: Ansi-Common-Lisp; Base: 10; Package: CL-USER -*-
;;; (c) 2021 Symbolics Pte. Ltd. All rights reserved.
;;; Genera has two problems in compiling this file:
;;; 1. Error: Offset is past end of function
;;; 2. Pretty Printing functions are undefined
;;; The latter is because we load XP into the DATA-FRAME package, not CL. I tried to get it loading in CL, but ran out of time futzing with it. An issue on the Genera github was raised to get these functions into CL, and a question posted on stackoverflow
;;; The former problem is somewhat of a mystery. Bisecting and compiling this file showed the offending code to be in various locations, leading me to think this is an issue in a CLUNIT2 macro. The most important tests can be buffer compiled and run, those tests being the number parsing, which all pass on Genera, meaning Genera is safe to use from an accuracy perspective with LISP-STAT. So, to run the tests, load this file in Zmacs and M-X Compile Buffer, then run the tests from the Listener. Be sure to only compile the tests you want, and you may have to "Continue as if loading fasl was successful" because you'll get caught out at issue 2, since CLUNIT uses the pretty printer for output reports.
(defpackage #:dfio-tests
(:use #:cl
#:alexandria
#:clunit
#:dfio.decimal
#:dfio.string-table
#:dfio.data-column
#:dfio
#:let-plus)
(:import-from #:nu #:as-plist)
(:export #:run))
(in-package #:dfio-tests)
;;; interface
(defsuite dfio-tests ())
(defparameter *iterations* 10000
"Number of iterations for random tests.")
(defun run (&optional interactive?)
(let ((*iterations* (if interactive?
100
*iterations*)))
(run-suite 'dfio-tests :use-debugger interactive?)))
;;; decimals
(defsuite decimal-tests (dfio-tests))
(defun random-sign ()
"Return a random sign (+,-) or an empty string."
(ecase (random 3)
(0 "")
(1 "+")
(2 "-")))
(defun random-signless (digits)
"Random sequence of digits, representing an integer. DIGITS suggests a total length for nonzero digits, but it is random, padded with a random number of zeroes. For testing. When digits is NIL, an empty string is returned."
(if digits
(progn
(check-type digits (integer 1))
(format nil "~v,'0D"
(random (* 2 digits))
(random (expt 10 digits))))
""))
(defun random-integer-string (digits)
"Return (cons VALUE STRING) such that (PARSE-REAL STRING) is expected to return VALUE, which is an integer.
See RANDOM-SIGNLESS for the semantics of DIGITS, the result of (RANDOM-SIGN) is prepended."
(let ((string (concatenate 'string (random-sign) (random-signless digits))))
(cons (parse-integer string) string)))
(defun random-float-string (&key (exponent-char #\d)
(whole-digits 6)
(fraction-digits 6)
(exponent-digits 2))
"Return (cons VALUE STRING) such that (PARSE-REAL STRING) is expected to give VALUE. Useful for testing.
STRING represents a number, randomly generated according to the following rules:
- Whole and fractional parts are generated using RANDOM-SIGNLESS called with WHOLE-DIGITS and FRACTION-DIGITS, prepended by a random sign.
- EXPONENT-DIGITS has similar semantics. EXPONENT-CHAR is used before the exponent (if applicable)."
(assert (or whole-digits fraction-digits) ()
"No digits around the decimal dot.")
(let+ (((&flet cat (&rest strings)
(apply #'concatenate 'string strings)))
(whole-sign (random-sign))
(whole (random-signless whole-digits))
(fraction (random-signless fraction-digits))
(exponent (if exponent-digits
(format nil "~C~A~A"
exponent-char
(random-sign)
(random-signless exponent-digits))
""))
(string (cat whole-sign whole "." fraction exponent))
(value (read-from-string
(concatenate 'string
(cat whole-sign
(if whole-digits
whole
"0"))
"."
(if fraction-digits
fraction
"0")
exponent))))
(cons value string)))
(defmacro random-parse-test (form)
"Evaluates FORM repeatedly, using the resulting (cons VALUE STRING) to test PARSE-REAL."
`(loop repeat *iterations*
do (let+ (((value . string) ,form))
(assert-eql value (parse-real string)
value string))))
(deftest parse-real-test (decimal-tests)
(random-parse-test (random-float-string)))
(deftest parse-real-test-noexp (decimal-tests)
(random-parse-test (random-float-string :exponent-digits nil)))
(deftest parse-real-test-nowhole (decimal-tests)
(random-parse-test (random-float-string :whole-digits nil)))
(deftest parse-real-test-nofrac (decimal-tests)
(random-parse-test (random-float-string :fraction-digits nil)))
(deftest parse-real-test-integer (decimal-tests)
(random-parse-test (random-integer-string 5)))
(deftest parse-rational-errors (decimal-tests)
(assert-condition parse-rational-error (parse-rational "")) ; empty
(assert-condition parse-rational-error (parse-rational "junk")) ; junk
(assert-condition parse-rational-error (parse-rational "1..2"))
(assert-condition parse-rational-error (parse-rational " 12 "))
(assert-condition parse-rational-error (parse-rational "1.-2")))
;;; string-table
(defsuite string-table-tests (dfio-tests))
(deftest string-table-basic-test (string-table-tests)
(let ((st (string-table)))
(assert-equal 0 (string-table-count st))
(assert-condition string-table-not-found (string-table-lookup st "foo"))
(assert-equal 0 (string-table-count st))
(string-table-add st "bar")
(assert-equal 1 (string-table-count st))
(assert-condition string-table-duplicate (string-table-add st "bar"))))
(deftest string-table-intern-test (string-table-tests)
(let+ ((st (string-table))
(strings '("foo" "bar" "foo" "baz" "bar"))
(interned-strings (mapcar (curry #'string-table-intern st) strings))
((foo1 bar1 foo2 &ign bar2) interned-strings))
(assert-eql 3 (string-table-count st))
(assert-eq foo1 foo2)
(assert-eq bar1 bar2)
(assert-equal strings interned-strings)))
(deftest string-table-map-test (string-table-tests)
(let+ ((st (string-table))
(alist '(("foo" . foo) ("bar" . bar) ("baz" . baz))))
(mapc (lambda+ ((string . symbol))
(assert-eq symbol (string-table-add st string symbol)))
alist)
(assert-equal 3 (string-table-count st))
(mapc (lambda+ ((string . symbol))
(assert-eq symbol (string-table-lookup st string)))
alist)))
;;; data-column
(defsuite data-column-tests (dfio-tests))
(deftest data-column-basic-test (data-column-tests)
(let* ((dc (data-column :map-alist '(("" . missing)
("NA" . not-available))))
(strings #("male" "female" "male" "male" "female"
"112.7" "99" "28" "1e2" "1e-2"
"" "NA" "NA" "" ""))
(added-elements (map 'vector (curry #'data-column-add dc)
strings)))
(assert-equalp #("male" "female" "male" "male" "female"
112.7d0 99d0 28d0 100d0 0.01d0
missing not-available not-available missing missing)
(data-column-vector dc))
(assert-equalp added-elements (data-column-vector dc))
(let+ (((e0 e1 e2 e3 e4 &rest &ign)
(coerce (data-column-vector dc) 'list)))
(assert-eq e0 e2)
(assert-eq e0 e3)
(assert-eq e1 e4))))
;;; data-frame
(defsuite csv-tests (dfio-tests))
(deftest csv-reading-basic (csv-tests)
(let ((df (read-csv
"Index,Gender,Age
0,\"Male\",30
1,\"Female\",31
2,Male,\"32\"
"
:column-keys-or-function #'string-to-keyword
)))
(assert-equalp '(:index #(0 1 2)
:gender #("Male" "Female" "Male")
:age #(30 31 32))
(nu:as-plist df))))
(deftest csv-reading-with-row-names (csv-tests)
(let ((df (read-csv
"Not,the,original
0,\"Male\",30
1,\"Female\",31
2,Male,\"32\"
" :column-keys-or-function '(index gender age))))
(assert-equalp '(index #(0 1 2)
gender #("Male" "Female" "Male")
age #(30 31 32))
(nu:as-plist df))))
(deftest csv-writing-basic (csv-tests)
(let ((df (read-csv
"Index,Gender,Age
0,\"Male\",30
1,\"Female\",31
2,Male,\"32\"
")))
(assert-equalp
"INDEX,GENDER,AGE
0,Male,30
1,Female,31
2,Male,32
"
(remove #\ (write-csv df nil :add-first-row t))))) ; remove CR if on windows