-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.lisp
451 lines (357 loc) · 15.2 KB
/
test.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
;;;; Last modified: 2013-12-12 08:55:00 tkych
;; cl-spark/test.lisp
;; Copyright (c) 2013 Takaya OCHIAI <tkych.repl@gmail.com>
;; This software is released under the MIT License.
;; For more details, see cl-spark/LICENSE
;;====================================================================
;; Test for CL-Spark
;;====================================================================
;;
;; Usage:
;; CL-REPL> (load "test.lisp")
(in-package :cl-user)
(defpackage #:cl-spark-test (:use :cl #:cl-spark))
(in-package #:cl-spark-test)
(defun =>? (form want)
(assert (equal form want)))
;;--------------------------------------------------------------------
;; Spark Tests
;;--------------------------------------------------------------------
;; original
(=>? (spark '(1 5 22 13 5))
"▁▂█▅▂")
(=>? (spark '(5.5 20))
"▁█")
(=>? (spark '(1 2 3 4 100 5 10 20 50 300))
"▁▁▁▁▃▁▁▁▂█")
(=>? (spark '(1 50 100))
"▁▄█")
(=>? (spark '(2 4 8))
"▁▃█")
(=>? (spark '(1 2 3 4 5))
"▁▂▄▆█")
(=>? (spark '(0 30 55 80 33 150))
"▁▂▃▄▂█")
;;; limit case
;; null
(=>? (spark '())
"")
;; singleton
(=>? (spark '(42))
"▁")
;; constant
(=>? (spark '(42 42))
"▁▁")
;;; min, max
;; `min/max' idea is due to A. Gordon's `SPARK_MIN/SPARK_MAX' env
;; variables. I think this idea is not good for the original spark.
;; Because original spark is shell script, using pipeline might be
;; same work. But in common lisp this is good idea, I think.
;; If sequence functions (e.g. find, remove, substitute, etc.) does not
;; have any keywords, it is inconvenient.
;; For example, thoguh the followings are all same work, the no.1 is
;; the most readable and convenient.
;; 1. (spark '(0 1 2 3 4 5 6 7 8 9 10) :min 5 :max 8)
;; 2. (spark '(0 1 2 3 4 5 6 7 8 9 10) :key (lambda (x) (min 8 (max 5 x))))
;; 3. (spark (mapcar (lambda (x) (min 8 (max 5 x))) '(0 1 2 3 4 5 6 7 8 9 10)))
(=>? (spark '(0 30 55 80 33 150) :min -100)
"▃▄▅▆▄█")
(=>? (spark '(0 30 55 80 33 150) :max 50)
"▁▅██▅█")
(=>? (spark '(0 30 55 80 33 150) :min 30 :max 80)
"▁▁▄█▁█")
;;; double-float, minus
(=>? (spark '(1.000000000005d0 0.000000000005d0 1.0d0))
"█▁▇")
(=>? (spark '(-1 0 -1))
"▁█▁")
(=>? (spark '(-1.000000000005d0 0.000000000005d0 -1.0d0))
"▁█▁")
;;; *ticks*
(defparameter ternary '(-1 0 1 -1 1 0 0 -1 1 1 0))
(=>? (spark ternary)
"▁▄█▁█▄▄▁██▄")
(=>? (let ((*ticks* #(#\_ #\- #\¯)))
(spark ternary))
"_-¯_¯--_¯¯-")
(=>? (let ((*ticks* #(#\▄ #\⎯ #\▀)))
(spark ternary))
"▄⎯▀▄▀⎯⎯▄▀▀⎯")
(=>? (let ((*ticks* #(#\E #\O)))
(spark '(4 8 15 22 42) :key (lambda (n) (mod n 2))))
"EEOEE")
;;; key
(defun range (start end)
(loop :for i :from start :below end :collect i))
(=>? (spark (range 0 51)
:key (lambda (x) (sin (* x pi 1/4))))
"▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█")
(=>? (spark (range 0 51)
:key (lambda (x) (cos (* x pi 1/4))))
"█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄")
(=>? (spark (range 0 51)
:key (lambda (x) (abs (cis (* x pi 1/4)))))
"▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁")
(=>? (spark (range 0 51)
:key (lambda (x) (float (phase (cis (* x pi 1/4))) 1.0)))
"▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆")
(defun fib (n)
(loop :for f1 := 0 :then f2
:and f2 := 1 :then (+ f1 f2)
:repeat n
:finally (return f1)))
(defun fac (n)
(labels ((rec (n acc)
(if (<= n 1)
acc
(rec (1- n) (* n acc)))))
(rec n 1)))
(=>? (spark (range 1 7) :key #'log) "▁▃▅▆▇█")
(=>? (spark (range 1 7) :key #'sqrt) "▁▃▄▅▆█")
(=>? (spark (range 1 7)) "▁▂▃▅▆█")
(=>? (spark (range 1 7) :key #'fib) "▁▁▂▃▅█")
(=>? (spark (range 1 7) :key #'exp) "▁▁▁▁▃█")
(=>? (spark (range 1 7) :key #'fac) "▁▁▁▁▂█")
(=>? (spark (range 1 7) :key #'isqrt) "▁▁▁███")
;;; misc
(defun look-bits (n)
(spark (map 'list #'digit-char-p (write-to-string n :base 2))))
(=>? (look-bits 42) "█▁█▁█▁")
(=>? (look-bits 43) "█▁█▁██")
(=>? (look-bits 44) "█▁██▁▁")
(=>? (look-bits 45) "█▁██▁█")
;;--------------------------------------------------------------------
;; Vspark Tests
;;--------------------------------------------------------------------
;;; limit case
;; null
(=>? (vspark '())
"")
;; singleton
(=>? (vspark '(1))
"
1 1.5 2
˫-----------------------+------------------------˧
▏
")
;; constant
(=>? (vspark '(1 1))
"
1 1.5 2
˫-----------------------+------------------------˧
▏
▏
")
(=>? (vspark '(0 30 55 80 33 150))
"
0 75 150
˫-----------------------+------------------------˧
▏
██████████▏
██████████████████▍
██████████████████████████▋
███████████▏
██████████████████████████████████████████████████
")
;;; min, max
(=>? (vspark '(0 30 55 80 33 150) :min -100)
"
-100 25 150
˫-----------------------+------------------------˧
████████████████████▏
██████████████████████████▏
███████████████████████████████▏
████████████████████████████████████▏
██████████████████████████▋
██████████████████████████████████████████████████
")
(=>? (vspark '(0 30 55 80 33 150) :max 50)
"
0 25 50
˫-----------------------+------------------------˧
▏
██████████████████████████████▏
██████████████████████████████████████████████████
██████████████████████████████████████████████████
█████████████████████████████████▏
██████████████████████████████████████████████████
")
(=>? (vspark '(0 30 55 80 33 150) :min 30 :max 80)
"
30 55 80
˫-----------------------+------------------------˧
▏
▏
█████████████████████████▏
██████████████████████████████████████████████████
███▏
██████████████████████████████████████████████████
")
;;; labels
(=>? (vspark '(1 0 .5) :labels '("on" "off" "unknown")
:size 1
:scale? nil)
"
on █
off ▏
unknown ▌
")
(=>? (vspark '(1 0 .5) :labels '("on" "off")
:size 1
:scale? nil)
"
on █
off ▏
▌
")
(=>? (vspark '(1 0) :labels '("on" "off" "unknown")
:size 1
:scale? nil)
"
on █
off ▏
")
;;; key
(=>? (vspark '(0 1 2 3 4 5 6 7 8) :key (lambda (x) (sin (* x pi 1/4))))
"
-1.0 0.0 1.0
˫-----------------------+------------------------˧
█████████████████████████▏
██████████████████████████████████████████▋
██████████████████████████████████████████████████
██████████████████████████████████████████▋
█████████████████████████▏
███████▍
▏
███████▍
████████████████████████▉
")
;;; size
(=>? (vspark '(0 1 2 3 4 5 6 7 8) :key (lambda (x) (sin (* x pi 1/4)))
:size 10)
"
-1.0 1.0
˫--------˧
█████▏
████████▌
██████████
████████▌
█████▏
█▌
▏
█▌
████▉
")
;;; scale (mid-point)
(=>? (vspark '(0 1 2 3 4 5 6 7 8) :key (lambda (x) (sin (* x pi 1/4)))
:size 20)
"
-1.0 0.0 1.0
˫--------+---------˧
██████████▏
█████████████████▏
████████████████████
█████████████████▏
██████████▏
██▉
▏
██▉
█████████▉
")
;; Life expectancy by WHO region, 2011, bothsexes
;; see. http://apps.who.int/gho/data/view.main.690
(defvar life-expectancies '(("Africa" 56)
("Americans" 76)
("South-East Asia" 67)
("Europe" 76)
("Eastern Mediterranean" 68)
("Western Pacific" 76)
("Global" 70)))
(=>? (vspark life-expectancies :key #'second)
"
56 66 76
˫-----------------------+------------------------˧
▏
██████████████████████████████████████████████████
███████████████████████████▌
██████████████████████████████████████████████████
██████████████████████████████▏
██████████████████████████████████████████████████
███████████████████████████████████▏
")
;;; newline?
(=>? (vspark life-expectancies :key #'second :scale? nil :newline? nil)
"▏
██████████████████████████████████████████████████
███████████████████████████▌
██████████████████████████████████████████████████
██████████████████████████████▏
██████████████████████████████████████████████████
███████████████████████████████████▏")
;;; scale?
(=>? (vspark life-expectancies :key #'second :scale? nil)
"
▏
██████████████████████████████████████████████████
███████████████████████████▌
██████████████████████████████████████████████████
██████████████████████████████▏
██████████████████████████████████████████████████
███████████████████████████████████▏
")
;;; labels
(=>? (vspark life-expectancies
:key #'second
:labels (mapcar #'first life-expectancies))
"
56 66 76
˫------------+-------------˧
Africa ▏
Americans ████████████████████████████
South-East Asia ███████████████▍
Europe ████████████████████████████
Eastern Mediterranean ████████████████▊
Western Pacific ████████████████████████████
Global ███████████████████▋
")
;;; title
(=>? (vspark life-expectancies
:min 50 :max 80
:key #'second
:labels (mapcar #'first life-expectancies)
:title "Life Expectancy")
"
Life Expectancy
50 65 80
˫------------+-------------˧
Africa █████▋
Americans ████████████████████████▎
South-East Asia ███████████████▉
Europe ████████████████████████▎
Eastern Mediterranean ████████████████▊
Western Pacific ████████████████████████▎
Global ██████████████████▋
")
(=>? (spark (range 0 15) :key #'fib)
"▁▁▁▁▁▁▁▁▁▁▂▂▃▅█")
(=>? (vspark (range 0 15) :key #'fib)
"
0 188.5 377
˫-----------------------+------------------------˧
▏
▏
▏
▎
▍
▋
█▏
█▊
██▊
████▌
███████▍
███████████▊
███████████████████▏
██████████████████████████████▉
██████████████████████████████████████████████████
")
;;====================================================================