1*75c3ca1eSKyle Evans#
2*75c3ca1eSKyle Evans# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*75c3ca1eSKyle Evans#
4*75c3ca1eSKyle Evans# Copyright (c) 2022 Klara Systems
5*75c3ca1eSKyle Evans#
6*75c3ca1eSKyle Evans# Redistribution and use in source and binary forms, with or without
7*75c3ca1eSKyle Evans# modification, are permitted provided that the following conditions
8*75c3ca1eSKyle Evans# are met:
9*75c3ca1eSKyle Evans# 1. Redistributions of source code must retain the above copyright
10*75c3ca1eSKyle Evans#    notice, this list of conditions and the following disclaimer.
11*75c3ca1eSKyle Evans# 2. Redistributions in binary form must reproduce the above copyright
12*75c3ca1eSKyle Evans#    notice, this list of conditions and the following disclaimer in the
13*75c3ca1eSKyle Evans#    documentation and/or other materials provided with the distribution.
14*75c3ca1eSKyle Evans#
15*75c3ca1eSKyle Evans# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*75c3ca1eSKyle Evans# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*75c3ca1eSKyle Evans# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*75c3ca1eSKyle Evans# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*75c3ca1eSKyle Evans# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*75c3ca1eSKyle Evans# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*75c3ca1eSKyle Evans# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*75c3ca1eSKyle Evans# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*75c3ca1eSKyle Evans# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*75c3ca1eSKyle Evans# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*75c3ca1eSKyle Evans# SUCH DAMAGE.
26*75c3ca1eSKyle Evans#
27*75c3ca1eSKyle Evans# $FreeBSD$
28*75c3ca1eSKyle Evans
29*75c3ca1eSKyle Evans# sys/param.h
30*75c3ca1eSKyle Evans: ${MAXBSIZE:=65536}
31*75c3ca1eSKyle Evans
32*75c3ca1eSKyle Evansatf_test_case bytes
33*75c3ca1eSKyle Evansbytes_body()
34*75c3ca1eSKyle Evans{
35*75c3ca1eSKyle Evans	printf "aaaa" > foo-aa
36*75c3ca1eSKyle Evans	printf "bb\nc" > foo-ab
37*75c3ca1eSKyle Evans	printf "ccc\n" > foo-ac
38*75c3ca1eSKyle Evans
39*75c3ca1eSKyle Evans	cat foo-* > foo
40*75c3ca1eSKyle Evans	atf_check split -b 4 foo split-
41*75c3ca1eSKyle Evans	atf_check -o file:foo-aa cat split-aa
42*75c3ca1eSKyle Evans	atf_check -o file:foo-ab cat split-ab
43*75c3ca1eSKyle Evans	atf_check -o file:foo-ac cat split-ac
44*75c3ca1eSKyle Evans
45*75c3ca1eSKyle Evans	# MAXBSIZE is the default buffer size, so we'll split at just a little
46*75c3ca1eSKyle Evans	# bit past the buffer size to make sure that it still properly splits
47*75c3ca1eSKyle Evans	# even when it needs to read again to hit the limit.
48*75c3ca1eSKyle Evans	bsize=$((MAXBSIZE + 12))
49*75c3ca1eSKyle Evans	rm foo-* foo
50*75c3ca1eSKyle Evans	jot -ns "" -b "a" ${bsize} > foo-aa
51*75c3ca1eSKyle Evans	jot -ns "" -b "b" ${bsize} > foo-ab
52*75c3ca1eSKyle Evans	jot -ns "" -b "c" 12 > foo-ac
53*75c3ca1eSKyle Evans
54*75c3ca1eSKyle Evans	cat foo-* > foo
55*75c3ca1eSKyle Evans	atf_check split -b ${bsize} foo split-
56*75c3ca1eSKyle Evans	atf_check -o file:foo-aa cat split-aa
57*75c3ca1eSKyle Evans	atf_check -o file:foo-ab cat split-ab
58*75c3ca1eSKyle Evans	atf_check -o file:foo-ac cat split-ac
59*75c3ca1eSKyle Evans}
60*75c3ca1eSKyle Evans
61*75c3ca1eSKyle Evansatf_test_case chunks
62*75c3ca1eSKyle Evanschunks_body()
63*75c3ca1eSKyle Evans{
64*75c3ca1eSKyle Evans	jot -ns "" -b "a" 4096 > foo
65*75c3ca1eSKyle Evans	jot -ns "" -b "b" 4096 >> foo
66*75c3ca1eSKyle Evans	jot -ns "" -b "c" 4104 >> foo
67*75c3ca1eSKyle Evans
68*75c3ca1eSKyle Evans	chunks=3
69*75c3ca1eSKyle Evans	jot -ns "" -b "a" 4096 > foo-aa
70*75c3ca1eSKyle Evans	jot -ns "" -b "b" 2 >> foo-aa
71*75c3ca1eSKyle Evans	jot -ns "" -b "b" 4094 > foo-ab
72*75c3ca1eSKyle Evans	jot -ns "" -b "c" 4 >> foo-ab
73*75c3ca1eSKyle Evans	jot -ns "" -b "c" 4100 > foo-ac
74*75c3ca1eSKyle Evans
75*75c3ca1eSKyle Evans	atf_check split -n ${chunks} foo split-
76*75c3ca1eSKyle Evans	atf_check -o file:foo-aa cat split-aa
77*75c3ca1eSKyle Evans	atf_check -o file:foo-ab cat split-ab
78*75c3ca1eSKyle Evans	atf_check -o file:foo-ac cat split-ac
79*75c3ca1eSKyle Evans}
80*75c3ca1eSKyle Evans
81*75c3ca1eSKyle Evansatf_test_case sensible_lines
82*75c3ca1eSKyle Evanssensible_lines_body()
83*75c3ca1eSKyle Evans{
84*75c3ca1eSKyle Evans	echo "The quick brown fox" > foo-aa
85*75c3ca1eSKyle Evans	echo "jumps over" > foo-ab
86*75c3ca1eSKyle Evans	echo "the lazy dog" > foo-ac
87*75c3ca1eSKyle Evans
88*75c3ca1eSKyle Evans	cat foo-* > foo
89*75c3ca1eSKyle Evans	atf_check split -l 1 foo split-
90*75c3ca1eSKyle Evans	atf_check -o file:foo-aa cat split-aa
91*75c3ca1eSKyle Evans	atf_check -o file:foo-ab cat split-ab
92*75c3ca1eSKyle Evans	atf_check -o file:foo-ac cat split-ac
93*75c3ca1eSKyle Evans
94*75c3ca1eSKyle Evans	# Try again, make sure that `-` uses stdin as documented.
95*75c3ca1eSKyle Evans	atf_check rm split-*
96*75c3ca1eSKyle Evans	atf_check -x 'split -l 1 - split- < foo'
97*75c3ca1eSKyle Evans	atf_check -o file:foo-aa cat split-aa
98*75c3ca1eSKyle Evans	atf_check -o file:foo-ab cat split-ab
99*75c3ca1eSKyle Evans	atf_check -o file:foo-ac cat split-ac
100*75c3ca1eSKyle Evans
101*75c3ca1eSKyle Evans	# Finally, try with -l == 2; we should see a 2/1 split instead of the
102*75c3ca1eSKyle Evans	# previous 1/1/1.
103*75c3ca1eSKyle Evans	cat foo-aa foo-ab > foo-aa-ng
104*75c3ca1eSKyle Evans	cat foo-ac > foo-ab-ng
105*75c3ca1eSKyle Evans
106*75c3ca1eSKyle Evans	atf_check rm split-*
107*75c3ca1eSKyle Evans	atf_check split -l 2 foo split-
108*75c3ca1eSKyle Evans
109*75c3ca1eSKyle Evans	atf_check -o file:foo-aa-ng cat split-aa
110*75c3ca1eSKyle Evans	atf_check -o file:foo-ab-ng cat split-ab
111*75c3ca1eSKyle Evans}
112*75c3ca1eSKyle Evans
113*75c3ca1eSKyle Evansatf_test_case long_lines
114*75c3ca1eSKyle Evanslong_lines_body()
115*75c3ca1eSKyle Evans{
116*75c3ca1eSKyle Evans
117*75c3ca1eSKyle Evans	# Test file lines will be:
118*75c3ca1eSKyle Evans	# a x MAXBSIZE
119*75c3ca1eSKyle Evans	# b x MAXBSIZE + c x MAXBSIZE
120*75c3ca1eSKyle Evans	# d x 1024
121*75c3ca1eSKyle Evans	#
122*75c3ca1eSKyle Evans	# The historical split(1) implementation wouldn't grow its internal
123*75c3ca1eSKyle Evans	# buffer, so we'd end up with 2/3 split- files being wrong with -l 1.
124*75c3ca1eSKyle Evans	# Notably, split-aa would include most of the first two lines, split-ab
125*75c3ca1eSKyle Evans	# a tiny fraction of the second line, and split-ac the third line.
126*75c3ca1eSKyle Evans	#
127*75c3ca1eSKyle Evans	# Recent split(1) instead grows the buffer until we can either fit the
128*75c3ca1eSKyle Evans	# line or we run out of memory.
129*75c3ca1eSKyle Evans	jot -s "" -b "a" ${MAXBSIZE} > foo-aa
130*75c3ca1eSKyle Evans	jot -ns "" -b "b" ${MAXBSIZE} > foo-ab
131*75c3ca1eSKyle Evans	jot -s "" -b "c" ${MAXBSIZE} >> foo-ab
132*75c3ca1eSKyle Evans	jot -s "" -b "d" 1024 > foo-ac
133*75c3ca1eSKyle Evans
134*75c3ca1eSKyle Evans	cat foo-* > foo
135*75c3ca1eSKyle Evans	atf_check split -l 1 foo split-
136*75c3ca1eSKyle Evans
137*75c3ca1eSKyle Evans	atf_check -o file:foo-aa cat split-aa
138*75c3ca1eSKyle Evans	atf_check -o file:foo-ab cat split-ab
139*75c3ca1eSKyle Evans	atf_check -o file:foo-ac cat split-ac
140*75c3ca1eSKyle Evans}
141*75c3ca1eSKyle Evans
142*75c3ca1eSKyle Evansatf_test_case numeric_suffix
143*75c3ca1eSKyle Evansnumeric_suffix_body()
144*75c3ca1eSKyle Evans{
145*75c3ca1eSKyle Evans	echo "The quick brown fox" > foo-00
146*75c3ca1eSKyle Evans	echo "jumps over" > foo-01
147*75c3ca1eSKyle Evans	echo "the lazy dog" > foo-02
148*75c3ca1eSKyle Evans
149*75c3ca1eSKyle Evans	cat foo-* > foo
150*75c3ca1eSKyle Evans	atf_check split -d -l 1 foo split-
151*75c3ca1eSKyle Evans
152*75c3ca1eSKyle Evans	atf_check -o file:foo-00 cat split-00
153*75c3ca1eSKyle Evans	atf_check -o file:foo-01 cat split-01
154*75c3ca1eSKyle Evans	atf_check -o file:foo-02 cat split-02
155*75c3ca1eSKyle Evans}
156*75c3ca1eSKyle Evans
157*75c3ca1eSKyle Evansatf_test_case larger_suffix_length
158*75c3ca1eSKyle Evanslarger_suffix_length_body()
159*75c3ca1eSKyle Evans{
160*75c3ca1eSKyle Evans	:> foo
161*75c3ca1eSKyle Evans
162*75c3ca1eSKyle Evans	# Generate foo-000 through foo-009, then foo-010 and foo-011
163*75c3ca1eSKyle Evans	for i in $(seq -w 0 11); do
164*75c3ca1eSKyle Evans		len=$((${i##0} + 1))
165*75c3ca1eSKyle Evans		file="foo-0${i}"
166*75c3ca1eSKyle Evans		jot -s "" -b "a" ${len} > ${file}
167*75c3ca1eSKyle Evans		cat ${file} >> foo
168*75c3ca1eSKyle Evans	done
169*75c3ca1eSKyle Evans
170*75c3ca1eSKyle Evans	atf_check split -a 3 -d -l 1 foo split-
171*75c3ca1eSKyle Evans	for i in $(seq -w 0 11); do
172*75c3ca1eSKyle Evans		srcfile="foo-0${i}"
173*75c3ca1eSKyle Evans		splitfile="split-0${i}"
174*75c3ca1eSKyle Evans		atf_check -o file:"${srcfile}" cat "${splitfile}"
175*75c3ca1eSKyle Evans	done
176*75c3ca1eSKyle Evans}
177*75c3ca1eSKyle Evans
178*75c3ca1eSKyle Evansatf_test_case pattern
179*75c3ca1eSKyle Evanspattern_body()
180*75c3ca1eSKyle Evans{
181*75c3ca1eSKyle Evans
182*75c3ca1eSKyle Evans	# Some fake yaml gives us a good realistic use-case for -p, as we can
183*75c3ca1eSKyle Evans	# split on top-level stanzas.
184*75c3ca1eSKyle Evans	cat <<EOF > foo-aa
185*75c3ca1eSKyle Evanscat:
186*75c3ca1eSKyle Evans  aa: true
187*75c3ca1eSKyle Evans  ab: true
188*75c3ca1eSKyle Evans  ac: true
189*75c3ca1eSKyle EvansEOF
190*75c3ca1eSKyle Evans	cat <<EOF > foo-ab
191*75c3ca1eSKyle Evansdog:
192*75c3ca1eSKyle Evans  ba: true
193*75c3ca1eSKyle Evans  bb: true
194*75c3ca1eSKyle Evans  bc: true
195*75c3ca1eSKyle EvansEOF
196*75c3ca1eSKyle Evans
197*75c3ca1eSKyle Evans	cat foo-* > foo
198*75c3ca1eSKyle Evans
199*75c3ca1eSKyle Evans	atf_check split -p "^[^[:space:]]+:" foo split-
200*75c3ca1eSKyle Evans	atf_check -o file:foo-aa cat split-aa
201*75c3ca1eSKyle Evans	atf_check -o file:foo-ab cat split-ab
202*75c3ca1eSKyle Evans}
203*75c3ca1eSKyle Evans
204*75c3ca1eSKyle Evansatf_init_test_cases()
205*75c3ca1eSKyle Evans{
206*75c3ca1eSKyle Evans	atf_add_test_case bytes
207*75c3ca1eSKyle Evans	atf_add_test_case chunks
208*75c3ca1eSKyle Evans	atf_add_test_case sensible_lines
209*75c3ca1eSKyle Evans	atf_add_test_case long_lines
210*75c3ca1eSKyle Evans	atf_add_test_case numeric_suffix
211*75c3ca1eSKyle Evans	atf_add_test_case larger_suffix_length
212*75c3ca1eSKyle Evans	atf_add_test_case pattern
213*75c3ca1eSKyle Evans}
214