Implemented the RANK command.
[pspp-builds.git] / tests / command / rank.sh
1 #!/bin/sh
2
3 # This program tests the RANK command.
4
5 TEMPDIR=/tmp/pspp-tst-$$
6 TESTFILE=$TEMPDIR/`basename $0`.sps
7
8 # ensure that top_srcdir and top_builddir  are absolute
9 if [ -z "$top_srcdir" ] ; then top_srcdir=. ; fi
10 if [ -z "$top_builddir" ] ; then top_builddir=. ; fi
11 top_srcdir=`cd $top_srcdir; pwd`
12 top_builddir=`cd $top_builddir; pwd`
13
14 PSPP=$top_builddir/src/ui/terminal/pspp
15
16 STAT_CONFIG_PATH=$top_srcdir/config
17 export STAT_CONFIG_PATH
18
19 LANG=C
20 export LANG
21
22
23 cleanup()
24 {
25      if [ x"$PSPP_TEST_NO_CLEANUP" != x ] ; then 
26         echo "NOT cleaning $TEMPDIR"
27         return ; 
28      fi
29      rm -rf $TEMPDIR
30 }
31
32
33 fail()
34 {
35     echo $activity
36     echo FAILED
37     cleanup;
38     exit 1;
39 }
40
41
42 no_result()
43 {
44     echo $activity
45     echo NO RESULT;
46     cleanup;
47     exit 2;
48 }
49
50 pass()
51 {
52     cleanup;
53     exit 0;
54 }
55
56 mkdir -p $TEMPDIR
57
58 cd $TEMPDIR
59
60
61 # Some tests for proper behaviour in the face of invalid input.
62 activity="create file 1"
63 cat > $TESTFILE <<EOF
64 DATA LIST LIST NOTABLE /x * a (a2).
65 BEGIN DATA.
66 -1 s
67 0  s
68 1  s
69 2  s
70 2  s
71 4  s
72 5  s
73 END DATA.
74
75 DEBUG XFORM FAIL.
76
77 RANK x.
78
79 EOF
80 if [ $? -ne 0 ] ; then no_result ; fi
81
82 # Check that it properly handles failed transformations.
83 activity="run program 1"
84 $SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE > $TEMPDIR/err
85 if [ $? -ne 1 ] ; then fail ; fi
86
87 activity="diff 1"
88 perl -pi -e 's/^\s*$//g' $TEMPDIR/err
89 diff  -b $TEMPDIR/err - <<EOF
90 $TEMPDIR/rank.sh.sps:14: error: Stopping syntax file processing here to avoid a cascade of dependent command failures.
91 EOF
92 if [ $? -ne 0 ] ; then fail ; fi
93
94
95 #Now for some general error conditions.
96 activity="create file 2"
97 cat > $TESTFILE <<EOF
98 DATA LIST LIST NOTABLE /x * a (a2).
99 BEGIN DATA.
100 -1 s
101 0  s
102 1  s
103 2  s
104 2  s
105 4  s
106 5  s
107 END DATA.
108
109 * invalid NTILES (no parameter)
110 RANK x 
111   /NTILES
112 .
113
114 * invalid NTILES (not an integer)
115 RANK x 
116   /NTILES(d)
117 .
118
119
120 * destination variable already exists
121 RANK x 
122  /RANK INTO x.
123
124
125 * Too many variables in INTO
126 RANK x 
127  /RANK INTO foo  bar wiz.
128
129 * Alpha group
130 RANK x BY a.
131
132
133 EOF
134 if [ $? -ne 0 ] ; then no_result ; fi
135
136
137 activity="run program (syntax errors)"
138 $SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE > $TEMPDIR/errs
139 if [ $? -ne 1 ] ; then fail ; fi
140
141 activity="compare errors"
142 perl -pi -e 's/^\s*$//g' $TEMPDIR/errs
143 diff  -b $TEMPDIR/errs - << EOF
144 $TEMPDIR/rank.sh.sps:15: error: RANK: Syntax error expecting \`(' at end of command.
145 $TEMPDIR/rank.sh.sps:19: error: RANK: Syntax error expecting integer at \`d'.
146 $TEMPDIR/rank.sh.sps:25: error: RANK: Variable x already exists.
147 $TEMPDIR/rank.sh.sps:30: error: RANK: Too many variables in INTO clause.
148 $TEMPDIR/rank.sh.sps:33: warning: RANK: a is not a numeric variable.  It will not be included in the variable list.
149 EOF
150 if [ $? -ne 0 ] ; then fail ; fi
151
152 # Now some real tests.
153
154 activity="create file 3"
155 cat > $TESTFILE <<EOF
156 DATA LIST LIST NOTABLE /x (f8).
157 BEGIN DATA.
158 -1
159
160
161
162
163
164
165 END DATA.
166
167 ECHO  'Simple example using defaults'.
168
169 RANK x.
170
171 LIST.
172
173 NEW FILE.
174 DATA LIST LIST NOTABLE /a * b *.
175 BEGIN DATA.
176 0 24
177 1 32
178 2 31
179 2 32
180 4 30
181 5 29
182 6 1
183 7 43
184 8 .
185 9 45
186 END DATA.
187
188 RANK a b (D)
189    /PRINT=YES 
190    /RANK
191    /TIES=HIGH
192    /RFRACTION
193    /N INTO count
194    .
195
196 DISPLAY DICTIONARY.
197
198 LIST.
199
200
201 ECHO  'Test variable name fallback'.
202
203 NEW FILE.
204 DATA LIST LIST NOTABLE /foo * rfoo * ran003 *.
205 BEGIN DATA.
206 0 3 2
207 1 3 2
208 2 3 2
209 2 3 2
210 4 3 2
211 5 3 2
212 6 3 2
213 7 3 2
214 8 3 2
215 9 3 2
216 END DATA.
217
218 RANK foo.
219
220
221 DISPLAY DICTIONARY.
222
223
224 NEW FILE.
225 DATA LIST LIST NOTABLE /a * b *.
226 BEGIN DATA.
227 0 24
228 1 32
229 2 31
230 2 32
231 4 30
232 5 29
233 6 1
234 7 43
235 8 8 
236 9 45
237 END DATA.
238
239 RANK a
240   /PRINT=YES
241   /TIES=CONDENSE
242   /SAVAGE
243   /PERCENT
244   /PROPORTION
245   /NTILES(4)
246   /NORMAL
247 .
248
249 LIST.
250
251 NEW FILE.
252 DATA LIST LIST NOTABLE /a * g1 g2 *.
253 BEGIN DATA.
254 1 0 2
255 2 0 2
256 3 0 2
257 4 0 2
258 5 0 2
259 6 0 2
260 7 0 2
261 8 0 2
262 2 1 2
263 2 1 2
264 3 1 2
265 4 1 2
266 5 1 2
267 6 1 2
268 7 1 2
269 7 1 2
270 8 1 2
271 9 1 1
272 END DATA.
273
274 RANK a (D) BY g2 g1
275   /PRINT=YES
276   /TIES=LOW
277   /MISSING=INCLUDE
278   /FRACTION=RANKIT
279   /RANK
280   /NORMAL
281   .
282
283 LIST.
284
285
286 ECHO 'fractional ranks ( including small ones for special case of SAVAGE ranks)'
287 NEW FILE.
288 DATA LIST LIST NOTABLE  /a *  w * .
289 BEGIN DATA.
290 1 1.5
291 2 0.2
292 3 0.1
293 4 1
294 5 1
295 6 1
296 7 1
297 8 1
298 END DATA.
299
300 WEIGHT BY w.
301
302 RANK a 
303   /FRACTION=TUKEY
304   /PROPORTION
305   /SAVAGE
306 .
307
308 LIST.
309
310
311 ECHO 'test all the ties cases with low caseweight values'.
312
313 NEW FILE.
314 DATA LIST LIST NOTABLE /x * w *.
315 BEGIN DATA.
316 1 0.1
317 2 0.1
318 3 0.1
319 4 0.2
320 5 0.1
321 6 0.1
322 7 0.1
323 8 0.1
324 END DATA.
325
326 WEIGHT BY w.
327
328 RANK x
329  /TIES=low
330  /RANK into xl.
331
332
333 RANK x
334  /TIES=high
335  /RANK into xh.
336
337 RANK x
338  /TIES=condense
339  /RANK into xc.
340
341
342 * Test VW fraction
343
344 RANK x
345  /FRACTION=VW
346  /NORMAL.
347
348 LIST.
349 EOF
350 if [ $? -ne 0 ] ; then no_result ; fi
351
352
353 activity="run program 3"
354 $SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE
355 if [ $? -ne 0 ] ; then no_result ; fi
356
357
358 activity="compare output 3"
359 perl -pi -e 's/^\s*$//g' $TEMPDIR/pspp.list
360 diff  -b $TEMPDIR/pspp.list - <<EOF
361 Simple example using defaults
362 Variables Created By RANK
363 x into Rx(RANK of x)
364        x        Rx
365 -------- ---------
366       -1     1.000 
367        0     2.000 
368        1     3.000 
369        2     4.500 
370        2     4.500 
371        4     6.000 
372        5     7.000 
373 Variables Created By RANK
374 a into Ra(RANK of a)
375 b into Rb(RANK of b)
376 a into RFR001(RFRACTION of a)
377 b into RFR002(RFRACTION of b)
378 a into count(N of a)
379 b into Nb(N of b)
380 1.1 DISPLAY.  
381 +--------+-------------------------------------------+--------+
382 |Variable|Description                                |Position|
383 #========#===========================================#========#
384 |a       |Format: F8.2                               |       1|
385 +--------+-------------------------------------------+--------+
386 |b       |Format: F8.2                               |       2|
387 +--------+-------------------------------------------+--------+
388 |count   |N of a                                     |       3|
389 |        |Format: F6.0                               |        |
390 +--------+-------------------------------------------+--------+
391 |Ra      |RANK of a                                  |       4|
392 |        |Format: F9.3                               |        |
393 +--------+-------------------------------------------+--------+
394 |Rb      |RANK of b                                  |       5|
395 |        |Format: F9.3                               |        |
396 +--------+-------------------------------------------+--------+
397 |RFR001  |RFRACTION of a                             |       6|
398 |        |Format: F6.4                               |        |
399 +--------+-------------------------------------------+--------+
400 |RFR002  |RFRACTION of b                             |       7|
401 |        |Format: F6.4                               |        |
402 +--------+-------------------------------------------+--------+
403 |Nb      |N of b                                     |       8|
404 |        |Format: F6.0                               |        |
405 +--------+-------------------------------------------+--------+
406        a        b  count        Ra        Rb RFR001 RFR002     Nb
407 -------- -------- ------ --------- --------- ------ ------ ------
408      .00    24.00     10    10.000     8.000 1.0000  .8889      9 
409     1.00    32.00     10     9.000     4.000  .9000  .4444      9 
410     2.00    31.00     10     8.000     5.000  .8000  .5556      9 
411     2.00    32.00     10     8.000     4.000  .8000  .4444      9 
412     4.00    30.00     10     6.000     6.000  .6000  .6667      9 
413     5.00    29.00     10     5.000     7.000  .5000  .7778      9 
414     6.00     1.00     10     4.000     9.000  .4000 1.0000      9 
415     7.00    43.00     10     3.000     2.000  .3000  .2222      9 
416     8.00      .       10     2.000      .     .2000  .          . 
417     9.00    45.00     10     1.000     1.000  .1000  .1111      9 
418 Test variable name fallback
419 Variables Created By RANK
420 foo into RAN001(RANK of foo)
421 2.1 DISPLAY.  
422 +--------+-------------------------------------------+--------+
423 |Variable|Description                                |Position|
424 #========#===========================================#========#
425 |foo     |Format: F8.2                               |       1|
426 +--------+-------------------------------------------+--------+
427 |rfoo    |Format: F8.2                               |       2|
428 +--------+-------------------------------------------+--------+
429 |ran003  |Format: F8.2                               |       3|
430 +--------+-------------------------------------------+--------+
431 |RAN001  |RANK of foo                                |       4|
432 |        |Format: F9.3                               |        |
433 +--------+-------------------------------------------+--------+
434 Variables Created By RANK
435 a into Sa(SAVAGE of a)
436 a into Pa(PERCENT of a)
437 a into PRO001(PROPORTION of a using BLOM)
438 a into Na(NTILES of a)
439 a into NOR001(NORMAL of a using BLOM)
440        a        b       Sa     Pa PRO001  Na NOR001
441 -------- -------- -------- ------ ------ --- ------
442      .00    24.00   -.9000  10.00  .0610   1 -1.547 
443     1.00    32.00   -.7889  20.00  .1585   1 -1.000 
444     2.00    31.00   -.5925  30.00  .2561   2 -.6554 
445     2.00    32.00   -.5925  30.00  .2561   2 -.6554 
446     4.00    30.00   -.3544  40.00  .3537   2 -.3755 
447     5.00    29.00   -.1544  50.00  .4512   2 -.1226 
448     6.00     1.00    .0956  60.00  .5488   3  .1226 
449     7.00    43.00    .4290  70.00  .6463   3  .3755 
450     8.00     8.00    .9290  80.00  .7439   3  .6554 
451     9.00    45.00   1.9290  90.00  .8415   4 1.0005 
452 Variables Created By RANK
453 a into Ra(RANK of a BY g2 g1)
454 a into Na(NORMAL of a using RANKIT BY g2 g1)
455        a       g1       g2        Ra     Na
456 -------- -------- -------- --------- ------
457     1.00      .00     2.00     8.000 1.5341 
458     2.00      .00     2.00     7.000  .8871 
459     3.00      .00     2.00     6.000  .4888 
460     4.00      .00     2.00     5.000  .1573 
461     5.00      .00     2.00     4.000 -.1573 
462     6.00      .00     2.00     3.000 -.4888 
463     7.00      .00     2.00     2.000 -.8871 
464     8.00      .00     2.00     1.000 -1.534 
465     2.00     1.00     2.00     8.000  .9674 
466     2.00     1.00     2.00     8.000  .9674 
467     3.00     1.00     2.00     7.000  .5895 
468     4.00     1.00     2.00     6.000  .2822 
469     5.00     1.00     2.00     5.000  .0000 
470     6.00     1.00     2.00     4.000 -.2822 
471     7.00     1.00     2.00     2.000 -.9674 
472     7.00     1.00     2.00     2.000 -.9674 
473     8.00     1.00     2.00     1.000 -1.593 
474     9.00     1.00     1.00     1.000  .0000 
475 fractional ranks ( including small ones for special case of SAVAGE ranks)
476 Variables Created By RANK
477 a into Pa(PROPORTION of a using TUKEY)
478 a into Sa(SAVAGE of a)
479        a        w     Pa       Sa
480 -------- -------- ------ --------
481     1.00     1.50  .1285   -.8016 
482     2.00      .20  .1776   -.6905 
483     3.00      .10  .1986   -.6905 
484     4.00     1.00  .3458   -.5305 
485     5.00     1.00  .4860   -.2905 
486     6.00     1.00  .6262    .0262 
487     7.00     1.00  .7664    .4929 
488     8.00     1.00  .9065   1.3929 
489 test all the ties cases with low caseweight values
490 Variables Created By RANK
491 x into xl(RANK of x)
492 Variables Created By RANK
493 x into xh(RANK of x)
494 Variables Created By RANK
495 x into xc(RANK of x)
496 Variables Created By RANK
497 x into Nx(NORMAL of x using VW)
498        x        w        xl        xh        xc     Nx
499 -------- -------- --------- --------- --------- ------
500     1.00      .10      .000      .100     1.000 -1.938 
501     2.00      .10      .100      .200     2.000 -1.412 
502     3.00      .10      .200      .300     3.000 -1.119 
503     4.00      .20      .300      .500     4.000 -.8046 
504     5.00      .10      .500      .600     5.000 -.5549 
505     6.00      .10      .600      .700     6.000 -.4067 
506     7.00      .10      .700      .800     7.000 -.2670 
507     8.00      .10      .800      .900     8.000 -.1323 
508 EOF
509 if [ $? -ne 0 ] ; then fail ; fi
510
511
512
513 # A test to make sure that variable name creation is really robust
514 activity="create file 4"
515 cat > $TESTFILE <<EOF
516 DATA LIST LIST notable /x * rx * ran001 TO ran999.
517 BEGIN DATA.
518 1
519 2
520 3
521 4
522 5
523 6
524 7
525 END DATA.
526
527 RANK x.
528
529 MODIFY VARS /drop ran001 TO ran999.
530
531 LIST.
532
533 EOF
534 if [ $? -ne 0 ] ; then no_result ; fi
535
536 activity="run program 4"
537 $SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE > /dev/null
538 if [ $? -ne 0 ] ; then fail ; fi
539
540
541 activity="compare output 4"
542 perl -pi -e 's/^\s*$//g' $TEMPDIR/pspp.list
543 diff  -b $TEMPDIR/pspp.list - << EOF
544 Variables Created By RANK
545 x into RNKRA01(RANK of x)
546        x       rx   RNKRA01
547 -------- -------- ---------
548     1.00      .       1.000 
549     2.00      .       2.000 
550     3.00      .       3.000 
551     4.00      .       4.000 
552     5.00      .       5.000 
553     6.00      .       6.000 
554     7.00      .       7.000 
555 EOF
556 if [ $? -ne 0 ] ; then fail ; fi
557
558 pass;