OSDN Git Service

Merge remote-tracking branch 'toybox/master' into HEAD am: 14cafec540
[android-x86/external-toybox.git] / tests / tail.test
1 #!/bin/bash
2
3 [ -f testing.sh ] && . testing.sh
4
5 #testing "name" "command" "result" "infile" "stdin"
6
7 BIGTEST="one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\neleven\n"
8 echo -ne "$BIGTEST" > file1
9 testing "tail" "tail && echo yes" "oneyes\n" "" "one"
10 testing "file" "tail file1" \
11         "two\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\neleven\n" "" ""
12 testing "-n in bounds" "tail -n 3 file1" "nine\nten\neleven\n" "" ""
13 testing "-n out of bounds" "tail -n 999 file1" "$BIGTEST" "" ""
14 testing "-n+ in bounds" "tail -n +3 file1" \
15         "three\nfour\nfive\nsix\nseven\neight\nnine\nten\neleven\n" "" ""
16 testing "-n+ outof bounds" "tail -n +999 file1" "" "" ""
17 testing "-c in bounds" "tail -c 27 file1" \
18         "even\neight\nnine\nten\neleven\n" "" ""
19 testing "-c out of bounds" "tail -c 999 file1" "$BIGTEST" "" ""
20 testing "-c+ in bounds" "tail -c +27 file1" \
21         "x\nseven\neight\nnine\nten\neleven\n" "" ""
22 testing "-c+ out of bonds" "tail -c +999 file1" "" "" ""
23 testing "-N" "tail -1 file1" "eleven\n" "" ""
24 rm file1
25
26 testing "stdin no trailing newline" "tail -n 1 - " "c" "" "a\nb\nc"
27 testing "file no trailing newline" "tail -n 1 input" "c" "a\nb\nc" ""
28
29 optional TAIL_SEEK
30 testing "noseek -n in bounds" "tail -n 3" "nine\nten\neleven\n" \
31         "" "$BIGTEST"
32 testing "noseek -n out of bounds" "tail -n 999" "$BIGTEST" "" "$BIGTEST"
33 testing "noseek -n+ in bounds" "tail -n +3" \
34         "three\nfour\nfive\nsix\nseven\neight\nnine\nten\neleven\n" "" \
35         "$BIGTEST"
36 testing "noseek -n+ outof bounds" "tail -n +999" "" "" "$BIGTEST"
37 testing "noseek -c in bounds" "tail -c 27" \
38         "even\neight\nnine\nten\neleven\n" "" "$BIGTEST"
39 testing "noseek -c out of bounds" "tail -c 999" "$BIGTEST" "" "$BIGTEST"
40 testing "noseek -c+ in bounds" "tail -c +27" \
41         "x\nseven\neight\nnine\nten\neleven\n" "" "$BIGTEST"
42 testing "noseek -c+ out of bonds" "tail -c +999" "" "" "$BIGTEST"
43
44 makebigfile()
45 {
46   for j in $(seq 1 100)
47   do
48     echo -n "$j "
49     for i in $(seq 1 100)
50     do
51       echo -n 123456789abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
52     done
53     echo
54   done
55 }
56 makebigfile > bigfile
57
58 testing "-c 12345 -n 3 bigfile" "tail -c 12345 -n 3 bigfile | md5sum" \
59   "347bbdcbad8a313f4dc7bd558c5bfcb8  -\n" "" ""
60 testing "-n 3 -c 12345 bigfile" "tail -n 3 -c 12345 bigfile | md5sum" \
61   "1698825a750288284ec3ba7d8a59f302  -\n" "" ""
62 rm bigfile
63
64 echo 111 > one
65 testing "-f one" \
66   'tail -f one & sleep .25 ; echo two >> one; sleep .25; echo three >> one; sleep .25; kill $! >/dev/null' \
67   "111\ntwo\nthree\n" "" ""
68 rm one
69
70 echo uno > one
71 echo dos > two
72 echo tres > three
73 testing "-f one two three" \
74   'tail -f one two three & sleep .25 ; echo more >> three ; echo also >> one; sleep .25; kill $! >/dev/null' \
75   "==> one <==\nuno\n\n==> two <==\ndos\n\n==> three <==\ntres\nmore\n\n==> one <==\nalso\n" "" ""
76 rm one two three