xref: /freebsd/usr.bin/head/tests/head_test.sh (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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_init_test_cases() {
123	atf_add_test_case empty_file
124	atf_add_test_case default_no_options
125	atf_add_test_case line_count
126	atf_add_test_case byte_count
127	atf_add_test_case sparse_file_text_at_beginning
128	atf_add_test_case sparse_file_text_at_end
129	atf_add_test_case missing_line_count
130	atf_add_test_case invalid_line_count
131	atf_add_test_case read_from_stdin
132}
133