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();
CuAssertIntEquals(tc, 2, rpeasants(r));
random_source_inject_array(inject, 2);
config_set("rules.wage.function", "0");
immigration();
CuAssertIntEquals(tc, 2, rpeasants(r));

View File

@ -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<value_size);
return values[value_index++];
}
struct random_source array_provider = {
array_source
struct random_source constants_provider = {
constant_double, constant_int32
};
void random_source_inject_array(double inject[], int size) {
int i;
assert(size > 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;
}

View File

@ -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

View File

@ -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