]> git.draconx.ca Git - cdecl99.git/blobdiff - t/rng.c
tests: Avoid dependency on strtoumax.
[cdecl99.git] / t / rng.c
diff --git a/t/rng.c b/t/rng.c
index 8ae2f1369cb812dff820e368ceab54d0e133ef17..cbab4a7b8f8e6a545f6b91046d26dd000c4e0a44 100644 (file)
--- a/t/rng.c
+++ b/t/rng.c
@@ -1,6 +1,6 @@
 /*
  * Simple random number generator for testing.
- * Copyright © 2022 Nick Bowler
+ * Copyright © 2022-2023 Nick Bowler
  *
  * 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
@@ -27,8 +27,6 @@
 #include <inttypes.h>
 #include <errno.h>
 #include <limits.h>
-#include <float.h>
-#include <math.h>
 
 #include "test.h"
 
@@ -72,34 +70,18 @@ static unsigned long long splitmix64(unsigned long long *state)
        return z ^ (z >> 31);
 }
 
-#if HAVE_STRTOULL
-#  define STRTOULL strtoull
-#elif HAVE___STRTOULL
-/* HP-UX 11 has __strtoull in <inttypes.h> */
-#  define STRTOULL __strtoull
-#else
-/*
- * Just fall back to strtoul -- in the worst case we just lose the ability
- * to set all 64 bits of the seed.
- */
-#  define STRTOULL strtoul
-#endif
-
 struct test_rng *test_rng_alloc(const char *seed_str)
 {
        unsigned long long seed;
+       uintmax_t limit, seed_val;
        struct test_rng *rng;
-       char *end;
 
-       errno = 0;
-       seed = STRTOULL(seed_str, &end, 0);
-       if (*end != 0) {
+       limit  = (uintmax_t)0xffffffff;
+       limit |= (limit << 16 << 16);
+
+       if (!test_strtoumax(&seed_val, seed_str, limit)) {
                fprintf(stderr, "%s: invalid seed\n", seed_str);
                return NULL;
-       } else if (errno != 0) {
-               fprintf(stderr, "%s: invalid seed: %s\n",
-                               seed_str, strerror(errno));
-               return NULL;
        }
 
        rng = malloc_nofail(sizeof *rng);
@@ -116,20 +98,6 @@ void test_rng_free(struct test_rng *rng)
        free(rng);
 }
 
-#define BITS_PER_FP_DIGIT ( FLT_RADIX <  4 ? 1 \
-                         : FLT_RADIX <  8 ? 2 \
-                         : FLT_RADIX < 16 ? 3 \
-                         : FLT_RADIX < 32 ? 4 \
-                         : 5 )
-
-double test_rng_uniform(struct test_rng *rng)
-{
-       unsigned long long val = xoshiro256p(rng->state);
-       int prec = MIN(64, DBL_MANT_DIG*BITS_PER_FP_DIGIT);
-
-       return ldexp(val >> (64-prec), -prec);
-}
-
 /* Calculate the least power of two greater than val, minus 1. */
 static unsigned rng_mask(unsigned val)
 {