This repository has been archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
cl-data-frame-tests.lisp
168 lines (143 loc) · 5.9 KB
/
cl-data-frame-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
;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; Coding:utf-8 -*-
(cl:defpackage #:cl-data-frame-tests
(:use
#:cl
#:alexandria
#:anaphora
#:clunit
#:let-plus
#:cl-slice
#:cl-data-frame)
(:import-from #:clnu #:as-alist #:as-plist)
(:export #:run))
(cl:in-package #:cl-data-frame-tests)
(defsuite data-frame ())
(defun run (&optional interactive?)
(run-suite 'data-frame :use-debugger interactive?))
(defsuite data-vector (data-frame))
(deftest data-vector-basics (data-vector)
(let ((dv (dv :a 1 :b 2 :c 3)))
(assert-equalp '(:a 1 :b 2 :c 3) (as-plist dv))
(assert-equalp #(1 2 3) (columns dv))
(assert-equalp #(:a :b :c) (keys dv))
(assert-equalp '((:a . 1) (:b . 2) (:c . 3)) (as-alist dv))
(assert-equalp '(:a 1 :b 2) (as-plist (slice dv #(:a :b))))
(assert-equalp 3 (slice dv :c))
(let ((dv2 (map-columns dv #'1+)))
(assert-equalp '(:a 2 :b 3 :c 4) (as-plist dv2))
(assert-true (typep dv2 'data-vector)))))
(defsuite data-frame-basics (data-frame))
(deffixture data-frame-basics (@body)
(let ((v #(1 2 3 4))
(b #*0110)
(s #(a b c d)))
@body))
(deftest data-frame-creation (data-frame-basics)
(let* ((plist `(:vector ,v :symbols ,s))
(df (apply #'df plist))
(df-plist (plist-df plist))
(df-alist (alist-df (plist-alist plist))))
(assert-equalp #(:vector :symbols) (keys df))
(assert-equalp (vector v s) (columns df))
(assert-equalp (vector v s) (columns df t))
(assert-equalp (vector v) (columns df #(:vector)))
(assert-equalp v (columns df :vector))
(assert-equalp v (columns df -2))
(assert-equalp `(:vector ,v :symbols ,s) (as-plist df))
(assert-equalp `((:vector . ,v) (:symbols . ,s)) (as-alist df))
(assert-equalp (as-alist df) (as-alist df-plist))
(assert-equalp (as-alist df) (as-alist df-alist))))
(deftest data-frame-slice (data-frame-basics)
(let ((df (df :vector v :symbols s)))
(assert-equalp `(:vector ,v) (as-plist (slice df t #(:vector))))
(assert-equalp `(:vector ,(slice v b)) (as-plist (slice df b #(0))))
(assert-equalp (slice v b) (slice df b :vector))
(assert-equalp '(:vector 3 :symbols c) (as-plist (slice df 2 t)))
(assert-equalp `(:vector #(2 4)) (as-plist
(slice df
(mask-rows df :vector #'evenp)
#(:vector))))
(assert-equalp #(2 4) (slice df (mask-rows df :vector #'evenp) :vector))))
(deftest data-frame-map (data-frame-basics)
(let+ ((df (df :a #(2 3 5)
:b #(7 11 13)))
(product #(14 33 65))
((&flet predicate (a b) (<= 30 (* a b))))
((&flet predicate-bit (a b) (if (predicate a b) 1 0)))
(mask #*011))
(assert-equalp product
(map-rows df '(:a :b) #'*))
(assert-equalp `(:p ,product :m ,mask)
(as-plist (map-df df '(:a :b)
(lambda (a b)
(vector (* a b) (predicate-bit a b)))
'((:p fixnum) (:m bit)))))
(let ((mask-rows (mask-rows df '(:a :b) #'predicate)))
(assert-equal mask mask-rows)
(assert-eq 'bit (array-element-type mask-rows)))
(assert-equalp (count 1 mask)
(count-rows df '(:a :b) #'predicate))))
(deftest print-object (data-frame-basics)
(let ((df (df :a v :b b :c s)))
(assert-true (with-output-to-string (stream)
(print-object df stream)))))
;;;
(defsuite data-frame-add (data-frame))
(deffixture data-frame-add (@body)
(let* ((plist1 '(:a #(1 2 3)))
(plist2 '(:b #(4 5 6)))
(plist12 (append plist1 plist2)))
@body))
(defmacro test-add (add-function plist1 plist2 append?)
"Macro for generating the following test:
1. create a data frame using plist1,
2. add plist2 using add-function to get a second data frame,
3. test that the first data frame is uncorrupted if append? is nil, or
equivalent the concatenated plist otherwise,
4. test that the second data frame is equivalent to the concatenated plist.
This is a pretty comprehensive test of the add-column family of functions,
destructive or non-destructive."
(with-unique-names (df df2 plist12)
(once-only (plist1 plist2)
`(let* ((,df (plist-df ,plist1))
(,df2 (apply ,add-function ,df ,plist2))
(,plist12 (append ,plist1 ,plist2)))
(assert-equal (if ,append?
,plist12
,plist1)
(as-plist ,df))
(assert-equal ,plist12
(as-plist ,df2))))))
(deftest add-column (data-frame-add)
(test-add #'add-columns plist1 plist2 nil)
(test-add #'add-column! plist1 plist2 t)
(test-add #'add-columns! plist1 plist2 t))
(deftest add-map (data-frame-add)
(let* ((plist3 '(:c #(4 10 18)))
(plist123 (append plist12 plist3)))
;; non-destructive
(let* ((df (plist-df plist12))
(df2 (add-columns df :c (map-rows df '(:a :b) #'*))))
(assert-equalp plist12 (as-plist df))
(assert-equalp plist123 (as-plist df2)))
;; destructive, function
(let* ((df (plist-df plist12))
(df2 (add-column! df :c (map-rows df '(:a :b) #'*))))
(assert-equalp plist123 (as-plist df))
(assert-equalp plist123 (as-plist df2)))))
;;; replace-column
(defsuite replace-column (data-frame))
(deftest replace-column1 (replace-column)
(let* ((plist '(:a #(1 2 3) :b #(5 7 11)))
(df (plist-df plist))
(df-copy (copy df))
(df1 (replace-column df :a #'1+))
(df2 (replace-column df :a #(2 3 4)))
(expected-plist '(:a #(2 3 4) :b #(5 7 11))))
(assert-equalp expected-plist (as-plist df1))
(assert-equalp expected-plist (as-plist df2))
(assert-equalp plist (as-plist df))
;; modify destructively
(replace-column! df :a #'1+)
(assert-false (equalp plist (as-plist df)))
(assert-equalp expected-plist (as-plist df))))