maint: factor out common macros of stat and printf
[coreutils.git] / tests / sort / sort-float.sh
blob37fe27ca2ed7a17269c25b51e65530cedb64985b
1 #!/bin/sh
2 # Ensure sort -g sorts floating point limits correctly
4 # Copyright (C) 2010-2024 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 <https://www.gnu.org/licenses/>.
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ sort
22 # Return 0 if LDBL_MIN is smaller than DBL_MIN, else 1.
23 # Dissect numbers like these, comparing first exponent, then
24 # whole part of mantissa, then fraction, until finding enough
25 # of a difference to determine the relative order of the numbers.
26 # These are "reversed":
27 # $ ./getlimits |grep DBL_MIN
28 # DBL_MIN=2.225074e-308
29 # LDBL_MIN=2.004168e-292
31 # These are in the expected order:
32 # $ ./getlimits|grep DBL_MIN
33 # DBL_MIN=2.225074e-308
34 # LDBL_MIN=3.362103e-4932
36 dbl_minima_order()
38 LC_ALL=C getlimits_
39 set -- $(echo $LDBL_MIN | tr .e- ' ')
40 local ldbl_whole=$1 ldbl_frac=$2 ldbl_exp=$3
42 set -- $(echo $DBL_MIN |tr .e- ' ')
43 local dbl_whole=$1 dbl_frac=$2 dbl_exp=$3
45 test "$dbl_exp" -lt "$ldbl_exp" && return 0
46 test "$ldbl_exp" -lt "$dbl_exp" && return 1
47 test "$ldbl_whole" -lt "$dbl_whole" && return 0
48 test "$dbl_whole" -lt "$ldbl_whole" && return 1
49 test "$ldbl_frac" -le "$dbl_frac" && return 0
50 return 1
53 # On some systems, DBL_MIN < LDBL_MIN. Detect that.
54 dbl_minima_order; reversed=$?
56 for LOC in C $LOCALE_FR; do
58 LC_ALL=$LOC getlimits_
60 # If DBL_MIN happens to be smaller than LDBL_MIN, swap them,
61 # so that out expected output is sorted.
62 if test $reversed = 1; then
63 t=$LDBL_MIN
64 LDBL_MIN=$DBL_MIN
65 DBL_MIN=$t
68 printf -- "\
69 -$LDBL_MAX
70 -$DBL_MAX
71 -$FLT_MAX
72 -$FLT_MIN
73 -$DBL_MIN
74 -$LDBL_MIN
76 $LDBL_MIN
77 $DBL_MIN
78 $FLT_MIN
79 $FLT_MAX
80 $DBL_MAX
81 $LDBL_MAX
82 " |
83 grep '^[0-9.,e+-]*$' > exp # restrict to numeric just in case
85 tac exp | LC_ALL=$LOC sort -sg > out || fail=1
87 compare exp out || fail=1
88 done
90 Exit $fail