clean up CLSv2 demo to sketch through how one might do data analysis.
[CommonLispStat.git] / src / unittests / unittests-dataframe.lisp
blob101aefb017c1032e10fc034e6456c455251e6a4f
1 ;;; -*- mode: lisp -*-
3 ;;; File: unittests-dataframe.lisp
4 ;;; Author: AJ Rossini <blindglobe@gmail.com>
5 ;;; Copyright: (c)2008, AJ Rossini. BSD, LLGPL, or GPLv2, depending
6 ;;; on how it arrives.
7 ;;; Purpose: unittests for the dataframe package
8 ;;; Time-stamp: <2009-04-20 18:59:02 tony>
9 ;;; Creation: <2008-05-09 14:18:19 tony>
12 ;;; What is this talk of 'release'? Klingons do not make software
13 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
14 ;;; designers and quality assurance people in its wake.
17 (in-package :lisp-stat-unittests)
19 (deftestsuite lisp-stat-ut-dataframe (lisp-stat-ut)
20 ((my-df-1
21 (make-instance 'dataframe-array
22 :storage #2A((1 2 3 4 5)
23 (10 20 30 40 50))
24 :doc "This is an interesting legal dataframe-array"
25 :case-labels (list "x" "y")
26 :var-labels (list "a" "b" "c" "d" "e")))
27 (my-df-0
28 (make-instance 'dataframe-array
29 :storage #2A((1 2 3 4 5)
30 (10 20 30 40 50))
31 :doc "This is an interesting illegal dataframe-array"
32 :case-labels (list "a" "b" "c" "d" "e")
33 :var-labels (list "x" "y")))))
35 ;;; Ensure helper-functions work
37 (addtest (lisp-stat-ut-dataframe) genseq
38 (ensure
39 (equal (cls-dataframe::gen-seq 4)
40 (list 1 2 3 4)))
41 (ensure
42 (equal (cls-dataframe::gen-seq 0)
43 nil))
44 (ensure
45 (equal (cls-dataframe::gen-seq 4 2)
46 (list 2 3 4))))
48 (addtest (lisp-stat-ut-dataframe) repeatseq
49 (ensure
50 (equal (cls-dataframe::repeat-seq 3 "d")
51 (list "d" "d" "d")))
52 (ensure
53 (equal (cls-dataframe::repeat-seq 3 'd)
54 (list 'd 'd 'd))))
57 (addtest (lisp-stat-ut-dataframe) make-labels
58 (ensure
59 (equal (cls-dataframe::make-labels "c" 3)
60 (list "c1" "c2" "c3")))
61 (ensure-error
62 (cls-dataframe::make-labels 'c 3)))
65 ;;; Dataframe tests
67 (addtest (lisp-stat-ut-dataframe) df-equalp
68 (ensure
69 (equalp (dataset (make-instance 'dataframe-array
70 :storage #2A(('a 'b)
71 ('c 'd))))
72 #2A(('a 'b)
73 ('c 'd)))))
75 (addtest (lisp-stat-ut-dataframe) df-consdata
76 (ensure
77 (consistent-dataframe-p my-df-1)))
79 (addtest (lisp-stat-ut-dataframe) df-consdata
80 (ensure-error
81 (slot-value my-df-1 'store)))
83 (addtest (lisp-stat-ut-dataframe) df-consdata
84 (ensure
85 (slot-value my-df-1 'cls-dataframe::store)))
87 (addtest (lisp-stat-ut-dataframe) df-consdata
88 (ensure
89 (dataset my-df-1)))
91 (addtest (lisp-stat-ut-dataframe) df-consdata
92 (ensure
93 (equalp
94 (slot-value my-df-1 'cls-dataframe::store)
95 (cls-dataframe::dataset my-df-1))))
97 (addtest (lisp-stat-ut-dataframe) df-consdata
98 (ensure
99 (eq (cls-dataframe::dataset my-df-1)
100 (slot-value my-df-1 'cls-dataframe::store))))
102 ;; NEVER REACH INTO CLASS INTERIORS, NO PROMISE
103 ;; GUARANTEE OF LATER CONSISTENCY...
105 (addtest (lisp-stat-ut-dataframe) df-consdata
106 (ensure
107 (cls-dataframe::doc-string my-df-1))
108 (ensure-error
109 (doc-string my-df-1)))
111 (addtest (lisp-stat-ut-dataframe) df-consdata
112 (ensure
113 (cls-dataframe::case-labels my-df-1))
114 (ensure-error
115 (case-labels my-df-1)))
117 (addtest (lisp-stat-ut-dataframe) df-consdata
118 (ensure
119 (cls-dataframe::var-labels my-df-1))
120 (ensure-error
121 (var-labels my-df-1)))
123 ;; need to ensure that for things like the following, that we protect
124 ;; this a bit more so that the results are not going to to be wrong.
125 ;; That would be a bit nasty if the dataframe-array becomes
126 ;; inconsistent.
128 (addtest (lisp-stat-ut-dataframe) badAccess9
129 (ensure
130 (setf (cls-dataframe::var-labels my-df-1)
131 (list "a" "b"))))
133 (addtest (lisp-stat-ut-dataframe) badAccess10
134 (ensure
135 (progn
136 ;; no error, but corrupts structure
137 (setf (cls-dataframe::var-labels my-df-1)
138 (list "a" "b" "c"))
139 ;; error happens here
140 (not (consistent-dataframe-p my-df-1))))) ;; Nil
142 (addtest (lisp-stat-ut-dataframe) badAccess12
143 (ensure
144 (setf (cls-dataframe::var-labels my-df-1)
145 (list "a" "b"))))
147 (addtest (lisp-stat-ut-dataframe) badAccess13
148 (ensure
149 (consistent-dataframe-p my-df-1))) ;; T
151 ;; This is now done by:
152 (addtest (lisp-stat-ut-dataframe) badAccess14
153 (ensure-error
154 (let ((old-varnames (varNames my-df-1)))
155 (setf (varNames my-df-1) (list "a" "b")) ;; should error
156 (setf (varNames my-df-1) old-varnames)
157 (error "don't reach this point in badaccess14"))))
160 ;; NEED TO CONFIRM THERE IS AN ERROR.
161 (addtest (lisp-stat-ut-dataframe) badAccess15
162 (ensure-error
163 (let ((origCaseNames (caselabels my-df-1)))
164 (setf (caselabels my-df-1) (list "a" "b" "c" 4 5))
165 (caselabels my-df-1)
166 (ignore-errors
167 (setf (caselabels my-df-1)
168 (list "a" "b" 4 5)))
169 (setf (caselabels my-df-1) origCaseNames))))
172 ;; (run-tests)
173 ;; (describe (run-tests))
177 (equalp (dataset
178 (make-instance 'dataframe-array
179 :storage #2A(('a 'b)
180 ('c 'd))))
181 #2A(('a 'b)
182 ('c 'd)) )
184 (equalp (dataset
185 (make-instance 'dataframe-array
186 :storage #2A((1 2)
187 (3 4))))
188 #2A((1 2)
189 (3 4)))
191 (equalp (dataset
192 (make-instance 'dataframe-array
193 :storage #2A((1d0 2d0)
194 (3d0 4d0))))
195 #2A((1d0 2d0)
196 (3d0 4d0)))