| 1 |
#!/bin/sh |
| 2 |
# Test 'cmp'. |
| 3 |
|
| 4 |
# Copyright 2017-2025 Free Software Foundation, Inc. |
| 5 |
|
| 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. |
| 10 |
|
| 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. |
| 15 |
|
| 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/>. |
| 18 |
|
| 19 |
. "${srcdir=.}/init.sh"; path_prepend_ ../src |
| 20 |
|
| 21 |
fail=0 |
| 22 |
|
| 23 |
cat <<'EOF' > exp || fail=1 |
| 24 |
cmp a a |
| 25 |
0 |
| 26 |
cmp a b |
| 27 |
a b differ: char 1, line 1 |
| 28 |
1 |
| 29 |
cmp a c |
| 30 |
cmp: EOF on 'c' which is empty |
| 31 |
1 |
| 32 |
cmp a d |
| 33 |
cmp: d: No such file or directory |
| 34 |
2 |
| 35 |
cmp b a |
| 36 |
b a differ: char 1, line 1 |
| 37 |
1 |
| 38 |
cmp b b |
| 39 |
0 |
| 40 |
cmp b c |
| 41 |
cmp: EOF on 'c' which is empty |
| 42 |
1 |
| 43 |
cmp b d |
| 44 |
cmp: d: No such file or directory |
| 45 |
2 |
| 46 |
cmp c a |
| 47 |
cmp: EOF on 'c' which is empty |
| 48 |
1 |
| 49 |
cmp c b |
| 50 |
cmp: EOF on 'c' which is empty |
| 51 |
1 |
| 52 |
cmp c c |
| 53 |
0 |
| 54 |
cmp c d |
| 55 |
cmp: d: No such file or directory |
| 56 |
2 |
| 57 |
cmp d a |
| 58 |
cmp: d: No such file or directory |
| 59 |
2 |
| 60 |
cmp d b |
| 61 |
cmp: d: No such file or directory |
| 62 |
2 |
| 63 |
cmp d c |
| 64 |
cmp: d: No such file or directory |
| 65 |
2 |
| 66 |
cmp d d |
| 67 |
cmp: d: No such file or directory |
| 68 |
2 |
| 69 |
cmp -l a a |
| 70 |
0 |
| 71 |
cmp -l a b |
| 72 |
1 141 142 |
| 73 |
1 |
| 74 |
cmp -l a c |
| 75 |
cmp: EOF on 'c' which is empty |
| 76 |
1 |
| 77 |
cmp -l a d |
| 78 |
cmp: d: No such file or directory |
| 79 |
2 |
| 80 |
cmp -l b a |
| 81 |
1 142 141 |
| 82 |
1 |
| 83 |
cmp -l b b |
| 84 |
0 |
| 85 |
cmp -l b c |
| 86 |
cmp: EOF on 'c' which is empty |
| 87 |
1 |
| 88 |
cmp -l b d |
| 89 |
cmp: d: No such file or directory |
| 90 |
2 |
| 91 |
cmp -l c a |
| 92 |
cmp: EOF on 'c' which is empty |
| 93 |
1 |
| 94 |
cmp -l c b |
| 95 |
cmp: EOF on 'c' which is empty |
| 96 |
1 |
| 97 |
cmp -l c c |
| 98 |
0 |
| 99 |
cmp -l c d |
| 100 |
cmp: d: No such file or directory |
| 101 |
2 |
| 102 |
cmp -l d a |
| 103 |
cmp: d: No such file or directory |
| 104 |
2 |
| 105 |
cmp -l d b |
| 106 |
cmp: d: No such file or directory |
| 107 |
2 |
| 108 |
cmp -l d c |
| 109 |
cmp: d: No such file or directory |
| 110 |
2 |
| 111 |
cmp -l d d |
| 112 |
cmp: d: No such file or directory |
| 113 |
2 |
| 114 |
cmp -s a a |
| 115 |
0 |
| 116 |
cmp -s a b |
| 117 |
1 |
| 118 |
cmp -s a c |
| 119 |
1 |
| 120 |
cmp -s a d |
| 121 |
2 |
| 122 |
cmp -s b a |
| 123 |
1 |
| 124 |
cmp -s b b |
| 125 |
0 |
| 126 |
cmp -s b c |
| 127 |
1 |
| 128 |
cmp -s b d |
| 129 |
2 |
| 130 |
cmp -s c a |
| 131 |
1 |
| 132 |
cmp -s c b |
| 133 |
1 |
| 134 |
cmp -s c c |
| 135 |
0 |
| 136 |
cmp -s c d |
| 137 |
2 |
| 138 |
cmp -s d a |
| 139 |
2 |
| 140 |
cmp -s d b |
| 141 |
2 |
| 142 |
cmp -s d c |
| 143 |
2 |
| 144 |
cmp -s d d |
| 145 |
2 |
| 146 |
EOF |
| 147 |
|
| 148 |
echo a >a |
| 149 |
echo b >b |
| 150 |
: >c |
| 151 |
rm -f d |
| 152 |
|
| 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 |
| 164 |
|
| 165 |
compare exp out || fail=1 |
| 166 |
|
| 167 |
cat <<'EOF' > exp1 || fail=1 |
| 168 |
cmp a0 a1 |
| 169 |
cmp: EOF on 'a0' which is empty |
| 170 |
1 |
| 171 |
cmp a1 a2 |
| 172 |
cmp: EOF on 'a1' after byte 2, line 1 |
| 173 |
1 |
| 174 |
cmp a2 a3 |
| 175 |
cmp: EOF on 'a2' after byte 5, in line 2 |
| 176 |
1 |
| 177 |
cmp -l a0 a1 |
| 178 |
cmp: EOF on 'a0' which is empty |
| 179 |
1 |
| 180 |
cmp -l a1 a2 |
| 181 |
cmp: EOF on 'a1' after byte 2 |
| 182 |
1 |
| 183 |
cmp -l a2 a3 |
| 184 |
cmp: EOF on 'a2' after byte 5 |
| 185 |
1 |
| 186 |
cmp -s a0 a1 |
| 187 |
1 |
| 188 |
cmp -s a1 a2 |
| 189 |
1 |
| 190 |
cmp -s a2 a3 |
| 191 |
1 |
| 192 |
EOF |
| 193 |
|
| 194 |
printf '' >a0 |
| 195 |
printf '1\n' >a1 |
| 196 |
printf '1\nfoo' >a2 |
| 197 |
printf '1\nfoolery\n' >a3 |
| 198 |
|
| 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 |
| 208 |
|
| 209 |
compare exp1 out1 || fail=1 |
| 210 |
|
| 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 |
| 220 |
|
| 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 . |
| 226 |
EOF |
| 227 |
cmp -bl j1 j2 > out2 |
| 228 |
test $? -eq 1 || fail=1 |
| 229 |
compare exp2 out2 || fail=1 |
| 230 |
|
| 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 ^? |
| 238 |
EOF |
| 239 |
LC_ALL=en_US.iso8859-1 cmp -bl j3 j4 >out3 |
| 240 |
test $? -eq 1 || fail=1 |
| 241 |
compare exp3 out3 || fail=1 |
| 242 |
fi |
| 243 |
|
| 244 |
big=99999999999999999999999999999999999999999999999999999999999 |
| 245 |
cmp -i $big j1 j2 || fail=1 |
| 246 |
cmp -i 1000 -n $big j1 j2 || fail=1 |
| 247 |
|
| 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.4 cmp a b >/dev/null || fail=1 |
| 251 |
fi |
| 252 |
rm -f a b |
| 253 |
|
| 254 |
Exit $fail |