1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <data/casefile.h>
22 #include <data/fastfile.h>
24 #include <data/case.h>
26 #include <gsl/gsl_randist.h>
27 #include <gsl/gsl_rng.h>
29 #include <language/command.h>
30 #include <language/lexer/lexer.h>
31 #include <libpspp/assertion.h>
35 static void test_casefile (int pattern, size_t value_cnt, size_t case_cnt);
36 static void get_random_case (struct ccase *, size_t value_cnt,
38 static void write_random_case (struct casefile *cf, size_t case_idx);
39 static void read_and_verify_random_case (struct casefile *cf,
40 struct casereader *reader,
42 static void test_casereader_clone (struct casereader *reader1, size_t case_cnt);
45 static void fail_test (const char *message, ...);
48 cmd_debug_casefile (void)
50 static const size_t sizes[] =
52 1, 2, 3, 4, 5, 6, 7, 14, 15, 16, 17, 31, 55, 73,
53 100, 137, 257, 521, 1031, 2053
59 size_max = sizeof sizes / sizeof *sizes;
60 if (lex_match_id ("SMALL"))
68 return lex_end_of_command ();
70 for (pattern = 0; pattern < 7; pattern++)
74 for (size = sizes; size < sizes + size_max; size++)
78 for (case_cnt = 0; case_cnt <= case_max;
79 case_cnt = (case_cnt * 2) + 1)
80 test_casefile (pattern, *size, case_cnt);
83 printf ("Casefile tests succeeded.\n");
88 test_casefile (int pattern, size_t value_cnt, size_t case_cnt)
91 struct casereader *r1, *r2;
96 rng = gsl_rng_alloc (gsl_rng_mt19937);
97 cf = fastfile_create (value_cnt);
99 casefile_to_disk (cf);
100 for (i = 0; i < case_cnt; i++)
101 write_random_case (cf, i);
104 r1 = casefile_get_reader (cf);
105 r2 = casefile_get_reader (cf);
110 for (i = 0; i < case_cnt; i++)
112 read_and_verify_random_case (cf, r1, i);
113 read_and_verify_random_case (cf, r2, i);
117 for (i = 0; i < case_cnt; i++)
118 read_and_verify_random_case (cf, r1, i);
119 for (i = 0; i < case_cnt; i++)
120 read_and_verify_random_case (cf, r2, i);
125 for (i = j = 0; i < case_cnt; i++)
127 read_and_verify_random_case (cf, r1, i);
128 if (gsl_rng_get (rng) % pattern == 0)
129 read_and_verify_random_case (cf, r2, j++);
130 if (i == case_cnt / 2)
131 casefile_to_disk (cf);
133 for (; j < case_cnt; j++)
134 read_and_verify_random_case (cf, r2, j);
137 test_casereader_clone (r1, case_cnt);
138 test_casereader_clone (r2, case_cnt);
143 if (casereader_read (r1, &c))
144 fail_test ("Casereader 1 not at end of file.");
145 if (casereader_read (r2, &c))
146 fail_test ("Casereader 2 not at end of file.");
148 casereader_destroy (r1);
150 casereader_destroy (r2);
153 r1 = casefile_get_destructive_reader (cf);
154 for (i = 0; i < case_cnt; i++)
156 struct ccase read_case, expected_case;
158 get_random_case (&expected_case, value_cnt, i);
159 if (!casereader_read_xfer (r1, &read_case))
160 fail_test ("Premature end of casefile.");
161 for (j = 0; j < value_cnt; j++)
163 double a = case_num (&read_case, j);
164 double b = case_num (&expected_case, j);
166 fail_test ("Case %lu fails comparison.", (unsigned long) i);
168 case_destroy (&expected_case);
169 case_destroy (&read_case);
171 casereader_destroy (r1);
173 casefile_destroy (cf);
178 get_random_case (struct ccase *c, size_t value_cnt, size_t case_idx)
181 case_create (c, value_cnt);
182 for (i = 0; i < value_cnt; i++)
183 case_data_rw (c, i)->f = case_idx % 257 + i;
187 write_random_case (struct casefile *cf, size_t case_idx)
190 get_random_case (&c, casefile_get_value_cnt (cf), case_idx);
191 casefile_append_xfer (cf, &c);
195 read_and_verify_random_case (struct casefile *cf,
196 struct casereader *reader, size_t case_idx)
198 struct ccase read_case, expected_case;
202 value_cnt = casefile_get_value_cnt (cf);
203 get_random_case (&expected_case, value_cnt, case_idx);
204 if (!casereader_read (reader, &read_case))
205 fail_test ("Premature end of casefile.");
206 for (i = 0; i < value_cnt; i++)
208 double a = case_num (&read_case, i);
209 double b = case_num (&expected_case, i);
211 fail_test ("Case %lu fails comparison.", (unsigned long) case_idx);
213 case_destroy (&read_case);
214 case_destroy (&expected_case);
218 test_casereader_clone (struct casereader *reader1, size_t case_cnt)
224 struct casefile *src = casereader_get_casefile (reader1);
225 struct casereader *clone = NULL;
227 size_t value_cnt = casefile_get_value_cnt (src);
229 struct casefile *newfile = fastfile_create (value_cnt);
230 struct casereader *newreader;
233 /* Read a 3rd of the cases */
234 for ( i = 0 ; i < case_cnt / 3 ; ++i )
236 casereader_read (reader1, &c1);
240 clone = casereader_clone (reader1);
242 /* Copy all the cases into a new file */
243 while( casereader_read (reader1, &c1))
245 casefile_append_xfer (newfile, &c1);
249 newreader = casefile_get_reader (newfile);
251 /* Make sure that the new file's are identical to those returned from
253 while( casereader_read (clone, &c1))
255 const union value *v1;
256 const union value *v2;
259 if ( ! casereader_read_xfer (newreader, &c2) )
265 v1 = case_data_all (&c1) ;
266 v2 = case_data_all (&c2) ;
268 if ( 0 != memcmp (v1, v2, value_cnt * MAX_SHORT_STRING))
269 fail_test ("Cloned reader read different value at case %ld", cases);
276 fail_test ("Cloned reader reads different number of cases.");
281 fail_test (const char *message, ...)
285 va_start (args, message);
286 vprintf (message, args);