Tizen 2.0 Release
[external/tizen-coreutils.git] / tests / misc / csplit
1 #!/bin/sh
2 # various csplit tests
3
4 # Copyright (C) 2001, 2002, 2003, 2004, 2005 2006 Free Software
5 # Foundation, Inc.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301, USA.
21
22 if test "$VERBOSE" = yes; then
23   set -x
24   csplit --version
25 fi
26
27 . $srcdir/../lang-default
28
29 pwd=`pwd`
30 tmp=csplit.$$
31 trap 'status=$?; cd "$pwd" && rm -rf $tmp && exit $status' 0
32 trap '(exit $?); exit' 1 2 13 15
33
34 framework_failure=0
35 mkdir $tmp || framework_failure=1
36 cd $tmp || framework_failure=1
37
38 if test $framework_failure = 1; then
39   echo "$0: failure in testing framework" 1>&2
40   (exit 1); exit 1
41 fi
42
43 fail=0
44
45 # csplit could get a failed assertion to 2.0.17
46 (echo a; echo; echo) > in
47 csplit in '/^$/' 2 > out || fail=1
48 cat <<EOF > exp
49 2
50 0
51 2
52 EOF
53 cmp out exp || fail=1
54 test $fail = 1 && diff out exp 2> /dev/null
55 rm -f in out exp
56
57 # Ensure that xx02 contains just two newlines.
58 # This would fail due to reading from freed buffer with coreutils-5.0.91.
59 printf '\n\n' > exp
60 cp xx02 out || fail=1
61 cmp out exp || fail=1
62 test $fail = 1 && diff out exp 2> /dev/null
63 rm -f in out exp
64
65 # csplit would infloop
66 (echo; echo a) > in
67 csplit in '/a/-1' '{*}' > out || fail=1
68 cat <<EOF > exp
69 0
70 3
71 EOF
72 cmp out exp || fail=1
73 test $fail = 1 && diff out exp 2> /dev/null
74 rm -f in out exp
75
76 # `echo |csplit - 1 1' used to abort.
77 echo > in
78 csplit in 1 1 > out 2> err || fail=1
79 cat <<EOF > exp
80 0
81 0
82 1
83 EOF
84 cmp out exp || fail=1
85 test $fail = 1 && diff out exp 2> /dev/null
86 cat <<\EOF > experr
87 csplit: warning: line number `1' is the same as preceding line number
88 EOF
89 cmp err experr || fail=1
90 test $fail = 1 && diff err experr 2> /dev/null
91 rm -f in out exp err experr
92
93 # make sure `csplit FILE 0' fails.
94 echo > in
95 csplit in 0 > out 2> err && fail=1
96 csplit in 2 1 > out 2>> err && fail=1
97 csplit in 3 3 > out 2>> err && fail=1
98 cat <<\EOF > experr
99 csplit: 0: line number must be greater than zero
100 csplit: line number `1' is smaller than preceding line number, 2
101 csplit: warning: line number `3' is the same as preceding line number
102 csplit: `3': line number out of range
103 EOF
104 cmp err experr || fail=1
105 test $fail = 1 && diff err experr 2> /dev/null
106
107 # Ensure that lines longer than the initial buffer length don't cause
108 # trouble (e.g. reading from freed memory, resulting in corrupt output).
109 # This test failed at least in coreutils-5.2.1 and 5.3.0, and was fixed
110 # in 5.3.1.
111 rm -f in out exp err experr xx??
112 printf 'x%8199s\nx\n%8199s\nx\n' x x > in
113 csplit in '/x\{1\}/' '{*}' > /dev/null || fail=1
114 cat xx?? | cmp - in || fail=1
115
116 (exit $fail); exit $fail