doc: rewrite the "Unusual File Names" section
[diffutils.git] / tests / cmp
blob0c7fa1563e5820a4a01ec028266c552a62851f5a
1 #!/bin/sh
2 # Test 'cmp'.
4 # Copyright 2017-2025 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 . "${srcdir=.}/init.sh"; path_prepend_ ../src
21 fail=0
23 cat <<'EOF' > exp || fail=1
24 cmp a a
26 cmp a b
27 a b differ: char 1, line 1
29 cmp a c
30 cmp: EOF on 'c' which is empty
32 cmp a d
33 cmp: d: No such file or directory
35 cmp b a
36 b a differ: char 1, line 1
38 cmp b b
40 cmp b c
41 cmp: EOF on 'c' which is empty
43 cmp b d
44 cmp: d: No such file or directory
46 cmp c a
47 cmp: EOF on 'c' which is empty
49 cmp c b
50 cmp: EOF on 'c' which is empty
52 cmp c c
54 cmp c d
55 cmp: d: No such file or directory
57 cmp d a
58 cmp: d: No such file or directory
60 cmp d b
61 cmp: d: No such file or directory
63 cmp d c
64 cmp: d: No such file or directory
66 cmp d d
67 cmp: d: No such file or directory
69 cmp -l a a
71 cmp -l a b
72 1 141 142
74 cmp -l a c
75 cmp: EOF on 'c' which is empty
77 cmp -l a d
78 cmp: d: No such file or directory
80 cmp -l b a
81 1 142 141
83 cmp -l b b
85 cmp -l b c
86 cmp: EOF on 'c' which is empty
88 cmp -l b d
89 cmp: d: No such file or directory
91 cmp -l c a
92 cmp: EOF on 'c' which is empty
94 cmp -l c b
95 cmp: EOF on 'c' which is empty
97 cmp -l c c
99 cmp -l c d
100 cmp: d: No such file or directory
102 cmp -l d a
103 cmp: d: No such file or directory
105 cmp -l d b
106 cmp: d: No such file or directory
108 cmp -l d c
109 cmp: d: No such file or directory
111 cmp -l d d
112 cmp: d: No such file or directory
114 cmp -s a a
116 cmp -s a b
118 cmp -s a c
120 cmp -s a d
122 cmp -s b a
124 cmp -s b b
126 cmp -s b c
128 cmp -s b d
130 cmp -s c a
132 cmp -s c b
134 cmp -s c c
136 cmp -s c d
138 cmp -s d a
140 cmp -s d b
142 cmp -s d c
144 cmp -s d d
148 echo a >a
149 echo b >b
150 : >c
151 rm -f d
153 for option in '' -l -s; do
154 for i in a b c d; do
155 for j in a b c d; do
156 echo cmp $option $i $j
157 cmp $option $i $j >stdout 2>stderr
158 status=$?
159 cat stderr stdout
160 echo $status
161 done
162 done
163 done >out
165 compare exp out || fail=1
167 cat <<'EOF' > exp1 || fail=1
168 cmp a0 a1
169 cmp: EOF on 'a0' which is empty
171 cmp a1 a2
172 cmp: EOF on 'a1' after byte 2, line 1
174 cmp a2 a3
175 cmp: EOF on 'a2' after byte 5, in line 2
177 cmp -l a0 a1
178 cmp: EOF on 'a0' which is empty
180 cmp -l a1 a2
181 cmp: EOF on 'a1' after byte 2
183 cmp -l a2 a3
184 cmp: EOF on 'a2' after byte 5
186 cmp -s a0 a1
188 cmp -s a1 a2
190 cmp -s a2 a3
194 printf '' >a0
195 printf '1\n' >a1
196 printf '1\nfoo' >a2
197 printf '1\nfoolery\n' >a3
199 for option in '' -l -s; do
200 for files in 'a0 a1' 'a1 a2' 'a2 a3'; do
201 echo cmp $option $files
202 cmp $option $files >stdout 2>stderr
203 status=$?
204 cat stderr stdout
205 echo $status
206 done
207 done >out1
209 compare exp1 out1 || fail=1
211 printf 'bad\n' >bad
212 printf 'bug\n' >bug
213 echo LC_ALL=C cmp -b bad bug
214 LC_ALL=C cmp -b bad bug
215 test $? -eq 1 || fail=1
216 case `LC_ALL=C cmp -b bad bug` in
217 'bad bug differ: byte 2, line 1 is '*' a '*' u') ;;
218 *) echo 'expected cmp -b to report a and u'; fail=1;;
219 esac
221 printf 'Jackdaws love my big sphinx of quartz!' > j1
222 printf 'jackdaws love my big sphinx of quartz.' > j2
223 cat <<'EOF' > exp2 || fail=1
224 1 112 J 152 j
225 38 41 ! 56 .
227 cmp -bl j1 j2 > out2
228 test $? -eq 1 || fail=1
229 compare exp2 out2 || fail=1
231 if (LC_ALL=en_US.iso8859-1 locale charmap) >/dev/null 2>&1; then
232 printf '\200\300\377\n' >j3
233 printf '\0@\177\n' >j4
234 cat <<'EOF' >exp3 || fail=1
235 1 200 M-^@ 0 ^@
236 2 300 M-@ 100 @
237 3 377 M-^? 177 ^?
239 LC_ALL=en_US.iso8859-1 cmp -bl j3 j4 >out3
240 test $? -eq 1 || fail=1
241 compare exp3 out3 || fail=1
244 big=99999999999999999999999999999999999999999999999999999999999
245 cmp -i $big j1 j2 || fail=1
246 cmp -i 1000 -n $big j1 j2 || fail=1
248 rm -f a b
249 if timeout 0.1 true && truncate -s 14T a && truncate -s 15T b; then
250 returns_ 1 timeout 0.1 cmp a b >/dev/null || fail=1
252 rm -f a b
254 Exit $fail