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 => 32;
11 use File::Temp qw/ tempfile tempdir /;
12 BEGIN { use_ok('PSPP') };
14 #########################
20 return ! diff ("$file", \$pattern);
23 my $pspp_cmd = $ENV{PSPP_TEST_CMD};
35 my $syntaxfile = "$tempdir/foo.sps";
37 open (FH, ">$syntaxfile");
41 system ("cd $tempdir; $pspp_cmd -o raw-ascii $syntaxfile");
44 sub run_pspp_syntax_cmp
51 run_pspp_syntax ($tempdir, $syntax);
53 my $diff = diff ("$tempdir/pspp.list", \$result);
64 # Insert your test code below, the Test::More module is used here so read
65 # its man page ( perldoc Test::More ) for help writing this test script.
68 my $d = PSPP::Dict->new();
69 ok (ref $d, "Dictionary Creation");
71 $d->set_label ("My Dictionary");
72 $d->set_documents ("These Documents");
74 # Tests for variable creation
76 my $var0 = PSPP::Var->new ($d, "le");
77 ok (!ref $var0, "Trap illegal variable name");
79 $var0 = PSPP::Var->new ($d, "legal");
80 ok (ref $var0, "Accept legal variable name");
82 my $var1 = PSPP::Var->new ($d, "legal");
83 ok (!ref $var1, "Trap duplicate variable name");
85 $var1 = PSPP::Var->new ($d, "money",
86 (fmt=>PSPP::Fmt::DOLLAR,
87 width=>4, decimals=>2) );
88 ok (ref $var1, "Accept valid format");
90 $d->set_weight ($var1);
93 # Tests for system file creation
94 # Make sure a system file can be created
96 my $tempdir = tempdir( CLEANUP => 1 );
97 my $tempfile = "$tempdir/testfile.sav";
98 my $syntaxfile = "$tempdir/syntax.sps";
99 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
100 ok (ref $sysfile, "Create sysfile object");
103 ok (-s "$tempfile", "Write system file");
108 # Make sure we can write cases to a file
110 my $d = PSPP::Dict->new();
111 PSPP::Var->new ($d, "id",
119 PSPP::Var->new ($d, "name",
126 $d->set_documents ("This should not appear");
127 $d->clear_documents ();
128 $d->add_document ("This is a document line");
130 $d->set_label ("This is the file label");
132 # Check that we can write system files
134 my $tempdir = tempdir( CLEANUP => 1 );
135 my $tempfile = "$tempdir/testfile.sav";
136 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
138 my $res = $sysfile->append_case ( [34, "frederick"]);
139 ok ($res, "Append Case");
141 $res = $sysfile->append_case ( [34, "frederick", "extra"]);
142 ok (!$res, "Appending Case with too many variables");
145 ok (-s "$tempfile", "existance");
148 # Check that sysfiles are closed properly
150 my $tempdir = tempdir( CLEANUP => 1 );
151 my $tempfile = "$tempdir/testfile.sav";
153 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
155 my $res = $sysfile->append_case ( [21, "wheelbarrow"]);
156 ok ($res, "Append Case 2");
158 # Don't close. We want to test that the destructor does that
161 ok (-s "$tempfile", "existance2");
163 ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output");
165 GET FILE='$tempfile'.
172 +--------+-------------------------------------------+--------+
173 |Variable|Description |Position|
174 #========#===========================================#========#
175 |id |Format: F2.0 | 1|
176 | |Measure: Scale | |
177 | |Display Alignment: Right | |
178 | |Display Width: 8 | |
179 +--------+-------------------------------------------+--------+
180 |name |Format: A20 | 2|
181 | |Measure: Nominal | |
182 | |Display Alignment: Left | |
183 | |Display Width: 20 | |
184 +--------+-------------------------------------------+--------+
187 This is the file label
189 Documents in the active file:
191 This is a document line
194 -- --------------------
202 # Now do some tests to make sure all the variable parameters
203 # can be written properly.
206 my $tempdir = tempdir( CLEANUP => 1 );
207 my $tempfile = "$tempdir/testfile.sav";
208 my $dict = PSPP::Dict->new();
209 ok (ref $dict, "Dictionary Creation 2");
211 my $int = PSPP::Var->new ($dict, "integer",
212 (width=>8, decimals=>0) );
214 $int->set_label ("My Integer");
216 $int->add_value_label (99, "Silly");
217 $int->clear_value_labels ();
218 $int->add_value_label (0, "Zero");
219 $int->add_value_label (1, "Unity");
220 $int->add_value_label (2, "Duality");
222 my $str = PSPP::Var->new ($dict, "string",
223 (fmt=>PSPP::Fmt::A, width=>8) );
226 $str->set_label ("My String");
227 ok ($str->add_value_label ("xx", "foo"), "Value label for short string");
228 diag ($PSPP::errstr);
229 $str->add_value_label ("yy", "bar");
231 $str->set_missing_values ("this", "that");
233 my $longstr = PSPP::Var->new ($dict, "longstring",
234 (fmt=>PSPP::Fmt::A, width=>9) );
237 $longstr->set_label ("My Long String");
238 my $re = $longstr->add_value_label ("xxx", "xfoo");
239 ok (($re == 0), "Long strings cant have labels");
241 ok ($PSPP::errstr eq "Cannot add label to a long string variable", "Error msg");
243 $int->set_missing_values (9, 99);
245 my $sysfile = PSPP::Sysfile->new ("$tempfile", $dict);
250 ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output 2");
251 GET FILE='$tempfile'.
255 +----------+-----------------------------------------+--------+
256 |Variable |Description |Position|
257 #==========#=========================================#========#
258 |integer |My Integer | 1|
260 | |Measure: Scale | |
261 | |Display Alignment: Right | |
262 | |Display Width: 8 | |
263 | |Missing Values: 9; 99 | |
264 | +-----+-----------------------------------+ |
268 +----------+-----+-----------------------------------+--------+
269 |string |My String | 2|
271 | |Measure: Nominal | |
272 | |Display Alignment: Left | |
273 | |Display Width: 8 | |
274 | |Missing Values: "this "; "that " | |
275 | +-----+-----------------------------------+ |
278 +----------+-----+-----------------------------------+--------+
279 |longstring|My Long String | 3|
281 | |Measure: Nominal | |
282 | |Display Alignment: Left | |
283 | |Display Width: 9 | |
284 +----------+-----------------------------------------+--------+
292 sub generate_sav_file
294 my $filename = shift;
297 run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT);
298 data list notable list /string (a8) longstring (a12) numeric (f10) date (date11) dollar (dollar8.2) datetime (datetime17)
300 1111 One 1 1/1/1 1 1/1/1+01:01
301 2222 Two 2 2/2/2 2 2/2/2+02:02
302 3333 Three 3 3/3/3 3 3/3/3+03:03
304 5555 Five 5 5/5/5 5 5/5/5+05:05
308 variable labels string 'A Short String Variable'
309 /longstring 'A Long String Variable'
310 /numeric 'A Numeric Variable'
311 /date 'A Date Variable'
312 /dollar 'A Dollar Variable'
313 /datetime 'A Datetime Variable'.
316 missing values numeric (9, 5, 999).
318 missing values string ("3333").
321 /string '1111' 'ones' '2222' 'twos' '3333' 'threes'
322 /numeric 1 'Unity' 2 'Duality' 3 'Thripality'.
326 attribute=colour[1]('blue') colour[2]('pink') colour[3]('violet')
327 attribute=size('large') nationality('foreign').
330 save outfile='$filename'.
338 # Test to make sure that the dictionary survives the sysfile.
339 # Thanks to Rob Messer for reporting this problem
341 my $tempdir = tempdir( CLEANUP => 1 );
342 my $tempfile = "$tempdir/testfile.sav";
346 my $d = PSPP::Dict->new();
348 PSPP::Var->new ($d, "id",
356 $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
359 my $res = $sysfile->append_case ([3]);
361 ok ($res, "Dictionary survives sysfile");
367 my $tempdir = tempdir( CLEANUP => 1 );
369 generate_sav_file ("$tempdir/in.sav", "$tempdir");
371 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
373 my $dict = $sf->get_dict ();
375 open (MYFILE, ">$tempdir/out.txt");
376 for ($v = 0 ; $v < $dict->get_var_cnt() ; $v++)
378 my $var = $dict->get_var ($v);
379 my $name = $var->get_name ();
380 my $label = $var->get_label ();
382 print MYFILE "Variable $v is \"$name\", label is \"$label\"\n";
384 my $vl = $var->get_value_labels ();
386 print MYFILE "Value Labels:\n";
387 print MYFILE "$_ => $vl->{$_}\n" for keys %$vl;
390 while (my $c = $sf->get_next_case () )
392 for ($v = 0; $v < $dict->get_var_cnt(); $v++)
394 print MYFILE "val$v: \"@$c[$v]\"\n";
401 ok (compare ("$tempdir/out.txt", <<EOF), "Basic reader operation");
402 Variable 0 is "string", label is "A Short String Variable"
407 Variable 1 is "longstring", label is "A Long String Variable"
409 Variable 2 is "numeric", label is "A Numeric Variable"
414 Variable 3 is "date", label is "A Date Variable"
416 Variable 4 is "dollar", label is "A Dollar Variable"
418 Variable 5 is "datetime", label is "A Datetime Variable"
460 # Check that we can stream one file into another
462 my $tempdir = tempdir( CLEANUP => 1 );
464 generate_sav_file ("$tempdir/in.sav", "$tempdir");
466 my $input = PSPP::Reader->open ("$tempdir/in.sav");
468 my $dict = $input->get_dict ();
470 my $output = PSPP::Sysfile->new ("$tempdir/out.sav", $dict);
472 while (my $c = $input->get_next_case () )
474 $output->append_case ($c);
480 #Check the two files are the same (except for metadata)
482 run_pspp_syntax ($tempdir, <<SYNTAX);
483 get file='$tempdir/in.sav'.
489 system ("cp $tempdir/pspp.list $tempdir/in.txt");
491 run_pspp_syntax ($tempdir, <<SYNTAX);
492 get file='$tempdir/out.sav'.
498 ok (! diff ("$tempdir/pspp.list", "$tempdir/in.txt"), "Streaming of files");
503 # Check that the format_value function works properly
505 my $tempdir = tempdir( CLEANUP => 1 );
507 run_pspp_syntax ($tempdir, <<SYNTAX);
509 data list list /d (datetime17).
514 save outfile='$tempdir/dd.sav'.
518 my $sf = PSPP::Reader->open ("$tempdir/dd.sav");
520 my $dict = $sf->get_dict ();
522 my $c = $sf->get_next_case ();
524 my $var = $dict->get_var (0);
526 my $formatted = PSPP::format_value ($val, $var);
527 my $str = gmtime ($val - PSPP::PERL_EPOCH);
528 print "Formatted string is \"$formatted\"\n";
529 ok ( $formatted eq "11-SEP-2001 08:20", "format_value function");
530 ok ( $str eq "Tue Sep 11 08:20:00 2001", "Perl representation of time");
534 # Check that attempting to open a non-existent file results in an error
536 my $tempdir = tempdir( CLEANUP => 1 );
538 unlink ("$tempdir/no-such-file.sav");
540 my $sf = PSPP::Reader->open ("$tempdir/no-such-file.sav");
542 ok ( !ref $sf, "Returns undef on opening failure");
544 ok ("$PSPP::errstr" eq "Error opening \"$tempdir/no-such-file.sav\" for reading as a system file: No such file or directory.",
545 "Error string on open failure");
549 # Missing value tests.
551 my $tempdir = tempdir( CLEANUP => 1 );
553 generate_sav_file ("$tempdir/in.sav", "$tempdir");
555 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
557 my $dict = $sf->get_dict ();
560 my $c = $sf->get_next_case ();
562 my $stringvar = $dict->get_var (0);
563 my $numericvar = $dict->get_var (2);
566 ok ( !PSPP::value_is_missing ($val, $stringvar), "Missing Value Negative String");
570 ok ( !PSPP::value_is_missing ($val, $numericvar), "Missing Value Negative Num");
572 $c = $sf->get_next_case ();
573 $c = $sf->get_next_case ();
576 ok ( PSPP::value_is_missing ($val, $stringvar), "Missing Value Positive");
578 $c = $sf->get_next_case ();
580 ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive SYS");
582 $c = $sf->get_next_case ();
584 ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive Num");
588 #Test reading of custom attributes
590 my $tempdir = tempdir( CLEANUP => 1 );
592 generate_sav_file ("$tempdir/in.sav", "$tempdir");
594 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
596 my $dict = $sf->get_dict ();
598 my $var = $dict->get_var_by_name ("numeric");
600 my $attr = $var->get_attributes ();
602 open (MYFILE, ">$tempdir/out.txt");
604 foreach $k (keys %$attr)
606 my $ll = $attr->{$k};
607 print MYFILE "$k =>";
608 print MYFILE map "$_\n", join ', ', @$ll;
613 ok (compare ("$tempdir/out.txt", <<EOF), "Custom Attributes");
614 colour =>blue, pink, violet
615 nationality =>foreign