shred: fix pattern selection for certain iteration counts
[coreutils.git] / tests / split / l-chunk.sh
blobc97b3b562957c1eae29dd9cd116a620b5a637f9e
1 #!/bin/sh
2 # test splitting into newline delineated chunks (-n l/...)
4 # Copyright (C) 2010-2015 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=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ split
22 # invalid number of chunks
23 echo "split: invalid number of chunks: '1o'" > 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: invalid chunk number: '16'" > exp
57 split -n l/16/15 in 2>err.t && fail=1
58 sed "s/': .*/'/" < err.t > err || framework_failure_
59 compare exp err || fail=1
61 printf '%s' "\
62 14 16 09 15 16 10
63 14 08 08 10 14 08 08 10
64 06 08 08 02 06 08 08 02 06 08 08 10
65 06 08 02 06 08 00 08 02 06 08 02 06 08 00 10
66 06 00 08 00 02 06 00 02 06 00 08 00 01 07 00 02 06 00 08 00 02 16
67 " > exp || framework_failure_
69 sed 's/00 *//g' exp > exp.elide_empty || framework_failure_
71 DEBUGGING=
72 test "$DEBUGGING" && test "$VERBOSE" && set +x
73 for ELIDE_EMPTY in '' '-e'; do
74 for IO_BLKSIZE in 1 2 5 10 80 100; do
75 > out
76 test "$DEBUGGING" && printf "\n---io-blk-size=$IO_BLKSIZE $ELIDE_EMPTY\n"
77 for N in 6 8 12 15 22; do
78 rm -f x*
80 if test -z "$ELIDE_EMPTY"; then
81 split ---io-blksize=$IO_BLKSIZE -n l/2/$N in > chunk.k
82 stat x* 2>/dev/null && fail=1
85 split ---io-blksize=$IO_BLKSIZE $ELIDE_EMPTY -n l/$N in
86 echo $(stat -c "%02s" x*) >> out
88 if test -z "$ELIDE_EMPTY"; then
89 compare chunk.k xab || fail=1
92 if test "$DEBUGGING"; then
93 # Output partition pattern
94 size=$(printf "%s" "$lines" | wc -c)
95 chunk_size=$(($size/$N))
96 end_size=$(($chunk_size + ($size % $N)))
98 yes "$(printf %${chunk_size}s ])" | head -n$(($N-1))
99 printf %${end_size}s ]
100 } | tr -d '\n' | sed "s/\\(^.\\{1,$size\\}\\).*/\\1/"
101 echo
103 # Output pattern generated for comparison
104 for s in $(stat -c "%s" x*); do
105 #s=0 transitions are not shown
106 test "$m" = "_" && m=- || m=_
107 printf "%${s}s" '' | tr ' ' $m
108 done
109 echo
111 # Output lines for reference
112 echo "$lines"
114 done
115 test -z "$ELIDE_EMPTY" && EXP=exp || EXP=exp.elide_empty
116 compare out $EXP || fail=1
117 done
118 done
119 test "$DEBUGGING" && test "$VERBOSE" && set -x
122 # Check extraction of particular chunks
123 > out
124 printf '1\n12345\n' > exp
125 split -n l/13/15 in > out
126 compare exp out || fail=1
127 > out
128 printf '' > exp
129 split -n l/14/15 in > out
130 compare exp out || fail=1
131 > out
132 printf '1\n12345\n1\n' > exp
133 split -n l/15/15 in > out
134 compare exp out || fail=1
136 # test input with no \n at end
137 printf '12\n34\n5' > in
138 printf '5' > exp
139 split -n l/7/7 in > out
140 compare exp out || fail=1
142 Exit $fail