Files
RedBear-OS/local/recipes/tools/diffutils/source/gnulib-tests/test-timespec.c
T
vasilito 068a1ca63e bootloader: rebase onto upstream 1.0.0, sync firmware-loader version
Bootloader fork rebase:
- Base changed from 0.1.0 pre-patched archive to upstream 1.0.0 tag (c7eeb9f)
- Applied 0001-redbear-local-forks.patch (Cargo.toml crate path redirects)
- Applied fix-uefi-alloc-panic.patch equivalents (4 panic!() -> graceful
  error handling in src/main.rs)
- Applied P5-live-preload-cap-1gib.patch (1 GiB cap on live image preload)
- Skipped: P0 GPT partition scan (requires new module + integration),
  P1 timeout/default-resolution, P2 live preload guard (subsumed by
  panic fixes + cap), P3 live image safe read, P4 large ISO boot,
  redox.patch — to be applied in dedicated rebase session.

firmware-loader/Cargo.toml: version 0.1.0 -> 0.3.0 (sync with other
Red Bear custom crates which are at 0.3.0).

fork-upstream-map.toml: bootloader back from PENDING_REBASE to 1.0.0
since the partial rebase matches upstream 1.0.0 content.

fork-upstream-map.toml: base restored to 'main' tracked (was correctly
tracked by build-redbear.sh).
2026-07-11 09:47:59 +03:00

167 lines
4.5 KiB
C

/* Test timespec functions.
Copyright 2015-2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Paul Eggert. */
#include <config.h>
#include "timespec.h"
#include "intprops.h"
#include "macros.h"
#include <limits.h>
static struct { int s; int ns; } const prototype[] =
{
{ INT_MIN, 0 },
{ INT_MIN, 1 },
{ INT_MIN, TIMESPEC_HZ - 1 },
{ INT_MIN + 1, 0 },
{ INT_MIN + 1, 1 },
{ INT_MIN + 1, TIMESPEC_HZ - 1 },
{ -1, 0 },
{ -1, 1 },
{ -1, TIMESPEC_HZ - 1 },
{ 0, 0 },
{ 0, 1 },
{ 0, TIMESPEC_HZ - 1 },
{ 1, 0 },
{ 1, 1 },
{ 1, TIMESPEC_HZ - 1 },
{ 1234567890, 0 },
{ 1234567890, 1 },
{ 1234567890, TIMESPEC_HZ - 1 },
{ INT_MAX - 1, 0 },
{ INT_MAX - 1, 1 },
{ INT_MAX - 1, TIMESPEC_HZ - 1 },
{ INT_MAX, 0 },
{ INT_MAX, 1 },
{ INT_MAX, TIMESPEC_HZ - 1 },
{ INT_MAX, 2 * TIMESPEC_HZ }
};
enum { nprototypes = sizeof prototype / sizeof *prototype };
static bool
valid (struct timespec a)
{
return 0 <= a.tv_nsec && a.tv_nsec < TIMESPEC_HZ;
}
static int
sign (int i)
{
return i < 0 ? -1 : 0 < i;
}
static int
cmp (struct timespec a, struct timespec b)
{
return sign (timespec_cmp (a, b));
}
static bool
eq (struct timespec a, struct timespec b)
{
return timespec_cmp (a, b) == 0;
}
static bool
extremal (struct timespec a)
{
return ((a.tv_sec == TYPE_MINIMUM (time_t) && a.tv_nsec == 0)
|| (a.tv_sec == TYPE_MAXIMUM (time_t)
&& a.tv_nsec == TIMESPEC_HZ - 1));
}
int
main (void)
{
int i, j, k;
struct timespec test[nprototypes + 1];
int ntests;
int computed_hz = 1;
struct timespec prevroundtrip;
test[0] = make_timespec (TYPE_MINIMUM (time_t), -1);
ntests = 1;
for (i = 0; i < nprototypes; i++)
{
int s = prototype[i].s;
if (TYPE_SIGNED (time_t) || 0 <= s)
{
time_t t = (s <= INT_MIN + 1 ? s - INT_MIN + TYPE_MINIMUM (time_t)
: INT_MAX - 1 <= s ? s - INT_MAX + TYPE_MAXIMUM (time_t)
: s);
test[ntests++] = make_timespec (t, prototype[i].ns);
}
}
for (i = 0; i < LOG10_TIMESPEC_HZ; i++)
computed_hz *= 10;
ASSERT (computed_hz == TIMESPEC_HZ);
for (i = 0; i < ntests; i++)
{
struct timespec a = test[i];
struct timespec roundtrip = dtotimespec (timespectod (a));
if (i != 0)
ASSERT (cmp (prevroundtrip, roundtrip) <= 0);
prevroundtrip = roundtrip;
ASSERT (sign (timespec_sign (a)) == cmp (a, make_timespec (0, 0)));
if (valid (a))
for (j = 0; j < ntests; j++)
{
struct timespec b = test[j];
if (valid (b))
{
struct timespec sum = timespec_add (a, b);
struct timespec diff = timespec_sub (a, b);
struct timespec rdiff = timespec_sub (b, a);
ASSERT (cmp (a, b) == sign (i - j));
ASSERT (eq (sum, timespec_add (b, a)));
if (! extremal (sum))
{
ASSERT (eq (a, timespec_sub (sum, b)));
ASSERT (eq (b, timespec_sub (sum, a)));
for (k = 0; k < ntests; k++)
{
struct timespec c = test[k];
if (valid (c))
{
struct timespec sumbc = timespec_add (b, c);
if (! extremal (sumbc))
ASSERT (eq (timespec_add (a, sumbc),
timespec_add (sum, c)));
}
}
}
if (! extremal (diff))
ASSERT (eq (a, timespec_add (diff, b)));
if (! extremal (rdiff))
ASSERT (eq (b, timespec_add (rdiff, a)));
}
}
}
return test_exit_status;
}