diff --git a/src/laws.test.c b/src/laws.test.c index 9fd36d924..722b1dd8e 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -1714,8 +1714,6 @@ static void test_immigration(CuTest * tc) immigration(); CuAssertIntEquals(tc, 2, rpeasants(r)); - random_source_inject_array(inject, 2); - config_set("rules.wage.function", "0"); immigration(); CuAssertIntEquals(tc, 2, rpeasants(r)); diff --git a/src/util/rand.c b/src/util/rand.c index a14f1def4..e48c69f52 100644 --- a/src/util/rand.c +++ b/src/util/rand.c @@ -69,62 +69,53 @@ bool chance(double x) typedef struct random_source { double (*double_source) (void); + int (*int32_source) (void); } random_source; random_source *r_source = 0; double rng_injectable_double(void) { - if (r_source) + if (r_source && r_source->double_source) return r_source->double_source(); 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) { - return constant_value; +static double constant_value_double; +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 = { - constant_source + constant_double, NULL }; -void random_source_inject_constant(double value) { - constant_value = value; +void random_source_inject_constant(double dval) { + constant_value_double = dval; r_source = &constant_provider; } -static double *values; -static int value_size; -static int value_index; - -static double array_source (void) { - assert(value_index 0); - value_size = size; - 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_inject_constants(double dval, int ival) { + constant_value_double = dval; + constant_value_int32 = ival; + r_source = &constants_provider; } void random_source_reset(void) { - if (values) - free(values); - values = NULL; r_source = NULL; } diff --git a/src/util/rand.h b/src/util/rand.h index b659a68d3..a4178abf6 100644 --- a/src/util/rand.h +++ b/src/util/rand.h @@ -22,9 +22,10 @@ extern "C" { By calling the random_source_inject... functions you can set a special random source, which is handy for testing */ 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_array(double inject[], int size); void random_source_reset(void); #ifdef __cplusplus diff --git a/src/util/rng.h b/src/util/rng.h index 9c619b42e..42ebc5c57 100644 --- a/src/util/rng.h +++ b/src/util/rng.h @@ -9,11 +9,12 @@ extern "C" { /* generates a random number on [0,1)-real-interval */ double rng_injectable_double(void); + int rng_injectable_int(void); #ifdef RNG_MT # include "mtrand.h" # 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_double rng_injectable_double # define RNG_RAND_MAX 0x7fffffff