From 440679da87796df8d6c98c16f781fea5dd9bca00 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 5 Nov 2017 17:00:40 +0100 Subject: [PATCH] code that uses newterrain needs to set up terrains in tests. --- src/economy.test.c | 20 ++++++++++++++++++++ src/kernel/terrain.c | 15 ++++++++++----- src/laws.test.c | 15 +++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/economy.test.c b/src/economy.test.c index 460ebb5fb..e147277b5 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -170,6 +171,23 @@ static void test_normals_recruit(CuTest * tc) { test_cleanup(); } +/** + * Create any terrain types that are used by the trade rules. + * + * This should prevent newterrain from returning NULL. + */ +static void setup_terrains(CuTest *tc) { + test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION | FLY_INTO); + test_create_terrain("ocean", SEA_REGION | SWIM_INTO | FLY_INTO); + test_create_terrain("swamp", LAND_REGION | WALK_INTO | FLY_INTO); + test_create_terrain("desert", LAND_REGION | WALK_INTO | FLY_INTO); + init_terrains(); + CuAssertPtrNotNull(tc, newterrain(T_OCEAN)); + CuAssertPtrNotNull(tc, newterrain(T_PLAIN)); + CuAssertPtrNotNull(tc, newterrain(T_SWAMP)); + CuAssertPtrNotNull(tc, newterrain(T_DESERT)); +} + static region *setup_trade_region(CuTest *tc, const struct terrain_type *terrain) { region *r; item_type *it_luxury; @@ -206,6 +224,7 @@ static void test_trade_insect(CuTest *tc) { test_setup(); init_resources(); test_create_locale(); + setup_terrains(tc); r = setup_trade_region(tc, test_create_terrain("swamp", LAND_REGION)); init_terrains(); @@ -233,6 +252,7 @@ static void test_buy_cmd(CuTest *tc) { test_setup(); init_resources(); test_create_locale(); + setup_terrains(tc); r = setup_trade_region(tc, test_create_terrain("swamp", LAND_REGION)); init_terrains(); diff --git a/src/kernel/terrain.c b/src/kernel/terrain.c index 8c7ab8878..5a70a21e9 100644 --- a/src/kernel/terrain.c +++ b/src/kernel/terrain.c @@ -128,15 +128,20 @@ static const terrain_type *newterrains[MAXTERRAINS]; const struct terrain_type *newterrain(terrain_t t) { - if (t == NOTERRAIN) + const struct terrain_type *result; + if (t == NOTERRAIN) { return NULL; + } assert(t >= 0); assert(t < MAXTERRAINS); - if (!newterrains[t]) { - log_warning("did you call init_terrains?"); - newterrains[t] = get_terrain(terraindata[t]); + result = newterrains[t]; + if (!result) { + result = newterrains[t] = get_terrain(terraindata[t]); } - return newterrains[t]; + if (!result) { + log_error("no such terrain: %s.", terraindata[t]); + } + return result; } terrain_t oldterrain(const struct terrain_type * terrain) diff --git a/src/laws.test.c b/src/laws.test.c index 942b673b0..a4630332a 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -864,11 +865,25 @@ static void test_peasant_luck_effect(CuTest *tc) { test_cleanup(); } +/** +* Create any terrain types that are used by demographics. +* +* This should prevent newterrain from returning NULL. +*/ +static void setup_terrains(CuTest *tc) { + test_create_terrain("volcano", SEA_REGION | SWIM_INTO | FLY_INTO); + test_create_terrain("activevolcano", LAND_REGION | WALK_INTO | FLY_INTO); + init_terrains(); + CuAssertPtrNotNull(tc, newterrain(T_VOLCANO)); + CuAssertPtrNotNull(tc, newterrain(T_VOLCANO_SMOKING)); +} + static void test_luck_message(CuTest *tc) { region* r; attrib *a; test_setup(); + setup_terrains(tc); r = test_create_region(0, 0, NULL); rsetpeasants(r, 1);