1#!/bin/sh
2# Validate sleep parameters
3
4# Copyright (C) 2016-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_ sleep printf
21getlimits_
22
23# invalid timeouts
24returns_ 1 timeout 10 sleep invalid || fail=1
25returns_ 1 timeout 10 sleep -- -1 || fail=1
26returns_ 1 timeout 10 sleep 42D || fail=1
27returns_ 1 timeout 10 sleep 42d 42day || fail=1
28returns_ 1 timeout 10 sleep nan || fail=1
29returns_ 1 timeout 10 sleep '' || fail=1
30returns_ 1 timeout 10 sleep || fail=1
31
32# subsecond actual sleep
33timeout 10 sleep 0.001 || fail=1
34timeout 10 sleep 0x.002p1 || fail=1
35
36# Using small timeouts for larger sleeps is racy,
37# but false positives should be avoided on most systems
38returns_ 124 timeout 0.1 sleep 1d 2h 3m 4s || fail=1
39returns_ 124 timeout 0.1 sleep inf || fail=1
40returns_ 124 timeout 0.1 sleep $LDBL_MAX || fail=1
41
42# Test locale decimal handling for printf, sleep, timeout
43: ${LOCALE_FR_UTF8=none}
44if test "$LOCALE_FR_UTF8" != "none"; then
45  f=$LOCALE_FR_UTF8
46  locale_decimal=$(LC_ALL=$f env printf '%0.3f' 0.001) || fail=1
47  locale_decimal=$(LC_ALL=$f env printf '%0.3f' "$locale_decimal") || fail=1
48  case "$locale_decimal" in
49    0?001)
50      LC_ALL=$f timeout 1$locale_decimal sleep "$locale_decimal" || fail=1 ;;
51    *) fail=1 ;;
52  esac
53fi
54
55Exit $fail
56