3 ## PSPP - a program for statistical analysis.
4 ## Copyright (C) 2019 Free Software Foundation, Inc.
6 ## This program is free software: you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation, either version 3 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Before `make install' is performed this script should be runnable
20 # with `make test' as long as libpspp-core-$VERSION.so is in
21 # LD_LIBRARY_PATH. After `make install' it should work as `perl
24 #########################
26 # change 'tests => 1' to 'tests => last_test_to_print';
28 use Test::More tests => 37;
30 use File::Temp qw/ tempfile tempdir /;
31 BEGIN { use_ok('PSPP') };
33 #########################
39 return ! diff ("$file", \$pattern);
42 my $pspp_cmd = $ENV{PSPP_TEST_CMD};
54 my $syntaxfile = "$tempdir/foo.sps";
56 open (FH, ">$syntaxfile");
60 system ("cd $tempdir; $pspp_cmd -o pspp.csv $syntaxfile");
63 sub run_pspp_syntax_cmp
70 run_pspp_syntax ($tempdir, $syntax);
72 my $diff = diff ("$tempdir/pspp.csv", \$result);
83 # Insert your test code below, the Test::More module is used here so read
84 # its man page ( perldoc Test::More ) for help writing this test script.
87 my $d = PSPP::Dict->new();
88 ok (ref $d, "Dictionary Creation");
89 ok ($d->get_var_cnt () == 0);
91 $d->set_label ("My Dictionary");
92 $d->add_document ("These Documents");
94 # Tests for variable creation
96 my $var0 = PSPP::Var->new ($d, "le");
97 ok (!ref $var0, "Trap illegal variable name");
98 ok ($d->get_var_cnt () == 0);
100 $var0 = PSPP::Var->new ($d, "legal");
101 ok (ref $var0, "Accept legal variable name");
102 ok ($d->get_var_cnt () == 1);
104 my $var1 = PSPP::Var->new ($d, "legal");
105 ok (!ref $var1, "Trap duplicate variable name");
106 ok ($d->get_var_cnt () == 1);
108 $var1 = PSPP::Var->new ($d, "money",
109 (fmt=>PSPP::Fmt::DOLLAR,
110 width=>4, decimals=>2) );
111 ok (ref $var1, "Accept valid format");
112 ok ($d->get_var_cnt () == 2);
114 $d->set_weight ($var1);
117 # Tests for system file creation
118 # Make sure a system file can be created
120 my $tempdir = tempdir( CLEANUP => 1 );
121 my $tempfile = "$tempdir/testfile.sav";
122 my $syntaxfile = "$tempdir/syntax.sps";
123 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
124 ok (ref $sysfile, "Create sysfile object");
127 ok (-s "$tempfile", "Write system file");
132 # Make sure we can write cases to a file
134 my $d = PSPP::Dict->new();
135 PSPP::Var->new ($d, "id",
143 PSPP::Var->new ($d, "name",
150 $d->add_document ("This should not appear");
151 $d->clear_documents ();
152 $d->add_document ("This is a document line");
154 $d->set_label ("This is the file label");
156 # Check that we can write system files
158 my $tempdir = tempdir( CLEANUP => 1 );
159 my $tempfile = "$tempdir/testfile.sav";
160 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
162 my $res = $sysfile->append_case ( [34, "frederick"]);
163 ok ($res, "Append Case");
165 $res = $sysfile->append_case ( [34, "frederick", "extra"]);
166 ok (!$res, "Appending Case with too many variables");
169 ok (-s "$tempfile", "existance");
172 # Check that sysfiles are closed properly
174 my $tempdir = tempdir( CLEANUP => 1 );
175 my $tempfile = "$tempdir/testfile.sav";
177 my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
179 my $res = $sysfile->append_case ( [21, "wheelbarrow"]);
180 ok ($res, "Append Case 2");
182 # Don't close. We want to test that the destructor does that
185 ok (-s "$tempfile", "existance2");
187 ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output");
189 GET FILE='$tempfile'.
196 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
197 id,1,,Scale,Input,8,Right,F2.0,F2.0,
198 name,2,,Nominal,Input,20,Left,A20,A20,
201 Label,This is the file label
204 This is a document line
214 # Now do some tests to make sure all the variable parameters
215 # can be written properly.
218 my $tempdir = tempdir( CLEANUP => 1 );
219 my $tempfile = "$tempdir/testfile.sav";
220 my $dict = PSPP::Dict->new();
221 ok (ref $dict, "Dictionary Creation 2");
223 my $int = PSPP::Var->new ($dict, "integer",
224 (width=>8, decimals=>0) );
226 $int->set_label ("My Integer");
228 $int->add_value_label (99, "Silly");
229 $int->clear_value_labels ();
230 $int->add_value_label (0, "Zero");
231 $int->add_value_label (1, "Unity");
232 $int->add_value_label (2, "Duality");
234 my $str = PSPP::Var->new ($dict, "string",
235 (fmt=>PSPP::Fmt::A, width=>8) );
238 $str->set_label ("My String");
239 ok ($str->add_value_label ("xx", "foo"), "Value label for short string");
240 diag ($PSPP::errstr);
241 $str->add_value_label ("yy", "bar");
243 $str->set_missing_values ("this", "that");
245 my $longstr = PSPP::Var->new ($dict, "longstring",
246 (fmt=>PSPP::Fmt::A, width=>9) );
249 $longstr->set_label ("My Long String");
250 my $re = $longstr->add_value_label ("xxx", "xfoo");
251 ok ($re, "Value label for long string");
253 $int->set_missing_values (9, 99);
255 my $sysfile = PSPP::Sysfile->new ("$tempfile", $dict);
260 ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output 2");
261 GET FILE='$tempfile'.
265 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
266 integer,1,My Integer,Scale,Input,8,Right,F8.0,F8.0,9; 99
267 string,2,My String,Nominal,Input,8,Left,A8,A8,"""this ""; ""that """
268 longstring,3,My Long String,Nominal,Input,9,Left,A9,A9,
271 Variable Value,,Label
277 My Long String,xxx,xfoo
284 sub generate_sav_file
286 my $filename = shift;
289 run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT);
290 data list notable list /string (a8) longstring (a12) numeric (f10) date (date11) dollar (dollar8.2) datetime (datetime17)
292 1111 One 1 1/1/1 1 1/1/1+01:01
293 2222 Two 2 2/2/2 2 2/2/2+02:02
294 3333 Three 3 3/3/3 3 3/3/3+03:03
296 5555 Five 5 5/5/5 5 5/5/5+05:05
300 variable labels string 'A Short String Variable'
301 /longstring 'A Long String Variable'
302 /numeric 'A Numeric Variable'
303 /date 'A Date Variable'
304 /dollar 'A Dollar Variable'
305 /datetime 'A Datetime Variable'.
308 missing values numeric (9, 5, 999).
310 missing values string ("3333").
313 /string '1111' 'ones' '2222' 'twos' '3333' 'threes'
314 /numeric 1 'Unity' 2 'Duality' 3 'Thripality'.
318 attribute=colour[1]('blue') colour[2]('pink') colour[3]('violet')
319 attribute=size('large') nationality('foreign').
322 save outfile='$filename'.
330 # Test to make sure that the dictionary survives the sysfile.
331 # Thanks to Rob Messer for reporting this problem
333 my $tempdir = tempdir( CLEANUP => 1 );
334 my $tempfile = "$tempdir/testfile.sav";
338 my $d = PSPP::Dict->new();
340 PSPP::Var->new ($d, "id",
348 $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
351 my $res = $sysfile->append_case ([3]);
353 ok ($res, "Dictionary survives sysfile");
359 my $tempdir = tempdir( CLEANUP => 1 );
361 generate_sav_file ("$tempdir/in.sav", "$tempdir");
363 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
365 my $dict = $sf->get_dict ();
367 open (MYFILE, ">$tempdir/out.txt");
368 for ($v = 0 ; $v < $dict->get_var_cnt() ; $v++)
370 my $var = $dict->get_var ($v);
371 my $name = $var->get_name ();
372 my $label = $var->get_label ();
374 print MYFILE "Variable $v is \"$name\", label is \"$label\"\n";
376 my $vl = $var->get_value_labels ();
378 print MYFILE "Value Labels:\n";
379 print MYFILE "$_ => $vl->{$_}\n" for (sort keys %$vl);
382 while (my @c = $sf->get_next_case () )
384 for ($v = 0; $v < $dict->get_var_cnt(); $v++)
386 print MYFILE "val$v: \"$c[$v]\"\n";
393 ok (compare ("$tempdir/out.txt", <<EOF), "Basic reader operation");
394 Variable 0 is "string", label is "A Short String Variable"
399 Variable 1 is "longstring", label is "A Long String Variable"
401 Variable 2 is "numeric", label is "A Numeric Variable"
406 Variable 3 is "date", label is "A Date Variable"
408 Variable 4 is "dollar", label is "A Dollar Variable"
410 Variable 5 is "datetime", label is "A Datetime Variable"
452 # Check that we can stream one file into another
454 my $tempdir = tempdir( CLEANUP => 1 );
456 generate_sav_file ("$tempdir/in.sav", "$tempdir");
458 my $input = PSPP::Reader->open ("$tempdir/in.sav");
460 my $dict = $input->get_dict ();
462 my $output = PSPP::Sysfile->new ("$tempdir/out.sav", $dict);
464 while (my (@c) = $input->get_next_case () )
466 $output->append_case (\@c);
472 #Check the two files are the same (except for metadata)
474 run_pspp_syntax ($tempdir, <<SYNTAX);
475 get file='$tempdir/in.sav'.
481 system ("cp $tempdir/pspp.csv $tempdir/in.txt");
483 run_pspp_syntax ($tempdir, <<SYNTAX);
484 get file='$tempdir/out.sav'.
490 ok (! diff ("$tempdir/pspp.csv", "$tempdir/in.txt"), "Streaming of files");
495 # Check that the format_value function works properly
497 my $tempdir = tempdir( CLEANUP => 1 );
499 run_pspp_syntax ($tempdir, <<SYNTAX);
501 data list list /d (datetime17).
506 save outfile='$tempdir/dd.sav'.
510 my $sf = PSPP::Reader->open ("$tempdir/dd.sav");
512 my $dict = $sf->get_dict ();
514 my (@c) = $sf->get_next_case ();
516 my $var = $dict->get_var (0);
518 my $formatted = PSPP::format_value ($val, $var);
519 my $str = gmtime ($val - PSPP::PERL_EPOCH);
520 print "Formatted string is \"$formatted\"\n";
521 ok ( $formatted eq "11-SEP-2001 08:20", "format_value function");
522 ok ( $str eq "Tue Sep 11 08:20:00 2001", "Perl representation of time");
526 # Check that attempting to open a non-existent file results in an error
528 my $tempdir = tempdir( CLEANUP => 1 );
530 unlink ("$tempdir/no-such-file.sav");
532 my $sf = PSPP::Reader->open ("$tempdir/no-such-file.sav");
534 ok ( !ref $sf, "Returns undef on opening failure");
536 ok ("$PSPP::errstr" eq "An error occurred while opening `$tempdir/no-such-file.sav': No such file or directory.",
537 "Error string on open failure");
541 # Missing value tests.
543 my $tempdir = tempdir( CLEANUP => 1 );
545 generate_sav_file ("$tempdir/in.sav", "$tempdir");
547 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
549 my $dict = $sf->get_dict ();
552 my (@c) = $sf->get_next_case ();
554 my $stringvar = $dict->get_var (0);
555 my $numericvar = $dict->get_var (2);
558 ok ( !PSPP::value_is_missing ($val, $stringvar), "Missing Value Negative String");
562 ok ( !PSPP::value_is_missing ($val, $numericvar), "Missing Value Negative Num");
564 @c = $sf->get_next_case ();
565 @c = $sf->get_next_case ();
568 ok ( PSPP::value_is_missing ($val, $stringvar), "Missing Value Positive");
570 @c = $sf->get_next_case ();
572 ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive SYS");
574 @c = $sf->get_next_case ();
576 ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive Num");
580 #Test reading of custom attributes
582 my $tempdir = tempdir( CLEANUP => 1 );
584 generate_sav_file ("$tempdir/in.sav", "$tempdir");
586 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
588 my $dict = $sf->get_dict ();
590 my $var = $dict->get_var_by_name ("numeric");
592 my $attr = $var->get_attributes ();
594 open (MYFILE, ">$tempdir/out.txt");
596 foreach $k (sort (keys (%$attr)))
598 my $ll = $attr->{$k};
599 print MYFILE "$k =>";
600 print MYFILE map "$_\n", join ', ', @$ll;
605 ok (compare ("$tempdir/out.txt", <<'EOF'), "Custom Attributes");
607 colour =>blue, pink, violet
608 nationality =>foreign
614 # Test of the get_case_cnt function
616 my $tempdir = tempdir( CLEANUP => 1 );
618 generate_sav_file ("$tempdir/in.sav", "$tempdir");
620 my $sf = PSPP::Reader->open ("$tempdir/in.sav");
622 my $n = $sf->get_case_cnt ();
624 ok ($n == 5, "Case count");