simplify random injection, extend it to integer constants

This commit is contained in:
Enno Rehling 2021-03-12 19:24:44 +01:00
parent cd8cd3c45d
commit b288a62542
4 changed files with 29 additions and 38 deletions

View File

@ -1714,8 +1714,6 @@ static void test_immigration(CuTest * tc)
immigration(); immigration();
CuAssertIntEquals(tc, 2, rpeasants(r)); CuAssertIntEquals(tc, 2, rpeasants(r));
random_source_inject_array(inject, 2);
config_set("rules.wage.function", "0"); config_set("rules.wage.function", "0");
immigration(); immigration();
CuAssertIntEquals(tc, 2, rpeasants(r)); CuAssertIntEquals(tc, 2, rpeasants(r));

View File

@ -69,62 +69,53 @@ bool chance(double x)
typedef struct random_source { typedef struct random_source {
double (*double_source) (void); double (*double_source) (void);
int (*int32_source) (void);
} random_source; } random_source;
random_source *r_source = 0; random_source *r_source = 0;
double rng_injectable_double(void) { double rng_injectable_double(void) {
if (r_source) if (r_source && r_source->double_source)
return r_source->double_source(); return r_source->double_source();
return genrand_real2(); return genrand_real2();
} }
static double constant_value; int rng_injectable_int(void) {
if (r_source && r_source->int32_source)
return r_source->int32_source();
return (int)genrand_int31();
}
static double constant_source (void) { static double constant_value_double;
return constant_value; static int constant_value_int32;
static double constant_double(void) {
return constant_value_double;
}
static int constant_int32(void) {
return constant_value_int32;
} }
struct random_source constant_provider = { struct random_source constant_provider = {
constant_source constant_double, NULL
}; };
void random_source_inject_constant(double value) { void random_source_inject_constant(double dval) {
constant_value = value; constant_value_double = dval;
r_source = &constant_provider; r_source = &constant_provider;
} }
static double *values; struct random_source constants_provider = {
static int value_size; constant_double, constant_int32
static int value_index;
static double array_source (void) {
assert(value_index<value_size);
return values[value_index++];
}
struct random_source array_provider = {
array_source
}; };
void random_source_inject_array(double inject[], int size) { void random_source_inject_constants(double dval, int ival) {
int i; constant_value_double = dval;
assert(size > 0); constant_value_int32 = ival;
value_size = size; r_source = &constants_provider;
if (values)
free(values);
values = malloc(sizeof(double) * size);
if (!values) abort();
for (i=0; i < size; ++i) {
values[i] = inject[i];
}
value_index = 0;
r_source = &array_provider;
} }
void random_source_reset(void) { void random_source_reset(void) {
if (values)
free(values);
values = NULL;
r_source = NULL; r_source = NULL;
} }

View File

@ -22,9 +22,10 @@ extern "C" {
By calling the random_source_inject... functions you can set a special random source, By calling the random_source_inject... functions you can set a special random source,
which is handy for testing */ which is handy for testing */
double rng_injectable_double(void); double rng_injectable_double(void);
int rng_injectable_int(void);
void random_source_inject_constants(double dval, int ival);
void random_source_inject_constant(double value); void random_source_inject_constant(double value);
void random_source_inject_array(double inject[], int size);
void random_source_reset(void); void random_source_reset(void);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -9,11 +9,12 @@ extern "C" {
/* generates a random number on [0,1)-real-interval */ /* generates a random number on [0,1)-real-interval */
double rng_injectable_double(void); double rng_injectable_double(void);
int rng_injectable_int(void);
#ifdef RNG_MT #ifdef RNG_MT
# include "mtrand.h" # include "mtrand.h"
# define rng_init(seed) init_genrand(seed) # define rng_init(seed) init_genrand(seed)
# define rng_int (int)genrand_int31 # define rng_int rng_injectable_int
# define rng_uint (unsigned int)genrand_int32 # define rng_uint (unsigned int)genrand_int32
# define rng_double rng_injectable_double # define rng_double rng_injectable_double
# define RNG_RAND_MAX 0x7fffffff # define RNG_RAND_MAX 0x7fffffff