File: Pspp.t

package info (click to toggle)
pspp 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,676 kB
  • sloc: ansic: 267,210; xml: 18,446; sh: 5,534; python: 2,881; makefile: 125; perl: 64
file content (625 lines) | stat: -rw-r--r-- 14,183 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
## -*-perl-*-

## PSPP - a program for statistical analysis.
## Copyright (C) 2019, 2020 Free Software Foundation, Inc.
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Before `make install' is performed this script should be runnable
# with `make test' as long as libpspp-core-$VERSION.so is in
# LD_LIBRARY_PATH.  After `make install' it should work as `perl
# PSPP.t'

#########################

# change 'tests => 1' to 'tests => last_test_to_print';

use Test::More tests => 37;
use Text::Diff;
use File::Temp qw/ tempfile tempdir /;
BEGIN { use_ok('PSPP') };

#########################

sub compare
{
    my $file = shift;
    my $pattern = shift;
    return ! diff ("$file", \$pattern);
}

my $pspp_cmd = $ENV{PSPP_TEST_CMD};

if ( ! $pspp_cmd)
{
    $pspp_cmd="pspp";
}

sub run_pspp_syntax
{
    my $tempdir = shift;
    my $syntax = shift;

    my $syntaxfile = "$tempdir/foo.sps";

    open (FH, ">$syntaxfile");
    print FH "$syntax";
    close (FH);

    system ("cd $tempdir; $pspp_cmd -o pspp.csv $syntaxfile");
}

sub run_pspp_syntax_cmp
{
    my $tempdir = shift;
    my $syntax = shift;

    my $result = shift;

    run_pspp_syntax ($tempdir, $syntax);

    my $diff =  diff ("$tempdir/pspp.csv", \$result);

    if ( ! ($diff eq ""))
    {
	diag ("$diff");
    }

    return ($diff eq "");
}


# Insert your test code below, the Test::More module is used here so read
# its man page ( perldoc Test::More ) for help writing this test script.

{
  my $d = PSPP::Dict->new();
  ok (ref $d, "Dictionary Creation");
  ok ($d->get_var_cnt () == 0);

  $d->set_label ("My Dictionary");
  $d->add_document ("These Documents");

  # Tests for variable creation

  my $var0 = PSPP::Var->new ($d, "le");
  ok (!ref $var0, "Trap illegal variable name");
  ok ($d->get_var_cnt () == 0);

  $var0 = PSPP::Var->new ($d, "legal");
  ok (ref $var0, "Accept legal variable name");
  ok ($d->get_var_cnt () == 1);

  my $var1 = PSPP::Var->new ($d, "legal");
  ok (!ref $var1, "Trap duplicate variable name");
  ok ($d->get_var_cnt () == 1);

  $var1 = PSPP::Var->new ($d, "money",
			  (fmt=>PSPP::Fmt::DOLLAR,
			   width=>4, decimals=>2) );
  ok (ref $var1, "Accept valid format");
  ok ($d->get_var_cnt () == 2);

  $d->set_weight ($var1);


  # Tests for system file creation
  # Make sure a system file can be created
  {
      my $tempdir = tempdir( CLEANUP => 1 );
      my $tempfile = "$tempdir/testfile.sav";
      my $syntaxfile = "$tempdir/syntax.sps";
      my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
      ok (ref $sysfile, "Create sysfile object");

      $sysfile->close ();
      ok (-s "$tempfile", "Write system file");
  }
}


