xref: /freebsd/usr.bin/head/tests/head_test.sh (revision 9768746b)
1# Copyright (c) 2017 Fred Schlechter
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23# SUCH DAMAGE.
24#
25# $FreeBSD$
26
27atf_test_case empty_file
28empty_file_head() {
29	atf_set "descr" "Test head(1)'s handling of an empty file"
30}
31empty_file_body() {
32	touch infile expectfile
33	head infile > outfile
34	head < infile > outpipe
35	atf_check cmp expectfile outfile
36	atf_check cmp expectfile outpipe
37}
38
39atf_test_case default_no_options
40default_no_options_head() {
41	atf_set "descr" "Test head(1)'s default mode"
42}
43default_no_options_body() {
44	#head(1) is supposed to default to 10 lines of output. Verify that it does that.
45	jot -b test 10 > expectfile
46	jot -b test 100 > infile
47	head infile > outfile
48	atf_check -e empty cmp expectfile outfile
49}
50
51atf_test_case line_count
52line_count_head() {
53	atf_set "descr" "Test head(1)'s -n option"
54}
55line_count_body() {
56	jot -b test 100 > outfile
57	head -n 50 outfile > expectfile
58	atf_check -o inline:"      50 expectfile\n" wc -l expectfile
59}
60
61atf_test_case byte_count
62byte_count_head() {
63	atf_set "descr" "Test head(1)'s -c option"
64}
65byte_count_body() {
66	jot -b test 100 > outfile
67	head -c 50 outfile > expectfile
68	atf_check -o inline:"      50 expectfile\n" wc -c expectfile
69}
70
71atf_test_case sparse_file_text_at_beginning
72sparse_file_text_at_beginning_head() {
73	atf_set "descr" "Test head(1)'s handling of a sparse file with text at the beginning of the file"
74}
75sparse_file_text_at_beginning_body () {
76	jot -b test 10 > outfile
77	truncate -s +1K outfile
78	head -c 512 outfile > expectfile
79	atf_check -o inline:"     512 expectfile\n" wc -c expectfile
80}
81
82atf_test_case sparse_file_text_at_end
83sparse_file_text_at_end_head() {
84	atf_set "descr" "Test head(1)'s handling of a sparse file with text at the end of the file"
85}
86sparse_file_text_at_end_body () {
87	truncate -s +1K infile
88	echo test >> infile
89	head -c 4096 < infile > outpipe
90	atf_check cmp infile outpipe
91}
92
93atf_test_case missing_line_count
94missing_line_count_head() {
95	atf_set "descr" "Test head(1)'s handling of a missing line count arg"
96}
97missing_line_count_body () {
98	jot -b test 100 > outfile
99	atf_check -s not-exit:0 -e not-empty head -n outfile
100}
101
102atf_test_case invalid_line_count
103invalid_line_count_head() {
104	atf_set "descr" "Test head(1)'s handling of an invalid line count arg"
105}
106invalid_line_count_body () {
107	jot -b test 100 > outfile
108	atf_check -s not-exit:0 -e not-empty head -n -10 outfile
109}
110
111atf_test_case read_from_stdin
112read_from_stdin_head() {
113	atf_set "descr" "Test head(1)'s reading of stdin"
114}
115read_from_stdin_body() {
116	#head(1) defaults to head -n 10 if no args are given.
117	jot -b test 10 > outfile
118	jot -b test 20 | head > expectfile
119	atf_check cmp outfile expectfile
120}
121
122atf_test_case silent_header
123silent_header_head() {
124	atf_set "descr" "Test head(1)'s silent header feature"
125}
126silent_header_body() {
127	#head(1) defaults to head -n 10 if no args are given.
128	jot 11 1 11 > file1
129	jot 11 2 12 > file2
130	jot 10 1 10 > expectfile
131	jot 10 2 11 >> expectfile
132	head -q file1 file2 > outfile
133	atf_check cmp outfile expectfile
134}
135
136atf_test_case verbose_header
137verbose_header_head() {
138	atf_set "descr" "Test head(1)'s verbose header feature"
139}
140verbose_header_body() {
141	#head(1) defaults to head -n 10 if no args are given.
142	jot -b test 10 > file1
143	echo '==> file1 <==' > expectfile
144	cat file1 >> expectfile
145	head -v file1 > outfile
146	atf_check cmp outfile expectfile
147}
148
149atf_test_case si_number
150si_number_head() {
151	atf_set "descr" "Test head(1)'s SI number feature"
152}
153si_number_body() {
154	jot -b aaaaaaa 129 > file1
155	jot -b aaaaaaa 128 > expectfile
156	head -c 1k file1 > outfile
157	atf_check cmp outfile expectfile
158	jot 1025 1 1025 > file1
159	jot 1024 1 1024 > expectfile
160	head -n 1k file1 > outfile
161	atf_check cmp outfile expectfile
162}
163
164atf_init_test_cases() {
165	atf_add_test_case empty_file
166	atf_add_test_case default_no_options
167	atf_add_test_case line_count
168	atf_add_test_case byte_count
169	atf_add_test_case sparse_file_text_at_beginning
170	atf_add_test_case sparse_file_text_at_end
171	atf_add_test_case missing_line_count
172	atf_add_test_case invalid_line_count
173	atf_add_test_case read_from_stdin
174	atf_add_test_case silent_header
175	atf_add_test_case verbose_header
176	atf_add_test_case si_number
177}
178