1#!/bin/sh
2# Test annotation of sort keys
3
4# Copyright (C) 2010-2023 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 <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ sort printf
21
22number() { cat -n | sed 's/^ *//'; }
23
24cat <<\EOF > exp
251
26 ^ no match for key
27
28^ no match for key
2944
30  ^ no match for key
3133
32  ^ no match for key
332
34 ^ no match for key
351
36 ^ no match for key
37
38^ no match for key
3944
40  ^ no match for key
4133
42  ^ no match for key
432
44 ^ no match for key
45
46^ no match for key
471
48_
492
50_
5133
52__
5344
54__
552>
56  ^ no match for key
573>1
58  _
591>2
60  _
611
62 ^ no match for key
63
64^ no match for key
6544
66  ^ no match for key
6733
68  ^ no match for key
692
70 ^ no match for key
711
72 ^ no match for key
73
74^ no match for key
7544
76  ^ no match for key
7733
78  ^ no match for key
792
80 ^ no match for key
81
82^ no match for key
831
84_
852
86_
8733
88__
8944
90__
912>
92  ^ no match for key
933>1
94  _
951>2
96  _
971
98 ^ no match for key
99
100^ no match for key
10144
102  ^ no match for key
10333
104  ^ no match for key
1052
106 ^ no match for key
1071
108 ^ no match for key
109
110^ no match for key
11144
112  ^ no match for key
11333
114  ^ no match for key
1152
116 ^ no match for key
117
118^ no match for key
1191
120_
1212
122_
12333
124__
12544
126__
1272>
128  ^ no match for key
1293>1
130  _
1311>2
132  _
133
134^ no match for key
135JAN
136___
137FEB
138___
139FEB
140   ^ no match for key
141
142^ no match for key
143JAN
144   ^ no match for key
145JAZZ
146^ no match for key
147
148^ no match for key
149JAN
150___
151FEB
152___
1532>JAZZ
154  ^ no match for key
1553>
156  ^ no match for key
1574>JAN
158  ___
1591>FEB
160  ___
161
162^ no match for key
163JANZ
164___
165JAN
166___
167FEB
168___
1693>
170  ^ no match for key
1712>JANZ
172  ___
1734>JAN
174  ___
1751>FEB
176  ___
177 1.2ignore
178 ___
179 1.1e4ignore
180 _____
181>>a
182___
183>b
184__
185a
186 ^ no match for key
187
188^ no match for key
189a
190_
191b
192_
193-3
194__
195-2
196__
197-0
198__
199--Mi-1
200^ no match for key
201-0
202__
2031
204_
205 1
206 _
207__
2081
209_
210_
211 1
212 _
2131
214_
215 1
216__
2171
218_
2192,5
220_
2212.4
222___
2232.,,3
224__
2252.4
226___
2272,,3
228_
2292.4
230___
2311a
232_
2332b
234_
235>a
236 _
237A>chr10
238     ^ no match for key
239B>chr1
240     ^ no match for key
2411 2
242 __
2431 3
244 __
245EOF
246
247(
248for type in n h g; do
249  printf '1\n\n44\n33\n2\n' | sort -s -k2$type --debug
250  printf '1\n\n44\n33\n2\n' | sort -s -k1.3$type --debug
251  printf '1\n\n44\n33\n2\n' | sort -s -k1$type --debug
252  printf '2\n\n1\n' | number | sort -s -k2g --debug
253done
254
255printf 'FEB\n\nJAN\n' | sort -s -k1M --debug
256printf 'FEB\n\nJAN\n' | sort -s -k2,2M --debug
257printf 'FEB\nJAZZ\n\nJAN\n' | sort -s -k1M --debug
258printf 'FEB\nJAZZ\n\nJAN\n' | number | sort -s -k2,2M --debug
259printf 'FEB\nJANZ\n\nJAN\n' | sort -s -k1M --debug
260printf 'FEB\nJANZ\n\nJAN\n' | number | sort -s -k2,2M --debug
261
262printf ' 1.2ignore\n 1.1e4ignore\n' | sort -s -g --debug
263
264printf '\tb\n\t\ta\n' | sort -s -d --debug # ignore = 1
265
266printf 'a\n\n' | sort -s -k2,2 --debug #lena = 0
267
268printf 'b\na\n' | sort -s -k1 --debug #otherwise key compare
269
270printf -- '-0\n1\n-2\n--Mi-1\n-3\n-0\n' | sort -s --debug -k1,1h
271
272printf ' 1\n1\n' | sort -b --debug
273printf ' 1\n1\n' | sort -sb --debug
274printf ' 1\n1\n' | sort --debug
275
276# strnumcmp is a bit weird, so we don't match exactly
277printf '2,5\n2.4\n' | sort -s -k1n --debug
278printf '2.,,3\n2.4\n' | sort -s -k1n --debug
279printf '2,,3\n2.4\n' | sort -s -k1n --debug
280
281# -z means we convert \0 to \n
282env printf '1a\x002b\x00' | sort -s -n -z --debug
283
284# Check that \0 and \t intermix.
285printf '\0\ta\n' | sort -s -k2b,2 --debug | tr -d '\0'
286
287# Check that key end before key start is not underlined
288printf 'A\tchr10\nB\tchr1\n' | sort -s -k2.4b,2.3n --debug
289
290# Ensure that -b applied before -k offsets
291printf '1 2\n1 3\n' | sort -s -k1.2b --debug
292) > out
293
294compare exp out || fail=1
295
296cat <<\EOF > exp
297   1²---++3   1,234  Mi
298               _
299   _________
300________________________
301   1²---++3   1,234  Mi
302              _____
303   ________
304_______________________
305+1234 1234Gi 1,234M
306^ no match for key
307_____
308^ no match for key
309      ____
310      ____
311      _____
312             _____
313             _____
314             ______
315___________________
316EOF
317
318unset LC_ALL
319f=$LOCALE_FR_UTF8
320
321: ${LOCALE_FR_UTF8=none}
322if test "$LOCALE_FR_UTF8" != "none"; then
323 LC_NUMERIC=$f LC_MESSAGES=C sort -g --debug /dev/null 2> debug.out
324 if grep 'numbers use .*,.* as a decimal point' debug.out >/dev/null; then
325  (
326  echo '   1²---++3   1,234  Mi' |
327    LC_ALL=C sort --debug -k2g -k1b,1
328  echo '   1²---++3   1,234  Mi' |
329    LC_ALL=$f sort --debug -k2g -k1b,1
330  echo '+1234 1234Gi 1,234M' |
331    LC_ALL=$f sort --debug \
332        -k1,1n -k1,1g -k1,1h \
333        -k2,2n -k2,2g -k2,2h \
334        -k3,3n -k3,3g -k3,3h
335  ) | sed 's/^^ .*/^ no match for key/' > out
336  compare exp out || touch locale_fail
337 fi
338 test -f locale_fail && fail=1
339fi
340
341Exit $fail
342