# Make sure we can write cases to a file
{
  my $d = PSPP::Dict->new();
  PSPP::Var->new ($d, "id",
			 (
			  fmt=>PSPP::Fmt::F,
			  width=>2,
			  decimals=>0
			  )
			 );

  PSPP::Var->new ($d, "name",
			 (
			  fmt=>PSPP::Fmt::A,
			  width=>20,
			  )
			 );

  $d->add_document ("This should not appear");
  $d->clear_documents ();
  $d->add_document ("This is a document line");

  $d->set_label ("This is the file label");

  # Check that we can write system files
  {
      my $tempdir = tempdir( CLEANUP => 1 );
      my $tempfile = "$tempdir/testfile.sav";
      my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);

      my $res = $sysfile->append_case ( [34, "frederick"]);
      ok ($res, "Append Case");

      $res = $sysfile->append_case ( [34, "frederick", "extra"]);
      ok (!$res, "Appending Case with too many variables");

      $sysfile->close ();
      ok (-s  "$tempfile", "existance");
  }

  # Check that sysfiles are closed properly
  {
      my $tempdir = tempdir( CLEANUP => 1 );
      my $tempfile = "$tempdir/testfile.sav";
      {
	  my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);

	  my $res = $sysfile->append_case ( [21, "wheelbarrow"]);
	  ok ($res, "Append Case 2");

	  # Don't close.  We want to test that the destructor  does that
	  # automatically
      }
      ok (-s "$tempfile", "existance2");

    ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output");

        GET FILE='$tempfile'.
	DISPLAY DICTIONARY.
	DISPLAY FILE LABEL.
	DISPLAY DOCUMENTS.
	LIST.
SYNTAX
Table: Variables
Name,Position,Measurement Level,Role,Width,Alignment,Print Format,Write Format
id,1,Scale,Input,8,Right,F2.0,F2.0
name,2,Nominal,Input,20,Left,A20,A20

Table: File Label
Label,This is the file label

Table: Documents
This is a document line

Table: Data List
id,name
21,wheelbarrow
RESULT


  }

  # Now do some tests to make sure all the variable parameters
  # can be written properly.

  {
      my $tempdir = tempdir( CLEANUP => 1 );
      my $tempfile = "$tempdir/testfile.sav";
      my $dict = PSPP::Dict->new();
      ok (ref $dict, "Dictionary Creation 2");

      my $int = PSPP::Var->new ($dict, "integer",
				(width=>8, decimals=>0) );

      $int->set_label ("My Integer");

      $int->add_value_label (99, "Silly");
      $int->clear_value_labels ();
      $int->add_value_label (0, "Zero");
      $int->add_value_label (1, "Unity");
      $int->add_value_label (2, "Duality");

      my $str = PSPP::Var->new ($dict, "string",
				(fmt=>PSPP::Fmt::A, width=>8) );


      $str->set_label ("My String");
      ok ($str->add_value_label ("xx", "foo"), "Value label for short string");
      diag ($PSPP::errstr);
      $str->add_value_label ("yy", "bar");

      $str->set_missing_values ("this", "that");

      my $longstr = PSPP::Var->new ($dict, "longstring",
 				(fmt=>PSPP::Fmt::A, width=>9) );


      $longstr->set_label ("My Long String");
      my $re = $longstr->add_value_label ("xxx", "xfoo");
      ok ($re, "Value label for long string");

      $int->set_missing_values (9, 99);

      my $sysfile = PSPP::Sysfile->new ("$tempfile", $dict);


      $sysfile->close ();

      ok (run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT), "Check output 2");
GET FILE='$tempfile'.
DISPLAY DICTIONARY.
SYNTAX
Table: Variables
Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format,Missing Values
integer,1,My Integer,Scale,Input,8,Right,F8.0,F8.0,9; 99
string,2,My String,Nominal,Input,8,Left,A8,A8,"""this    ""; ""that    """
longstring,3,My Long String,Nominal,Input,9,Left,A9,A9,

Table: Value Labels
Variable Value,,Label
My Integer,0,Zero
,1,Unity
,2,Duality
My String,xx,foo
,yy,bar
My Long String,xxx,xfoo
RESULT

  }

}

sub generate_sav_file
{
    my $filename = shift;
    my $tempdir = shift;

    run_pspp_syntax_cmp ($tempdir, <<SYNTAX, <<RESULT);
data list notable list /string (a8) longstring (a12) numeric (f10) date (date11) dollar (dollar8.2) datetime (datetime17)
begin data.
1111 One   1 1/1/1 1   1/1/1+01:01
2222 Two   2 2/2/2 2   2/2/2+02:02
3333 Three 3 3/3/3 3   3/3/3+03:03
.    .     . .     .   .
5555 Five  5 5/5/5 5   5/5/5+05:05
end data.


variable labels string 'A Short String Variable'
  /longstring 'A Long String Variable'
  /numeric 'A Numeric Variable'
  /date 'A Date Variable'
  /dollar 'A Dollar Variable'
  /datetime 'A Datetime Variable'.


missing values numeric (9, 5, 999).

missing values string ("3333").

add value labels
  /string '1111' 'ones' '2222' 'twos' '3333' 'threes'
  /numeric 1 'Unity' 2 'Duality' 3 'Thripality'.

variable attribute
    variables = numeric
    attribute=colour[1]('blue') colour[2]('pink') colour[3]('violet')
    attribute=size('large') nationality('foreign').


save outfile='$filename'.
SYNTAX

RESULT

}


