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/>.
16 dnl AT_BANNER([Perl module tests])
18 dnl This command can be used to run with the PSPP Perl module after it has been
19 dnl built (with "make") but before it has been installed. The -I options are
20 dnl equivalent to "use ExtUtils::testlib;" inside the Perl program, but it does
21 dnl not need to be run with the perl-module build directory as the current
22 dnl working directory.
24 dnl XXX "libtool --mode=execute" is probably better than setting
26 m4_define([RUN_PERL_MODULE],
27 [LD_LIBRARY_PATH=$abs_top_builddir/src/.libs \
28 DYLD_LIBRARY_PATH=$abs_top_builddir/src/.libs \
29 $PERL -I$abs_top_builddir/perl-module/blib/arch \
30 -I$abs_top_builddir/perl-module/blib/lib])
32 AT_SETUP([Perl create system file])
33 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
39 my $d = PSPP::Dict->new();
40 die "dictionary creation" if !ref $d;
41 die if $d->get_var_cnt () != 0;
43 $d->set_label ("My Dictionary");
44 $d->set_documents ("These Documents");
46 # Tests for variable creation
48 my $var0 = PSPP::Var->new ($d, "le");
49 die "trap illegal variable name" if ref $var0;
50 die if $d->get_var_cnt () != 0;
52 $var0 = PSPP::Var->new ($d, "legal");
53 die "accept legal variable name" if !ref $var0;
54 die if $d->get_var_cnt () != 1;
56 my $var1 = PSPP::Var->new ($d, "money",
57 (fmt=>PSPP::Fmt::DOLLAR,
58 width=>4, decimals=>2) );
59 die "cappet valid format" if !ref $var1;
60 die if $d->get_var_cnt () != 2;
62 $d->set_weight ($var1);
64 my $sysfile = PSPP::Sysfile->new ('testfile.sav', $d);
65 die "create sysfile object" if !ref $sysfile;
69 AT_CHECK([RUN_PERL_MODULE test.pl])
70 AT_DATA([dump-dict.sps],
71 [GET FILE='testfile.sav'.
77 AT_CHECK([pspp -O format=csv dump-dict.sps], [0],
78 [File label: My Dictionary
80 Documents in the active dataset:
84 Variable,Description,Position
86 money,Format: DOLLAR6.2,2
88 dump-dict.sps:5: note: SHOW: WEIGHT is money.
92 AT_SETUP([Perl writing cases to system files])
93 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
99 my $d = PSPP::Dict->new();
100 PSPP::Var->new ($d, "id",
108 PSPP::Var->new ($d, "name",
115 $d->set_documents ("This should not appear");
116 $d->clear_documents ();
117 $d->add_document ("This is a document line");
119 $d->set_label ("This is the file label");
121 # Check that we can write cases to system files.
122 my $sysfile = PSPP::Sysfile->new ("testfile.sav", $d);
123 my $res = $sysfile->append_case ( [34, "frederick"]);
124 die "append case" if !$res;
126 $res = $sysfile->append_case ( [34, "frederick", "extra"]);
127 die "append case with too many variables" if $res;
130 # Check that sysfiles are closed properly automaticallly in the destructor.
131 my $sysfile2 = PSPP::Sysfile->new ("testfile2.sav", $d);
132 $res = $sysfile2->append_case ( [21, "wheelbarrow"]);
133 die "append case 2" if !$res;
135 $res = $sysfile->append_case ( [34, "frederick", "extra"]);
136 die "append case with too many variables" if $res;
138 # Don't close. We want to test that the destructor does that.
140 AT_CHECK([RUN_PERL_MODULE test.pl])
141 AT_DATA([dump-dicts.sps],
142 [GET FILE='testfile.sav'.
148 GET FILE='testfile2.sav'.
154 AT_CHECK([pspp -O format=csv dump-dicts.sps], [0],
155 [Variable,Description,Position
159 File label: This is the file label
161 Documents in the active dataset:
163 This is a document line
169 Variable,Description,Position
173 File label: This is the file label
175 Documents in the active dataset:
177 This is a document line
185 AT_SETUP([Perl write variable parameters])
186 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
192 my $dict = PSPP::Dict->new();
193 die "dictionary creation" if !ref $dict;
195 my $int = PSPP::Var->new ($dict, "integer",
196 (width=>8, decimals=>0) );
198 $int->set_label ("My Integer");
200 $int->add_value_label (99, "Silly");
201 $int->clear_value_labels ();
202 $int->add_value_label (0, "Zero");
203 $int->add_value_label (1, "Unity");
204 $int->add_value_label (2, "Duality");
206 my $str = PSPP::Var->new ($dict, "string",
207 (fmt=>PSPP::Fmt::A, width=>8) );
210 $str->set_label ("My String");
211 $str->add_value_label ("xx", "foo");
212 $str->add_value_label ("yy", "bar");
214 $str->set_missing_values ("this", "that");
216 my $longstr = PSPP::Var->new ($dict, "longstring",
217 (fmt=>PSPP::Fmt::A, width=>9) );
220 $longstr->set_label ("My Long String");
221 my $re = $longstr->add_value_label ("xxx", "xfoo");
223 $int->set_missing_values (9, 99);
225 my $sysfile = PSPP::Sysfile->new ("testfile.sav", $dict);
230 AT_CHECK([RUN_PERL_MODULE test.pl])
231 AT_DATA([dump-dict.sps],
232 [GET FILE='testfile.sav'.
235 AT_CHECK([pspp -O format=csv dump-dict.sps], [0],
236 [Variable,Description,Position
237 integer,"Label: My Integer
239 Missing Values: 9; 99
245 string,"Label: My String
247 Missing Values: ""this ""; ""that ""
252 longstring,"Label: My Long String
260 AT_SETUP([Perl dictionary survives system file])
261 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
270 my $d = PSPP::Dict->new();
272 PSPP::Var->new ($d, "id",
280 $sysfile = PSPP::Sysfile->new ("testfile.sav", $d);
283 my $res = $sysfile->append_case ([3]);
284 print "Dictionary survives sysfile\n" if $res;
286 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
287 [Dictionary survives sysfile
291 m4_define([PERL_GENERATE_SYSFILE],
292 [AT_DATA([sample.sps],
293 [[data list notable list /string (a8) longstring (a12) numeric (f10) date (date11) dollar (dollar8.2) datetime (datetime17)
295 1111 One 1 1/1/1 1 1/1/1+01:01
296 2222 Two 2 2/2/2 2 2/2/2+02:02
297 3333 Three 3 3/3/3 3 3/3/3+03:03
299 5555 Five 5 5/5/5 5 5/5/5+05:05
303 variable labels string 'A Short String Variable'
304 /longstring 'A Long String Variable'
305 /numeric 'A Numeric Variable'
306 /date 'A Date Variable'
307 /dollar 'A Dollar Variable'
308 /datetime 'A Datetime Variable'.
311 missing values numeric (9, 5, 999).
313 missing values string ("3333").
316 /string '1111' 'ones' '2222' 'twos' '3333' 'threes'
317 /numeric 1 'Unity' 2 'Duality' 3 'Thripality'.
321 attribute=colour[1]('blue') colour[2]('pink') colour[3]('violet')
322 attribute=size('large') nationality('foreign').
325 save outfile='sample.sav'.
327 AT_CHECK([pspp -O format=csv sample.sps])])
329 AT_SETUP([Perl read system file])
330 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
331 PERL_GENERATE_SYSFILE
337 my $sf = PSPP::Reader->open ("sample.sav");
339 my $dict = $sf->get_dict ();
341 for (my $v = 0 ; $v < $dict->get_var_cnt() ; $v++)
343 my $var = $dict->get_var ($v);
344 my $name = $var->get_name ();
345 my $label = $var->get_label ();
347 print "Variable $v is \"$name\", label is \"$label\"\n";
349 my $vl = $var->get_value_labels ();
351 print "Value Labels:\n";
352 print "$_ => $vl->{$_}\n" for sort (keys %$vl);
355 while (my @c = $sf->get_next_case () )
357 for (my $v = 0; $v < $dict->get_var_cnt(); $v++)
359 print "val$v: \"$c[$v]\"\n";
364 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
365 [Variable 0 is "string", label is "A Short String Variable"
370 Variable 1 is "longstring", label is "A Long String Variable"
372 Variable 2 is "numeric", label is "A Numeric Variable"
377 Variable 3 is "date", label is "A Date Variable"
379 Variable 4 is "dollar", label is "A Dollar Variable"
381 Variable 5 is "datetime", label is "A Datetime Variable"
421 AT_SETUP([Perl copying system files])
422 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
423 PERL_GENERATE_SYSFILE
429 my $input = PSPP::Reader->open ("sample.sav");
431 my $dict = $input->get_dict ();
433 my $output = PSPP::Sysfile->new ("copy.sav", $dict);
435 while (my (@c) = $input->get_next_case () )
437 $output->append_case (\@c);
442 AT_CHECK([RUN_PERL_MODULE test.pl])
443 AT_DATA([dump-dicts.sps],
444 [GET FILE='sample.sav'.
452 AT_CHECK([pspp -O format=csv dump-dicts.sps], [0],
453 [[Variable,Description,Position
454 string,"Label: A Short String Variable
456 Missing Values: ""3333 ""
462 longstring,"Label: A Long String Variable
464 numeric,"Label: A Numeric Variable
466 Missing Values: 9; 5; 999
479 date,"Label: A Date Variable
481 dollar,"Label: A Dollar Variable
482 Format: DOLLAR11.2",5
483 datetime,"Label: A Datetime Variable
484 Format: DATETIME17.0",6
487 string,longstring,numeric,date,dollar,datetime
488 1111 ,One ,1,01-JAN-2001,$1.00,01-JAN-2001 01:01
489 2222 ,Two ,2,02-FEB-2002,$2.00,02-FEB-2002 02:02
490 3333 ,Three ,3,03-MAR-2003,$3.00,03-MAR-2003 03:03
492 5555 ,Five ,5,05-MAY-2005,$5.00,05-MAY-2005 05:05
494 Variable,Description,Position
495 string,"Label: A Short String Variable
497 Missing Values: ""3333 ""
503 longstring,"Label: A Long String Variable
505 numeric,"Label: A Numeric Variable
507 Missing Values: 9; 5; 999
520 date,"Label: A Date Variable
522 dollar,"Label: A Dollar Variable
523 Format: DOLLAR11.2",5
524 datetime,"Label: A Datetime Variable
525 Format: DATETIME17.0",6
528 string,longstring,numeric,date,dollar,datetime
529 1111 ,One ,1,01-JAN-2001,$1.00,01-JAN-2001 01:01
530 2222 ,Two ,2,02-FEB-2002,$2.00,02-FEB-2002 02:02
531 3333 ,Three ,3,03-MAR-2003,$3.00,03-MAR-2003 03:03
533 5555 ,Five ,5,05-MAY-2005,$5.00,05-MAY-2005 05:05
537 AT_SETUP([Perl value formatting])
538 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
540 [DATA LIST LIST /d (DATETIME17).
545 SAVE OUTFILE='dd.sav'.
547 AT_CHECK([pspp -O format=csv dd.sps], [0],
548 [Table: Reading free-form data from INLINE.
557 my $sf = PSPP::Reader->open ("dd.sav");
559 my $dict = $sf->get_dict ();
561 my (@c) = $sf->get_next_case ();
563 my $var = $dict->get_var (0);
565 my $formatted = PSPP::format_value ($val, $var);
566 my $str = gmtime ($val - PSPP::PERL_EPOCH);
567 print "Formatted string is \"$formatted\"\n";
568 print "Perl representation is \"$str\"\n";
570 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
571 [[Formatted string is "11-SEP-2001 08:20"
572 Perl representation is "Tue Sep 11 08:20:00 2001"
576 AT_SETUP([Perl opening nonexistent file])
577 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
583 my $sf = PSPP::Reader->open ("no-such-file.sav");
585 die "Returns undef on opening failure" if ref $sf;
586 print $PSPP::errstr, "\n";
588 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
589 [[An error occurred while opening `no-such-file.sav': No such file or directory.
591 [[Name "PSPP::errstr" used only once: possible typo at test.pl line 8.
595 AT_SETUP([Perl missing values])
596 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
597 PERL_GENERATE_SYSFILE
603 my $sf = PSPP::Reader->open ("sample.sav");
605 my $dict = $sf->get_dict ();
607 my (@c) = $sf->get_next_case ();
609 my $stringvar = $dict->get_var (0);
610 my $numericvar = $dict->get_var (2);
613 die "Missing Value Negative String"
614 if PSPP::value_is_missing ($val, $stringvar);
618 die "Missing Value Negative Num"
619 if PSPP::value_is_missing ($val, $numericvar);
621 @c = $sf->get_next_case ();
622 @c = $sf->get_next_case ();
625 die "Missing Value Positive"
626 if !PSPP::value_is_missing ($val, $stringvar);
628 @c = $sf->get_next_case ();
630 die "Missing Value Positive SYS"
631 if !PSPP::value_is_missing ($val, $numericvar);
633 @c = $sf->get_next_case ();
635 die "Missing Value Positive Num"
636 if !PSPP::value_is_missing ($val, $numericvar);
638 AT_CHECK([RUN_PERL_MODULE test.pl])
641 AT_SETUP([Perl custom attributes])
642 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
643 PERL_GENERATE_SYSFILE
649 my $sf = PSPP::Reader->open ("sample.sav");
651 my $dict = $sf->get_dict ();
653 my $var = $dict->get_var_by_name ("numeric");
655 my $attr = $var->get_attributes ();
657 foreach my $k (sort (keys (%$attr)))
659 my $ll = $attr->{$k};
661 print map "$_\n", join ', ', @$ll;
664 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
666 colour =>blue, pink, violet
667 nationality =>foreign
672 AT_SETUP([Perl Pspp.t])
673 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
674 # Skip this test if Perl's Text::Diff module is not installed.
675 AT_CHECK([perl -MText::Diff -e '' || exit 77])
676 AT_CHECK([RUN_PERL_MODULE $abs_top_builddir/perl-module/t/Pspp.t], [0],
679 ok 2 - Dictionary Creation
681 ok 4 - Trap illegal variable name
683 ok 6 - Accept legal variable name
685 ok 8 - Trap duplicate variable name
687 ok 10 - Accept valid format
689 ok 12 - Create sysfile object
690 ok 13 - Write system file
692 ok 15 - Appending Case with too many variables
694 ok 17 - Append Case 2
697 ok 20 - Dictionary Creation 2
698 ok 21 - Value label for short string
699 ok 22 - Value label for long string
700 ok 23 - Check output 2
701 ok 24 - Dictionary survives sysfile
702 ok 25 - Basic reader operation
703 ok 26 - Streaming of files
704 Formatted string is "11-SEP-2001 08:20"
705 ok 27 - format_value function
706 ok 28 - Perl representation of time
707 ok 29 - Returns undef on opening failure
708 ok 30 - Error string on open failure
709 ok 31 - Missing Value Negative String
710 ok 32 - Missing Value Negative Num
711 ok 33 - Missing Value Positive
712 ok 34 - Missing Value Positive SYS
713 ok 35 - Missing Value Positive Num
714 ok 36 - Custom Attributes