/*
* 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
{
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)) {
{
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;
}
{
struct cdecl_declspec *s;
- if (test_rng_uniform(rng) < 0.5)
+ if (test_rng_50_50(rng))
return NULL;
s = new_specifier(NULL);
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;
}
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. */
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)) {
/*
* 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
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
-#include <float.h>
-#include <math.h>
#include "test.h"
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)
{
/*
- * 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
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