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