1#!/bin/sh
2# Test discriminator-based sorting.
3
4# Copyright (C) 2012-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
21
22# Set limit variables.
23getlimits_
24
25# These tests are designed for a 'sort' implementation that uses a
26# discriminator, i.e., a brief summary of a key that may have lost info,
27# but whose ordering is consistent with that of the original key.
28# The tests are useful even if 'sort' does not use this representation.
29
30# Test lexicographic sorting.
31
32# A long-enough string so that it overruns a small discriminator buffer size.
33long_prefix='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
34seq -f "$long_prefix%5.0f" 10000 > exp || fail=1
35sort -R exp | LC_ALL=C sort > out || fail=1
36compare exp out || fail=1
37
38
39# Test numeric sorting.
40
41# These tests are designed for an internal representation that ordinarily
42# looks at the number plus two decimal digits, but if -h is
43# used it looks at one decimal place plus a 4-bit SI prefix value.
44# In both cases, there's an extra factor of 2 for the sign.
45max_int200=$(expr $UINTMAX_MAX / 200) &&
46max_frac200=$(printf '%.2d' $(expr $UINTMAX_MAX / 2 % 100)) &&
47max_int320=$(expr $UINTMAX_MAX / 320) &&
48max_frac320=$(expr $UINTMAX_MAX / 32 % 10) &&
49{ printf -- "\
50    -$UINTMAX_OFLOW
51    -$UINTMAX_MAX
52    -${max_int200}0.1
53    -${max_int200}0
54    -${max_int200}0.0
55    -${max_int320}0.1
56    -${max_int320}0
57    -${max_int320}0.0
58    -$max_int200.${max_frac200}1
59    -$max_int200.$max_frac200
60    -$max_int320.${max_frac320}1
61    -$max_int320.$max_frac320
62" &&
63  seq -- -10 .001 10 &&
64  printf "\
65    $max_int320
66    $max_int320.$max_frac320
67    $max_int320.${max_frac320}1
68    $max_int200
69    $max_int200.$max_frac200
70    $max_int200.${max_frac200}1
71    ${max_int320}0
72    ${max_int320}0.0
73    ${max_int320}0.1
74    ${max_int200}0
75    ${max_int200}0.0
76    ${max_int200}0.1
77    $UINTMAX_MAX
78    $UINTMAX_OFLOW
79"
80} > exp || fail=1
81
82for opts in -n -h; do
83  sort -R exp | LC_ALL=C sort $opts > out || fail=1
84  compare exp out || fail=1
85done
86
87Exit $fail
88