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