Send error message to non-screen output devices also, if routing to
[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 -e $TEMPDIR/err $TESTFILE 
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 EOF
129 if [ $? -ne 0 ] ; then no_result ; fi
130
131
132 activity="run program (syntax errors)"
133 $SUPERVISOR $PSPP --testing-mode -o raw-ascii -e $TEMPDIR/errs $TESTFILE 
134 if [ $? -ne 1 ] ; then fail ; fi
135
136 activity="compare errors"
137 perl -pi -e 's/^\s*$//g' $TEMPDIR/errs
138 diff  -b $TEMPDIR/errs - << EOF
139 $TEMPDIR/rank.sh.sps:15: error: RANK: Syntax error expecting \`(' at end of command.
140 $TEMPDIR/rank.sh.sps:19: error: RANK: Syntax error expecting integer at \`d'.
141 $TEMPDIR/rank.sh.sps:25: error: RANK: Variable x already exists.
142 $TEMPDIR/rank.sh.sps:30: error: RANK: Too many variables in INTO clause.
143 EOF
144 if [ $? -ne 0 ] ; then fail ; fi
145
146 # Now some real tests.
147
148 activity="create file 3"
149 cat > $TESTFILE <<EOF
150 DATA LIST LIST NOTABLE /x (f8).
151 BEGIN DATA.
152 -1
153
154
155
156
157
158
159 END DATA.
160
161 ECHO  'Simple example using defaults'.
162
163 RANK x.
164
165 LIST.
166
167 NEW FILE.
168 DATA LIST LIST NOTABLE /a * b *.
169 BEGIN DATA.
170 0 24
171 1 32
172 2 31
173 2 32
174 4 30
175 5 29
176 6 1
177 7 43
178 8 .
179 9 45
180 END DATA.
181
182 RANK a b (D)
183    /PRINT=YES 
184    /RANK
185    /TIES=HIGH
186    /RFRACTION
187    /N INTO count
188    .
189
190 DISPLAY DICTIONARY.
191
192 LIST.
193
194
195 ECHO  'Test variable name fallback'.
196
197 NEW FILE.
198 DATA LIST LIST NOTABLE /foo * rfoo * ran003 *.
199 BEGIN DATA.
200 0 3 2
201 1 3 2
202 2 3 2
203 2 3 2
204 4 3 2
205 5 3 2
206 6 3 2
207 7 3 2
208 8 3 2
209 9 3 2
210 END DATA.
211
212 RANK foo.
213
214
215 DISPLAY DICTIONARY.
216
217
218 NEW FILE.
219 DATA LIST LIST NOTABLE /a * b *.
220 BEGIN DATA.
221 0 24
222 1 32
223 2 31
224 2 32
225 4 30
226 5 29
227 6 1
228 7 43
229 8 8 
230 9 45
231 END DATA.
232
233 RANK a
234   /PRINT=YES
235   /TIES=CONDENSE
236   /SAVAGE
237   /PERCENT
238   /PROPORTION
239   /NTILES(4)
240   /NORMAL
241 .
242
243 LIST.
244
245 NEW FILE.
246 DATA LIST LIST NOTABLE /a * g1 g2 *.
247 BEGIN DATA.
248 2 1 2
249 2 1 2
250 3 1 2
251 4 1 2
252 5 1 2
253 1 0 2
254 2 0 2
255 3 0 2
256 4 0 2
257 5 0 2
258 6 0 2
259 7 0 2
260 8 0 2
261 6 1 2
262 7 1 2
263 7 1 2
264 8 1 2
265 9 1 1
266 END DATA.
267
268 RANK a (D) BY g2 g1
269   /PRINT=YES
270   /TIES=LOW
271   /MISSING=INCLUDE
272   /FRACTION=RANKIT
273   /RANK
274   /NORMAL
275   .
276
277 SPLIT FILE BY g1.
278
279 RANK a (D) BY g2
280   /PRINT=YES
281   /TIES=LOW
282   /MISSING=INCLUDE
283   /FRACTION=RANKIT
284   /RANK
285   /NORMAL
286   .
287
288 SPLIT FILE OFF.
289
290 LIST.
291
292
293 ECHO 'fractional ranks ( including small ones for special case of SAVAGE ranks)'
294 NEW FILE.
295 DATA LIST LIST NOTABLE  /a *  w * .
296 BEGIN DATA.
297 1 1.5
298 2 0.2
299 3 0.1
300 4 1
301 5 1
302 6 1
303 7 1
304 8 1
305 END DATA.
306
307 WEIGHT BY w.
308
309 RANK a 
310   /FRACTION=TUKEY
311   /PROPORTION
312   /SAVAGE
313 .
314
315 LIST.
316
317
318 ECHO 'test all the ties cases with low caseweight values'.
319
320 NEW FILE.
321 DATA LIST LIST NOTABLE /x * w *.
322 BEGIN DATA.
323 1 0.1
324 2 0.1
325 3 0.1
326 4 0.2
327 5 0.1
328 6 0.1
329 7 0.1
330 8 0.1
331 END DATA.
332
333 WEIGHT BY w.
334
335 RANK x
336  /TIES=low
337  /RANK into xl.
338
339
340 RANK x
341  /TIES=high
342  /RANK into xh.
343
344 RANK x
345  /TIES=condense
346  /RANK into xc.
347
348
349 * Test VW fraction
350
351 RANK x
352  /FRACTION=VW
353  /NORMAL.
354
355 LIST.
356 EOF
357 if [ $? -ne 0 ] ; then no_result ; fi
358
359
360 activity="run program 3"
361 $SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE
362 if [ $? -ne 0 ] ; then no_result ; fi
363
364
365 activity="compare output 3"
366 perl -pi -e 's/^\s*$//g' $TEMPDIR/pspp.list
367 diff  -b $TEMPDIR/pspp.list - <<EOF
368 Simple example using defaults
369 Variables Created By RANK
370 x into Rx(RANK of x)
371        x        Rx
372 -------- ---------
373       -1     1.000 
374        0     2.000 
375        1     3.000 
376        2     4.500 
377        2     4.500 
378        4     6.000 
379        5     7.000 
380 Variables Created By RANK
381 a into Ra(RANK of a)
382 b into Rb(RANK of b)
383 a into RFR001(RFRACTION of a)
384 b into RFR002(RFRACTION of b)
385 a into count(N of a)
386 b into Nb(N of b)
387 1.1 DISPLAY.  
388 +--------+-------------------------------------------+--------+
389 |Variable|Description                                |Position|
390 #========#===========================================#========#
391 |a       |Format: F8.2                               |       1|
392 |        |Measure: Scale                             |        |
393 |        |Display Alignment: Right                   |        |
394 |        |Display Width: 8                           |        |
395 +--------+-------------------------------------------+--------+
396 |b       |Format: F8.2                               |       2|
397 |        |Measure: Scale                             |        |
398 |        |Display Alignment: Right                   |        |
399 |        |Display Width: 8                           |        |
400 +--------+-------------------------------------------+--------+
401 |count   |N of a                                     |       3|
402 |        |Format: F6.0                               |        |
403 |        |Measure: Scale                             |        |
404 |        |Display Alignment: Right                   |        |
405 |        |Display Width: 8                           |        |
406 +--------+-------------------------------------------+--------+
407 |Ra      |RANK of a                                  |       4|
408 |        |Format: F9.3                               |        |
409 |        |Measure: Scale                             |        |
410 |        |Display Alignment: Right                   |        |
411 |        |Display Width: 8                           |        |
412 +--------+-------------------------------------------+--------+
413 |Rb      |RANK of b                                  |       5|
414 |        |Format: F9.3                               |        |
415 |        |Measure: Scale                             |        |
416 |        |Display Alignment: Right                   |        |
417 |        |Display Width: 8                           |        |
418 +--------+-------------------------------------------+--------+
419 |RFR001  |RFRACTION of a                             |       6|
420 |        |Format: F6.4                               |        |
421 |        |Measure: Scale                             |        |
422 |        |Display Alignment: Right                   |        |
423 |        |Display Width: 8                           |        |
424 +--------+-------------------------------------------+--------+
425 |RFR002  |RFRACTION of b                             |       7|
426 |        |Format: F6.4                               |        |
427 |        |Measure: Scale                             |        |
428 |        |Display Alignment: Right                   |        |
429 |        |Display Width: 8                           |        |
430 +--------+-------------------------------------------+--------+
431 |Nb      |N of b                                     |       8|
432 |        |Format: F6.0                               |        |
433 |        |Measure: Scale                             |        |
434 |        |Display Alignment: Right                   |        |
435 |        |Display Width: 8                           |        |
436 +--------+-------------------------------------------+--------+
437        a        b  count        Ra        Rb RFR001 RFR002     Nb
438 -------- -------- ------ --------- --------- ------ ------ ------
439      .00    24.00     10    10.000     8.000 1.0000  .8889      9 
440     1.00    32.00     10     9.000     4.000  .9000  .4444      9 
441     2.00    31.00     10     8.000     5.000  .8000  .5556      9 
442     2.00    32.00     10     8.000     4.000  .8000  .4444      9 
443     4.00    30.00     10     6.000     6.000  .6000  .6667      9 
444     5.00    29.00     10     5.000     7.000  .5000  .7778      9 
445     6.00     1.00     10     4.000     9.000  .4000 1.0000      9 
446     7.00    43.00     10     3.000     2.000  .3000  .2222      9 
447     8.00      .       10     2.000      .     .2000  .          . 
448     9.00    45.00     10     1.000     1.000  .1000  .1111      9 
449 Test variable name fallback
450 Variables Created By RANK
451 foo into RAN001(RANK of foo)
452 2.1 DISPLAY.  
453 +--------+-------------------------------------------+--------+
454 |Variable|Description                                |Position|
455 #========#===========================================#========#
456 |foo     |Format: F8.2                               |       1|
457 |        |Measure: Scale                             |        |
458 |        |Display Alignment: Right                   |        |
459 |        |Display Width: 8                           |        |
460 +--------+-------------------------------------------+--------+
461 |rfoo    |Format: F8.2                               |       2|
462 |        |Measure: Scale                             |        |
463 |        |Display Alignment: Right                   |        |
464 |        |Display Width: 8                           |        |
465 +--------+-------------------------------------------+--------+
466 |ran003  |Format: F8.2                               |       3|
467 |        |Measure: Scale                             |        |
468 |        |Display Alignment: Right                   |        |
469 |        |Display Width: 8                           |        |
470 +--------+-------------------------------------------+--------+
471 |RAN001  |RANK of foo                                |       4|
472 |        |Format: F9.3                               |        |
473 |        |Measure: Scale                             |        |
474 |        |Display Alignment: Right                   |        |
475 |        |Display Width: 8                           |        |
476 +--------+-------------------------------------------+--------+
477 Variables Created By RANK
478 a into Sa(SAVAGE of a)
479 a into Pa(PERCENT of a)
480 a into PRO001(PROPORTION of a using BLOM)
481 a into Na(NTILES of a)
482 a into NOR001(NORMAL of a using BLOM)
483        a        b       Sa     Pa PRO001  Na NOR001
484 -------- -------- -------- ------ ------ --- ------
485      .00    24.00   -.9000  10.00  .0610   1 -1.547 
486     1.00    32.00   -.7889  20.00  .1585   1 -1.000 
487     2.00    31.00   -.5925  30.00  .2561   2 -.6554 
488     2.00    32.00   -.5925  30.00  .2561   2 -.6554 
489     4.00    30.00   -.3544  40.00  .3537   2 -.3755 
490     5.00    29.00   -.1544  50.00  .4512   2 -.1226 
491     6.00     1.00    .0956  60.00  .5488   3  .1226 
492     7.00    43.00    .4290  70.00  .6463   3  .3755 
493     8.00     8.00    .9290  80.00  .7439   3  .6554 
494     9.00    45.00   1.9290  90.00  .8415   4 1.0005 
495 Variables Created By RANK
496 a into Ra(RANK of a BY g2 g1)
497 a into Na(NORMAL of a using RANKIT BY g2 g1)
498 Variables Created By RANK
499 a into RAN001(RANK of a BY g2)
500 a into NOR001(NORMAL of a using RANKIT BY g2)
501        a       g1       g2        Ra     Na    RAN001 NOR001 
502 -------- -------- -------- --------- ------ --------- ------ 
503     2.00     1.00     2.00     8.000  .9674     4.000  .5244  
504     2.00     1.00     2.00     8.000  .9674     4.000  .5244  
505     3.00     1.00     2.00     7.000  .5895     3.000  .0000  
506     4.00     1.00     2.00     6.000  .2822     2.000 -.5244  
507     5.00     1.00     2.00     5.000  .0000     1.000 -1.282  
508     1.00      .00     2.00     8.000 1.5341     8.000 1.5341  
509     2.00      .00     2.00     7.000  .8871     7.000  .8871  
510     3.00      .00     2.00     6.000  .4888     6.000  .4888  
511     4.00      .00     2.00     5.000  .1573     5.000  .1573  
512     5.00      .00     2.00     4.000 -.1573     4.000 -.1573  
513     6.00      .00     2.00     3.000 -.4888     3.000 -.4888  
514     7.00      .00     2.00     2.000 -.8871     2.000 -.8871  
515     8.00      .00     2.00     1.000 -1.534     1.000 -1.534  
516     6.00     1.00     2.00     4.000 -.2822     4.000 1.1503  
517     7.00     1.00     2.00     2.000 -.9674     2.000 -.3186  
518     7.00     1.00     2.00     2.000 -.9674     2.000 -.3186  
519     8.00     1.00     2.00     1.000 -1.593     1.000 -1.150  
520     9.00     1.00     1.00     1.000  .0000     1.000  .0000  
521 fractional ranks ( including small ones for special case of SAVAGE ranks)
522 Variables Created By RANK
523 a into Pa(PROPORTION of a using TUKEY)
524 a into Sa(SAVAGE of a)
525        a        w     Pa       Sa
526 -------- -------- ------ --------
527     1.00     1.50  .1285   -.8016 
528     2.00      .20  .1776   -.6905 
529     3.00      .10  .1986   -.6905 
530     4.00     1.00  .3458   -.5305 
531     5.00     1.00  .4860   -.2905 
532     6.00     1.00  .6262    .0262 
533     7.00     1.00  .7664    .4929 
534     8.00     1.00  .9065   1.3929 
535 test all the ties cases with low caseweight values
536 Variables Created By RANK
537 x into xl(RANK of x)
538 Variables Created By RANK
539 x into xh(RANK of x)
540 Variables Created By RANK
541 x into xc(RANK of x)
542 Variables Created By RANK
543 x into Nx(NORMAL of x using VW)
544        x        w        xl        xh        xc     Nx
545 -------- -------- --------- --------- --------- ------
546     1.00      .10      .000      .100     1.000 -1.938 
547     2.00      .10      .100      .200     2.000 -1.412 
548     3.00      .10      .200      .300     3.000 -1.119 
549     4.00      .20      .300      .500     4.000 -.8046 
550     5.00      .10      .500      .600     5.000 -.5549 
551     6.00      .10      .600      .700     6.000 -.4067 
552     7.00      .10      .700      .800     7.000 -.2670 
553     8.00      .10      .800      .900     8.000 -.1323 
554 EOF
555 if [ $? -ne 0 ] ; then fail ; fi
556
557
558
559 # A test to make sure that variable name creation is really robust
560 activity="create file 4"
561 cat > $TESTFILE <<EOF
562 DATA LIST LIST notable /x * rx * ran001 TO ran999.
563 BEGIN DATA.
564 1
565 2
566 3
567 4
568 5
569 6
570 7
571 END DATA.
572
573 RANK x.
574
575 DELETE VAR ran001 TO ran999.
576
577 LIST.
578
579 EOF
580 if [ $? -ne 0 ] ; then no_result ; fi
581
582 activity="run program 4"
583 $SUPERVISOR $PSPP --testing-mode -o raw-ascii -e /dev/null $TESTFILE 
584 if [ $? -ne 0 ] ; then fail ; fi
585
586
587 activity="compare output 4"
588 perl -pi -e 's/^\s*$//g' $TEMPDIR/pspp.list
589 diff  -b $TEMPDIR/pspp.list - << EOF
590 $TEMPDIR/rank.sh.sps:3: warning: BEGIN DATA: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate.
591 $TEMPDIR/rank.sh.sps:4: warning: BEGIN DATA: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate.
592 $TEMPDIR/rank.sh.sps:5: warning: BEGIN DATA: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate.
593 $TEMPDIR/rank.sh.sps:6: warning: BEGIN DATA: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate.
594 $TEMPDIR/rank.sh.sps:7: warning: BEGIN DATA: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate.
595 $TEMPDIR/rank.sh.sps:8: warning: BEGIN DATA: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate.
596 $TEMPDIR/rank.sh.sps:9: warning: BEGIN DATA: Missing value(s) for all variables from rx onward.  These will be filled with the system-missing value or blanks, as appropriate.
597 Variables Created By RANK
598 x into RNKRA01(RANK of x)
599        x       rx   RNKRA01
600 -------- -------- ---------
601     1.00      .       1.000 
602     2.00      .       2.000 
603     3.00      .       3.000 
604     4.00      .       4.000 
605     5.00      .       5.000 
606     6.00      .       6.000 
607     7.00      .       7.000 
608 EOF
609 if [ $? -ne 0 ] ; then fail ; fi
610
611 pass;