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