Add copyright and licence notices to files which lack them.
[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 AT_BANNER([SORT CASES])
17
18 m4_divert_push([PREPARE_TESTS])
19 [sort_cases_gen_data () {
20   cat > gen-data.pl <<'EOF'
21 use strict;
22 use warnings;
23
24 # Generate shuffled data.
25 my (@data);
26 for my $i (0...$ARGV[0] - 1) {
27     push (@data, ($i) x $ARGV[1]);
28 }
29 fisher_yates_shuffle (\@data);
30
31 # Output shuffled data.
32 my (@shuffled) = map ([$data[$_], $_], 0...$#data);
33 open (SHUFFLED, ">data.txt");
34 print SHUFFLED "$data[$_] $_\n" foreach 0...$#data;
35
36 # Output sorted data.
37 my (@sorted) = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @shuffled;
38 open (SORTED, ">expout");
39 printf SORTED " %8d %8d \n", $_->[0], $_->[1] foreach @sorted;
40
41 # From perlfaq4.
42 sub fisher_yates_shuffle {
43     my $deck = shift;  # $deck is a reference to an array
44     my $i = @$deck;
45     while ($i--) {
46         my $j = int rand ($i+1);
47         @$deck[$i,$j] = @$deck[$j,$i];
48     }
49 }
50 EOF
51   $PERL gen-data.pl "$@"]
52 }
53 m4_divert_pop([PREPARE_TESTS])
54
55 m4_define([SORT_CASES_TEST], 
56   [AT_SETUP([sort m4_eval([$1 * $2]) cases[]m4_if([$2], [1], [], [ ($1 unique)])[]m4_if([$3], [], [], [ with $3 buffers])])
57    AT_KEYWORDS([SORT CASES])
58    AT_CHECK([sort_cases_gen_data $1 $2 $3])
59    AT_CAPTURE_FILE([data.txt])
60    AT_CAPTURE_FILE([output.txt])
61    AT_CAPTURE_FILE([sort-cases.sps])
62    AT_DATA([sort-cases.sps], [dnl
63 DATA LIST LIST NOTABLE FILE='data.txt'/x y (F8).
64 SORT CASES BY x[]m4_if([$3], [], [], [/BUFFERS=$3]).
65 PRINT OUTFILE='output.txt'/x y.
66 EXECUTE.
67 ])
68    AT_CHECK([pspp --testing-mode -o pspp.csv sort-cases.sps])
69    AT_CHECK([cat output.txt], [0], [expout])
70    AT_CLEANUP])
71
72 SORT_CASES_TEST(100, 5, 2)
73 SORT_CASES_TEST(100, 5, 3)
74 SORT_CASES_TEST(100, 5, 4)
75 SORT_CASES_TEST(100, 5, 5)
76 SORT_CASES_TEST(100, 5, 10)
77 SORT_CASES_TEST(100, 5, 50)
78 SORT_CASES_TEST(100, 5, 100)
79 SORT_CASES_TEST(100, 5)
80
81 SORT_CASES_TEST(100, 10, 2)
82 SORT_CASES_TEST(100, 10, 3)
83 SORT_CASES_TEST(100, 10, 5)
84 SORT_CASES_TEST(100, 10)
85
86 SORT_CASES_TEST(1000, 5, 5)
87 SORT_CASES_TEST(1000, 5, 50)
88 SORT_CASES_TEST(1000, 5)
89
90 SORT_CASES_TEST(100, 100, 3)
91 SORT_CASES_TEST(100, 100, 5)
92 SORT_CASES_TEST(100, 100)
93
94 SORT_CASES_TEST(10000, 5, 500)
95
96 SORT_CASES_TEST(50000, 1)
97
98 dnl Bug #33089 caused SORT CASES to delete filtered cases permanently.
99 AT_SETUP([SORT CASES preserves filtered cases])
100 AT_DATA([sort-cases.sps], [dnl
101 DATA LIST FREE /x.
102 BEGIN DATA.
103 5 4 3 2 1 0
104 END DATA.
105 COMPUTE mod2 = MOD(x, 2).
106 LIST.
107 FILTER BY mod2.
108 LIST.
109 SORT CASES BY x.
110 LIST.
111 FILTER OFF.
112 LIST.
113 ])
114 AT_CHECK([pspp -O format=csv sort-cases.sps], [0], [dnl
115 Table: Data List
116 x,mod2
117 5.00,1.00
118 4.00,.00
119 3.00,1.00
120 2.00,.00
121 1.00,1.00
122 .00,.00
123
124 Table: Data List
125 x,mod2
126 5.00,1.00
127 3.00,1.00
128 1.00,1.00
129
130 Table: Data List
131 x,mod2
132 1.00,1.00
133 3.00,1.00
134 5.00,1.00
135
136 Table: Data List
137 x,mod2
138 .00,.00
139 1.00,1.00
140 2.00,.00
141 3.00,1.00
142 4.00,.00
143 5.00,1.00
144 ])
145 AT_CLEANUP