split: avoid failure due to leftover 'errno' value
[coreutils.git] / tests / split / l-chunk
blobc4e696815f6203fdea8482f07a6ad8e9d8a90f90
1 #!/bin/sh
2 # test splitting into newline delineated chunks (-n l/...)
4 # Copyright (C) 2010-2012 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 . "${srcdir=.}/init.sh"; path_prepend_ ../src
20 print_ver_ split
22 # invalid number of chunks
23 echo 'split: 1o: invalid number of chunks' > exp
24 split -n l/1o 2>err && fail=1
25 compare exp err || fail=1
27 echo "split: \`-': cannot determine file size" > exp
28 echo | split -n l/1 2>err && fail=1
29 compare exp err || fail=1
31 # N can be greater than the file size
32 # in which case no data is extracted, or empty files are written
33 split -n l/10 /dev/null || fail=1
34 test "$(stat -c %s x* | uniq -c | sed 's/^ *//; s/ /x/')" = "10x0" || fail=1
35 rm x??
37 # Ensure the correct number of files written
38 # even if there is more data than the reported file size
39 split -n l/2 /dev/zero
40 test "$(stat -c %s x* | wc -l)" = '2' || fail=1
41 rm x??
43 # Repeat the above, but with 1/2, not l/2:
44 split -n 1/2 /dev/zero || fail=1
46 # Ensure --elide-empty-files is honored
47 split -e -n l/10 /dev/null || fail=1
48 stat x?? 2>/dev/null && fail=1
50 # 80 bytes. ~ transformed to \n below
51 lines=\
52 12345~1~12345~1~12345~1~12345~1~12345~~~12345~1~12345~1~12345~1~12345~1~12345~1~
54 printf "%s" "$lines" | tr '~' '\n' > in || framework_failure_
56 echo 'split: 16: invalid chunk number' > exp
57 split -n l/16/15 in 2>err && fail=1
58 compare exp err || fail=1
60 printf '%s' "\
61 14 16 09 15 16 10
62 14 08 08 10 14 08 08 10
63 06 08 08 02 06 08 08 02 06 08 08 10
64 06 08 02 06 08 00 08 02 06 08 02 06 08 00 10
65 06 00 08 00 02 06 00 02 06 00 08 00 01 07 00 02 06 00 08 00 02 16
66 " > exp || framework_failure_
68 sed 's/00 *//g' exp > exp.elide_empty || framework_failure_
70 DEBUGGING=
71 test "$DEBUGGING" && test "$VERBOSE" && set +x
72 for ELIDE_EMPTY in '' '-e'; do
73 for IO_BLKSIZE in 1 2 5 10 80 100; do
74 : > out
75 test "$DEBUGGING" && printf "\n---io-blk-size=$IO_BLKSIZE $ELIDE_EMPTY\n"
76 for N in 6 8 12 15 22; do
77 rm -f x*
79 if test -z "$ELIDE_EMPTY"; then
80 split ---io-blksize=$IO_BLKSIZE -n l/2/$N in > chunk.k
81 stat x* 2>/dev/null && fail=1
84 split ---io-blksize=$IO_BLKSIZE $ELIDE_EMPTY -n l/$N in
85 echo $(stat -c "%02s" x*) >> out
87 if test -z "$ELIDE_EMPTY"; then
88 compare chunk.k xab || fail=1
91 if test "$DEBUGGING"; then
92 # Output partition pattern
93 size=$(printf "%s" "$lines" | wc -c)
94 chunk_size=$(($size/$N))
95 end_size=$(($chunk_size + ($size % $N)))
97 yes "$(printf %${chunk_size}s ])" | head -n$(($N-1))
98 printf %${end_size}s ]
99 } | tr -d '\n' | sed "s/\\(^.\\{1,$size\\}\\).*/\\1/"
100 echo
102 # Output pattern generated for comparison
103 for s in $(stat -c "%s" x*); do
104 #s=0 transitions are not shown
105 test "$m" = "_" && m=- || m=_
106 printf "%${s}s" '' | tr ' ' $m
107 done
108 echo
110 # Output lines for reference
111 echo "$lines"
113 done
114 test -z "$ELIDE_EMPTY" && EXP=exp || EXP=exp.elide_empty
115 compare out $EXP || fail=1
116 done
117 done
118 test "$DEBUGGING" && test "$VERBOSE" && set -x
121 # Check extraction of particular chunks
122 : > out
123 printf '1\n12345\n' > exp
124 split -n l/13/15 in > out
125 compare exp out || fail=1
126 : > out
127 printf '' > exp
128 split -n l/14/15 in > out
129 compare exp out || fail=1
130 : > out
131 printf '1\n12345\n1\n' > exp
132 split -n l/15/15 in > out
133 compare exp out || fail=1
135 # test input with no \n at end
136 printf '12\n34\n5' > in
137 printf '5' > exp
138 split -n l/7/7 in > out
139 compare exp out || fail=1
141 Exit $fail