From: Nick Bowler Date: Fri, 16 Jun 2023 04:12:42 +0000 (-0400) Subject: tests: Eliminate random floating-point generation. X-Git-Tag: v1.3~162 X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/commitdiff_plain/d994d4c8bc35514ab5fafff7177cdf6d4e3c61ce tests: Eliminate random floating-point generation. 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. --- diff --git a/m4/gnulib-cache.m4 b/m4/gnulib-cache.m4 index 3d38968..6113698 100644 --- a/m4/gnulib-cache.m4 +++ b/m4/gnulib-cache.m4 @@ -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 diff --git a/t/declgen.c b/t/declgen.c index 0e76348..459a68e 100644 --- a/t/declgen.c +++ b/t/declgen.c @@ -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 8ae2f13..fec991c 100644 --- 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 #include #include -#include -#include #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) { diff --git a/t/test.h b/t/test.h index 5ffe291..091ad88 100644 --- 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