1 # Copyright (c) 2017 Fred Schlechter
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
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.
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
27 atf_test_case empty_file
29 atf_set "descr" "Test head(1)'s handling of an empty file"
32 touch infile expectfile
34 head < infile > outpipe
35 atf_check cmp expectfile outfile
36 atf_check cmp expectfile outpipe
39 atf_test_case default_no_options
40 default_no_options_head() {
41 atf_set "descr" "Test head(1)'s default mode"
43 default_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
48 atf_check -e empty cmp expectfile outfile
51 atf_test_case line_count
53 atf_set "descr" "Test head(1)'s -n option"
56 jot -b test 100 > outfile
57 head -n 50 outfile > expectfile
58 atf_check -o inline:" 50 expectfile\n" wc -l expectfile
61 atf_test_case byte_count
63 atf_set "descr" "Test head(1)'s -c option"
66 jot -b test 100 > outfile
67 head -c 50 outfile > expectfile
68 atf_check -o inline:" 50 expectfile\n" wc -c expectfile
71 atf_test_case sparse_file_text_at_beginning
72 sparse_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"
75 sparse_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
82 atf_test_case sparse_file_text_at_end
83 sparse_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"
86 sparse_file_text_at_end_body () {
87 truncate -s +1K infile
89 head -c 4096 < infile > outpipe
90 atf_check cmp infile outpipe
93 atf_test_case missing_line_count
94 missing_line_count_head() {
95 atf_set "descr" "Test head(1)'s handling of a missing line count arg"
97 missing_line_count_body () {
98 jot -b test 100 > outfile
99 atf_check -s not-exit:0 -e not-empty head -n outfile
102 atf_test_case invalid_line_count
103 invalid_line_count_head() {
104 atf_set "descr" "Test head(1)'s handling of an invalid line count arg"
106 invalid_line_count_body () {
107 jot -b test 100 > outfile
108 atf_check -s not-exit:0 -e not-empty head -n -10 outfile
111 atf_test_case read_from_stdin
112 read_from_stdin_head() {
113 atf_set "descr" "Test head(1)'s reading of stdin"
115 read_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
122 atf_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