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], [dnl
86 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
87 legal,1,,Scale,Input,8,Right,F9.2,F9.2,
88 money,2,,Scale,Input,8,Right,DOLLAR6.2,DOLLAR6.2,
90 dump-dict.sps:5: note: SHOW: WEIGHT is money.
94 AT_SETUP([Perl writing cases to system files])
95 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
101 my $d = PSPP::Dict->new();
102 PSPP::Var->new ($d, "id",
110 PSPP::Var->new ($d, "name",
117 $d->set_documents ("This should not appear");
118 $d->clear_documents ();
119 $d->add_document ("This is a document line");
121 $d->set_label ("This is the file label");
123 # Check that we can write cases to system files.
124 my $sysfile = PSPP::Sysfile->new ("testfile.sav", $d);
125 my $res = $sysfile->append_case ( [34, "frederick"]);
126 die "append case" if !$res;
128 $res = $sysfile->append_case ( [34, "frederick", "extra"]);
129 die "append case with too many variables" if $res;
132 # Check that sysfiles are closed properly automaticallly in the destructor.
133 my $sysfile2 = PSPP::Sysfile->new ("testfile2.sav", $d);
134 $res = $sysfile2->append_case ( [21, "wheelbarrow"]);
135 die "append case 2" if !$res;
137 $res = $sysfile->append_case ( [34, "frederick", "extra"]);
138 die "append case with too many variables" if $res;
140 # Don't close. We want to test that the destructor does that.
142 AT_CHECK([RUN_PERL_MODULE test.pl])
143 AT_DATA([dump-dicts.sps],
144 [GET FILE='testfile.sav'.
150 GET FILE='testfile2.sav'.
156 AT_CHECK([pspp -O format=csv dump-dicts.sps], [0], [dnl
158 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
159 id,1,,Scale,Input,8,Right,F2.0,F2.0,
160 name,2,,Nominal,Input,20,Left,A20,A20,
163 Label,This is the file label
166 This is a document line
173 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
174 id,1,,Scale,Input,8,Right,F2.0,F2.0,
175 name,2,,Nominal,Input,20,Left,A20,A20,
178 Label,This is the file label
181 This is a document line
189 AT_SETUP([Perl write variable parameters])
190 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
196 my $dict = PSPP::Dict->new();
197 die "dictionary creation" if !ref $dict;
199 my $int = PSPP::Var->new ($dict, "integer",
200 (width=>8, decimals=>0) );
202 $int->set_label ("My Integer");
204 $int->add_value_label (99, "Silly");
205 $int->clear_value_labels ();
206 $int->add_value_label (0, "Zero");
207 $int->add_value_label (1, "Unity");
208 $int->add_value_label (2, "Duality");
210 my $str = PSPP::Var->new ($dict, "string",
211 (fmt=>PSPP::Fmt::A, width=>8) );
214 $str->set_label ("My String");
215 $str->add_value_label ("xx", "foo");
216 $str->add_value_label ("yy", "bar");
218 $str->set_missing_values ("this", "that");
220 my $longstr = PSPP::Var->new ($dict, "longstring",
221 (fmt=>PSPP::Fmt::A, width=>9) );
224 $longstr->set_label ("My Long String");
225 my $re = $longstr->add_value_label ("xxx", "xfoo");
227 $int->set_missing_values (9, 99);
229 my $sysfile = PSPP::Sysfile->new ("testfile.sav", $dict);
234 AT_CHECK([RUN_PERL_MODULE test.pl], [0], [], [stderr])
236 AT_DATA([dump-dict.sps],
237 [GET FILE='testfile.sav'.
240 AT_CHECK([pspp -O format=csv dump-dict.sps], [0], [dnl
242 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
243 integer,1,My Integer,Scale,Input,8,Right,F8.0,F8.0,9; 99
244 string,2,My String,Nominal,Input,8,Left,A8,A8,"""this ""; ""that """
245 longstring,3,My Long String,Nominal,Input,9,Left,A9,A9,
248 Variable Value,,Label
254 My Long String,xxx,xfoo
258 AT_SETUP([Perl dictionary survives system file])
259 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
268 my $d = PSPP::Dict->new();
270 PSPP::Var->new ($d, "id",
278 $sysfile = PSPP::Sysfile->new ("testfile.sav", $d);
281 my $res = $sysfile->append_case ([3]);
282 print "Dictionary survives sysfile\n" if $res;
284 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
285 [Dictionary survives sysfile
289 m4_define([PERL_GENERATE_SYSFILE],
290 [AT_DATA([sample.sps],
291 [[data list notable list /string (a8) longstring (a12) numeric (f10) date (date11) dollar (dollar8.2) datetime (datetime17)
293 1111 One 1 1/1/1 1 1/1/1+01:01
294 2222 Two 2 2/2/2 2 2/2/2+02:02
295 3333 Three 3 3/3/3 3 3/3/3+03:03
297 5555 Five 5 5/5/5 5 5/5/5+05:05
301 variable labels string 'A Short String Variable'
302 /longstring 'A Long String Variable'
303 /numeric 'A Numeric Variable'
304 /date 'A Date Variable'
305 /dollar 'A Dollar Variable'
306 /datetime 'A Datetime Variable'.
309 missing values numeric (9, 5, 999).
311 missing values string ("3333").
314 /string '1111' 'ones' '2222' 'twos' '3333' 'threes'
315 /numeric 1 'Unity' 2 'Duality' 3 'Thripality'.
319 attribute=colour[1]('blue') colour[2]('pink') colour[3]('violet')
320 attribute=size('large') nationality('foreign').
323 save outfile='sample.sav'.
325 AT_CHECK([pspp -O format=csv sample.sps])])
327 AT_SETUP([Perl read system file])
328 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
329 PERL_GENERATE_SYSFILE
335 my $sf = PSPP::Reader->open ("sample.sav");
337 my $dict = $sf->get_dict ();
339 for (my $v = 0 ; $v < $dict->get_var_cnt() ; $v++)
341 my $var = $dict->get_var ($v);
342 my $name = $var->get_name ();
343 my $label = $var->get_label ();
345 print "Variable $v is \"$name\", label is \"$label\"\n";
347 my $vl = $var->get_value_labels ();
349 print "Value Labels:\n";
350 print "$_ => $vl->{$_}\n" for sort (keys %$vl);
353 while (my @c = $sf->get_next_case () )
355 for (my $v = 0; $v < $dict->get_var_cnt(); $v++)
357 print "val$v: \"$c[$v]\"\n";
362 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
363 [Variable 0 is "string", label is "A Short String Variable"
368 Variable 1 is "longstring", label is "A Long String Variable"
370 Variable 2 is "numeric", label is "A Numeric Variable"
375 Variable 3 is "date", label is "A Date Variable"
377 Variable 4 is "dollar", label is "A Dollar Variable"
379 Variable 5 is "datetime", label is "A Datetime Variable"
419 AT_SETUP([Perl copying system files])
420 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
421 PERL_GENERATE_SYSFILE
427 my $input = PSPP::Reader->open ("sample.sav");
429 my $dict = $input->get_dict ();
431 my $output = PSPP::Sysfile->new ("copy.sav", $dict);
433 while (my (@c) = $input->get_next_case () )
435 $output->append_case (\@c);
440 AT_CHECK([RUN_PERL_MODULE test.pl])
441 AT_DATA([dump-dicts.sps],
442 [GET FILE='sample.sav'.
452 AT_CHECK([pspp -O format=csv dump-dicts.sps], [0],
454 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
455 string,1,A Short String Variable,Nominal,Input,8,Left,A8,A8,"""3333 """
456 longstring,2,A Long String Variable,Nominal,Input,12,Left,A12,A12,
457 numeric,3,A Numeric Variable,Scale,Input,8,Right,F10.0,F10.0,9; 5; 999
458 date,4,A Date Variable,Scale,Input,8,Right,DATE11,DATE11,
459 dollar,5,A Dollar Variable,Scale,Input,8,Right,DOLLAR11.2,DOLLAR11.2,
460 datetime,6,A Datetime Variable,Scale,Input,8,Right,DATETIME17.0,DATETIME17.0,
463 Variable Value,,Label
464 A Short String Variable,1111,ones
467 A Numeric Variable,1,Unity
474 Table: Variable and Dataset Attributes
475 Variable and Name,,Value
476 A Numeric Variable,colour[1],blue
483 string,longstring,numeric,date,dollar,datetime
484 1111,One,1,01-JAN-2001,$1.00,01-JAN-2001 01:01
485 2222,Two,2,02-FEB-2002,$2.00,02-FEB-2002 02:02
486 3333,Three,3,03-MAR-2003,$3.00,03-MAR-2003 03:03
488 5555,Five,5,05-MAY-2005,$5.00,05-MAY-2005 05:05
491 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
492 string,1,A Short String Variable,Nominal,Input,8,Left,A8,A8,"""3333 """
493 longstring,2,A Long String Variable,Nominal,Input,12,Left,A12,A12,
494 numeric,3,A Numeric Variable,Scale,Input,8,Right,F10.0,F10.0,9; 5; 999
495 date,4,A Date Variable,Scale,Input,8,Right,DATE11,DATE11,
496 dollar,5,A Dollar Variable,Scale,Input,8,Right,DOLLAR11.2,DOLLAR11.2,
497 datetime,6,A Datetime Variable,Scale,Input,8,Right,DATETIME17.0,DATETIME17.0,
500 Variable Value,,Label
501 A Short String Variable,1111,ones
504 A Numeric Variable,1,Unity
511 Table: Variable and Dataset Attributes
512 Variable and Name,,Value
513 A Numeric Variable,colour[1],blue
520 string,longstring,numeric,date,dollar,datetime
521 1111,One,1,01-JAN-2001,$1.00,01-JAN-2001 01:01
522 2222,Two,2,02-FEB-2002,$2.00,02-FEB-2002 02:02
523 3333,Three,3,03-MAR-2003,$3.00,03-MAR-2003 03:03
525 5555,Five,5,05-MAY-2005,$5.00,05-MAY-2005 05:05
529 AT_SETUP([Perl value formatting])
530 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
532 [DATA LIST LIST /d (DATETIME17).
537 SAVE OUTFILE='dd.sav'.
539 AT_CHECK([pspp -O format=csv dd.sps], [0],
540 [Table: Reading free-form data from INLINE.
549 my $sf = PSPP::Reader->open ("dd.sav");
551 my $dict = $sf->get_dict ();
553 my (@c) = $sf->get_next_case ();
555 my $var = $dict->get_var (0);
557 my $formatted = PSPP::format_value ($val, $var);
558 my $str = gmtime ($val - PSPP::PERL_EPOCH);
559 print "Formatted string is \"$formatted\"\n";
560 print "Perl representation is \"$str\"\n";
562 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
563 [[Formatted string is "11-SEP-2001 08:20"
564 Perl representation is "Tue Sep 11 08:20:00 2001"
568 AT_SETUP([Perl opening nonexistent file])
569 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
575 my $sf = PSPP::Reader->open ("no-such-file.sav");
577 die "Returns undef on opening failure" if ref $sf;
578 print $PSPP::errstr, "\n";
580 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
581 [[An error occurred while opening `no-such-file.sav': No such file or directory.
583 [[Name "PSPP::errstr" used only once: possible typo at test.pl line 8.
587 AT_SETUP([Perl missing values])
588 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
589 PERL_GENERATE_SYSFILE
595 my $sf = PSPP::Reader->open ("sample.sav");
597 my $dict = $sf->get_dict ();
599 my (@c) = $sf->get_next_case ();
601 my $stringvar = $dict->get_var (0);
602 my $numericvar = $dict->get_var (2);
605 die "Missing Value Negative String"
606 if PSPP::value_is_missing ($val, $stringvar);
610 die "Missing Value Negative Num"
611 if PSPP::value_is_missing ($val, $numericvar);
613 @c = $sf->get_next_case ();
614 @c = $sf->get_next_case ();
617 die "Missing Value Positive"
618 if !PSPP::value_is_missing ($val, $stringvar);
620 @c = $sf->get_next_case ();
622 die "Missing Value Positive SYS"
623 if !PSPP::value_is_missing ($val, $numericvar);
625 @c = $sf->get_next_case ();
627 die "Missing Value Positive Num"
628 if !PSPP::value_is_missing ($val, $numericvar);
630 AT_CHECK([RUN_PERL_MODULE test.pl])
633 AT_SETUP([Perl custom attributes])
634 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
635 PERL_GENERATE_SYSFILE
641 my $sf = PSPP::Reader->open ("sample.sav");
643 my $dict = $sf->get_dict ();
645 my $var = $dict->get_var_by_name ("numeric");
647 my $attr = $var->get_attributes ();
649 foreach my $k (sort (keys (%$attr)))
651 my $ll = $attr->{$k};
653 print map "$_\n", join ', ', @$ll;
656 AT_CHECK([RUN_PERL_MODULE test.pl], [0],
658 colour =>blue, pink, violet
659 nationality =>foreign
664 AT_SETUP([Perl Pspp.t])
665 AT_SKIP_IF([test "$WITH_PERL_MODULE" = no])
666 # Skip this test if Perl's Text::Diff module is not installed.
667 AT_CHECK([perl -MText::Diff -e '' || exit 77])
668 AT_CHECK([RUN_PERL_MODULE $abs_top_builddir/perl-module/t/Pspp.t], [0],
671 ok 2 - Dictionary Creation
673 ok 4 - Trap illegal variable name
675 ok 6 - Accept legal variable name
677 ok 8 - Trap duplicate variable name
679 ok 10 - Accept valid format
681 ok 12 - Create sysfile object
682 ok 13 - Write system file
684 ok 15 - Appending Case with too many variables
686 ok 17 - Append Case 2
689 ok 20 - Dictionary Creation 2
690 ok 21 - Value label for short string
691 ok 22 - Value label for long string
692 ok 23 - Check output 2
693 ok 24 - Dictionary survives sysfile
694 ok 25 - Basic reader operation
695 ok 26 - Streaming of files
696 Formatted string is "11-SEP-2001 08:20"
697 ok 27 - format_value function
698 ok 28 - Perl representation of time
699 ok 29 - Returns undef on opening failure
700 ok 30 - Error string on open failure
701 ok 31 - Missing Value Negative String
702 ok 32 - Missing Value Negative Num
703 ok 33 - Missing Value Positive
704 ok 34 - Missing Value Positive SYS
705 ok 35 - Missing Value Positive Num
706 ok 36 - Custom Attributes