tests: add test for locale decimal processing
[coreutils.git] / tests / misc / sort-float.sh
blob0a9616dcfaa9c5d8cca7e008bbd260f3f9b4ccc0
1 #!/bin/sh
2 # Ensure sort -g sorts floating point limits correctly
4 # Copyright (C) 2010-2019 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 # See if sort should be using long doubles
61 grep '^#define HAVE_C99_STRTOLD 1' $CONFIG_HEADER > /dev/null ||
62 { LDBL_MAX="$DBL_MAX"; LDBL_MIN="$DBL_MIN"; }
64 # If DBL_MIN happens to be smaller than LDBL_MIN, swap them,
65 # so that out expected output is sorted.
66 if test $reversed = 1; then
67 t=$LDBL_MIN
68 LDBL_MIN=$DBL_MIN
69 DBL_MIN=$t
72 printf -- "\
73 -$LDBL_MAX
74 -$DBL_MAX
75 -$FLT_MAX
76 -$FLT_MIN
77 -$DBL_MIN
78 -$LDBL_MIN
80 $LDBL_MIN
81 $DBL_MIN
82 $FLT_MIN
83 $FLT_MAX
84 $DBL_MAX
85 $LDBL_MAX
86 " |
87 grep '^[0-9.,e+-]*$' > exp # restrict to numeric just in case
89 tac exp | LC_ALL=$LOC sort -sg > out || fail=1
91 compare exp out || fail=1
92 done
94 Exit $fail