diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:11:47 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:11:47 +0000 |
commit | 758f820bcc0f68aeebac1717e537ca13a320b909 (patch) | |
tree | 48111ece75cf4f98316848b37a7e26356e00669e /tests/split | |
parent | Initial commit. (diff) | |
download | coreutils-upstream.tar.xz coreutils-upstream.zip |
Adding upstream version 9.1.upstream/9.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/split')
-rwxr-xr-x | tests/split/additional-suffix.sh | 44 | ||||
-rwxr-xr-x | tests/split/b-chunk.sh | 73 | ||||
-rwxr-xr-x | tests/split/fail.sh | 81 | ||||
-rwxr-xr-x | tests/split/filter.sh | 80 | ||||
-rwxr-xr-x | tests/split/guard-input.sh | 33 | ||||
-rwxr-xr-x | tests/split/l-chunk.sh | 142 | ||||
-rwxr-xr-x | tests/split/line-bytes.sh | 87 | ||||
-rwxr-xr-x | tests/split/lines.sh | 42 | ||||
-rwxr-xr-x | tests/split/numeric.sh | 51 | ||||
-rwxr-xr-x | tests/split/r-chunk.sh | 64 | ||||
-rwxr-xr-x | tests/split/record-sep.sh | 78 | ||||
-rwxr-xr-x | tests/split/suffix-auto-length.sh | 59 | ||||
-rwxr-xr-x | tests/split/suffix-length.sh | 76 |
13 files changed, 910 insertions, 0 deletions
diff --git a/tests/split/additional-suffix.sh b/tests/split/additional-suffix.sh new file mode 100755 index 0000000..8cc8179 --- /dev/null +++ b/tests/split/additional-suffix.sh @@ -0,0 +1,44 @@ +#!/bin/sh +# show that 'split --additional-suffix=SUFFIX' works. + +# Copyright (C) 2012-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +printf '1\n2\n3\n4\n5\n' > in || framework_failure_ + +split --lines=2 --additional-suffix=.txt in > out || fail=1 +cat <<\EOF > exp-1 +1 +2 +EOF +cat <<\EOF > exp-2 +3 +4 +EOF +cat <<\EOF > exp-3 +5 +EOF + +compare exp-1 xaa.txt || fail=1 +compare exp-2 xab.txt || fail=1 +compare exp-3 xac.txt || fail=1 + +# Additional suffix must not contain slash +returns_ 1 split --lines=2 --additional-suffix=a/b in 2>/dev/null >out || fail=1 + +Exit $fail diff --git a/tests/split/b-chunk.sh b/tests/split/b-chunk.sh new file mode 100755 index 0000000..4e7caf3 --- /dev/null +++ b/tests/split/b-chunk.sh @@ -0,0 +1,73 @@ +#!/bin/sh +# test splitting into 3 chunks + +# Copyright (C) 2010-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +# N can be greater than the file size +# in which case no data is extracted, or empty files are written +split -n 10 /dev/null || fail=1 +test "$(stat -c %s x* | uniq -c | sed 's/^ *//; s/ /x/')" = "10x0" || fail=1 +rm -f x?? + +# When extracting K of N where N > file size +# no data is extracted, and no files are written +split -n 2/3 /dev/null || fail=1 +returns_ 1 stat x?? 2>/dev/null || fail=1 + +# Ensure --elide-empty-files is honored +split -e -n 10 /dev/null || fail=1 +returns_ 1 stat x?? 2>/dev/null || fail=1 + +printf '1\n2\n3\n4\n5\n' > input || framework_failure_ +printf '1\n2' > exp-1 || framework_failure_ +printf '\n3\n' > exp-2 || framework_failure_ +printf '4\n5\n' > exp-3 || framework_failure_ + +for file in input /proc/version /sys/kernel/profiling; do + test -f $file || continue + + for blksize in 1 2 4096; do + if ! test "$file" = 'input'; then + # For /proc like files we must be able to read all + # into the internal buffer to be able to determine size. + test "$blksize" = 4096 || continue + fi + + split -n 3 ---io-blksize=$blksize $file > out || fail=1 + split -n 1/3 ---io-blksize=$blksize $file > b1 || fail=1 + split -n 2/3 ---io-blksize=$blksize $file > b2 || fail=1 + split -n 3/3 ---io-blksize=$blksize $file > b3 || fail=1 + + case $file in + input) + compare exp-1 xaa || fail=1 + compare exp-2 xab || fail=1 + compare exp-3 xac || fail=1 + ;; + esac + + compare xaa b1 || fail=1 + compare xab b2 || fail=1 + compare xac b3 || fail=1 + cat xaa xab xac | compare - $file || fail=1 + test -f xad && fail=1 + done +done + +Exit $fail diff --git a/tests/split/fail.sh b/tests/split/fail.sh new file mode 100755 index 0000000..3753495 --- /dev/null +++ b/tests/split/fail.sh @@ -0,0 +1,81 @@ +#!/bin/sh +# split must fail when given length/count of zero. + +# Copyright (C) 2003-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split +getlimits_ + +touch in || framework_failure_ + + +split -a 0 in 2> /dev/null || fail=1 +returns_ 1 split -b 0 in 2> /dev/null || fail=1 +returns_ 1 split -C 0 in 2> /dev/null || fail=1 +returns_ 1 split -l 0 in 2> /dev/null || fail=1 +returns_ 1 split -n 0 in 2> /dev/null || fail=1 +returns_ 1 split -n 1/0 in 2> /dev/null || fail=1 +returns_ 1 split -n 0/1 in 2> /dev/null || fail=1 +returns_ 1 split -n 2/1 in 2> /dev/null || fail=1 + +# Make sure -C doesn't create empty files. +rm -f x?? || fail=1 +echo x | split -C 1 || fail=1 +test -f xaa && test -f xab || fail=1 +test -f xac && fail=1 + +# Make sure that the obsolete -N notation still works +split -1 in 2> /dev/null || fail=1 + +# Then make sure that -0 evokes a failure. +returns_ 1 split -0 in 2> /dev/null || fail=1 + +split --lines=$UINTMAX_MAX in || fail=1 +split --bytes=$OFF_T_MAX in || fail=1 +returns_ 1 split --line-bytes=$OFF_T_OFLOW 2> /dev/null in || fail=1 +returns_ 1 split --line-bytes=$SIZE_OFLOW 2> /dev/null in || fail=1 +if truncate -s$SIZE_OFLOW large; then + # Ensure we can split chunks of a large file on 32 bit hosts + split --number=$SIZE_OFLOW/$SIZE_OFLOW large >/dev/null || fail=1 +fi +split --number=r/$UINTMAX_MAX/$UINTMAX_MAX </dev/null >/dev/null || fail=1 +returns_ 1 split --number=r/$UINTMAX_OFLOW </dev/null 2>/dev/null || fail=1 + +# Make sure that a huge obsolete option evokes the right failure. +split -99999999999999999991 2> out + +# On losing systems (x86 Solaris 5.9 c89), we get a message like this: +# split: line count option -9999999999... is too large +# while on most, we get this: +# split: line count option -99999999999999999991... is too large +# so map them both to -99*. +sed 's/99[19]*/99*/' out > out-t +mv -f out-t out + +cat <<\EOF > exp +split: line count option -99*... is too large +EOF +compare exp out || fail=1 + +# Make sure split fails when it can't read input +# (the current directory in this case) +if ! cat . >/dev/null; then + # can't read() directories + returns_ 1 split . || fail=1 +fi + +Exit $fail diff --git a/tests/split/filter.sh b/tests/split/filter.sh new file mode 100755 index 0000000..66a93ac --- /dev/null +++ b/tests/split/filter.sh @@ -0,0 +1,80 @@ +#!/bin/sh +# Exercise split's new --filter option. + +# Copyright (C) 2011-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split +require_sparse_support_ # for 'truncate --size=$LARGE' +xz --version || skip_ 'xz required' + +for total_n_lines in 5 3000 20000; do + seq $total_n_lines > in || framework_failure_ + for i in 2 51 598; do + + # Don't create too many files/processes. + # Starting 10k (or even "only" 1500) processes would take a long time, + # and would provide little added benefit. + case $i:$total_n_lines in 2:5);; *) continue;; esac + + split -l$i --filter='xz -1 > $FILE.xz' in out- || fail=1 + xz -dc out-* > out || fail=1 + compare in out || fail=1 + rm -f out* + done + rm -f in +done + +# Show how --elide-empty-files works with --filter: +# split does not run the command (and effectively elides the file) +# only when the output to that command would have been empty. +split -e -n 10 --filter='xz > $FILE.xz' /dev/null || fail=1 +returns_ 1 stat x?? 2>/dev/null || fail=1 + +# Ensure this invalid combination is flagged +returns_ 1 split -n 1/2 --filter='true' /dev/null 2>&1 || fail=1 + +# Ensure SIGPIPEs sent by the children don't propagate back +# where they would result in a non zero exit from split. +yes | head -n200K | split -b1G --filter='head -c1 >/dev/null' || fail=1 + +# Ensure that "endless" input is ignored when all filters finish +for mode in '' 'r/'; do + in_file='-' + in_cmd='yes' + if test "$mode" = ''; then + in_file='zero.in' + in_cmd='true' + truncate -s10T "$FILE" || continue + fi + for N in 1 2; do + rm -f x??.n || framework_failure_ + $in_cmd | + timeout 10 split --filter='head -c1 >$FILE.n' -n $mode$N $in_file || fail=1 + # Also ensure we get appropriate output from each filter + seq 1 $N | tr '0-9' 1 > stat.exp + stat -c%s x??.n > stat.out || framework_failure_ + compare stat.exp stat.out || fail=1 + done +done + +# Ensure that "endless" input _is_ processed for unbounded number of filters +for buf in 1000 1000000; do + returns_ 124 timeout .5 sh -c \ + "yes | split --filter='head -c1 >/dev/null' -b $buf" || fail=1 +done + +Exit $fail diff --git a/tests/split/guard-input.sh b/tests/split/guard-input.sh new file mode 100755 index 0000000..812b6a5 --- /dev/null +++ b/tests/split/guard-input.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# ensure split doesn't overwrite input with output. + +# Copyright (C) 2012-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +seq 10 | tee exp-1 > xaa +ln -s xaa in2 +ln xaa in3 + +returns_ 1 split -C 6 xaa || fail=1 +returns_ 1 split -C 6 in2 || fail=1 +returns_ 1 split -C 6 in3 || fail=1 +returns_ 1 split -C 6 - < xaa || fail=1 + +compare exp-1 xaa || fail=1 + +Exit $fail diff --git a/tests/split/l-chunk.sh b/tests/split/l-chunk.sh new file mode 100755 index 0000000..0e9d236 --- /dev/null +++ b/tests/split/l-chunk.sh @@ -0,0 +1,142 @@ +#!/bin/sh +# test splitting into newline delineated chunks (-n l/...) + +# Copyright (C) 2010-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +# invalid number of chunks +echo "split: invalid number of chunks: '1o'" > exp +returns_ 1 split -n l/1o 2>err || fail=1 +compare exp err || fail=1 + +echo "split: -: cannot determine file size" > exp +: | returns_ 1 split -n l/1 2>err || fail=1 +compare exp err || fail=1 + +# N can be greater than the file size +# in which case no data is extracted, or empty files are written +split -n l/10 /dev/null || fail=1 +test "$(stat -c %s x* | uniq -c | sed 's/^ *//; s/ /x/')" = "10x0" || fail=1 +rm x?? + +# 'split' should reject any attempt to create an infinitely +# long output file. +returns_ 1 split -n l/2 /dev/zero || fail=1 +rm x?? + +# Repeat the above, but with 1/2, not l/2: +returns_ 1 split -n 1/2 /dev/zero || fail=1 +rm x?? + +# Ensure --elide-empty-files is honored +split -e -n l/10 /dev/null || fail=1 +returns_ 1 stat x?? 2>/dev/null || fail=1 + +# 80 bytes. ~ transformed to \n below +lines=\ +12345~1~12345~1~12345~1~12345~1~12345~~~12345~1~12345~1~12345~1~12345~1~12345~1~ + +printf "%s" "$lines" | tr '~' '\n' > in || framework_failure_ + +echo "split: invalid chunk number: '16'" > exp +returns_ 1 split -n l/16/15 in 2>err.t || fail=1 +sed "s/': .*/'/" < err.t > err || framework_failure_ +compare exp err || fail=1 + +printf '%s' "\ +14 16 09 15 16 10 +14 08 08 10 14 08 08 10 +06 08 08 02 06 08 08 02 06 08 08 10 +06 08 02 06 08 00 08 02 06 08 02 06 08 00 10 +06 00 08 00 02 06 00 02 06 00 08 00 01 07 00 02 06 00 08 00 02 16 +" > exp || framework_failure_ + +sed 's/00 *//g' exp > exp.elide_empty || framework_failure_ + +DEBUGGING= +test "$DEBUGGING" && test "$VERBOSE" && set +x +for ELIDE_EMPTY in '' '-e'; do + for IO_BLKSIZE in 1 2 5 10 80 100; do + > out + test "$DEBUGGING" && printf "\n---io-blk-size=$IO_BLKSIZE $ELIDE_EMPTY\n" + for N in 6 8 12 15 22; do + rm -f x* + + if test -z "$ELIDE_EMPTY"; then + split ---io-blksize=$IO_BLKSIZE -n l/2/$N in > chunk.k + returns_ 1 stat x* 2>/dev/null || fail=1 + fi + + split ---io-blksize=$IO_BLKSIZE $ELIDE_EMPTY -n l/$N in + echo $(stat -c "%02s" x*) >> out + + if test -z "$ELIDE_EMPTY"; then + compare chunk.k xab || fail=1 + fi + + if test "$DEBUGGING"; then + # Output partition pattern + size=$(printf "%s" "$lines" | wc -c) + chunk_size=$(($size/$N)) + end_size=$(($chunk_size + ($size % $N))) + { + yes "$(printf %${chunk_size}s ])" | head -n$(($N-1)) + printf %${end_size}s ] + } | tr -d '\n' | sed "s/\\(^.\\{1,$size\\}\\).*/\\1/" + echo + + # Output pattern generated for comparison + for s in $(stat -c "%s" x*); do + #s=0 transitions are not shown + test "$m" = "_" && m=- || m=_ + printf "%${s}s" '' | tr ' ' $m + done + echo + + # Output lines for reference + echo "$lines" + fi + done + test -z "$ELIDE_EMPTY" && EXP=exp || EXP=exp.elide_empty + compare out $EXP || fail=1 + done +done +test "$DEBUGGING" && test "$VERBOSE" && set -x + + +# Check extraction of particular chunks +> out +printf '1\n12345\n' > exp +split -n l/13/15 in > out +compare exp out || fail=1 +> out +printf '' > exp +split -n l/14/15 in > out +compare exp out || fail=1 +> out +printf '1\n12345\n1\n' > exp +split -n l/15/15 in > out +compare exp out || fail=1 + +# test input with no \n at end +printf '12\n34\n5' > in +printf '5' > exp +split -n l/7/7 in > out +compare exp out || fail=1 + +Exit $fail diff --git a/tests/split/line-bytes.sh b/tests/split/line-bytes.sh new file mode 100755 index 0000000..c153000 --- /dev/null +++ b/tests/split/line-bytes.sh @@ -0,0 +1,87 @@ +#!/bin/sh +# test -C, --lines-bytes + +# Copyright (C) 2013-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +vm=$(get_min_ulimit_v_ split -C 'K' /dev/null) \ + || skip_ "this shell lacks ulimit support" + +# Ensure memory is not allocated up front +(ulimit -v $vm && split -C 'G' /dev/null) || fail=1 + + +# Ensure correct operation with various split and buffer size combinations + +lines=\ +1~2222~3~4 + +printf '%s' "$lines" | tr '~' '\n' > in || framework_failure_ + +cat <<\EOF > splits_exp +1 1 1 1 1 1 1 1 1 1 +2 2 2 1 2 1 +2 3 2 2 1 +2 4 3 1 +2 5 3 +2 5 3 +7 3 +7 3 +9 1 +9 1 +10 +EOF + +seq 0 9 | tr -d '\n' > no_eol_in + +cat <<\EOF > no_eol_splits_exp +1 1 1 1 1 1 1 1 1 1 +2 2 2 2 2 +3 3 3 1 +4 4 2 +5 5 +6 4 +7 3 +8 2 +9 1 +10 +10 +EOF + +for b in $(seq 10); do + > splits + > no_eol_splits + for s in $(seq 11); do + rm x?? + split ---io=$b -C$s in || fail=1 + cat x* > out || framework_failure_ + compare in out || fail=1 + stat -c %s x* | paste -s -d ' ' >> splits + + rm x?? + split ---io=$b -C$s no_eol_in || fail=1 + cat x* > out || framework_failure_ + cat xaa + compare no_eol_in out || fail=1 + stat -c %s x* | paste -s -d ' ' >> no_eol_splits + done + compare splits_exp splits || fail=1 + compare no_eol_splits_exp no_eol_splits || fail=1 +done + +Exit $fail diff --git a/tests/split/lines.sh b/tests/split/lines.sh new file mode 100755 index 0000000..8780a2c --- /dev/null +++ b/tests/split/lines.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# show that 'split --lines=2' works. + +# Copyright (C) 2002-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +printf '1\n2\n3\n4\n5\n' > in || framework_failure_ + +split --lines=2 in > out || fail=1 +cat <<\EOF > exp-1 +1 +2 +EOF +cat <<\EOF > exp-2 +3 +4 +EOF +cat <<\EOF > exp-3 +5 +EOF + +compare exp-1 xaa || fail=1 +compare exp-2 xab || fail=1 +compare exp-3 xac || fail=1 +test -f xad && fail=1 + +Exit $fail diff --git a/tests/split/numeric.sh b/tests/split/numeric.sh new file mode 100755 index 0000000..23edd14 --- /dev/null +++ b/tests/split/numeric.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# Test --{hex,numeric}-suffixes[=from] + +# Copyright (C) 2012-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +printf '1\n2\n3\n4\n5\n' > in || framework_failure_ + +printf '%s\n' 1 2 > exp-0 || framework_failure_ +printf '%s\n' 3 4 > exp-1 || framework_failure_ +printf '%s\n' 5 > exp-2 || framework_failure_ + +for mode in 'numeric' 'hex'; do + + for start in 0 9; do + mode_option="--$mode-suffixes" + # check with and without specified start value + test $start != '0' && mode_option="$mode_option=$start" + split $mode_option --lines=2 in || fail=1 + + test $mode = 'hex' && format=x || format=d + for i in $(seq $start $(($start+2))); do + compare exp-$(($i-$start)) x$(printf %02$format $i) || fail=1 + done + done + + # Check that split failed when suffix length is not large enough for + # the numerical suffix start value + returns_ 1 split -a 3 --$mode-suffixes=1000 in 2>/dev/null || fail=1 + + # check invalid --$mode-suffixes start values are flagged + returns_ 1 split --$mode-suffixes=-1 in 2> /dev/null || fail=1 + returns_ 1 split --$mode-suffixes=one in 2> /dev/null || fail=1 +done + +Exit $fail diff --git a/tests/split/r-chunk.sh b/tests/split/r-chunk.sh new file mode 100755 index 0000000..a32b7ea --- /dev/null +++ b/tests/split/r-chunk.sh @@ -0,0 +1,64 @@ +#!/bin/sh +# test splitting into round-robin chunks + +# Copyright (C) 2010-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +# N can be greater than the file size +# in which case no data is extracted, or empty files are written +split -n r/10 /dev/null || fail=1 +test "$(stat -c %s x* | uniq -c | sed 's/^ *//; s/ /x/')" = "10x0" || fail=1 +rm x?? + +# Ensure --elide-empty-files is honored +split -e -n r/10 /dev/null || fail=1 +stat x?? 2>/dev/null && fail=1 + +printf '1\n2\n3\n4\n5\n' > in || framework_failure_ + +split -n r/3 in > out || fail=1 +compare /dev/null out || fail=1 + +split -n r/1/3 in > r1 || fail=1 +split -n r/2/3 in > r2 || fail=1 +split -n r/3/3 in > r3 || fail=1 + +printf '1\n4\n' > exp-1 +printf '2\n5\n' > exp-2 +printf '3\n' > exp-3 + +compare exp-1 xaa || fail=1 +compare exp-2 xab || fail=1 +compare exp-3 xac || fail=1 +compare exp-1 r1 || fail=1 +compare exp-2 r2 || fail=1 +compare exp-3 r3 || fail=1 +test -f xad && fail=1 + +# Test input without trailing \n +printf '1\n2\n3\n4\n5' | split -n r/2/3 > out +printf '2\n5' > exp +compare exp out || fail=1 + +# Ensure we fall back to appending to a file at a time +# if we hit the limit for the number of open files. +rm x* +(ulimit -n 20 && yes | head -n90 | split -n r/30 ) || fail=1 +test "$(stat -c %s x* | uniq -c | sed 's/^ *//; s/ /x/')" = "30x6" || fail=1 + +Exit $fail diff --git a/tests/split/record-sep.sh b/tests/split/record-sep.sh new file mode 100755 index 0000000..f51e8fa --- /dev/null +++ b/tests/split/record-sep.sh @@ -0,0 +1,78 @@ +#!/bin/sh +# test split with custom record separators + +# Copyright (C) 2015-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +NL=' +' + +for sep in "$NL" '\0' ':'; do + + test "$sep" = "$NL" && tr='\n' || tr="$sep" + + for mode in '--lines=2' '--line-bytes=4' '--number=l/3' '--number=r/3'; do + + # Generate in default mode for comparison + printf '1\n2\n3\n4\n5\n' > in || framework_failure_ + split $mode in || fail=1 + tr '\n' "$tr" < xaa > exp1 + tr '\n' "$tr" < xab > exp2 + tr '\n' "$tr" < xac > exp3 + + rm -f x?? + + # Generate output with specified --separator + printf '1\n2\n3\n4\n5\n' | tr '\n' "$tr" > in || framework_failure_ + split $mode -t "$sep" in || fail=1 + + compare exp1 xaa || fail=1 + compare exp2 xab || fail=1 + compare exp3 xac || fail=1 + test -f xad && fail=1 + done + +done + + +# +# Test usage edge cases +# + +# Should fail: '-t' requires an argument +returns_ 1 split -t </dev/null || + { warn_ "-t without argument did not trigger an error" ; fail=1 ; } + +# should fail: multi-character separator +returns_ 1 split -txx </dev/null || + { warn_ "-txx did not trigger an error" ; fail=1 ; } + +# should fail: different separators used +returns_ 1 split -ta -tb </dev/null || + { warn_ "-ta -tb did not trigger an error" ; fail=1 ; } + +# should fail: different separators used, including default +returns_ 1 split -t"$NL" -tb </dev/null || + { warn_ "-t\$NL -tb did not trigger an error" ; fail=1 ; } + +# should not fail: same separator used multiple times +split -t: -t: </dev/null || + { warn_ "-t: -t: triggered an error" ; fail=1 ; } + + +Exit $fail diff --git a/tests/split/suffix-auto-length.sh b/tests/split/suffix-auto-length.sh new file mode 100755 index 0000000..3f9378b --- /dev/null +++ b/tests/split/suffix-auto-length.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# Test the suffix auto width functionality + +# Copyright (C) 2012-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + + +# ensure auto widening is off when start number specified +truncate -s12 file.in || framework_failure_ +returns_ 1 split file.in -b1 --numeric=89 || fail=1 +test "$(ls -1 x* | wc -l)" = 11 || fail=1 +rm -f x* + +# ensure auto widening works when no start num specified +truncate -s91 file.in || framework_failure_ +for prefix in 'x' 'xx' ''; do + for add_suffix in '.txt' ''; do + split file.in "$prefix" -b1 --numeric --additional-suffix="$add_suffix" \ + || fail=1 + test "$(ls -1 $prefix*[0-9]*$add_suffix | wc -l)" = 91 || fail=1 + test -e "${prefix}89$add_suffix" || fail=1 + test -e "${prefix}9000$add_suffix" || fail=1 + rm -f $prefix*[0-9]*$add_suffix + done +done + +# ensure auto width with --number and start num < number of files +# That's the single run use case which is valid to adjust suffix len +truncate -s100 file.in || framework_failure_ +split --numeric-suffixes=1 --number=r/100 file.in || fail=1 +rm -f x* + +# ensure no auto width with --number and start num >= number of files +# That's the multi run use case which is invalid to adjust suffix len +# as that would result in an incorrect order for the total output file set +returns_ 1 split --numeric-suffixes=100 --number=r/100 file.in || fail=1 + +# coreutils v8.24 - v8.31 inclusive would incorrectly auto calculate +# a suffix length that was too small, when the number of files was +# evenly divisible by the suffix base (10,16,26). +truncate -s0 file.in || framework_failure_ +split --numeric-suffixes --number=110 file.in || fail=1 + +Exit $fail diff --git a/tests/split/suffix-length.sh b/tests/split/suffix-length.sh new file mode 100755 index 0000000..bb2f9fa --- /dev/null +++ b/tests/split/suffix-length.sh @@ -0,0 +1,76 @@ +#!/bin/sh +# Show that split -a works. + +# Copyright (C) 2002-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ split + +a_z='a b c d e f g h i j k l m n o p q r s t u v w x y z' + +# Generate a 27-byte file +printf %s $a_z 0 |tr -d ' ' > in || framework_failure_ + +files= +for i in $a_z; do + files="${files}xa$i " +done +files="${files}xba" + +for f in $files; do + printf "creating file '%s'"'\n' $f +done > exp || framework_failure_ + +echo split: output file suffixes exhausted \ + > exp-too-short || framework_failure_ + + +# This should fail. +split -b 1 -a 1 in 2> err && fail=1 +test -f xa || fail=1 +test -f xz || fail=1 +test -f xaa && fail=1 +test -f xaz && fail=1 +rm -f x* +compare exp-too-short err || fail=1 + +# With a longer suffix, it must succeed. +split --verbose -b 1 -a 2 in > err || fail=1 +compare exp err || fail=1 + +# Ensure that xbb is *not* created. +test -f xbb && fail=1 + +# Ensure that the 27 others files *were* created, and with expected contents. +n=1 +for f in $files; do + expected_byte=$(cut -b $n in) + b=$(cat $f) || fail=1 + test "$b" = "$expected_byte" || fail=1 + n=$(expr $n + 1) +done + +# Ensure that -a is independent of -[bCl] +split -a2 -b1000 < /dev/null || fail=1 +split -a2 -l1000 < /dev/null || fail=1 +split -a2 -C1000 < /dev/null || fail=1 + +# Ensure that -a fails early with a -n that is too large +rm -f x* +returns_ 1 split -a2 -n1000 < /dev/null || fail=1 +test -f xaa && fail=1 + +Exit $fail |