SAMPLE: Convert test to Autotest framework.
[pspp-builds.git] / tests / command / sort.sh
1 #!/bin/sh
2
3 # This program tests the sort command
4
5 TEMPDIR=/tmp/pspp-tst-$$
6 TESTFILE=$TEMPDIR/`basename $0`.sps
7 : ${PERL:=perl}
8
9 # ensure that top_builddir  are absolute
10 if [ -z "$top_builddir" ] ; then top_builddir=. ; fi
11 if [ -z "$top_srcdir" ] ; then top_srcdir=. ; fi
12 top_builddir=`cd $top_builddir; pwd`
13 PSPP=$top_builddir/src/ui/terminal/pspp$EXEEXT
14
15 # ensure that top_srcdir is absolute
16 top_srcdir=`cd $top_srcdir; pwd`
17
18 STAT_CONFIG_PATH=$top_srcdir/config
19 export STAT_CONFIG_PATH
20
21
22 cleanup()
23 {
24      if [ x"$PSPP_TEST_NO_CLEANUP" != x ] ; then 
25         echo "NOT cleaning $TEMPDIR" 
26         return ; 
27      fi
28      cd /
29      rm -rf $TEMPDIR
30 }
31
32
33 fail()
34 {
35     echo $activity
36     echo FAILED
37     cleanup;
38     exit 1;
39 }
40
41
42 no_result()
43 {
44     echo $activity
45     echo NO RESULT;
46     cleanup;
47     exit 2;
48 }
49
50 pass()
51 {
52     cleanup;
53     exit 0;
54 }
55
56 mkdir -p $TEMPDIR
57
58 cd $TEMPDIR
59
60 activity="write perl program for generating data"
61 cat > gen-data.pl <<'EOF'
62 use strict;
63 use warnings;
64
65 # Generate shuffled data.
66 my (@data);
67 for my $i (0...$ARGV[0] - 1) {
68     push (@data, $i) foreach 1...$ARGV[1];
69 }
70 fisher_yates_shuffle (\@data);
71
72 # Output shuffled data.
73 my (@shuffled) = map ([$data[$_], $_], 0...$#data);
74 open (SHUFFLED, ">sort.in");
75 print SHUFFLED "$data[$_] $_\n" foreach 0...$#data;
76
77 # Output sorted data.
78 my (@sorted) = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @shuffled;
79 open (SORTED, ">sort.exp");
80 print SORTED "$_->[0] $_->[1]\n" foreach @sorted;
81
82 # From perlfaq4.
83 sub fisher_yates_shuffle {
84     my $deck = shift;  # $deck is a reference to an array
85     my $i = @$deck;
86     while ($i--) {
87         my $j = int rand ($i+1);
88         @$deck[$i,$j] = @$deck[$j,$i];
89     }
90 }
91 EOF
92
93 for count_repeat_buffers in \
94     "100 5 2" "100 5 3" "100 5 4" "100 5 5" "100 5 10" "100 5 50" "100 5 100" "100 5" \
95     "100 10 2" "100 10 3" "100 10 5" "100 10" \
96     "1000 5 5" "1000 5 50" "1000 5" \
97     "100 100 3" "100 100 5" "100 100" \
98     "10000 5 500" \
99     "50000 1"; do
100   set $count_repeat_buffers
101   count=$1
102   repeat=$2
103   buffers=$3
104
105   printf .
106
107   activity="generate data for $count_repeat_buffers run"
108   $PERL gen-data.pl $count $repeat > sort.data
109   if [ $? -ne 0 ] ; then no_result ; fi
110   
111   activity="generate test program for $count_repeat_buffers run"
112   {
113       echo "data list list file='sort.in'/x y (f8)."
114       if test "$buffers" != ""; then
115           echo "sort by x/buffers=$buffers."
116       else
117           echo "sort by x."
118       fi
119       echo "print outfile='sort.out'/x y."
120       echo "execute."
121   } > sort.pspp || no_result
122   
123   activity="run program"
124   $SUPERVISOR $PSPP --testing-mode -o pspp.csv sort.pspp
125   if [ $? -ne 0 ] ; then no_result ; fi
126   
127   perl -pi -e 's/^\s*$//g' sort.exp sort.out
128   diff -w sort.exp sort.out
129   if [ $? -ne 0 ] ; then fail ; fi
130 done
131 echo
132 pass;