From 83d40962d205c34c5abc1d2e58363dac593608ed Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 2 May 2017 08:45:18 +0200 Subject: [PATCH] disable randomness that causes some tests to become intermittent. fix normalvariante and chance so they work better with constant rng always returning zero --- scripts/run-tests-e2.lua | 1 + scripts/run-tests-e3.lua | 1 + scripts/tests/common.lua | 3 +++ scripts/tests/production.lua | 1 + src/bindings.c | 7 +++++++ src/util/rand.c | 4 ++-- 6 files changed, 15 insertions(+), 2 deletions(-) diff --git a/scripts/run-tests-e2.lua b/scripts/run-tests-e2.lua index 4a2b3e59e..67a534ae6 100644 --- a/scripts/run-tests-e2.lua +++ b/scripts/run-tests-e2.lua @@ -17,6 +17,7 @@ require 'eressea.path' require 'tests.e2' require 'lunit' +rng_default(0) rules = require('eressea.' .. config.rules) result = lunit.main() return result.errors + result.failed diff --git a/scripts/run-tests-e3.lua b/scripts/run-tests-e3.lua index 47bcca60b..241b3e9fd 100644 --- a/scripts/run-tests-e3.lua +++ b/scripts/run-tests-e3.lua @@ -17,6 +17,7 @@ require 'eressea.xmlconf' require 'tests.e3' require 'lunit' +rng_default(0) eressea.settings.set("rules.alliances", "0") rules = require('eressea.' .. config.rules) result = lunit.main() diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 08a7ca94f..e612a17d7 100644 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -218,6 +218,9 @@ function test_gmtool() local r1 = region.create(1, 0, "plain") local r2 = region.create(1, 1, "plain") local r3 = region.create(1, 2, "plain") + if not gmtool then + return + end gmtool.open() gmtool.select(r1, true) gmtool.select_at(0, 1, true) diff --git a/scripts/tests/production.lua b/scripts/tests/production.lua index b470a2652..8727762d1 100644 --- a/scripts/tests/production.lua +++ b/scripts/tests/production.lua @@ -6,6 +6,7 @@ function setup() eressea.game.reset() eressea.settings.set("rules.food.flags", "4") -- food is free eressea.settings.set("NewbieImmunity", "0") + eressea.settings.set("study.random_progress", "0") end local function create_faction(race) diff --git a/src/bindings.c b/src/bindings.c index 9ea548465..d78ed8537 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -990,6 +990,12 @@ static void parse_inifile(lua_State * L, const dictionary * d, const char *secti } } +static int lua_rng_default(lua_State *L) { + UNUSED_ARG(L); + random_source_inject_constant(0); + return 0; +} + void tolua_bind_open(lua_State * L); int tolua_bindings_open(lua_State * L, const dictionary *inifile) @@ -1060,6 +1066,7 @@ int tolua_bindings_open(lua_State * L, const dictionary *inifile) tolua_function(L, TOLUA_CAST "get_ship", tolua_get_ship); tolua_function(L, TOLUA_CAST "get_building", tolua_get_building); tolua_function(L, TOLUA_CAST "get_region", tolua_get_region); + tolua_function(L, TOLUA_CAST "rng_default", lua_rng_default); tolua_function(L, TOLUA_CAST "factions", tolua_get_factions); tolua_function(L, TOLUA_CAST "regions", tolua_get_regions); tolua_function(L, TOLUA_CAST "read_turn", tolua_read_turn); diff --git a/src/util/rand.c b/src/util/rand.c index f5c8a28bc..584e8541e 100644 --- a/src/util/rand.c +++ b/src/util/rand.c @@ -46,7 +46,7 @@ double normalvariate(double mu, double sigma) double z; for (;;) { double u1 = rng_double(); - double u2 = 1.0 - rng_double(); + double u2 = rng_double(); z = NV_MAGICCONST * (u1 - 0.5) / u2; if (z * z / 4.0 <= -log(u2)) { break; @@ -73,7 +73,7 @@ bool chance(double x) { if (x >= 1.0) return true; - return rng_double() < x; + return (1-rng_double()) < x; } typedef struct random_source {