From 4bbc18decb6c26a9058d88fa0d4116a5730547b9 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Fri, 1 Jul 2011 17:04:50 +0200 Subject: [PATCH] Perl Module - Add function to count cases in a reader. Closes bug #33381 --- perl-module/PSPP.xs | 15 +++++++++++++++ perl-module/lib/PSPP.pm.in | 7 +++++++ perl-module/t/Pspp.t | 15 ++++++++++++++- tests/perl-module.at | 15 +++------------ 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/perl-module/PSPP.xs b/perl-module/PSPP.xs index 143eb549e1..77b7682b3e 100644 --- a/perl-module/PSPP.xs +++ b/perl-module/PSPP.xs @@ -727,6 +727,21 @@ CODE: OUTPUT: RETVAL +SV * +get_case_cnt (sfr) + struct sysreader_info *sfr; +CODE: + SV *ret; + casenumber n = casereader_get_case_cnt (sfr->reader); + if (n == CASENUMBER_MAX) + ret = &PL_sv_undef; + else + ret = newSViv (n); + RETVAL = ret; + OUTPUT: +RETVAL + + void get_next_case (sfr) diff --git a/perl-module/lib/PSPP.pm.in b/perl-module/lib/PSPP.pm.in index a99c77db27..7d077992bd 100644 --- a/perl-module/lib/PSPP.pm.in +++ b/perl-module/lib/PSPP.pm.in @@ -483,6 +483,13 @@ Open is used to read data from an existing system file. It creates and returns a PSPP::Reader object which can be used to read data and dictionary information from C. +=head3 get_case_cnt () + +Returns the number of cases in a open system file. Some files +do not store the number of cases. In these instances undef +will be returned. Therefore, then programmer must check that the +returned value is not undef before using it. + =cut sub get_dict diff --git a/perl-module/t/Pspp.t b/perl-module/t/Pspp.t index 6e267f6fb8..a7fb9578da 100644 --- a/perl-module/t/Pspp.t +++ b/perl-module/t/Pspp.t @@ -8,7 +8,7 @@ # change 'tests => 1' to 'tests => last_test_to_print'; -use Test::More tests => 36; +use Test::More tests => 37; use Text::Diff; use File::Temp qw/ tempfile tempdir /; BEGIN { use_ok('PSPP') }; @@ -605,5 +605,18 @@ 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"); } diff --git a/tests/perl-module.at b/tests/perl-module.at index 308dd7711b..15ed6c2dbd 100644 --- a/tests/perl-module.at +++ b/tests/perl-module.at @@ -701,7 +701,7 @@ AT_SKIP_IF([test "$WITH_PERL_MODULE" = no]) # Skip this test if Perl's Text::Diff module is not installed. AT_CHECK([perl -MText::Diff -e '' || exit 77]) AT_CHECK([RUN_PERL_MODULE $abs_top_builddir/perl-module/t/Pspp.t], [0], - [[1..36 + [[1..37 ok 1 - use PSPP; ok 2 - Dictionary Creation ok 3 @@ -739,15 +739,6 @@ ok 33 - Missing Value Positive ok 34 - Missing Value Positive SYS ok 35 - Missing Value Positive Num ok 36 - Custom Attributes -]], - [[# @&t@ -# @@ -0,0 +1 @@ -# + -# @@ -0,0 +1 @@ -# + -# @@ -0,0 +1 @@ -# + -# @@ -0,0 +1 @@ -# + -]]) +ok 37 - Case count +]],[ignore]) AT_CLEANUP -- 2.30.2