1 # Copyright (c) 2016 Alan Somers
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
30 atf_set "descr" "Reverse an empty file"
34 touch infile expectfile
35 tail -r infile > outfile
36 tail -r < infile > outpipe
37 atf_check cmp expectfile outfile
38 atf_check cmp expectfile outpipe
44 atf_set "descr" "Reverse a file"
49 This is the first line
50 This is the second line
51 This is the third line
53 cat > expectfile << HERE
54 This is the third line
55 This is the second line
56 This is the first line
58 tail -r infile > outfile
59 tail -r < infile > outpipe
60 atf_check cmp expectfile outfile
61 atf_check cmp expectfile outpipe
64 atf_test_case file_rn2
67 atf_set "descr" "Reverse the last two lines of a file"
72 This is the first line
73 This is the second line
74 This is the third line
76 cat > expectfile << HERE
77 This is the third line
78 This is the second line
80 tail -rn2 infile > outfile
81 tail -rn2 < infile > outpipe
82 atf_check cmp expectfile outfile
83 atf_check cmp expectfile outpipe
86 atf_test_case file_rc28
89 atf_set "descr" "Reverse a file and display the last 28 characters"
94 This is the first line
95 This is the second line
96 This is the third line
98 cat > expectfile << HERE
99 This is the third line
102 tail -rc28 infile > outfile
103 tail -rc28 < infile > outpipe
104 atf_check cmp expectfile outfile
105 atf_check cmp expectfile outpipe
108 atf_test_case longfile_r
111 atf_set "descr" "Reverse a long file"
115 jot -w "%0511d" 1030 0 > infile
116 jot -w "%0511d" 1030 1029 0 -1 > expectfile
117 tail -r infile > outfile
118 tail -r < infile > outpipe
119 atf_check cmp expectfile outfile
120 atf_check cmp expectfile outpipe
123 atf_test_case longfile_r_enomem
124 longfile_r_enomem_head()
126 atf_set "descr" "Reverse a file that's too long to store in RAM"
128 longfile_r_enomem_body()
130 # When we reverse a file that's too long for RAM, tail should drop the
131 # first part and just print what it can. We'll check that the last
134 ulimit -v 32768 || atf_skip "Can't adjust ulimit"
135 jot -w "%01023d" 32768 0 | tail -r > outfile ;
137 if [ "$?" -ne 1 ]; then
138 atf_skip "Didn't get ENOMEM. Adjust test parameters"
140 # We don't know how much of the input we dropped. So just check that
141 # the first ten lines of tail's output are the same as the last ten of
143 jot -w "%01023d" 10 32767 0 -1 > expectfile
144 head -n 10 outfile > outtrunc
145 diff expectfile outtrunc
146 atf_check cmp expectfile outtrunc
149 atf_test_case longfile_r_longlines
150 longfile_r_longlines_head()
152 atf_set "descr" "Reverse a long file with extremely long lines"
154 longfile_r_longlines_body()
156 jot -s " " -w "%07d" 18000 0 > infile
157 jot -s " " -w "%07d" 18000 18000 >> infile
158 jot -s " " -w "%07d" 18000 36000 >> infile
159 jot -s " " -w "%07d" 18000 36000 > expectfile
160 jot -s " " -w "%07d" 18000 18000 >> expectfile
161 jot -s " " -w "%07d" 18000 0 >> expectfile
162 tail -r infile > outfile
163 tail -r < infile > outpipe
164 atf_check cmp expectfile outfile
165 atf_check cmp expectfile outpipe
168 atf_test_case longfile_rc135782
169 longfile_rc135782_head()
171 atf_set "descr" "Reverse a long file and print the last 135,782 bytes"
173 longfile_rc135782_body()
175 jot -w "%063d" 9000 0 > infile
176 jot -w "%063d" 2121 8999 0 -1 > expectfile
177 echo "0000000000000000000000000000000006878" >> expectfile
178 tail -rc135782 infile > outfile
179 tail -rc135782 < infile > outpipe
180 atf_check cmp expectfile outfile
181 atf_check cmp expectfile outpipe
184 atf_test_case longfile_rc145782_longlines
185 longfile_rc145782_longlines_head()
187 atf_set "descr" "Reverse a long file with extremely long lines and print the last 145,782 bytes"
189 longfile_rc145782_longlines_body()
191 jot -s " " -w "%07d" 18000 0 > infile
192 jot -s " " -w "%07d" 18000 18000 >> infile
193 jot -s " " -w "%07d" 18000 36000 >> infile
194 jot -s " " -w "%07d" 18000 36000 > expectfile
195 echo -n "35777 " >> expectfile
196 jot -s " " -w "%07d" 222 35778 >> expectfile
197 tail -rc145782 infile > outfile
198 tail -rc145782 < infile > outpipe
199 atf_check cmp expectfile outfile
200 atf_check cmp expectfile outpipe
203 atf_test_case longfile_rn2500
204 longfile_rn2500_head()
206 atf_set "descr" "Reverse a long file and print the last 2,500 lines"
208 longfile_rn2500_body()
210 jot -w "%063d" 9000 0 > infile
211 jot -w "%063d" 2500 8999 0 -1 > expectfile
212 tail -rn2500 infile > outfile
213 tail -rn2500 < infile > outpipe
214 atf_check cmp expectfile outfile
215 atf_check cmp expectfile outpipe
219 atf_init_test_cases()
221 atf_add_test_case empty_r
222 atf_add_test_case file_r
223 atf_add_test_case file_rc28
224 atf_add_test_case file_rn2
225 # The longfile tests are designed to exercise behavior in r_buf(),
226 # which operates on 128KB blocks
227 atf_add_test_case longfile_r
228 atf_add_test_case longfile_r_enomem
229 atf_add_test_case longfile_r_longlines
230 atf_add_test_case longfile_rc135782
231 atf_add_test_case longfile_rc145782_longlines
232 atf_add_test_case longfile_rn2500