5217e64d3f7efad3a367bc5c2240a63ff2fff86d
[pspp] / tests / language / stats / sort-cases.at
1 dnl PSPP - a program for statistical analysis.
2 dnl Copyright (C) 2017 Free Software Foundation, Inc.
3 dnl
4 dnl This program is free software: you can redistribute it and/or modify
5 dnl it under the terms of the GNU General Public License as published by
6 dnl the Free Software Foundation, either version 3 of the License, or
7 dnl (at your option) any later version.
8 dnl
9 dnl This program is distributed in the hope that it will be useful,
10 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 dnl GNU General Public License for more details.
13 dnl
14 dnl You should have received a copy of the GNU General Public License
15 dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 dnl
17 AT_BANNER([SORT CASES])
18
19 m4_divert_push([PREPARE_TESTS])
20 [sort_cases_gen_data () {
21   cat > gen-data.pl <<'EOF'
22 use strict;
23 use warnings;
24
25 # Generate shuffled data.
26 my (@data);
27 for my $i (0...$ARGV[0] - 1) {
28     push (@data, ($i) x $ARGV[1]);
29 }
30 fisher_yates_shuffle (\@data);
31
32 # Output shuffled data.
33 my (@shuffled) = map ([$data[$_], $_], 0...$#data);
34 open (SHUFFLED, ">data.txt");
35 print SHUFFLED "$data[$_] $_\n" foreach 0...$#data;
36
37 # Output sorted data.
38 my (@sorted) = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @shuffled;
39 open (SORTED, ">expout");
40 printf SORTED " %8d %8d \n", $_->[0], $_->[1] foreach @sorted;
41
42 # From perlfaq4.
43 sub fisher_yates_shuffle {
44     my $deck = shift;  # $deck is a reference to an array
45     my $i = @$deck;
46     while ($i--) {
47         my $j = int rand ($i+1);
48         @$deck[$i,$j] = @$deck[$j,$i];
49     }
50 }
51 EOF
52   $PERL gen-data.pl "$@"]
53 }
54 m4_divert_pop([PREPARE_TESTS])
55
56 m4_define([SORT_CASES_TEST],
57   [AT_SETUP([sort m4_eval([$1 * $2]) cases[]m4_if([$2], [1], [], [ ($1 unique)])[]m4_if([$3], [], [], [ with $3 buffers])])
58    AT_KEYWORDS([SORT CASES $4])
59    AT_CHECK([sort_cases_gen_data $1 $2 $3])
60    AT_CAPTURE_FILE([data.txt])
61    AT_CAPTURE_FILE([output.txt])
62    AT_CAPTURE_FILE([sort-cases.sps])
63    AT_DATA([sort-cases.sps], [dnl
64 DATA LIST LIST NOTABLE FILE='data.txt'/x y (F8).
65 SORT CASES BY x[]m4_if([$3], [], [], [/BUFFERS=$3]).
66 PRINT OUTFILE='output.txt'/x y.
67 EXECUTE.
68 ])
69    AT_CHECK([pspp --testing-mode -o pspp.csv sort-cases.sps])
70    AT_CHECK([cat output.txt], [0], [expout])
71    AT_CLEANUP])
72
73 SORT_CASES_TEST(100, 5, 2)
74 SORT_CASES_TEST(100, 5, 3)
75 SORT_CASES_TEST(100, 5, 4)
76 SORT_CASES_TEST(100, 5, 5)
77 SORT_CASES_TEST(100, 5, 10)
78 SORT_CASES_TEST(100, 5, 50)
79 SORT_CASES_TEST(100, 5, 100)
80 SORT_CASES_TEST(100, 5)
81
82 SORT_CASES_TEST(100, 10, 2)
83 SORT_CASES_TEST(100, 10, 3)
84 SORT_CASES_TEST(100, 10, 5)
85 SORT_CASES_TEST(100, 10)
86
87 SORT_CASES_TEST(1000, 5, 5, slow)
88 SORT_CASES_TEST(1000, 5, 50, slow)
89 SORT_CASES_TEST(1000, 5, [], slow)
90
91 SORT_CASES_TEST(100, 100, 3, slow)
92 SORT_CASES_TEST(100, 100, 5, slow)
93 SORT_CASES_TEST(100, 100, [], slow)
94
95 SORT_CASES_TEST(10000, 5, 500, slow)
96
97 SORT_CASES_TEST(50000, 1, [], slow)
98
99 dnl Bug #33089 caused SORT CASES to delete filtered cases permanently.
100 AT_SETUP([SORT CASES preserves filtered cases])
101 AT_DATA([sort-cases.sps], [dnl
102 DATA LIST FREE /x.
103 BEGIN DATA.
104 5 4 3 2 1 0
105 END DATA.
106 COMPUTE mod2 = MOD(x, 2).
107 LIST.
108 FILTER BY mod2.
109 LIST.
110 SORT CASES BY x.
111 LIST.
112 FILTER OFF.
113 LIST.
114 ])
115 AT_CHECK([pspp -O format=csv sort-cases.sps], [0], [dnl
116 Table: Data List
117 x,mod2
118 5.00,1.00
119 4.00,.00
120 3.00,1.00
121 2.00,.00
122 1.00,1.00
123 .00,.00
124
125 Table: Data List
126 x,mod2
127 5.00,1.00
128 3.00,1.00
129 1.00,1.00
130
131 Table: Data List
132 x,mod2
133 1.00,1.00
134 3.00,1.00
135 5.00,1.00
136
137 Table: Data List
138 x,mod2
139 .00,.00
140 1.00,1.00
141 2.00,.00
142 3.00,1.00
143 4.00,.00
144 5.00,1.00
145 ])
146 AT_CLEANUP