2 # Before `make install' is performed this script should be runnable
3 # with `make test' as long as libpspp-core-$VERSION.so is in
4 # LD_LIBRARY_PATH. After `make install' it should work as `perl
7 #########################
9 # change 'tests => 1' to 'tests => last_test_to_print';
11 use Test::More tests => 36;
13 use File::Temp qw/ tempfile tempdir /;
14 BEGIN { use_ok('PSPP') };
16 #########################
22 return ! diff ("$file", \$pattern);
25 my $pspp_cmd = $ENV{PSPP_TEST_CMD};
37 my $syntaxfile = "$tempdir/foo.sps";
39 open (FH, ">$syntaxfile");
43 system ("cd $tempdir; $pspp_cmd -o raw-ascii $syntaxfile");
46 sub run_pspp_syntax_cmp
53 run_pspp_syntax ($tempdir, $syntax);
55 my $diff = diff ("$tempdir/pspp.list", \$result);
66 # Insert your test code below, the Test::More module is used here so read
67 # its man page ( perldoc Test::More ) for help writing this test script.
70 my $d = PSPP::Dict->new();
71 ok (ref $d, "Dictionary Creation");
72 ok ($d->get_var_cnt () == 0);
74 $d->set_label ("My Dictionary");
75 $d->set_documents ("These Documents");
77 # Tests for variable creation
79 my $var0 = PSPP::Var->new ($d, "le");
80 ok (!ref $var0, "Trap illegal variable name");
81 ok ($d->get_var_cnt () == 0);
83 $var0 = PSPP::Var->new ($d, "legal");
84 ok (ref $var0, "Accept legal variable name");
85 ok ($d->get_var_cnt () == 1);
87 my $var1 = PSPP::Var->new ($d, "legal");
88 ok (!ref $var1, "Trap duplicate variable name");
89 ok ($d->get_var_cnt () == 1);
91 $var1 = PSPP::Var->new ($d, "money",
92 (fmt=>PSPP::Fmt::DOLLAR,
93 width=>4, decimals=>2) );
94 ok (ref $var1, "Accept valid format");
95 ok ($d->get_var_cnt () == 2);
97 $d->set_weight ($var1);
100 # Tests for system file creation
101 # Make sure a system file can be created
103 my $tempdir = tempdir( CLEANUP => 1 );
104 my $tempfile = "$tempdir/testfile.sav";
105 my $syntaxfile = "$tempdir/syntax.sps";
106 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
107 ok (ref $sysfile, "Create sysfile object");
110 ok (-s "$tempfile", "Write system file");
115 # Make sure we can write cases to a file
117 my $d = PSPP::Dict->new();
118 PSPP::Var->new ($d, "id",
126 PSPP::Var->new ($d, "name",
133 $d->set_documents ("This should not appear");
134 $d->clear_documents ();
135 $d->add_document ("This is a document line");
137 $d->set_label ("This is the file label");
139 # Check that we can write system files
141 my $tempdir = tempdir( CLEANUP => 1 );
142 my $tempfile = "$tempdir/testfile.sav";
143 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
145 my $res = $sysfile->append_case ( [34, "frederick"]);
146 ok ($res, "Append Case");
148 $res = $sysfile->append_case ( [34, "frederick", "extra"]);
149 ok (!$res, "Appending Case with too many variables");
152 ok (-s "$tempfile", "existance");
155 # Check that sysfiles are closed properly
157 my $tempdir = tempdir( CLEANUP => 1 );
158 my $tempfile = "$tempdir/testfile.sav";
160 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
162 my $res = $sysfile->append_case ( [21, "wheelbarrow"]);
163 ok ($res, "Append Case 2");
165 # Don't close. We want to test that the destructor does that
168 ok (-s "$tempfile", "existance2");
170 ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output");
172 GET FILE='$tempfile'.
179 +--------+-------------------------------------------+--------+
180 |Variable|Description |Position|
181 #========#===========================================#========#
182 |id |Format: F2.0 | 1|
183 | |Measure: Scale | |
184 | |Display Alignment: Right | |
185 | |Display Width: 8 | |
186 +--------+-------------------------------------------+--------+
187 |name |Format: A20 | 2|
188 | |Measure: Nominal | |
189 | |Display Alignment: Left | |
190 | |Display Width: 20 | |
191 +--------+-------------------------------------------+--------+
194 This is the file label
196 Documents in the active file:
198 This is a document line
201 -- --------------------
209 # Now do some tests to make sure all the variable parameters
210 # can be written properly.
213 my $tempdir = tempdir( CLEANUP => 1 );
214 my $tempfile = "$tempdir/testfile.sav";
215 my $dict = PSPP::Dict->new();
216 ok (ref $dict, "Dictionary Creation 2");
218 my $int = PSPP::Var->new ($dict, "integer",
219 (width=>8, decimals=>0) );
221 $int->set_label ("My Integer");
223 $int->add_value_label (99, "Silly");
224 $int->clear_value_labels ();
225 $int->add_value_label (0, "Zero");
226 $int->add_value_label (1, "Unity");
227 $int->add_value_label (2, "Duality");
229 my $str = PSPP::Var->new ($dict, "string",
230 (fmt=>PSPP::Fmt::A, width=>8) );
233 $str->set_label ("My String");
234 ok ($str->add_value_label ("xx", "foo"), "Value label for short string");
235 diag ($PSPP::errstr);
236 $str->add_value_label ("yy", "bar");
238 $str->set_missing_values ("this", "that");
240 my $longstr = PSPP::Var->new ($dict, "longstring",
241 (fmt=>PSPP::Fmt::A, width=>9) );
244 $longstr->set_label ("My Long String");
245 my $re = $longstr->add_value_label ("xxx", "xfoo");
246 ok ($re, "Value label for long string");
248 $int->set_missing_values (9, 99);
250 my $sysfile = PSPP::Sysfile->new ("$tempfile", $dict);
255 ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output 2");
256 GET FILE='$tempfile'.
260 +----------+---------------------------------------------+--------+
261 |Variable |Description |Position|
262 #==========#=============================================#========#
263 |integer |My Integer | 1|
265 | |Measure: Scale | |
266 | |Display Alignment: Right | |
267 | |Display Width: 8 | |
268 | |Missing Values: 9; 99 | |
269 | +---------+-----------------------------------+ |
273 +----------+---------+-----------------------------------+--------+
274 |string |My String | 2|
276 | |Measure: Nominal | |
277 | |Display Alignment: Left | |
278 | |Display Width: 8 | |
279 | |Missing Values: "this "; "that " | |
280 | +---------+-----------------------------------+ |
283 +----------+---------+-----------------------------------+--------+
284 |longstring|My Long String | 3|
286 | |Measure: Nominal | |
287 | |Display Alignment: Left | |
288 | |Display Width: 9 | |
289 | +---------+-----------------------------------+ |
291 +----------+---------+-----------------------------------+--------+
299 sub generate_sav_file
301 my $filename = shift;
304 run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT);
305 data list notable list /string (a8) longstring (a12) numeric (f10) date (date11) dollar (dollar8.2) datetime (datetime17)
307 1111 One 1 1/1/1 1 1/1/1+01:01
308 2222 Two 2 2/2/2 2 2/2/2+02:02
309 3333 Three 3 3/3/3 3 3/3/3+03:03
311 5555 Five 5 5/5/5 5 5/5/5+05:05
315 variable labels string 'A Short String Variable'
316 /longstring 'A Long String Variable'
317 /numeric 'A Numeric Variable'
318 /date 'A Date Variable'
319 /dollar 'A Dollar Variable'
320 /datetime 'A Datetime Variable'.
323 missing values numeric (9, 5, 999).
325 missing values string ("3333").
328 /string '1111' 'ones' '2222' 'twos' '3333' 'threes'
329 /numeric 1 'Unity' 2 'Duality' 3 'Thripality'.
333 attribute=colour[1]('blue') colour[2]('pink') colour[3]('violet')
334 attribute=size('large') nationality('foreign').
337 save outfile='$filename'.
345 # Test to make sure that the dictionary survives the sysfile.
346 # Thanks to Rob Messer for reporting this problem
348 my $tempdir = tempdir( CLEANUP => 1 );
349 my $tempfile = "$tempdir/testfile.sav";
353 my $d = PSPP::Dict->new();
355 PSPP::Var->new ($d, "id",
363 $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
366 my $res = $sysfile->append_case ([3]);
368 ok ($res, "Dictionary survives sysfile");
374 my $tempdir = tempdir( CLEANUP => 1 );
376 generate_sav_file ("$tempdir/in.sav", "$tempdir");
378 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
380 my $dict = $sf->get_dict ();
382 open (MYFILE, ">$tempdir/out.txt");
383 for ($v = 0 ; $v < $dict->get_var_cnt() ; $v++)
385 my $var = $dict->get_var ($v);
386 my $name = $var->get_name ();
387 my $label = $var->get_label ();
389 print MYFILE "Variable $v is \"$name\", label is \"$label\"\n";
391 my $vl = $var->get_value_labels ();
393 print MYFILE "Value Labels:\n";
394 print MYFILE "$_ => $vl->{$_}\n" for keys %$vl;
397 while (my @c = $sf->get_next_case () )
399 for ($v = 0; $v < $dict->get_var_cnt(); $v++)
401 print MYFILE "val$v: \"$c[$v]\"\n";
408 ok (compare ("$tempdir/out.txt", <<EOF), "Basic reader operation");
409 Variable 0 is "string", label is "A Short String Variable"
414 Variable 1 is "longstring", label is "A Long String Variable"
416 Variable 2 is "numeric", label is "A Numeric Variable"
421 Variable 3 is "date", label is "A Date Variable"
423 Variable 4 is "dollar", label is "A Dollar Variable"
425 Variable 5 is "datetime", label is "A Datetime Variable"
467 # Check that we can stream one file into another
469 my $tempdir = tempdir( CLEANUP => 1 );
471 generate_sav_file ("$tempdir/in.sav", "$tempdir");
473 my $input = PSPP::Reader->open ("$tempdir/in.sav");
475 my $dict = $input->get_dict ();
477 my $output = PSPP::Sysfile->new ("$tempdir/out.sav", $dict);
479 while (my (@c) = $input->get_next_case () )
481 $output->append_case (\@c);
487 #Check the two files are the same (except for metadata)
489 run_pspp_syntax ($tempdir, <<SYNTAX);
490 get file='$tempdir/in.sav'.
496 system ("cp $tempdir/pspp.list $tempdir/in.txt");
498 run_pspp_syntax ($tempdir, <<SYNTAX);
499 get file='$tempdir/out.sav'.
505 ok (! diff ("$tempdir/pspp.list", "$tempdir/in.txt"), "Streaming of files");
510 # Check that the format_value function works properly
512 my $tempdir = tempdir( CLEANUP => 1 );
514 run_pspp_syntax ($tempdir, <<SYNTAX);
516 data list list /d (datetime17).
521 save outfile='$tempdir/dd.sav'.
525 my $sf = PSPP::Reader->open ("$tempdir/dd.sav");
527 my $dict = $sf->get_dict ();
529 my (@c) = $sf->get_next_case ();
531 my $var = $dict->get_var (0);
533 my $formatted = PSPP::format_value ($val, $var);
534 my $str = gmtime ($val - PSPP::PERL_EPOCH);
535 print "Formatted string is \"$formatted\"\n";
536 ok ( $formatted eq "11-SEP-2001 08:20", "format_value function");
537 ok ( $str eq "Tue Sep 11 08:20:00 2001", "Perl representation of time");
541 # Check that attempting to open a non-existent file results in an error
543 my $tempdir = tempdir( CLEANUP => 1 );
545 unlink ("$tempdir/no-such-file.sav");
547 my $sf = PSPP::Reader->open ("$tempdir/no-such-file.sav");
549 ok ( !ref $sf, "Returns undef on opening failure");
551 ok ("$PSPP::errstr" eq "Error opening \"$tempdir/no-such-file.sav\" for reading as a system file: No such file or directory.",
552 "Error string on open failure");
556 # Missing value tests.
558 my $tempdir = tempdir( CLEANUP => 1 );
560 generate_sav_file ("$tempdir/in.sav", "$tempdir");
562 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
564 my $dict = $sf->get_dict ();
567 my (@c) = $sf->get_next_case ();
569 my $stringvar = $dict->get_var (0);
570 my $numericvar = $dict->get_var (2);
573 ok ( !PSPP::value_is_missing ($val, $stringvar), "Missing Value Negative String");
577 ok ( !PSPP::value_is_missing ($val, $numericvar), "Missing Value Negative Num");
579 @c = $sf->get_next_case ();
580 @c = $sf->get_next_case ();
583 ok ( PSPP::value_is_missing ($val, $stringvar), "Missing Value Positive");
585 @c = $sf->get_next_case ();
587 ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive SYS");
589 @c = $sf->get_next_case ();
591 ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive Num");
595 #Test reading of custom attributes
597 my $tempdir = tempdir( CLEANUP => 1 );
599 generate_sav_file ("$tempdir/in.sav", "$tempdir");
601 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
603 my $dict = $sf->get_dict ();
605 my $var = $dict->get_var_by_name ("numeric");
607 my $attr = $var->get_attributes ();
609 open (MYFILE, ">$tempdir/out.txt");
611 foreach $k (keys %$attr)
613 my $ll = $attr->{$k};
614 print MYFILE "$k =>";
615 print MYFILE map "$_\n", join ', ', @$ll;
620 ok (compare ("$tempdir/out.txt", <<EOF), "Custom Attributes");
621 colour =>blue, pink, violet
622 nationality =>foreign