Annotation of researchv10dc/cmd/sorttest, revision 1.1.1.1

1.1       root        1: #!/bin/sh
                      2: 
                      3: # Tests for the Unix sort utility
                      4: # Test Posix features except for locale.
                      5: # Test some nonstandard features if present.
                      6: 
                      7: # Other tests may be needed for files too big to fit in memory;
                      8: # see TEST=15 below
                      9: 
                     10: echo pathname of sort under test:
                     11: which sort 2>/dev/null || whatis sort 2>/dev/null || echo can\'t tell
                     12: 
                     13: # Initialize switches for nonstandard features.
                     14: # Use parenthesized settings for supported features.
                     15: 
                     16: o=:    # officially obsolescent features: +1 -2, misplaced -o (o=)
                     17: g=:    # -g numeric sort including e-format numbers (g=)
                     18: M=:    # -M sort by month names (M=)
                     19: s=:    # -s stable, do not compare raw bytes on equal keys (s=)
                     20: y=     # -y user-specified memory size (y=-y10000)
                     21: 
                     22: # Detect what features are supported, assuming bad options cause
                     23: # errors.  Set switches accordingly.
                     24: 
                     25: echo obsolescent and nonstandard features recognized, if any:
                     26: if sort +0 </dev/null 2>/dev/null; then o=
                     27:                                echo '  +1 -2'; fi
                     28: if sort /dev/null -o xx 2>/dev/null; then o=
                     29:                                echo '  displaced -o'; fi
                     30: if sort -g </dev/null 2>/dev/null; then g=
                     31:                                echo '  -g g-format numbers'; fi
                     32: if sort -M </dev/null 2>/dev/null; then M=
                     33:                                echo '  -M months'; fi
                     34: if sort -s </dev/null 2>/dev/null; then s=
                     35:                                echo '  -s stable'; fi
                     36: if sort -y10000 </dev/null 2>/dev/null; then y=-y10000
                     37:                                echo '  -y space'; fi
                     38: if sort -z10000 </dev/null 2>/dev/null; then
                     39:                                echo '  -z recsize (not exercised)'; fi
                     40: if sort -T. </dev/null 2>/dev/null; then
                     41:                                echo '  -T tempdir (not exercised)'; fi
                     42: 
                     43: 
                     44: export TEST    # major sequence number of test
                     45: 
                     46: trap "rm -f in in1 in2 out xx -k - -o unwritablefile xsort ysort linecount;
                     47:       exit" 0 1 2 13 15
                     48: 
                     49: #      "sum" must be dumb; insensitive to the
                     50: #      order of lines within a file.
                     51: #      System V sum is suitable; sum -5 is the v10 equivalent.
                     52: 
                     53: cat >in <<!
                     54: a
                     55: b
                     56: !
                     57: cat >in1 <<!
                     58: b
                     59: a
                     60: !
                     61: s1="`sum in|sed '/[a-z].*/s///'`"
                     62: s2="`sum in1|sed '/[a-z].*/s///'`"
                     63: if test "$s1" = "$s2"
                     64: then sum="sum"                 # sum appears to be like Sys V
                     65: else
                     66:        s1="`sum -5 in 2>out|sed '/[a-z].*/s///'`"
                     67:        s2="`sum -5 in1 2>>out|sed '/[a-z].*/s///'`"
                     68:        if test ! -s out && test "$s1" = "$s2"
                     69:        then sum="sum -5"       # sum appears to be like v10
                     70:        else
                     71:                echo sum command is unsuitable for these tests
                     72:                exit 1
                     73:        fi
                     74: fi
                     75: export sum
                     76: 
                     77: # xsort testno options
                     78: # Sort file "in" with specified options.
                     79: # Compare with file "out" if that is supplied,
                     80: # otherwise make plausibility checks on output
                     81: 
                     82: cat <<'!' >xsort; chmod +x xsort
                     83: 
                     84:        X=$1; shift
                     85: 
                     86:        if sort "$@" in >xx  &&  sort -c "$@" xx 
                     87:        then 
                     88:                if test -f out
                     89:                then
                     90:                        cmp xx out >/dev/null && exit 0
                     91:                        echo $TEST$X comparison failed
                     92:                else
                     93:                        test "`$sum <in`" = "`$sum <xx`" && exit 0
                     94:                        echo $TEST$X checksum failed
                     95:                fi
                     96:        else
                     97:                echo $TEST$X failed
                     98:        fi
                     99:        exit 1
                    100: !
                    101: 
                    102: # linecount testno file count
                    103: # declares the given "testno" to be in error if number of
                    104: # lines in "file" differs from "count"
                    105: 
                    106: cat <<'!' >linecount; chmod +x linecount
                    107: awk 'END{ if(NR!='$3') print "'$TEST$1' failed" }' $2
                    108: !
                    109: 
                    110: rm -f out
                    111: 
                    112: #---------------------------------------------------------------
                    113: TEST=01; echo $TEST    # -c status, checksum
                    114:                        # obsolescent features go together
                    115: cat <<! >in
                    116: b
                    117: a
                    118: !
                    119: rm -f out -o
                    120: 
                    121: sort -c in 2>/dev/null && echo ${TEST}A failed
                    122: 
                    123: xsort B || '"sum"' is probably unsuitable - see comments
                    124: 
                    125: $o sort +0 in -o in || echo ${TEST}c failed
                    126: 
                    127: #---------------------------------------------------------------
                    128: TEST=02; echo $TEST    # output from -c
                    129: cat <<! >in
                    130: x
                    131: y
                    132: !
                    133: 
                    134: sort -cr in >out 2>xx && echo ${TEST}A failed
                    135: test -s out && echo ${TEST}B failed
                    136: test -s xx && echo option -c is noisy "(probably legal)"
                    137: test -s xx || echo option -c is quiet "(legal, not classical)"
                    138: 
                    139: sort -c /dev/null 2>xx || echo ${TEST}C failed
                    140: test -s xx && echo ${TEST}D failed
                    141: 
                    142: #---------------------------------------------------------------
                    143: TEST=03; echo $TEST    # -n
                    144: cat <<! >in
                    145: -99.0
                    146: -99.1
                    147: -.0002
                    148:   -10
                    149: 2
                    150: 2.0x
                    151: 2.0.1
                    152: 0010.000000000000000000000000000000000001
                    153: 10
                    154: 3x
                    155: x
                    156: !
                    157: cat <<! >out
                    158: -99.1
                    159: -99.0
                    160:   -10
                    161: -.0002
                    162: x
                    163: 2
                    164: 2.0.1
                    165: 2.0x
                    166: 3x
                    167: 10
                    168: 0010.000000000000000000000000000000000001
                    169: !
                    170: 
                    171: xsort "" -n
                    172: 
                    173: #---------------------------------------------------------------
                    174: TEST=04; echo $TEST    # -b without fields, piping, -c status return
                    175: cat <<! >in
                    176:   b
                    177:  a
                    178: !
                    179: cp in out
                    180: 
                    181: xsort A -b
                    182: 
                    183: cat in | sort | cat >xx
                    184: cmp xx out >/dev/null || echo ${TEST}B failed
                    185: 
                    186: sort in | sort -cr 2>/dev/null && echo ${TEST}C failed
                    187: 
                    188: #---------------------------------------------------------------
                    189: TEST=05; echo $TEST    # fields, reverse fields, -c status return
                    190: cat <<! >in
                    191: b b p
                    192: a b q
                    193: x a
                    194: !
                    195: cat <<! >out
                    196: x a
                    197: a b q
                    198: b b p
                    199: !
                    200: 
                    201: $o xsort A +1 -2
                    202: 
                    203: $o xsort B +1 -2 +2r
                    204: 
                    205: xsort C -k 2,2
                    206: 
                    207: xsort D -k 2,2 -k 3r
                    208: 
                    209: xsort E -k 2,2.0
                    210: 
                    211: xsort F -k 2,2 -k 1,1 -k 3
                    212: 
                    213: sort -c -k 2 in 2>/dev/null && ${TEST}G failed
                    214: 
                    215: #---------------------------------------------------------------
                    216: TEST=06; echo $TEST    # -t
                    217: cat <<! >in
                    218: a:
                    219: a!
                    220: !
                    221: cp in out
                    222: 
                    223: $o xsort A -t : -r +0
                    224: 
                    225: $o xsort B -t : +0 -1
                    226: 
                    227: xsort C -t : -r -k 1
                    228: 
                    229: xsort D -t : -k 1,1
                    230: 
                    231: #---------------------------------------------------------------
                    232: TEST=07; echo $TEST    # -t, character positions in fields
                    233:        # -t: as 1 arg is not strictly conforming, but classical
                    234: cat <<! >in
                    235: : ab
                    236: :bac
                    237: !
                    238: cat <<! >out
                    239: :bac
                    240: : ab
                    241: !
                    242: 
                    243: $o xsort A -b -t: +1.1
                    244:        
                    245: $o xsort B -t: +1.1r
                    246: 
                    247: xsort C -b -t: -k 2.2
                    248: 
                    249: xsort D -t: -k 2.2r
                    250: 
                    251: #---------------------------------------------------------------
                    252: TEST=08; echo $TEST    # space and tab as -t characters
                    253: cat <<! >in
                    254:  b c
                    255:  b     c
                    256:        b c
                    257: !
                    258: cp in out
                    259: 
                    260: xsort A -t ' ' -k2,2
                    261: 
                    262: xsort B -t ' ' -k2.1,2.0
                    263: 
                    264: cat <<! >out
                    265:  b c
                    266:        b c
                    267:  b     c
                    268: !
                    269: 
                    270: xsort C -t '   ' -k2,2
                    271: 
                    272: xsort D -t '   ' -k2.1,2.0
                    273: 
                    274: cat <<! >out
                    275:  b     c
                    276:        b c
                    277:  b c
                    278: !
                    279: 
                    280: xsort E -k2
                    281: 
                    282: cat <<! >out
                    283:        b c
                    284:  b     c
                    285:  b c
                    286: !
                    287: 
                    288: xsort F -k2b
                    289: 
                    290: #---------------------------------------------------------------
                    291: TEST=09; echo $TEST    # alphabetic as -t character
                    292: cat <<! >in
                    293: zXa
                    294: yXa
                    295: zXb
                    296: !
                    297: cp in out
                    298: 
                    299: xsort "" -tX -k2 -k1r,1
                    300: 
                    301: #---------------------------------------------------------------
                    302: TEST=10; echo $TEST    # -m
                    303: cat <<! >in
                    304: a
                    305: ab
                    306: ab
                    307: bc
                    308: ca
                    309: !
                    310: cat <<! >in1
                    311: Z
                    312: a
                    313: aa
                    314: ac
                    315: c
                    316: !
                    317: cat <<! >out
                    318: Z
                    319: a
                    320: a
                    321: aa
                    322: ab
                    323: ab
                    324: ac
                    325: bc
                    326: c
                    327: ca
                    328: !
                    329: 
                    330: sort -m in in1 >xx
                    331: cmp xx out >/dev/null || echo ${TEST}A failed
                    332: 
                    333: #---------------------------------------------------------------
                    334: TEST=11; echo $TEST    # multiple files, -o overwites input, -m, -mu
                    335: cat <<! >in
                    336: a
                    337: b
                    338: c
                    339: d
                    340: !
                    341: 
                    342: sort -o xx     in in in in in in in in in in in in in in in in in
                    343: linecount A xx 68
                    344: sort -o in -mu in in in in in in in in in in in in in in in in in
                    345: linecount B in 4
                    346: sort -o in -m  in in in in in in in in in in in in in in in in in
                    347: 
                    348: cmp in xx >/dev/null || echo ${TEST}C failed
                    349: 
                    350: #---------------------------------------------------------------
                    351: TEST=12; echo $TEST    # does -mu pick the first among equals?
                    352: cat <<! >in
                    353: 3B
                    354: 3b
                    355: 3B2
                    356: ~3B2
                    357: 4.1
                    358: 41
                    359: 5
                    360: 5.
                    361: !
                    362: cat <<! >out
                    363: 3B
                    364: 3B2
                    365: 4.1
                    366: 5
                    367: !
                    368: 
                    369: xsort A -mudf || echo "(other behavior is legal, not classical)"
                    370: 
                    371: xsort B -mudf -k1 || echo "(other behavior is legal, not classical)"
                    372: 
                    373: #---------------------------------------------------------------
                    374: TEST=13; echo $TEST    # long records (>8000 bytes, keys >16000), -r
                    375: awk '
                    376: BEGIN {        x="x"
                    377:        for(i=1; i<=12; i++) x = x x
                    378:        for(i=15; i<=25; i++) print x i
                    379: }' >in
                    380: awk '
                    381: BEGIN {        x="x"
                    382:        for(i=1; i<=12; i++) x = x x
                    383:        for(i=25; i>=15; i--) print x i
                    384: }' >out
                    385: 
                    386: xsort A -r
                    387: 
                    388: xsort B -k 1,1r -k 1
                    389: 
                    390: #---------------------------------------------------------------
                    391: TEST=14; echo $TEST "(3 long parts)"
                    392: awk 'BEGIN { for(i=0; i<100000; i++) print rand() }' | grep -v e >in
                    393: rm -f out
                    394: 
                    395: xsort A; echo $TEST "(part A done)"
                    396: 
                    397: xsort B -n; echo $TEST "(part B done)"
                    398: 
                    399: # next test is unclean: xx is a hidden side-effect of xsort
                    400: 
                    401: awk '
                    402:        $0 < x { print "test '${TEST}C' failed"; exit }
                    403:        $0 "" != x { print >"out"; x = $0 }
                    404: ' xx
                    405: 
                    406: xsort C -n -u
                    407: 
                    408: #---------------------------------------------------------------
                    409: TEST=15; echo $TEST "(long)"   # force intermediate files if possible
                    410: #              with option -y 10000 ($y) this makes 50 intermediat
                    411: #              files of 80-byte records.
                    412: case "$y" in
                    413: "")    echo "sorttest warning: inadequate test of large files; revise parameters"
                    414: esac
                    415: 
                    416: awk 'BEGIN {
                    417:        x = "xxxxxxxxxx" 
                    418:        x = x x x x x x x x
                    419:        for(i=0; i<4000; i++) print rand(), x }' >in
                    420: rm -f out
                    421: 
                    422: xsort A -r $y
                    423: 
                    424: rm -f in1
                    425: sort -r in -o in1
                    426: awk '$0 "x" != x { print ; x = $0 "x" }' in1 >out
                    427: 
                    428: xsort B -u -r $y
                    429: 
                    430: sort -r -u -m in1 -o in1
                    431: cmp in1 out >/dev/null || echo ${TEST}C failed
                    432: rm in in1 out
                    433: 
                    434: #---------------------------------------------------------------
                    435: TEST=16; echo $TEST    # -nr, -nm, file name -
                    436: awk 'BEGIN { for(i=-100; i<=100; i+=2) printf "%.10d\n", i }' >in
                    437: 
                    438: awk 'BEGIN { for(i=-99; i<=100; i+=2) print i }' | sort -nr in - >xx
                    439: awk '$0+0 != 101-NR { print "'${TEST}A' failed"; exit }' xx
                    440: 
                    441: awk 'BEGIN { for(i=-99; i<=100; i+=2) print i }' | sort -mn - in >xx
                    442: awk '$0+0 != -101+NR { print "'${TEST}B' failed"; exit }' xx
                    443: 
                    444: #---------------------------------------------------------------
                    445: TEST=17; echo $TEST    # -d, fields without end, modifier override
                    446: cat <<! >in
                    447: a-B
                    448: a+b
                    449: a b
                    450: A+b
                    451: a      b
                    452: !
                    453: cat <<! >out
                    454: a      b
                    455: a b
                    456: A+b
                    457: a-B
                    458: a+b
                    459: !
                    460: 
                    461: $o xsort A -df +0 +0d 
                    462: 
                    463: xsort B -df -k 1 -k 1d
                    464: 
                    465: #---------------------------------------------------------------
                    466: TEST=18; echo $TEST    # -u on key only
                    467: cat <<! >in
                    468: 12     y
                    469: 13     z
                    470: 12     x
                    471: !
                    472: cat <<! >out
                    473: 12     x
                    474: 12     y
                    475: 13     z
                    476: !
                    477: 
                    478: $o xsort A +0 -1
                    479: 
                    480: xsort B -k 1,1
                    481: 
                    482: sort -u -k 1,1 in >xx
                    483: linecount C xx 2
                    484: 
                    485: #---------------------------------------------------------------
                    486: TEST=19; echo $TEST    # -i, -d, -f
                    487: cat <<! >xx.c
                    488: run(i,j){ for( ; i<=j; i++) printf("%.3o %c\n",i,i); }
                    489: main(){        run(0, 011);            /* 012=='\n' */
                    490:        run(013, 0377); }
                    491: !
                    492: cc xx.c 
                    493: a.out >in
                    494: cat <<! >xx.c
                    495: run(i,j){ for( ; i<=j; i++) printf("%.3o %c\n",i,i); }
                    496: main(){ run(0, 011);
                    497:        run(013, ' '-1);
                    498:        run(0177, 0377);
                    499:        run(' ', 0176); }
                    500: !
                    501: cc xx.c
                    502: a.out >out
                    503: 
                    504: xsort A -i -k 2
                    505: 
                    506: cat <<! >xx.c
                    507: run(i,j){ for( ; i<=j; i++) printf("%.3o %c\n",i,i); }
                    508: main(){        run(0, 010);            /* 011=='\t', 012=='\n' */
                    509:        run(013, ' '-1);
                    510:        run(' '+1, '0'-1);
                    511:        run('9'+1, 'A'-1);
                    512:        run('Z'+1, 'a'-1);
                    513:        run('z'+1, 0377);
                    514:        run('\t', '\t');
                    515:        run(' ', ' ');
                    516:        run('0', '9');
                    517:        run('A', 'Z');
                    518:        run('a', 'z'); }
                    519: !
                    520: cc xx.c
                    521: a.out >out
                    522: 
                    523: xsort B -d -k 2
                    524: 
                    525: cat <<! >xx.c
                    526: run(i,j){ for( ; i<=j; i++) printf("%.3o %c\n",i,i); }
                    527: main(){        int i;
                    528:        run(0, 011);
                    529:        run(013, 'A'-1);
                    530:        for(i='A'; i<='Z'; i++) 
                    531:                printf("%.3o %c\n%.3o %c\n",i,i,i+040,i+040);
                    532:        run('Z'+1, 'a'-1);
                    533:        run('z'+1, 0377); }
                    534: !
                    535: cc xx.c
                    536: a.out >out
                    537: rm xx.c
                    538: 
                    539: xsort C -f -k 2
                    540: 
                    541: #---------------------------------------------------------------
                    542: TEST=20; echo $TEST    # -d, -f, -b applies only to fields
                    543: cat <<! >in
                    544:  b
                    545: 'C
                    546: a
                    547: !
                    548: cp in out
                    549: 
                    550: xsort A -d
                    551: 
                    552: xsort B -f
                    553: 
                    554: cat <<! >out
                    555:  b
                    556: a
                    557: 'C
                    558: !
                    559: 
                    560: xsort C -dfb
                    561: 
                    562: #---------------------------------------------------------------
                    563: TEST=21; echo $TEST    # behavior of null bytes
                    564: cat <<'!' >xx.c
                    565: main() { printf("\n%cb\n%ca\n",0,0); }
                    566: !
                    567: cc xx.c
                    568: a.out >in
                    569: sort -u in >xx
                    570: cmp in xx >/dev/null && echo ${TEST}A failed
                    571: test "`wc -c <in`" = "`wc -c <xx`" || echo ${TEST}B failed
                    572: rm xx.c a.out
                    573: 
                    574: #---------------------------------------------------------------
                    575: TEST=22; echo $TEST    # field limits
                    576: cat <<! >in
                    577: a      2
                    578: a      1
                    579: b      2
                    580: b      1
                    581: !
                    582: cat <<! >out
                    583: b      1
                    584: b      2
                    585: a      1
                    586: a      2
                    587: !
                    588: 
                    589: xsort "" -r -k1,1 -k2n
                    590: 
                    591: #---------------------------------------------------------------
                    592: TEST=23; echo $TEST    # empty file, compact -o
                    593: 
                    594: echo hi >xx
                    595: 
                    596: sort -oxx </dev/null
                    597: cmp xx /dev/null >/dev/null || echo ${TEST}A failed
                    598: 
                    599: sort -c </dev/null || echo ${TEST}B failed
                    600: 
                    601: sort -cu </dev/null || echo ${TEST}C failed
                    602: 
                    603: #---------------------------------------------------------------
                    604: TEST=24; echo $TEST    # many fields
                    605: cat <<! >in
                    606: 0:2:3:4:5:6:7:8:9
                    607: 1:1:3:4:5:6:7:8:9
                    608: 1:2:2:4:5:6:7:8:9
                    609: 1:2:3:3:5:6:7:8:9
                    610: 1:2:3:4:4:6:7:8:9
                    611: 1:2:3:4:5:5:7:8:9
                    612: 1:2:3:4:5:6:6:8:9
                    613: 1:2:3:4:5:6:7:7:9
                    614: 1:2:3:4:5:6:7:8:8
                    615: !
                    616: cat <<! >out
                    617: 1:2:3:4:5:6:7:8:8
                    618: 1:2:3:4:5:6:7:7:9
                    619: 1:2:3:4:5:6:6:8:9
                    620: 1:2:3:4:5:5:7:8:9
                    621: 1:2:3:4:4:6:7:8:9
                    622: 1:2:3:3:5:6:7:8:9
                    623: 1:2:2:4:5:6:7:8:9
                    624: 1:1:3:4:5:6:7:8:9
                    625: 0:2:3:4:5:6:7:8:9
                    626: !
                    627: 
                    628: xsort "" -t: -k9 -k8 -k7 -k6 -k5 -k4 -k3 -k2 -k1
                    629: 
                    630: #---------------------------------------------------------------
                    631: TEST=25; echo $TEST    # variously specified alpha fields
                    632:                        # numbers give the correct orderings
                    633: cat <<! >in
                    634: 01:04:19:01:16:01:21:01 a
                    635: 02:03:13:15:13:19:15:02  a
                    636: 03:02:07:09:07:13:09:03   a
                    637: 04:01:01:03:01:07:03:04    a
                    638: 05:08:20:16:17:02:20:05 aa
                    639: 06:07:14:18:14:20:14:06  aa
                    640: 07:06:08:10:08:14:08:07   aa
                    641: 08:05:02:04:02:08:02:08    aa
                    642: 09:16:22:02:22:04:24:13 b
                    643: 10:15:16:20:19:22:18:14  b
                    644: 11:14:10:12:10:16:12:15   b
                    645: 12:13:04:06:04:10:06:16    b
                    646: 13:24:24:22:24:06:22:21 bb
                    647: 14:23:18:24:21:24:16:22  bb
                    648: 15:22:12:14:12:18:10:23   bb
                    649: 16:21:06:08:06:12:04:24    bb
                    650: 17:12:21:21:18:03:19:09 ab
                    651: 18:11:15:19:15:21:13:10  ab
                    652: 19:10:09:11:09:15:07:11   ab
                    653: 20:09:03:05:03:09:01:12    ab
                    654: 21:20:23:17:23:05:23:17 ba
                    655: 22:19:17:23:20:23:17:18  ba
                    656: 23:18:11:13:11:17:11:19   ba
                    657: 24:17:05:07:05:11:05:20    ba
                    658: !
                    659: sort -k2b -k2 in >xx  
                    660:        sort -c -t: -k2n xx 2>/dev/null || echo ${TEST}A failed
                    661: sort -k2,2.1b -k2 in >xx  
                    662:        sort -c -t: -k3n xx 2>/dev/null || echo ${TEST}B failed
                    663: sort -k2.3 -k2 in >xx  
                    664:        sort -c -t: -k4n xx 2>/dev/null || echo ${TEST}C failed
                    665: sort -k2b,2.3 -k2 in >xx  
                    666:        sort -c -t: -k5n xx 2>/dev/null || echo ${TEST}D failed
                    667: sort -k2.3,2.1b -k2 in >xx  
                    668:        sort -c -t: -k6n xx 2>/dev/null || echo ${TEST}E failed
                    669: sort -k2,2.1b -k2r in >xx  
                    670:        sort -c -t: -k7n xx 2>/dev/null || echo ${TEST}F failed
                    671: sort -b -k2,2 -k2 in >xx  
                    672:        sort -c -t: -k8n xx 2>/dev/null || echo ${TEST}G failed
                    673: sort -b -k2,2b -k2 in >xx                      # perhaps same as G
                    674:        sort -c -t: -k3n xx 2>/dev/null || echo ${TEST}H failed\
                    675:  "(standard is not clear on this)"
                    676: 
                    677: #---------------------------------------------------------------
                    678: TEST=26; echo $TEST    # empty fields, out of bounds fields    
                    679: cat <<! >in
                    680: 0 5
                    681: 1 4
                    682: 2 3
                    683: 3 2
                    684: 4 1
                    685: 5 0
                    686: !
                    687: cp in out
                    688: 
                    689: xsort "" -k2.2,2.1 -k2.3,2.4
                    690: 
                    691: #---------------------------------------------------------------
                    692: TEST=27; echo $TEST    # displaced -o
                    693: rm -f out
                    694: 
                    695: $o sort /dev/null -o out || $o echo ${TEST}B failed
                    696: $o test -f out || $o echo ${TEST}C failed
                    697: 
                    698: #---------------------------------------------------------------
                    699: TEST=28; echo $TEST    # apparently nonmonotone field specs
                    700: cat <<! >in
                    701: aaaa c
                    702: x a
                    703: 0 b
                    704: !
                    705: cp in out
                    706: 
                    707: $o xsort A +1 -0.3 +1.4 -1.5
                    708: 
                    709: xsort B -k2,1.3 -k2.5,2.5
                    710: 
                    711: #---------------------------------------------------------------
                    712: TEST=29; echo $TEST    # determination of end of option list
                    713: cat >-k <<!
                    714: x
                    715: !
                    716: rm -f out -c
                    717: 
                    718: sort -- -k </dev/null >xx || echo ${TEST}A argument failed
                    719: cmp xx -k || echo ${TEST}A comparison failed
                    720: 
                    721: cat >in <<!
                    722: xxx
                    723: !
                    724: >-
                    725: >-o
                    726: >in1
                    727: 
                    728: if sort -- - -o in1 <in >out 2>xx && test ! -s xx
                    729:        cmp in out >/dev/null || echo ${TEST}C failed
                    730:        test -s in1 && echo ${TEST}D failed
                    731: fi
                    732: 
                    733: #---------------------------------------------------------------
                    734: TEST=30; echo $TEST    # missing newline
                    735: awk 'BEGIN{ printf "%s", "x"}' | sort >xx 2>/dev/null
                    736: wc -c <xx | awk '$1!=2{ print "'${TEST}' failed" }'
                    737: 
                    738: #---------------------------------------------------------------
                    739: TEST=31; echo $TEST    # -M, multiple fields
                    740: cat <<! >in
                    741: jan 10 1900
                    742: Feb 26 1900
                    743: feb 25 1900
                    744: January xx 1900
                    745: August 11 1900
                    746: jan 15 1990
                    747: feb 22 1990
                    748: mar 15 1990
                    749: apr 1 1990
                    750: may 45 1990
                    751: jun 14 1990
                    752: jul 4 1990
                    753: aug 1~ 1990
                    754: aug 11 1990
                    755: sep 1 1990
                    756: oct 12 1990
                    757: nov 24 1990
                    758: dec 25 1990
                    759: never 3 1990
                    760:  Dec 25 1990
                    761: !
                    762: cat <<! >out
                    763: January xx 1900
                    764: jan 10 1900
                    765: feb 25 1900
                    766: Feb 26 1900
                    767: August 11 1900
                    768: never 3 1990
                    769: jan 15 1990
                    770: feb 22 1990
                    771: mar 15 1990
                    772: apr 1 1990
                    773: may 45 1990
                    774: jun 14 1990
                    775: jul 4 1990
                    776: aug 1~ 1990
                    777: aug 11 1990
                    778: sep 1 1990
                    779: oct 12 1990
                    780: nov 24 1990
                    781:  Dec 25 1990
                    782: dec 25 1990
                    783: !
                    784: 
                    785: $M xsort "" -k3n -k1M -k2n
                    786: 
                    787: #---------------------------------------------------------------
                    788: TEST=32; echo $TEST    # -M case insensitivity, -r
                    789: cat <<! >in
                    790: x
                    791: june
                    792: january
                    793: december
                    794: !
                    795: cat <<! >out
                    796: december
                    797: june
                    798: january
                    799: x
                    800: !
                    801: 
                    802: $M xsort "" -Mr
                    803: 
                    804: #---------------------------------------------------------------
                    805: TEST=33; echo $TEST    # -g, big enough for IEEE floating point
                    806: cat <<! >in
                    807: 2
                    808: 1
                    809: 10
                    810: 1e-1
                    811: .2
                    812: 1e
                    813: 1E1
                    814: 1e.
                    815: 3e+308
                    816: 3e307
                    817: 1e-308
                    818: 1e-307
                    819: !
                    820: cat <<! >out
                    821: 1e-308
                    822: 1e-307
                    823: 1e-1
                    824: .2
                    825: 1
                    826: 1e
                    827: 1e.
                    828: 2
                    829: 10
                    830: 1E1
                    831: 3e307
                    832: 3e+308
                    833: !
                    834: 
                    835: $g xsort "" -g
                    836: 
                    837: #---------------------------------------------------------------
                    838: TEST=34; echo $TEST    # -g wide operands
                    839: cat <<! >in
                    840: .99999999999999999999
                    841: 099999999999999999999e-21
                    842: 099999999999999999999e-19
                    843: .1e1
                    844: !
                    845: cat <<! >out
                    846: 099999999999999999999e-21
                    847: .99999999999999999999
                    848: .1e1
                    849: 099999999999999999999e-19
                    850: !
                    851: 
                    852: $g xsort A -g
                    853: 
                    854: cat <<! >out
                    855: .1e1
                    856: .99999999999999999999
                    857: 099999999999999999999e-19
                    858: 099999999999999999999e-21
                    859: !
                    860: 
                    861: xsort B -n
                    862: 
                    863: #---------------------------------------------------------------
                    864: TEST=35; echo $TEST    #-g, -u with different fp reps
                    865: cat <<! >in
                    866: +0
                    867: -0
                    868: 0
                    869: 0.10
                    870: +.1
                    871: -.1
                    872: -100e-3x
                    873: x
                    874: !
                    875: cat <<! >out
                    876: -.1
                    877: -100e-3x
                    878: +0
                    879: -0
                    880: 0
                    881: x
                    882: +.1
                    883: 0.10
                    884: !
                    885: 
                    886: $g xsort A -g
                    887: 
                    888: $g sort -gu in >xx && $g sort -c -gu xx || echo ${TEST}B failed
                    889: $g linecount C xx 3
                    890: 
                    891: #---------------------------------------------------------------
                    892: TEST=36; echo $TEST    # -s
                    893: cat <<! >in
                    894: a 2
                    895: b 1
                    896: c 2
                    897: a 1
                    898: b 2
                    899: c 1
                    900: !
                    901: cat <<! >out
                    902: a 2
                    903: a 1
                    904: b 1
                    905: b 2
                    906: c 2
                    907: c 1
                    908: !
                    909: 
                    910: $s xsort "" -s -k1,1
                    911: 
                    912: #---------------------------------------------------------------
                    913: TEST=37; echo $TEST    # -s, multiple files
                    914: cat <<! >in
                    915: a 2
                    916: c 2
                    917: !
                    918: cat <<! >in1
                    919: a 1
                    920: b 1
                    921: c 1
                    922: !
                    923: cat <<! >out
                    924: c 2
                    925: b 1
                    926: a 2
                    927: !
                    928: 
                    929: $s sort -smru -k1,1 in in in1 in1 >xx
                    930: $s cmp xx out >/dev/null || echo $TEST failed
                    931: 
                    932: #---------------------------------------------------------------
                    933: TEST=38; echo $TEST    # -s
                    934: $s awk '
                    935:        BEGIN {
                    936:                for(i=1; i<50; i++)
                    937:                        for(j=1; j<=i; j++) {
                    938:                                print i, 2 >"in"
                    939:                                print i, 1 >"in1"
                    940:                        }
                    941:        }'
                    942: 
                    943: $s sort -m -s -k1,1n in in1 >out
                    944: 
                    945: $s awk '
                    946:        func stop()     { print "'$TEST' failed"; exit }
                    947:        $1!=last1       { if(count!=last1 || $2!=2) stop();
                    948:                          count = 0}
                    949:        $1==last1 && $2!=last2 { if(count!=last1 || $2!=1) stop();
                    950:                                 count = 0 }
                    951:                        { count++; last1 = $1; last2 = $2 }
                    952:        ' out
                    953: 
                    954: #---------------------------------------------------------------
                    955: TEST=39; echo $TEST    # empty fields
                    956: cat <<! >in
                    957: bXXa
                    958: aXXb
                    959: !
                    960: cp in out
                    961: 
                    962: xsort A -k3 -tX
                    963: xsort B -k2 -tX
                    964: xsort C -r -k2,2 -tX
                    965: xsort D -r -k4 -tX
                    966: 
                    967: #---------------------------------------------------------------
                    968: TEST=40; echo $TEST    # deceptive field boundaries
                    969: cat <<! >in
                    970:     1.2
                    971:   1.10
                    972: !
                    973: cp in out
                    974: 
                    975: xsort A -t. -k1,1n -k2,2n
                    976: xsort B -t. -k1nr -k2n
                    977: 
                    978: cat <<! >in
                    979:   feb
                    980:   jan
                    981:  jan
                    982: feb
                    983: !
                    984: cp in out
                    985: 
                    986: $M xsort C -k1.1,1.4M
                    987: 
                    988: #---------------------------------------------------------------
                    989: TEST=41; echo $TEST    # diagnostics
                    990: 
                    991: echo hi >in
                    992: 
                    993: cat <<\! >ysort
                    994: error=0 warning=0 works=0
                    995: sort "$@" 2>out <in >in1 || error=1
                    996: test -s out && warning=1
                    997: test -s in1 && works=1
                    998: case $error$warning$works in
                    999: 000)   echo sort "$@" does not indicate trouble, but does not sort ;;
                   1000: 001)   echo sort "$@" does not indicate trouble ;;
                   1001: 010)   echo sort "$@" warns, does not sort, and yields exit status zero ;;
                   1002: 011)   echo sort "$@" warns and continues ;;
                   1003: 100)   echo sort "$@" yields nonzero exit status and does not sort ;;
                   1004: 101)   echo sort "$@" yields nonzero exit status, but sorts ;;
                   1005: 111)   echo sort "$@" warns and yields nonzero exit status, but sorts
                   1006: esac
                   1007: !
                   1008: chmod +x ysort
                   1009: echo "behavior (other than message and error exit)"  
                   1010: echo "    in some questionable cases:"
                   1011: 
                   1012: rm -f nosuchfile
                   1013: ysort nosuchfile
                   1014: 
                   1015: echo x >unwritablefile
                   1016: chmod 0 unwritablefile
                   1017: ysort -o unwritablefile </dev/null
                   1018: 
                   1019: cat in | ysort - -
                   1020: 
                   1021: for i in -k -k0 -k-1 -k1, -kb -k1,2u -t -o
                   1022: do     ysort $i
                   1023: done
                   1024: 
                   1025: $g ysort -n -g
                   1026: $M ysort -n -M
                   1027: ysort -d -i
                   1028: ysort -n -i
                   1029: ysort -c in in
                   1030: ysort -t. -t:
                   1031: 
                   1032: for i in -a -e -h -j -p -q -v -w -x -0 -1 -2
                   1033: do     ysort $i
                   1034: done
                   1035: 
                   1036: $g false && ysort -g
                   1037: $s false && ysort -s
                   1038: $z false && ysort -z
                   1039: 
                   1040: case "$y" in
                   1041: "")    ysort -y
                   1042: esac
                   1043: 
                   1044: ysort -k1,1 -f
                   1045: 
                   1046: cat <<! >in
                   1047: A      b
                   1048: a      bc
                   1049: a      Bd
                   1050: B
                   1051: !
                   1052: cat <<! >in1
                   1053: A      b
                   1054: B
                   1055: a      bc
                   1056: a      Bd
                   1057: !
                   1058: cat <<! >in2
                   1059: A      b
                   1060: B
                   1061: a      Bd
                   1062: a      bc
                   1063: !
                   1064: 
                   1065: if sort -k1,1 -f -k2,2 <in >out 2>/dev/null
                   1066: then
                   1067:        echo sort -k1,1 -f -k2,2 :
                   1068:        if cmp out in >/dev/null
                   1069:        then    echo "    -f applies to fields 1 and 2"
                   1070:        elif cmp out in1 >/dev/null
                   1071:        then    echo "    -f applies to field 2 only"
                   1072:        elif cmp out in1 >/dev/null
                   1073:        then    echo "    -f ineffectual"
                   1074:        elif cmp out /dev/null >/dev/null
                   1075:        then    echo "    exit status zero, but no output"
                   1076:        else    echo "    inexplicable"
                   1077:        fi
                   1078: fi

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.