]> git.draconx.ca Git - cdecl99.git/commitdiff
tests: Eliminate random floating-point generation.
authorNick Bowler <nbowler@draconx.ca>
Fri, 16 Jun 2023 04:12:42 +0000 (00:12 -0400)
committerNick Bowler <nbowler@draconx.ca>
Fri, 16 Jun 2023 04:15:37 +0000 (00:15 -0400)
The only use of test_rng_uniform in the test suite is to do 50/50 coin
toss type checks, add a simple helper based on test_rng_uniform_int to
do this, instead of bringing in all this floating-point machinery for
no real reason.

m4/gnulib-cache.m4
t/declgen.c
t/rng.c
t/test.h

index 3d38968a8cd557e9d0eaf523dd078b15174a09d0..6113698877cdfb828de20be04f3d244a4939068f 100644 (file)
@@ -45,7 +45,6 @@
 #  getopt-gnu \
 #  gettext-h \
 #  gitlog-to-changelog \
-#  ldexp \
 #  localcharset \
 #  lock \
 #  mbswidth \
@@ -61,7 +60,6 @@ gl_MODULES([
   getopt-gnu
   gettext-h
   gitlog-to-changelog
-  ldexp
   localcharset
   lock
   mbswidth
index 0e76348c84c352acbf280bd206d3e4e1bbf6136d..459a68e077ae19958ef5d8ce2d2651e75df22b67 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Generate random C declarations for testing.
- *  Copyright © 2012, 2021-2022 Nick Bowler
+ *  Copyright © 2012, 2021-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
@@ -105,7 +105,7 @@ struct cdecl_declspec *gen_qualifiers(struct test_rng *rng, bool restrictqual)
 {
        struct cdecl_declspec *s = NULL;
 
-       while (test_rng_uniform(rng) < 0.5) {
+       while (test_rng_50_50(rng)) {
                s = new_specifier(s);
 
                switch (test_rng_uniform_int(rng, 2+restrictqual)) {
@@ -134,7 +134,7 @@ struct cdecl_declspec *gen_funcspecs(struct test_rng *rng)
 {
        struct cdecl_declspec *s = NULL;
 
-       while (test_rng_uniform(rng) < 0.5) {
+       while (test_rng_50_50(rng)) {
                s = new_specifier(s);
                s->type = CDECL_FUNC_INLINE;
        }
@@ -151,7 +151,7 @@ struct cdecl_declspec *gen_storspecs(struct test_rng *rng, bool registeronly)
 {
        struct cdecl_declspec *s;
 
-       if (test_rng_uniform(rng) < 0.5)
+       if (test_rng_50_50(rng))
                return NULL;
 
        s = new_specifier(NULL);
@@ -334,7 +334,7 @@ static void gen_function(struct test_rng *rng, struct cdecl_declarator *d)
        d->type = CDECL_DECL_FUNCTION;
        d->u.function.parameters = NULL;
 
-       while (test_rng_uniform(rng) < 0.5) {
+       while (test_rng_50_50(rng)) {
                unsigned flags = GEN_ONLY_REGISTER | GEN_NO_FUNCTION;
                struct cdecl *param;
 
@@ -350,8 +350,8 @@ static void gen_function(struct test_rng *rng, struct cdecl_declarator *d)
        }
 
        if (d->u.function.parameters) {
-               d->u.function.variadic = test_rng_uniform(rng) < 0.5;
-       } else if (test_rng_uniform(rng) < 0.5) {
+               d->u.function.variadic = test_rng_50_50(rng);
+       } else if (test_rng_50_50(rng)) {
                struct cdecl *param;
 
                /* We will never generate (void) above; do it here. */
@@ -375,12 +375,12 @@ struct cdecl_declarator *gen_declarators(struct test_rng *rng)
        unsigned limit = 3;
 
        d = new_declarator(NULL);
-       if (test_rng_uniform(rng) < 0.5) {
+       if (test_rng_50_50(rng)) {
                d->type = CDECL_DECL_IDENT;
                d->u.ident = gen_identifier(rng);
        }
 
-       while (test_rng_uniform(rng) < 0.5) {
+       while (test_rng_50_50(rng)) {
                d = new_declarator((p = d));
 
                switch (test_rng_uniform_int(rng, limit)) {
diff --git a/t/rng.c b/t/rng.c
index 8ae2f1369cb812dff820e368ceab54d0e133ef17..fec991ce1259b718bf10f6a4743bf7283ad50e2b 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"
 
@@ -116,20 +114,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)
 {
index 5ffe291e14c22112fda985226546c7cfb118947a..091ad88b8b322ab80629048edbe307bf16cea692 100644 (file)
--- a/t/test.h
+++ b/t/test.h
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2012, 2020 Nick Bowler
+ * Copyright © 2012, 2020, 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
@@ -59,13 +59,16 @@ struct test_rng *test_rng_alloc(const char *seed);
 void test_rng_free(struct test_rng *rng);
 
 /*
- * Return a random value uniformly on the half-open interval [0,1)
+ * Return a random integer uniformly on the closed interval [0, limit-1]
  */
-double test_rng_uniform(struct test_rng *rng);
+unsigned test_rng_uniform_int(struct test_rng *rng, unsigned limit);
 
 /*
- * Return a random integer uniformly on the closed interval [0, limit-1]
+ * Return false or true with 50% probablility either way.
  */
-unsigned test_rng_uniform_int(struct test_rng *rng, unsigned limit);
+static inline int test_rng_50_50(struct test_rng *rng)
+{
+       return test_rng_uniform_int(rng, 2) == 0;
+}
 
 #endif