1 dnl PSPP - a program for statistical analysis.
2 dnl Copyright (C) 2017 Free Software Foundation, Inc.
4 dnl This program is free software: you can redistribute it and/or modify
5 dnl it under the terms of the GNU General Public License as published by
6 dnl the Free Software Foundation, either version 3 of the License, or
7 dnl (at your option) any later version.
9 dnl This program is distributed in the hope that it will be useful,
10 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 dnl GNU General Public License for more details.
14 dnl You should have received a copy of the GNU General Public License
15 dnl along with this program. If not, see <http://www.gnu.org/licenses/>.
17 AT_BANNER([Perl module tests])
19 dnl This command can be used to run with the PSPP Perl module after it has been
20 dnl built (with "make") but before it has been installed. The -I options are
21 dnl equivalent to "use ExtUtils::testlib;" inside the Perl program, but it does
22 dnl not need to be run with the perl-module build directory as the current
23 dnl working directory.
25 dnl XXX "libtool --mode=execute" is probably better than setting
27 m4_define([RUN_PERL_MODULE],
28 [LD_LIBRARY_PATH=$abs_top_builddir/src/.libs \
29 DYLD_LIBRARY_PATH=$abs_top_builddir/src/.libs \
30 $PERL -I$abs_top_builddir/perl-module/blib/arch \
31 -I$abs_top_builddir/perl-module/blib/lib])
33 AT_SETUP([Perl create system file])
34 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
40 my $d = PSPP::Dict->new();
41 die "dictionary creation" if !ref $d;
42 die if $d->get_var_cnt () != 0;
44 $d->set_label ("My Dictionary");
45 $d->set_documents ("These Documents");
47 # Tests for variable creation
49 my $var0 = PSPP::Var->new ($d, "le");
50 die "trap illegal variable name" if ref $var0;
51 die if $d->get_var_cnt () != 0;
53 $var0 = PSPP::Var->new ($d, "legal");
54 die "accept legal variable name" if !ref $var0;
55 die if $d->get_var_cnt () != 1;
57 my $var1 = PSPP::Var->new ($d, "money",
58 (fmt=>PSPP::Fmt::DOLLAR,
59 width=>4, decimals=>2) );
60 die "cappet valid format" if !ref $var1;
61 die if $d->get_var_cnt () != 2;
63 $d->set_weight ($var1);
65 my $sysfile = PSPP::Sysfile->new ('testfile.sav', $d);
66 die "create sysfile object" if !ref $sysfile;
70 AT_CHECK([RUN_PERL_MODULE test.pl])
71 AT_DATA([dump-dict.sps],
72 [GET FILE='testfile.sav'.
78 AT_CHECK([pspp -O format=csv dump-dict.sps], [0],
79 [File label: My Dictionary
81 Documents in the active dataset:
85 Variable,Description,Position
87 money,Format: DOLLAR6.2,2
89 dump-dict.sps:5: note: SHOW: WEIGHT is money.
93 AT_SETUP([Perl writing cases to system files])
94 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
100 my $d = PSPP::Dict->new();
101 PSPP::Var->new ($d, "id",
109 PSPP::Var->new ($d, "name",
116 $d->set_documents ("This should not appear");
117 $d->clear_documents ();
118 $d->add_document ("This is a document line");
120 $d->set_label ("This is the file label");
122 # Check that we can write cases to system files.
123 my $sysfile = PSPP::Sysfile->new ("testfile.sav", $d);
124 my $res = $sysfile->append_case ( [34, "frederick"]);
125 die "append case" if !$res;
127 $res = $sysfile->append_case ( [34, "frederick", "extra"]);
128 die "append case with too many variables" if $res;
131 # Check that sysfiles are closed properly automaticallly in the destructor.
132 my $sysfile2 = PSPP::Sysfile->new ("testfile2.sav", $d);
133 $res = $sysfile2->append_case ( [21, "wheelbarrow"]);
134 die "append case 2" if !$res;
136 $res = $sysfile->append_case ( [34, "frederick", "extra"]);
137 die "append case with too many variables" if $res;
139 # Don't close. We want to test that the destructor does that.
141 AT_CHECK([RUN_PERL_MODULE test.pl])
142 AT_DATA([dump-dicts.sps],
143 [GET FILE='testfile.sav'.
149 GET FILE='testfile2.sav'.
155 AT_CHECK([pspp -O format=csv dump-dicts.sps], [0],
156 [Variable,Description,Position
160 File label: This is the file label
162 Documents in the active dataset:
164 This is a document line
170 Variable,Description,Position
174 File label: This is the file label
176 Documents in the active dataset:
178 This is a document line
186 AT_SETUP([Perl write variable parameters])
187 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
193 my $dict = PSPP::Dict->new();
194 die "dictionary creation" if !ref $dict;
196 my $int = PSPP::Var->new ($dict, "integer",
197 (width=>8, decimals=>0) );
199 $int->set_label ("My Integer");
201 $int->add_value_label (99, "Silly");
202 $int->clear_value_labels ();
203 $int->add_value_label (0, "Zero");
204 $int->add_value_label (1, "Unity");
205 $int->add_value_label (2, "Duality");
207 my $str = PSPP::Var->new ($dict, "string",
208 (fmt=>PSPP::Fmt::A, width=>8) );
211 $str->set_label ("My String");
212 $str->add_value_label ("xx", "foo");
213 $str->add_value_label ("yy", "bar");
215 $str->set_missing_values ("this", "that");
217 my $longstr = PSPP::Var->new ($dict, "longstring",
218 (fmt=>PSPP::Fmt::A, width=>9) );
221 $longstr->set_label ("My Long String");
222 my $re = $longstr->add_value_label ("xxx", "xfoo");
224 $int->set_missing_values (9, 99);
226 my $sysfile = PSPP::Sysfile->new ("testfile.sav", $dict);
231 AT_CHECK([RUN_PERL_MODULE test.pl])
232 AT_DATA([dump-dict.sps],
233 [GET FILE='testfile.sav'.
236 AT_CHECK([pspp -O format=csv dump-dict.sps], [0],
237 [Variable,Description,Position
238 integer,"Label: My Integer
240 Missing Values: 9; 99
246 string,"Label: My String
248 Missing Values: ""this ""; ""that ""
253 longstring,"Label: My Long String
261 AT_SETUP([Perl dictionary survives system file])
262 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
271 my $d = PSPP::Dict->new();
273 PSPP::Var->new ($d, "id",
281 $sysfile = PSPP::Sysfile->new ("testfile.sav", $d);
284 my $res = $sysfile->append_case ([3]);
285 print "Dictionary survives sysfile\n" if $res;
287 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
288 [Dictionary survives sysfile
292 m4_define([PERL_GENERATE_SYSFILE],
293 [AT_DATA([sample.sps],
294 [[data list notable list /string (a8) longstring (a12) numeric (f10) date (date11) dollar (dollar8.2) datetime (datetime17)
296 1111 One 1 1/1/1 1 1/1/1+01:01
297 2222 Two 2 2/2/2 2 2/2/2+02:02
298 3333 Three 3 3/3/3 3 3/3/3+03:03
300 5555 Five 5 5/5/5 5 5/5/5+05:05
304 variable labels string 'A Short String Variable'
305 /longstring 'A Long String Variable'
306 /numeric 'A Numeric Variable'
307 /date 'A Date Variable'
308 /dollar 'A Dollar Variable'
309 /datetime 'A Datetime Variable'.
312 missing values numeric (9, 5, 999).
314 missing values string ("3333").
317 /string '1111' 'ones' '2222' 'twos' '3333' 'threes'
318 /numeric 1 'Unity' 2 'Duality' 3 'Thripality'.
322 attribute=colour[1]('blue') colour[2]('pink') colour[3]('violet')
323 attribute=size('large') nationality('foreign').
326 save outfile='sample.sav'.
328 AT_CHECK([pspp -O format=csv sample.sps])])
330 AT_SETUP([Perl read system file])
331 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
332 PERL_GENERATE_SYSFILE
338 my $sf = PSPP::Reader->open ("sample.sav");
340 my $dict = $sf->get_dict ();
342 for (my $v = 0 ; $v < $dict->get_var_cnt() ; $v++)
344 my $var = $dict->get_var ($v);
345 my $name = $var->get_name ();
346 my $label = $var->get_label ();
348 print "Variable $v is \"$name\", label is \"$label\"\n";
350 my $vl = $var->get_value_labels ();
352 print "Value Labels:\n";
353 print "$_ => $vl->{$_}\n" for sort (keys %$vl);
356 while (my @c = $sf->get_next_case () )
358 for (my $v = 0; $v < $dict->get_var_cnt(); $v++)
360 print "val$v: \"$c[$v]\"\n";
365 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
366 [Variable 0 is "string", label is "A Short String Variable"
371 Variable 1 is "longstring", label is "A Long String Variable"
373 Variable 2 is "numeric", label is "A Numeric Variable"
378 Variable 3 is "date", label is "A Date Variable"
380 Variable 4 is "dollar", label is "A Dollar Variable"
382 Variable 5 is "datetime", label is "A Datetime Variable"
422 AT_SETUP([Perl copying system files])
423 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
424 PERL_GENERATE_SYSFILE
430 my $input = PSPP::Reader->open ("sample.sav");
432 my $dict = $input->get_dict ();
434 my $output = PSPP::Sysfile->new ("copy.sav", $dict);
436 while (my (@c) = $input->get_next_case () )
438 $output->append_case (\@c);
443 AT_CHECK([RUN_PERL_MODULE test.pl])
444 AT_DATA([dump-dicts.sps],
445 [GET FILE='sample.sav'.
453 AT_CHECK([pspp -O format=csv dump-dicts.sps], [0],
454 [[Variable,Description,Position
455 string,"Label: A Short String Variable
457 Missing Values: ""3333 ""
463 longstring,"Label: A Long String Variable
465 numeric,"Label: A Numeric Variable
467 Missing Values: 9; 5; 999
480 date,"Label: A Date Variable
482 dollar,"Label: A Dollar Variable
483 Format: DOLLAR11.2",5
484 datetime,"Label: A Datetime Variable
485 Format: DATETIME17.0",6
488 string,longstring,numeric,date,dollar,datetime
489 1111 ,One ,1,01-JAN-2001,$1.00,01-JAN-2001 01:01
490 2222 ,Two ,2,02-FEB-2002,$2.00,02-FEB-2002 02:02
491 3333 ,Three ,3,03-MAR-2003,$3.00,03-MAR-2003 03:03
493 5555 ,Five ,5,05-MAY-2005,$5.00,05-MAY-2005 05:05
495 Variable,Description,Position
496 string,"Label: A Short String Variable
498 Missing Values: ""3333 ""
504 longstring,"Label: A Long String Variable
506 numeric,"Label: A Numeric Variable
508 Missing Values: 9; 5; 999
521 date,"Label: A Date Variable
523 dollar,"Label: A Dollar Variable
524 Format: DOLLAR11.2",5
525 datetime,"Label: A Datetime Variable
526 Format: DATETIME17.0",6
529 string,longstring,numeric,date,dollar,datetime
530 1111 ,One ,1,01-JAN-2001,$1.00,01-JAN-2001 01:01
531 2222 ,Two ,2,02-FEB-2002,$2.00,02-FEB-2002 02:02
532 3333 ,Three ,3,03-MAR-2003,$3.00,03-MAR-2003 03:03
534 5555 ,Five ,5,05-MAY-2005,$5.00,05-MAY-2005 05:05
538 AT_SETUP([Perl value formatting])
539 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
541 [DATA LIST LIST /d (DATETIME17).
546 SAVE OUTFILE='dd.sav'.
548 AT_CHECK([pspp -O format=csv dd.sps], [0],
549 [Table: Reading free-form data from INLINE.
558 my $sf = PSPP::Reader->open ("dd.sav");
560 my $dict = $sf->get_dict ();
562 my (@c) = $sf->get_next_case ();
564 my $var = $dict->get_var (0);
566 my $formatted = PSPP::format_value ($val, $var);
567 my $str = gmtime ($val - PSPP::PERL_EPOCH);
568 print "Formatted string is \"$formatted\"\n";
569 print "Perl representation is \"$str\"\n";
571 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
572 [[Formatted string is "11-SEP-2001 08:20"
573 Perl representation is "Tue Sep 11 08:20:00 2001"
577 AT_SETUP([Perl opening nonexistent file])
578 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
584 my $sf = PSPP::Reader->open ("no-such-file.sav");
586 die "Returns undef on opening failure" if ref $sf;
587 print $PSPP::errstr, "\n";
589 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
590 [[An error occurred while opening `no-such-file.sav': No such file or directory.
592 [[Name "PSPP::errstr" used only once: possible typo at test.pl line 8.
596 AT_SETUP([Perl missing values])
597 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
598 PERL_GENERATE_SYSFILE
604 my $sf = PSPP::Reader->open ("sample.sav");
606 my $dict = $sf->get_dict ();
608 my (@c) = $sf->get_next_case ();
610 my $stringvar = $dict->get_var (0);
611 my $numericvar = $dict->get_var (2);
614 die "Missing Value Negative String"
615 if PSPP::value_is_missing ($val, $stringvar);
619 die "Missing Value Negative Num"
620 if PSPP::value_is_missing ($val, $numericvar);
622 @c = $sf->get_next_case ();
623 @c = $sf->get_next_case ();
626 die "Missing Value Positive"
627 if !PSPP::value_is_missing ($val, $stringvar);
629 @c = $sf->get_next_case ();
631 die "Missing Value Positive SYS"
632 if !PSPP::value_is_missing ($val, $numericvar);
634 @c = $sf->get_next_case ();
636 die "Missing Value Positive Num"
637 if !PSPP::value_is_missing ($val, $numericvar);
639 AT_CHECK([RUN_PERL_MODULE test.pl])
642 AT_SETUP([Perl custom attributes])
643 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
644 PERL_GENERATE_SYSFILE
650 my $sf = PSPP::Reader->open ("sample.sav");
652 my $dict = $sf->get_dict ();
654 my $var = $dict->get_var_by_name ("numeric");
656 my $attr = $var->get_attributes ();
658 foreach my $k (sort (keys (%$attr)))
660 my $ll = $attr->{$k};
662 print map "$_\n", join ', ', @$ll;
665 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
667 colour =>blue, pink, violet
668 nationality =>foreign
673 AT_SETUP([Perl Pspp.t])
674 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
675 # Skip this test if Perl's Text::Diff module is not installed.
676 AT_CHECK([perl -MText::Diff -e '' || exit 77])
677 AT_CHECK([RUN_PERL_MODULE $abs_top_builddir/perl-module/t/Pspp.t], [0],
680 ok 2 - Dictionary Creation
682 ok 4 - Trap illegal variable name
684 ok 6 - Accept legal variable name
686 ok 8 - Trap duplicate variable name
688 ok 10 - Accept valid format
690 ok 12 - Create sysfile object
691 ok 13 - Write system file
693 ok 15 - Appending Case with too many variables
695 ok 17 - Append Case 2
698 ok 20 - Dictionary Creation 2
699 ok 21 - Value label for short string
700 ok 22 - Value label for long string
701 ok 23 - Check output 2
702 ok 24 - Dictionary survives sysfile
703 ok 25 - Basic reader operation
704 ok 26 - Streaming of files
705 Formatted string is "11-SEP-2001 08:20"
706 ok 27 - format_value function
707 ok 28 - Perl representation of time
708 ok 29 - Returns undef on opening failure
709 ok 30 - Error string on open failure
710 ok 31 - Missing Value Negative String
711 ok 32 - Missing Value Negative Num
712 ok 33 - Missing Value Positive
713 ok 34 - Missing Value Positive SYS
714 ok 35 - Missing Value Positive Num
715 ok 36 - Custom Attributes