1#!/bin/sh
2# test splitting into 3 chunks
3
4# Copyright (C) 2010-2023 Free Software Foundation, Inc.
5
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.
10
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.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ split
21
22# N can be greater than the file size
23# in which case no data is extracted, or empty files are written
24split -n 10 /dev/null || fail=1
25test "$(stat -c %s x* | uniq -c | sed 's/^ *//; s/ /x/')" = "10x0" || fail=1
26rm -f x??
27
28printf 'abc' > abc || framework_failure_
29printf 'a' > exp-a || framework_failure_
30printf 'b' > exp-b || framework_failure_
31printf 'c' > exp-c || framework_failure_
32printf 'ab' > exp-ab || framework_failure_
33split -n 4 abc || fail=1
34compare exp-a xaa || fail=1
35compare exp-b xab || fail=1
36compare exp-c xac || fail=1
37compare /dev/null xad || fail=1
38test ! -f xae || fail=1
39rm -f x??
40split -n 2 abc || fail=1
41compare exp-ab xaa || fail=1
42compare exp-c xab || fail=1
43test ! -f xac || fail=1
44rm -f x??
45
46# When extracting K of N where N > file size
47# no data is extracted, and no files are written
48split -n 2/3 /dev/null || fail=1
49returns_ 1 stat x?? 2>/dev/null || fail=1
50
51# Ensure --elide-empty-files is honored
52split -e -n 10 /dev/null || fail=1
53returns_ 1 stat x?? 2>/dev/null || fail=1
54
55printf '1\n2\n3\n4\n5\n' > input || framework_failure_
56printf '1\n2\n' > exp-1 || framework_failure_
57printf '3\n4' > exp-2 || framework_failure_
58printf '\n5\n' > exp-3 || framework_failure_
59
60for file in input /proc/version /sys/kernel/profiling; do
61  test -f $file || continue
62
63  for blksize in 1 2 4096; do
64    if ! test "$file" = 'input'; then
65      # For /proc like files we must be able to read all
66      # into the internal buffer to be able to determine size.
67      test "$blksize" = 4096 || continue
68    fi
69
70    split -n 3 ---io-blksize=$blksize $file > out || fail=1
71    split -n 1/3 ---io-blksize=$blksize $file > b1 || fail=1
72    split -n 2/3 ---io-blksize=$blksize $file > b2 || fail=1
73    split -n 3/3 ---io-blksize=$blksize $file > b3 || fail=1
74
75    case $file in
76      input)
77        compare exp-1 xaa || fail=1
78        compare exp-2 xab || fail=1
79        compare exp-3 xac || fail=1
80        ;;
81    esac
82
83    compare xaa b1 || fail=1
84    compare xab b2 || fail=1
85    compare xac b3 || fail=1
86    cat xaa xab xac | compare - $file || fail=1
87    test -f xad && fail=1
88  done
89done
90
91Exit $fail
92