# Test to make sure that the dictionary survives the sysfile.
# Thanks to Rob Messer for reporting this problem
{
    my $tempdir = tempdir( CLEANUP => 1 );
    my $tempfile = "$tempdir/testfile.sav";
    my $sysfile ;

    {
	my $d = PSPP::Dict->new();

	PSPP::Var->new ($d, "id",
			(
			 fmt=>PSPP::Fmt::F,
			 width=>2,
			 decimals=>0
			 )
			);

	$sysfile = PSPP::Sysfile->new ("$tempfile", $d);
    }

    my $res = $sysfile->append_case ([3]);

    ok ($res, "Dictionary survives sysfile");
}


# Basic reader test
{
 my $tempdir = tempdir( CLEANUP => 1 );

 generate_sav_file ("$tempdir/in.sav", "$tempdir");

 my $sf = PSPP::Reader->open ("$tempdir/in.sav");

 my $dict = $sf->get_dict ();

 open (MYFILE, ">$tempdir/out.txt");
 for ($v = 0 ; $v < $dict->get_var_cnt() ; $v++)
 {
    my $var = $dict->get_var ($v);
    my $name = $var->get_name ();
    my $label = $var->get_label ();

    print MYFILE "Variable $v is \"$name\", label is \"$label\"\n";

    my $vl = $var->get_value_labels ();

    print MYFILE "Value Labels:\n";
    print MYFILE "$_ => $vl->{$_}\n" for (sort keys %$vl);
 }

 while (my @c = $sf->get_next_case () )
 {
    for ($v = 0; $v < $dict->get_var_cnt(); $v++)
    {
	print MYFILE "val$v: \"$c[$v]\"\n";
    }
    print MYFILE "\n";
 }

 close (MYFILE);

ok (compare ("$tempdir/out.txt", <<EOF), "Basic reader operation");
Variable 0 is "string", label is "A Short String Variable"
Value Labels:
1111     => ones
2222     => twos
3333     => threes
Variable 1 is "longstring", label is "A Long String Variable"
Value Labels:
Variable 2 is "numeric", label is "A Numeric Variable"
Value Labels:
1 => Unity
2 => Duality
3 => Thripality
Variable 3 is "date", label is "A Date Variable"
Value Labels:
Variable 4 is "dollar", label is "A Dollar Variable"
Value Labels:
Variable 5 is "datetime", label is "A Datetime Variable"
Value Labels:
val0: "1111    "
val1: "One         "
val2: "1"
val3: "13197686400"
val4: "1"
val5: "13197690060"

val0: "2222    "
val1: "Two         "
val2: "2"
val3: "13231987200"
val4: "2"
val5: "13231994520"

val0: "3333    "
val1: "Three       "
val2: "3"
val3: "13266028800"
val4: "3"
val5: "13266039780"

val0: ".       "
val1: ".           "
val2: ""
val3: ""
val4: ""
val5: ""

val0: "5555    "
val1: "Five        "
val2: "5"
val3: "13334630400"
val4: "5"
val5: "13334648700"

EOF

}


# Check that we can stream one file into another
{
 my $tempdir = tempdir( CLEANUP => 1 );

 generate_sav_file ("$tempdir/in.sav", "$tempdir");

 my $input = PSPP::Reader->open ("$tempdir/in.sav");

 my $dict = $input->get_dict ();

 my $output = PSPP::Sysfile->new ("$tempdir/out.sav", $dict);

 while (my (@c) = $input->get_next_case () )
 {
   $output->append_case (\@c);
 }

 $output->close ();


 #Check the two files are the same (except for metadata)

 run_pspp_syntax ($tempdir, <<SYNTAX);
 get file='$tempdir/in.sav'.
 display dictionary.
 list.

SYNTAX

 system ("cp $tempdir/pspp.csv $tempdir/in.txt");

 run_pspp_syntax ($tempdir, <<SYNTAX);
 get file='$tempdir/out.sav'.
 display dictionary.
 list.

SYNTAX

 ok (! diff ("$tempdir/pspp.csv", "$tempdir/in.txt"), "Streaming of files");
}



