2 # Before `make install' is performed this script should be runnable with
3 # `make test'. After `make install' it should work as `perl PSPP.t'
5 #########################
7 # change 'tests => 1' to 'tests => last_test_to_print';
9 use Test::More tests => 31;
11 use File::Temp qw/ tempfile tempdir /;
12 BEGIN { use_ok('PSPP') };
14 #########################
20 return ! diff ("$file", \$pattern);
28 my $syntaxfile = "$tempdir/foo.sps";
30 open (FH, ">$syntaxfile");
34 system ("cd $tempdir; pspp -o raw-ascii $syntaxfile");
37 sub run_pspp_syntax_cmp
44 run_pspp_syntax ($tempdir, $syntax);
46 my $diff = diff ("$tempdir/pspp.list", \$result);
57 # Insert your test code below, the Test::More module is used here so read
58 # its man page ( perldoc Test::More ) for help writing this test script.
61 my $d = PSPP::Dict->new();
62 ok (ref $d, "Dictionary Creation");
64 $d->set_label ("My Dictionary");
65 $d->set_documents ("These Documents");
67 # Tests for variable creation
69 my $var0 = PSPP::Var->new ($d, "le");
70 ok (!ref $var0, "Trap illegal variable name");
72 $var0 = PSPP::Var->new ($d, "legal");
73 ok (ref $var0, "Accept legal variable name");
75 my $var1 = PSPP::Var->new ($d, "legal");
76 ok (!ref $var1, "Trap duplicate variable name");
78 $var1 = PSPP::Var->new ($d, "money",
79 (fmt=>PSPP::Fmt::DOLLAR,
80 width=>4, decimals=>2) );
81 ok (ref $var1, "Accept valid format");
83 $d->set_weight ($var1);
86 # Tests for system file creation
87 # Make sure a system file can be created
89 my $tempdir = tempdir( CLEANUP => 1 );
90 my $tempfile = "$tempdir/testfile.sav";
91 my $syntaxfile = "$tempdir/syntax.sps";
92 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
93 ok (ref $sysfile, "Create sysfile object");
96 ok (-s "$tempfile", "Write system file");
101 # Make sure we can write cases to a file
103 my $d = PSPP::Dict->new();
104 PSPP::Var->new ($d, "id",
112 PSPP::Var->new ($d, "name",
119 $d->set_documents ("This should not appear");
120 $d->clear_documents ();
121 $d->add_document ("This is a document line");
123 $d->set_label ("This is the file label");
125 # Check that we can write system files
127 my $tempdir = tempdir( CLEANUP => 1 );
128 my $tempfile = "$tempdir/testfile.sav";
129 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
131 my $res = $sysfile->append_case ( [34, "frederick"]);
132 ok ($res, "Append Case");
134 $res = $sysfile->append_case ( [34, "frederick", "extra"]);
135 ok (!$res, "Appending Case with too many variables");
138 ok (-s "$tempfile", "existance");
141 # Check that sysfiles are closed properly
143 my $tempdir = tempdir( CLEANUP => 1 );
144 my $tempfile = "$tempdir/testfile.sav";
146 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
148 my $res = $sysfile->append_case ( [21, "wheelbarrow"]);
149 ok ($res, "Append Case 2");
151 # Don't close. We want to test that the destructor does that
154 ok (-s "$tempfile", "existance2");
156 ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output");
158 GET FILE='$tempfile'.
165 +--------+-------------------------------------------+--------+
166 |Variable|Description |Position|
167 #========#===========================================#========#
168 |id |Format: F2.0 | 1|
169 | |Measure: Scale | |
170 | |Display Alignment: Right | |
171 | |Display Width: 8 | |
172 +--------+-------------------------------------------+--------+
173 |name |Format: A20 | 2|
174 | |Measure: Nominal | |
175 | |Display Alignment: Left | |
176 | |Display Width: 20 | |
177 +--------+-------------------------------------------+--------+
180 This is the file label
182 Documents in the active file:
184 This is a document line
187 -- --------------------
195 # Now do some tests to make sure all the variable parameters
196 # can be written properly.
199 my $tempdir = tempdir( CLEANUP => 1 );
200 my $tempfile = "$tempdir/testfile.sav";
201 my $dict = PSPP::Dict->new();
202 ok (ref $dict, "Dictionary Creation 2");
204 my $int = PSPP::Var->new ($dict, "integer",
205 (width=>8, decimals=>0) );
207 $int->set_label ("My Integer");
209 $int->add_value_label (99, "Silly");
210 $int->clear_value_labels ();
211 $int->add_value_label (0, "Zero");
212 $int->add_value_label (1, "Unity");
213 $int->add_value_label (2, "Duality");
215 my $str = PSPP::Var->new ($dict, "string",
216 (fmt=>PSPP::Fmt::A, width=>8) );
219 $str->set_label ("My String");
220 ok ($str->add_value_label ("xx", "foo"), "Value label for short string");
221 diag ($PSPP::errstr);
222 $str->add_value_label ("yy", "bar");
224 $str->set_missing_values ("this", "that");
226 my $longstr = PSPP::Var->new ($dict, "longstring",
227 (fmt=>PSPP::Fmt::A, width=>9) );
230 $longstr->set_label ("My Long String");
231 my $re = $longstr->add_value_label ("xxx", "xfoo");
232 ok (($re == 0), "Long strings cant have labels");
234 ok ($PSPP::errstr eq "Cannot add label to a long string variable", "Error msg");
236 $int->set_missing_values (9, 99);
238 my $sysfile = PSPP::Sysfile->new ("$tempfile", $dict);
243 ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output 2");
244 GET FILE='$tempfile'.
248 +----------+-----------------------------------------+--------+
249 |Variable |Description |Position|
250 #==========#=========================================#========#
251 |integer |My Integer | 1|
253 | |Measure: Scale | |
254 | |Display Alignment: Right | |
255 | |Display Width: 8 | |
256 | |Missing Values: 9; 99 | |
257 | +-----+-----------------------------------+ |
261 +----------+-----+-----------------------------------+--------+
262 |string |My String | 2|
264 | |Measure: Nominal | |
265 | |Display Alignment: Left | |
266 | |Display Width: 8 | |
267 | |Missing Values: "this "; "that " | |
268 | +-----+-----------------------------------+ |
271 +----------+-----+-----------------------------------+--------+
272 |longstring|My Long String | 3|
274 | |Measure: Nominal | |
275 | |Display Alignment: Left | |
276 | |Display Width: 9 | |
277 +----------+-----------------------------------------+--------+
285 sub generate_sav_file
287 my $filename = shift;
290 run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT);
291 data list notable list /string (a8) longstring (a12) numeric (f10) date (date11) dollar (dollar8.2) datetime (datetime17)
293 1111 One 1 1/1/1 1 1/1/1+01:01
294 2222 Two 2 2/2/2 2 2/2/2+02:02
295 3333 Three 3 3/3/3 3 3/3/3+03:03
297 5555 Five 5 5/5/5 5 5/5/5+05:05
301 variable labels string 'A Short String Variable'
302 /longstring 'A Long String Variable'
303 /numeric 'A Numeric Variable'
304 /date 'A Date Variable'
305 /dollar 'A Dollar Variable'
306 /datetime 'A Datetime Variable'.
309 missing values numeric (9, 5, 999).
311 missing values string ("3333").
314 /string '1111' 'ones' '2222' 'twos' '3333' 'threes'
315 /numeric 1 'Unity' 2 'Duality' 3 'Thripality'.
317 save outfile='$filename'.
325 # Test to make sure that the dictionary survives the sysfile.
326 # Thanks to Rob Messer for reporting this problem
328 my $tempdir = tempdir( CLEANUP => 1 );
329 my $tempfile = "$tempdir/testfile.sav";
333 my $d = PSPP::Dict->new();
335 PSPP::Var->new ($d, "id",
343 $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
346 my $res = $sysfile->append_case ([3]);
348 ok ($res, "Dictionary survives sysfile");
354 my $tempdir = tempdir( CLEANUP => 1 );
356 generate_sav_file ("$tempdir/in.sav", "$tempdir");
358 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
360 my $dict = $sf->get_dict ();
362 open (MYFILE, ">$tempdir/out.txt");
363 for ($v = 0 ; $v < $dict->get_var_cnt() ; $v++)
365 my $var = $dict->get_var ($v);
366 my $name = $var->get_name ();
367 my $label = $var->get_label ();
369 print MYFILE "Variable $v is \"$name\", label is \"$label\"\n";
371 my $vl = $var->get_value_labels ();
373 print MYFILE "Value Labels:\n";
374 print MYFILE "$_ => $vl->{$_}\n" for keys %$vl;
377 while (my $c = $sf->get_next_case () )
379 for ($v = 0; $v < $dict->get_var_cnt(); $v++)
381 print MYFILE "val$v: \"@$c[$v]\"\n";
388 ok (compare ("$tempdir/out.txt", <<EOF), "Basic reader operation");
389 Variable 0 is "string", label is "A Short String Variable"
394 Variable 1 is "longstring", label is "A Long String Variable"
396 Variable 2 is "numeric", label is "A Numeric Variable"
401 Variable 3 is "date", label is "A Date Variable"
403 Variable 4 is "dollar", label is "A Dollar Variable"
405 Variable 5 is "datetime", label is "A Datetime Variable"
447 # Check that we can stream one file into another
449 my $tempdir = tempdir( CLEANUP => 1 );
451 generate_sav_file ("$tempdir/in.sav", "$tempdir");
453 my $input = PSPP::Reader->open ("$tempdir/in.sav");
455 my $dict = $input->get_dict ();
457 my $output = PSPP::Sysfile->new ("$tempdir/out.sav", $dict);
459 while (my $c = $input->get_next_case () )
461 $output->append_case ($c);
467 #Check the two files are the same (except for metadata)
469 run_pspp_syntax ($tempdir, <<SYNTAX);
470 get file='$tempdir/in.sav'.
476 system ("cp $tempdir/pspp.list $tempdir/in.txt");
478 run_pspp_syntax ($tempdir, <<SYNTAX);
479 get file='$tempdir/out.sav'.
485 ok (! diff ("$tempdir/pspp.list", "$tempdir/in.txt"), "Streaming of files");
490 # Check that the format_value function works properly
492 my $tempdir = tempdir( CLEANUP => 1 );
494 run_pspp_syntax ($tempdir, <<SYNTAX);
496 data list list /d (datetime17).
501 save outfile='$tempdir/dd.sav'.
505 my $sf = PSPP::Reader->open ("$tempdir/dd.sav");
507 my $dict = $sf->get_dict ();
509 my $c = $sf->get_next_case ();
511 my $var = $dict->get_var (0);
513 my $formatted = PSPP::format_value ($val, $var);
514 my $str = gmtime ($val - PSPP::PERL_EPOCH);
515 print "Formatted string is \"$formatted\"\n";
516 ok ( $formatted eq "11-SEP-2001 08:20", "format_value function");
517 ok ( $str eq "Tue Sep 11 08:20:00 2001", "Perl representation of time");
521 # Check that attempting to open a non-existent file results in an error
523 my $tempdir = tempdir( CLEANUP => 1 );
525 unlink ("$tempdir/no-such-file.sav");
527 my $sf = PSPP::Reader->open ("$tempdir/no-such-file.sav");
529 ok ( !ref $sf, "Returns undef on opening failure");
531 ok ("$PSPP::errstr" eq "Error opening \"$tempdir/no-such-file.sav\" for reading as a system file: No such file or directory.",
532 "Error string on open failure");
536 # Missing value tests.
538 my $tempdir = tempdir( CLEANUP => 1 );
540 generate_sav_file ("$tempdir/in.sav", "$tempdir");
542 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
544 my $dict = $sf->get_dict ();
547 my $c = $sf->get_next_case ();
549 my $stringvar = $dict->get_var (0);
550 my $numericvar = $dict->get_var (2);
553 ok ( !PSPP::value_is_missing ($val, $stringvar), "Missing Value Negative String");
557 ok ( !PSPP::value_is_missing ($val, $numericvar), "Missing Value Negative Num");
559 $c = $sf->get_next_case ();
560 $c = $sf->get_next_case ();
563 ok ( PSPP::value_is_missing ($val, $stringvar), "Missing Value Positive");
565 $c = $sf->get_next_case ();
567 ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive SYS");
569 $c = $sf->get_next_case ();
571 ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive Num");