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 # Regression test for PR 222671
87 # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=222671
88 atf_test_case pipe_leading_newline_r
89 pipe_leading_newline_r_head()
91 atf_set "descr" "Reverse a pipe whose first character is a newline"
93 pipe_leading_newline_r_body()
95 cat > expectfile << HERE
101 printf '\n1\n2\n3\n' | tail -r > outfile
102 printf '\n1\n2\n3\n' | tail -r > outpipe
103 atf_check cmp expectfile outfile
104 atf_check cmp expectfile outpipe
107 atf_test_case file_rc28
110 atf_set "descr" "Reverse a file and display the last 28 characters"
115 This is the first line
116 This is the second line
117 This is the third line
119 cat > expectfile << HERE
120 This is the third line
123 tail -rc28 infile > outfile
124 tail -rc28 < infile > outpipe
125 atf_check cmp expectfile outfile
126 atf_check cmp expectfile outpipe
129 atf_test_case file_rc28
132 atf_set "descr" "Reverse a file and display the last 28 characters"
137 This is the first line
138 This is the second line
139 This is the third line
141 cat > expectfile << HERE
142 This is the third line
145 tail -rc28 infile > outfile
146 tail -rc28 < infile > outpipe
147 atf_check cmp expectfile outfile
148 atf_check cmp expectfile outpipe
151 atf_test_case longfile_r
154 atf_set "descr" "Reverse a long file"
158 jot -w "%0511d" 1030 0 > infile
159 jot -w "%0511d" 1030 1029 0 -1 > expectfile
160 tail -r infile > outfile
161 tail -r < infile > outpipe
162 atf_check cmp expectfile outfile
163 atf_check cmp expectfile outpipe
166 atf_test_case longfile_r_enomem
167 longfile_r_enomem_head()
169 atf_set "descr" "Reverse a file that's too long to store in RAM"
171 longfile_r_enomem_body()
173 # When we reverse a file that's too long for RAM, tail should drop the
174 # first part and just print what it can. We'll check that the last
177 ulimit -v 32768 || atf_skip "Can't adjust ulimit"
178 jot -w "%01023d" 32768 0 | tail -r > outfile ;
180 if [ "$?" -ne 1 ]; then
181 atf_skip "Didn't get ENOMEM. Adjust test parameters"
183 # We don't know how much of the input we dropped. So just check that
184 # the first ten lines of tail's output are the same as the last ten of
186 jot -w "%01023d" 10 32767 0 -1 > expectfile
187 head -n 10 outfile > outtrunc
188 diff expectfile outtrunc
189 atf_check cmp expectfile outtrunc
192 atf_test_case longfile_r_longlines
193 longfile_r_longlines_head()
195 atf_set "descr" "Reverse a long file with extremely long lines"
197 longfile_r_longlines_body()
199 jot -s " " -w "%07d" 18000 0 > infile
200 jot -s " " -w "%07d" 18000 18000 >> infile
201 jot -s " " -w "%07d" 18000 36000 >> infile
202 jot -s " " -w "%07d" 18000 36000 > expectfile
203 jot -s " " -w "%07d" 18000 18000 >> expectfile
204 jot -s " " -w "%07d" 18000 0 >> expectfile
205 tail -r infile > outfile
206 tail -r < infile > outpipe
207 atf_check cmp expectfile outfile
208 atf_check cmp expectfile outpipe
211 atf_test_case longfile_rc135782
212 longfile_rc135782_head()
214 atf_set "descr" "Reverse a long file and print the last 135,782 bytes"
216 longfile_rc135782_body()
218 jot -w "%063d" 9000 0 > infile
219 jot -w "%063d" 2121 8999 0 -1 > expectfile
220 echo "0000000000000000000000000000000006878" >> expectfile
221 tail -rc135782 infile > outfile
222 tail -rc135782 < infile > outpipe
223 atf_check cmp expectfile outfile
224 atf_check cmp expectfile outpipe
227 atf_test_case longfile_rc145782_longlines
228 longfile_rc145782_longlines_head()
230 atf_set "descr" "Reverse a long file with extremely long lines and print the last 145,782 bytes"
232 longfile_rc145782_longlines_body()
234 jot -s " " -w "%07d" 18000 0 > infile
235 jot -s " " -w "%07d" 18000 18000 >> infile
236 jot -s " " -w "%07d" 18000 36000 >> infile
237 jot -s " " -w "%07d" 18000 36000 > expectfile
238 echo -n "35777 " >> expectfile
239 jot -s " " -w "%07d" 222 35778 >> expectfile
240 tail -rc145782 infile > outfile
241 tail -rc145782 < infile > outpipe
242 atf_check cmp expectfile outfile
243 atf_check cmp expectfile outpipe
246 atf_test_case longfile_rn2500
247 longfile_rn2500_head()
249 atf_set "descr" "Reverse a long file and print the last 2,500 lines"
251 longfile_rn2500_body()
253 jot -w "%063d" 9000 0 > infile
254 jot -w "%063d" 2500 8999 0 -1 > expectfile
255 tail -rn2500 infile > outfile
256 tail -rn2500 < infile > outpipe
257 atf_check cmp expectfile outfile
258 atf_check cmp expectfile outpipe
262 atf_init_test_cases()
264 atf_add_test_case empty_r
265 atf_add_test_case file_r
266 atf_add_test_case file_rc28
267 atf_add_test_case file_rn2
268 atf_add_test_case pipe_leading_newline_r
269 # The longfile tests are designed to exercise behavior in r_buf(),
270 # which operates on 128KB blocks
271 atf_add_test_case longfile_r
272 atf_add_test_case longfile_r_enomem
273 atf_add_test_case longfile_r_longlines
274 atf_add_test_case longfile_rc135782
275 atf_add_test_case longfile_rc145782_longlines
276 atf_add_test_case longfile_rn2500