# Check that the format_value function works properly
{
 my $tempdir = tempdir( CLEANUP => 1 );

 run_pspp_syntax ($tempdir, <<SYNTAX);

data list list /d (datetime17).
begin data.
11/9/2001+08:20
end data.

save outfile='$tempdir/dd.sav'.

SYNTAX

 my $sf = PSPP::Reader->open ("$tempdir/dd.sav");

 my $dict = $sf->get_dict ();

 my (@c) = $sf->get_next_case ();

 my $var = $dict->get_var (0);
 my $val = $c[0];
 my $formatted = PSPP::format_value ($val, $var);
 my $str = gmtime ($val - PSPP::PERL_EPOCH);
 print "Formatted string is \"$formatted\"\n";
 ok ( $formatted eq "11-SEP-2001 08:20", "format_value function");
 ok ( $str eq "Tue Sep 11 08:20:00 2001", "Perl representation of time");
}


# Check that attempting to open a non-existent file results in an error
{
  my $tempdir = tempdir( CLEANUP => 1 );

  unlink ("$tempdir/no-such-file.sav");

  my $sf = PSPP::Reader->open ("$tempdir/no-such-file.sav");

  ok ( !ref $sf, "Returns undef on opening failure");

  ok ("$PSPP::errstr" eq "An error occurred while opening `$tempdir/no-such-file.sav': No such file or directory.",
      "Error string on open failure");
}


# Missing value tests.
{
 my $tempdir = tempdir( CLEANUP => 1 );

 generate_sav_file ("$tempdir/in.sav", "$tempdir");

 my $sf = PSPP::Reader->open ("$tempdir/in.sav");

 my $dict = $sf->get_dict ();


 my (@c) = $sf->get_next_case ();

 my $stringvar = $dict->get_var (0);
 my $numericvar = $dict->get_var (2);
 my $val = $c[0];

 ok ( !PSPP::value_is_missing ($val, $stringvar), "Missing Value Negative String");

 $val = $c[2];

 ok ( !PSPP::value_is_missing ($val, $numericvar), "Missing Value Negative Num");

 @c = $sf->get_next_case ();
 @c = $sf->get_next_case ();

 $val = $c[0];
 ok ( PSPP::value_is_missing ($val, $stringvar), "Missing Value Positive");

 @c = $sf->get_next_case ();
 $val = $c[2];
 ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive SYS");

 @c = $sf->get_next_case ();
 $val = $c[2];
 ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive Num");
}


#Test reading of custom attributes
{
    my $tempdir = tempdir( CLEANUP => 1 );

    generate_sav_file ("$tempdir/in.sav", "$tempdir");

    my $sf = PSPP::Reader->open ("$tempdir/in.sav");

    my $dict = $sf->get_dict ();

    my $var = $dict->get_var_by_name ("numeric");

    my $attr = $var->get_attributes ();

    open (MYFILE, ">$tempdir/out.txt");

    foreach $k (sort (keys (%$attr)))
    {
	my $ll = $attr->{$k};
	print MYFILE "$k =>";
	print MYFILE map "$_\n", join ', ', @$ll;
    }

    close (MYFILE);

    ok (compare ("$tempdir/out.txt", <<'EOF'), "Custom Attributes");
$@Role =>0
colour =>blue, pink, violet
nationality =>foreign
size =>large
EOF
}


# Test of the get_case_cnt function
{
 my $tempdir = tempdir( CLEANUP => 1 );

 generate_sav_file ("$tempdir/in.sav", "$tempdir");

 my $sf = PSPP::Reader->open ("$tempdir/in.sav");

 my $n = $sf->get_case_cnt ();

 ok ($n == 5, "Case count");
}