From 6391812ee5ca7244eca308d4eb19e59fe9de23d4 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 25 Feb 2010 08:23:26 +0000 Subject: [PATCH] Eressea server: - passes tests - crashes in report writing (memory corruption) added more tests. --- src/common/bindings/bindings.c | 4 + src/common/kernel/curse.c | 4 - src/common/kernel/curse_test.c | 28 ++ src/common/kernel/types.h | 2 +- src/common/kernel/xmlreader.c | 2 +- src/common/tests.c | 4 +- src/common/util/base36_test.c | 5 +- src/eressea/res/config-eressea.xml | 1 + src/eressea/res/eressea/terrains.xml | 10 + src/res/terrains.xml | 4 - src/scripts/default.lua | 10 +- src/scripts/e3a/build.lua | 2 +- src/scripts/e3a/main.lua | 2 +- src/scripts/eressea.lua | 2 +- src/scripts/init.lua | 2 +- src/scripts/samples.lua | 446 --------------------------- src/scripts/tests/common.lua | 4 + src/scripts/tests/eressea.lua | 16 + 18 files changed, 80 insertions(+), 468 deletions(-) create mode 100644 src/common/kernel/curse_test.c create mode 100644 src/eressea/res/eressea/terrains.xml delete mode 100644 src/scripts/samples.lua diff --git a/src/common/bindings/bindings.c b/src/common/bindings/bindings.c index 4d4499f3c..3c073085a 100644 --- a/src/common/bindings/bindings.c +++ b/src/common/bindings/bindings.c @@ -1083,6 +1083,10 @@ parse_inifile(lua_State* L, dictionary * d, const char * section) lua_rawset(L,-3); } } + /* special case */ + lua_pushstring(L, "basepath"); + lua_pushstring(L, basepath()); + lua_rawset(L,-3); } int diff --git a/src/common/kernel/curse.c b/src/common/kernel/curse.c index 9df1901b9..9ed39d535 100644 --- a/src/common/kernel/curse.c +++ b/src/common/kernel/curse.c @@ -145,10 +145,6 @@ void destroy_curse(curse * c) { cunhash(c); - - if (c->data.v && c->type && c->type->typ == CURSETYP_UNIT) { - free(c->data.v); - } free(c); } diff --git a/src/common/kernel/curse_test.c b/src/common/kernel/curse_test.c new file mode 100644 index 000000000..e2ce81880 --- /dev/null +++ b/src/common/kernel/curse_test.c @@ -0,0 +1,28 @@ +#include +#include + +#include +#include "curse.h" +#include + +static void test_curse(CuTest * tc) { + attrib * attrs = NULL; + curse * c, * result; + int cid; + + curse_type ct_dummy = { "dummy", CURSETYP_NORM, 0, M_SUMEFFECT, NULL }; + c = create_curse(NULL, &attrs, &ct_dummy, 1.0, 1, 1, 1); + cid = c->no; + result = findcurse(cid); + CuAssertPtrEquals(tc, c, result); + destroy_curse(c); + result = findcurse(cid); + CuAssertPtrEquals(tc, NULL, result); +} + +CuSuite* get_curse_suite(void) +{ + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_curse); + return suite; +} diff --git a/src/common/kernel/types.h b/src/common/kernel/types.h index e666f360b..4d865eefd 100644 --- a/src/common/kernel/types.h +++ b/src/common/kernel/types.h @@ -310,7 +310,7 @@ enum { enum { RC_DWARF, /* 0 - Zwerg */ RC_ELF, - RC_GOBLIN, + RC_GOBLIN = 3, RC_HUMAN, RC_TROLL, diff --git a/src/common/kernel/xmlreader.c b/src/common/kernel/xmlreader.c index 8ad2305d6..b14bc8300 100644 --- a/src/common/kernel/xmlreader.c +++ b/src/common/kernel/xmlreader.c @@ -600,7 +600,7 @@ race_compat(void) { /* required for old_race, do not change order! */ const char * oldracenames[MAXRACES] = { - "dwarf", "elf", "goblin", "human", "troll", "demon", "insect", + "dwarf", "elf", NULL, "goblin", "human", "troll", "demon", "insect", "halfling", "cat", "aquarian", "orc", "snotling", "undead", "illusion", "youngdragon", "dragon", "wyrm", "ent", "catdragon", "dracoid", "special", "spell", "irongolem", "stonegolem", "shadowdemon", diff --git a/src/common/tests.c b/src/common/tests.c index 78bbe26c6..7443dd285 100644 --- a/src/common/tests.c +++ b/src/common/tests.c @@ -1,13 +1,15 @@ #include #include -CuSuite* get_base36_suite(); +CuSuite* get_base36_suite(void); +CuSuite* get_curse_suite(void); void RunAllTests(void) { CuString *output = CuStringNew(); CuSuite* suite = CuSuiteNew(); CuSuiteAddSuite(suite, get_base36_suite()); + CuSuiteAddSuite(suite, get_curse_suite()); CuSuiteRun(suite); CuSuiteSummary(suite, output); diff --git a/src/common/util/base36_test.c b/src/common/util/base36_test.c index 9b3d7c00e..25d8ef151 100644 --- a/src/common/util/base36_test.c +++ b/src/common/util/base36_test.c @@ -1,6 +1,7 @@ +#include #include -#include "base36.c" +#include "base36.h" static void test_atoi36(CuTest * tc) { CuAssertIntEquals(tc, 0, atoi36("0")); @@ -20,7 +21,7 @@ static void test_itoa36(CuTest * tc) { CuAssertStrEquals(tc, itoa36(666), "ii"); } -CuSuite* get_base36_suite() +CuSuite* get_base36_suite(void) { CuSuite* suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_itoa36); diff --git a/src/eressea/res/config-eressea.xml b/src/eressea/res/config-eressea.xml index c5c950461..db8aa2195 100644 --- a/src/eressea/res/config-eressea.xml +++ b/src/eressea/res/config-eressea.xml @@ -22,6 +22,7 @@ + diff --git a/src/eressea/res/eressea/terrains.xml b/src/eressea/res/eressea/terrains.xml new file mode 100644 index 000000000..8b2ce7b1c --- /dev/null +++ b/src/eressea/res/eressea/terrains.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/res/terrains.xml b/src/res/terrains.xml index dbd15d60f..edd8895ab 100644 --- a/src/res/terrains.xml +++ b/src/res/terrains.xml @@ -81,8 +81,4 @@ - - - - diff --git a/src/scripts/default.lua b/src/scripts/default.lua index 94dd9a222..7afd5fa8b 100644 --- a/src/scripts/default.lua +++ b/src/scripts/default.lua @@ -12,12 +12,12 @@ end function dbupdate() update_scores() - edb = db.open(basepath.."/eressea.db") + edb = db.open(config.basepath.."/eressea.db") if edb~=nil then edb:update_factions() edb:update_scores() else - print("could no open "..basepath.."/eressea.db") + print("could no open "..config.basepath.."/eressea.db") end end @@ -47,7 +47,7 @@ function write_emails(locales) local locale local file for key, locale in pairs(locales) do - files[locale] = io.open(basepath .. "/emails." .. locale, "w") + files[locale] = io.open(config.basepath .. "/emails." .. locale, "w") end local faction @@ -66,7 +66,7 @@ function write_addresses() local file local faction - file = io.open(basepath .. "/adressen", "w") + file = io.open(config.basepath .. "/adressen", "w") for faction in factions() do -- print(faction.id .. " - " .. faction.locale) file:write(tostring(faction) .. ":" .. faction.email .. ":" .. faction.info .. "\n") @@ -79,7 +79,7 @@ function write_aliases() local file local faction - file = io.open(basepath .. "/aliases", "w") + file = io.open(config.basepath .. "/aliases", "w") for faction in factions() do local unit if faction.email ~= "" then diff --git a/src/scripts/e3a/build.lua b/src/scripts/e3a/build.lua index dfe962b0a..8ea2693b8 100644 --- a/src/scripts/e3a/build.lua +++ b/src/scripts/e3a/build.lua @@ -123,7 +123,7 @@ function fill(pl, w, h) end function seed() - local input = io.open(basepath .. "/parteien.txt") + local input = io.open(config.basepath .. "/parteien.txt") for f in factions() do if f.race=="vampunicorn" then local str = input:read("*line") diff --git a/src/scripts/e3a/main.lua b/src/scripts/e3a/main.lua index 6c81ff853..8e75b092e 100644 --- a/src/scripts/e3a/main.lua +++ b/src/scripts/e3a/main.lua @@ -50,7 +50,7 @@ function process(orders) change_locales(localechange) -- use newfactions file to place out new players - -- autoseed(basepath .. "/newfactions", false) + -- autoseed(config.basepath .. "/newfactions", false) write_files(config.locales) diff --git a/src/scripts/eressea.lua b/src/scripts/eressea.lua index 6bfddabf0..70ce51cdd 100644 --- a/src/scripts/eressea.lua +++ b/src/scripts/eressea.lua @@ -46,7 +46,7 @@ function process(orders) change_locales() -- use newfactions file to place out new players - autoseed(basepath .. "/newfactions", false) + autoseed(config.basepath .. "/newfactions", false) write_files(locales) diff --git a/src/scripts/init.lua b/src/scripts/init.lua index 701615ede..ae39c50b2 100644 --- a/src/scripts/init.lua +++ b/src/scripts/init.lua @@ -33,7 +33,7 @@ function run_turn() set_turn(turn) end - orderfile = orderfile or basepath .. '/orders.' .. turn + orderfile = orderfile or config.basepath .. '/orders.' .. turn print("executing turn " .. get_turn() .. " with " .. orderfile) local result = process(orderfile) if result==0 then diff --git a/src/scripts/samples.lua b/src/scripts/samples.lua deleted file mode 100644 index 4f0fc3087..000000000 --- a/src/scripts/samples.lua +++ /dev/null @@ -1,446 +0,0 @@ -function mkunit(f, r, num) - u = unit.create(f, r) - u.number = num - u:add_item("money", num*10) - u:clear_orders() - return u -end - -function test_movement() - west = direction("west") - east = direction("east") - - -- im westen ohne strassen - ocean = region.create(-3, 0, "ocean") - w2 = region.create(-2, 0, "plain") - w1 = region.create(-1, 0, "plain") - - -- im osten mit strassen - r0 = region.create(0, 0, "plain") - r1 = region.create(1, 0, "desert") - r2 = region.create(2, 0, "glacier") - r3 = region.create(3, 0, "plain") - r4 = region.create(4, 0, "glacier") - - r0:add_direction(r4, "Wirbel", "Nimm die Abkürzung, Luke") - - r0:set_road(east, 1.0) - r1:set_road(west, 1.0) - r1:set_road(east, 1.0) - r2:set_road(west, 1.0) - r2:set_road(east, 1.0) - r3:set_road(west, 1.0) - r3:set_road(east, 1.0) - r4:set_road(west, 1.0) - - orcs = faction.create("orcs@eressea.de", "orc", "de") - orcs.age = 20 - - aqua = faction.create("aqua@eressea.de", "aquarian", "de") - aqua.age = 20 - - bugs = faction.create("bugz@eressea.de", "insect", "de") - bugs.age = 20 - - orc = mkunit(orcs, r0, 10) - orc:add_item("horse", orc.number*3) - orc:set_skill("riding", 10) - - -- schiffe zum abtreiben: - ships = {} - for i = 1, 100 do - ships[i] = add_ship(ocean, "boat") - end - - astra = mkunit(orcs, r0, 1) - astra:add_order("NACH Wirbel") - astra:add_order("NUMMER EINHEIT astr") - - foot = mkunit(orcs, r0, 1) - foot:add_order("ROUTE W W") - foot:add_order("NUMMER EINHEIT foot") - - watch = mkunit(orcs, w2, 1) - - ship = add_ship(ocean, "boat") - cptn = mkunit(aqua, ocean, 1) - cptn.ship = ship - cptn:add_order("NACH O") - cptn:add_order("NUMMER EINHEIT cptn") - cptn:add_order("BENENNE EINHEIT Landungsleiter") - cptn:add_order("BENENNE PARTEI Meermenschen") - - swim = mkunit(aqua, ocean, 1) - swim.ship = ship - swim:add_order("NACH O") - swim:add_order("NUMMER EINHEIT swim") - swim:add_order("BENENNE EINHEIT Landungstruppe") - - -- ein schiff im landesinneren - ship = add_ship(r0, "boat") - sail = mkunit(aqua, r0, 1) - sail.ship = ship - - crew = mkunit(aqua, r0, 1) - crew.ship = ship - - bug = mkunit(bugs, r0, 1) - - crew:add_order("NACH O") - crew:add_order("NUMMER EINHEIT crew") - crew:add_order("BENENNE EINHEIT Aussteiger") - crew:add_order("NUMMER PARTEI aqua") - - sail:add_order("NACH O") - sail:add_order("NUMMER EINHEIT saiL") - sail:add_order("BENENNE EINHEIT Aussteiger") - - orc:add_order("NUMMER PARTEI orcs") - orc:add_order("NUMMER EINHEIT orc") - orc:add_order("BENENNE EINHEIT Orks") - orc:add_order("ROUTE O O O P P O W W W W") - orc:add_order("GIB 0 ALLES Steine") - orc:add_order("GIB 0 ALLES Holz") - orc:add_order("TRANSPORTIEREN " .. itoa36(bug.id)) - - bug:add_order("NUMMER PARTEI bugs") - bug:add_order("NUMMER EINHEIT bug") - bug:add_order("BENENNE EINHEIT Käfer") - bug:add_order("GIB 0 ALLES Steine") - bug:add_order("GIB 0 ALLES Holz") - bug:add_order("FAHREN " .. itoa36(orc.id)) - - u = unit.create(orcs, r0) - u.number = 1 - u:add_item("horse", u.number*3) - u:add_item("money", u.number*10) - u:set_skill("riding", 10) - u:set_skill("stealth", 2) - u:clear_orders() - u:add_order("FOLGEN EINHEIT " .. itoa36(bug.id)) - u:add_order("NACH W") - u:add_order("NUMMER EINHEIT foLg") - u:add_order("BENENNE EINHEIT Verfolger") - - u2 = unit.create(orcs, r0) - u2.number = 1 - u2:add_item("horse", u2.number*3) - u2:add_item("money", u.number*10) - u2:set_skill("riding", 10) - u2:set_skill("stealth", 2) - u2:clear_orders() - u2:add_order("FOLGEN EINHEIT nix") - u2:add_order("NUMMER EINHEIT Last") - u2:add_order("BENENNE EINHEIT Verfolger-Verfolger") - -end - - -function test_sail() - r0 = region.create(0, 0, "plain") - - orcs = faction.create("enno@eressea.de", "orc", "de") - orcs.age = 20 - - orc = unit.create(orcs, r0) - orc.number = 1 - orc:add_item("speedsail", orc.number) - - orc:clear_orders() - orc:add_order("NUMMER PARTEI orcs") - orc:add_order("NUMMER EINHEIT orc") - orc:add_order("BENENNE EINHEIT Orks") - orc:add_order("ZEIGEN \"Sonnensegel\"") -end - -function test_handler() - - local function msg_handler(u, evt) - str = evt:get_string(0) - u2 = evt:get_unit(1) - print(u) - print(u2) - print(str) - message_unit(u, u2, "thanks unit, i got your message: " .. str) - message_faction(u, u2.faction, "thanks faction, i got your message: " .. str) - message_region(u, "thanks region, i got your message: " .. str) - end - - plain = region.create(0, 0, "plain") - skill = 8 - - f = faction.create("enno@eressea.de", "orc", "de") - f.age = 20 - - u = unit.create(f, plain) - u.number = 1 - u:add_item("money", u.number*100) - u:clear_orders() - u:add_order("NUMMER PARTEI test") - u:add_handler("message", msg_handler) - msg = "BOTSCHAFT EINHEIT " .. itoa36(u.id) .. " Du~Elf~stinken" - - f = faction.create("enno@eressea.de", "elf", "de") - f.age = 20 - - u = unit.create(f, plain) - u.number = 1 - u:add_item("money", u.number*100) - u:clear_orders() - u:add_order("NUMMER PARTEI eviL") - u:add_order(msg) - -end - -function test_combat() - - plain = region.create(0, 0, "plain") - skill = 8 - - f = faction.create("enno@eressea.de", "orc", "de") - f.age = 20 - - u = unit.create(f, plain) - u.number = 100 - u:add_item("money", u.number*100) - u:add_item("sword", u.number) - u:set_skill("melee", skill) - u:clear_orders() - u:add_order("NUMMER PARTEI test") - u:add_order("KÄMPFE") - u:add_order("BEFÖRDERUNG") - attack = "ATTACKIERE " .. itoa36(u.id) - - f = faction.create("enno@eressea.de", "elf", "de") - f.age = 20 - - u = unit.create(f, plain) - u.number = 100 - u:add_item("money", u.number*100) - u:add_item("sword", u.number) - u:set_skill("melee", skill+2) - u:clear_orders() - u:add_order("NUMMER PARTEI eviL") - u:add_order("KAEMPFE") - u:add_order(attack) - -end - -function test_rewards() - -- this script tests manufacturing and fighting. - - plain = region.create(0, 0, "plain") - skill = 5 - - f = faction.create("enno@eressea.de", "human", "de") - f.age = 20 - u = unit.create(f, plain) - u.number = 10 - u:add_item("money", u.number*100) - u:add_item("greatbow", u.number) - u:set_skill("bow", skill) - u:clear_orders() - u:add_order("KAEMPFE") - attack = "ATTACKIERE " .. itoa36(u.id) - - u = unit.create(f, plain) - u.number = 7 - u:add_item("money", u.number*100) - u:add_item("mallorn", u.number*10) - u:set_skill("weaponsmithing", 7) - u:clear_orders() - u:add_order("KAEMPFE NICHT") - u:add_order("MACHEN Elfenbogen") - u:add_order("NUMMER PARTEI test") - - f = faction.create("enno@eressea.de", "elf", "de") - f.age = 20 - u = unit.create(f, plain) - u.number = 7 - u:add_item("money", u.number*100) - u:add_item("greatbow", u.number) - u:set_skill("bow", skill) - u:clear_orders() - u:add_order("KAEMPFE HINTEN") - u:add_order(attack) - - u = unit.create(f, plain) - u.number = 7 - u:add_item("money", u.number*100) - u:add_item("mallorn", u.number*10) - u:set_skill("weaponsmithing", 7) - u:clear_orders() - u:add_order("KAEMPFE NICHT") - u:add_order("MACHEN Elfenbogen") - u:add_order("NUMMER PARTEI eviL") - - u = unit.create(f, plain) - u.number = 7 - u:add_item("money", u.number*100) - u:add_item("mallorn", u.number*10) - u:set_skill("weaponsmithing", 7) - u:clear_orders() - u:add_order("KAEMPFE NICHT") - - items = { "hornofdancing", "trappedairelemental", - "aurapotion50", "bagpipeoffear", - "instantartacademy", "instantartsculpture" } - local index - local item - for index, item in pairs(items) do - u:add_item(item, 1) - u:add_order('@BENUTZEN "' .. get_string("de", item) .. '"') - end - u:add_order("NUMMER PARTEI eviL") - -end - -function test_give() - plain = region.create(0, 0, "plain") - f = faction.create("enno@eressea.de", "human", "de") - f.age = 20 - u = unit.create(f, plain) - u.number = 10 - u:add_item("money", u.number*100) - u:clear_orders() - u:add_order("MACHE TEMP eins") - u:add_order("REKRUTIERE 1") - u:add_order("ENDE") - u:add_order("GIB TEMP eins ALLES silber") - u:add_order("NUMMER PARTEI test") - -end - -function test_write() - read_game("24") - read_orders("befehle") -end - -function move_north(u) - for order in u.orders do - print(order) - end - u:clear_orders() - u:add_order("NACH NORDEN") -end - -function test_monsters() - -- magrathea = get_region(-67, -5) - local magrathea = get_region(0, 0) - if magrathea ~= nil then - if pcall(dofile, scriptpath .. "/ponnuki.lua") then - init_ponnuki(magrathea) - else - print("could not open ponnuki") - end - end - - set_race_brain("braineater", move_north) - plan_monsters() -end - -function test_parser() - -- this script tests the changes to quotes - - plain = region.create(0, 0, "plain") - skill = 5 - - f = faction.create("enno@eressea.de", "human", "de") - f.age = 20 - u = unit.create(f, plain) - u.number = 10 - u:clear_orders() - u:add_order("Nummer Partei test") - u:add_order("BENENNE PARTEI \"Diese Partei heisst \\\"Enno's Schergen\\\".\"") - u:add_order("BENENNE EINHEIT \"Mein Name ist \\\"Enno\\\".\"") -end - -function test_fail() - plain = region.create(0, 0, "plain") - skill = 5 - - f = faction.create("enno@eressea.de", "human", "de") - print(f) -end - -function run_scripts() - scripts = { - "xmas2004.lua" - } - local index - local name - for index, name in pairs(scripts) do - local script = scriptpath .. "/" .. name - print("- loading " .. script) - if pcall(dofile, script)==0 then - print("Could not load " .. script) - end - end -end - -function test_moving() - test_movement() - run_scripts() - process_orders() - write_reports() - - if swim.region==ocean then - print "ERROR: Meermenschen können nicht anschwimmen" - end - if sail.region~=r0 then - print "ERROR: Kapitän kann Schiff mit NACH ohne VERLASSE verlassen" - end - if crew.region==r0 then - print "ERROR: Einheiten kann Schiff nicht mit NACH ohne VERLASSE verlassen" - end - drift = false - for i = 1, 100 do - if ships[i].region ~= ocean then - drift = true - break - end - end - if not drift then - print "ERROR: Unbemannte Schiffe treiben nicht ab" - end - if foot.region ~= w1 then - print "ERROR: Fusseinheit hat ihr NACH nicht korrekt ausgeführt" - end - if astra.region ~= r4 then - print "ERROR: Astraleinheit konnte Wirbel nicht benutzen" - end -end - --- test_movement() --- test_fail() --- test_handler() --- test_parser() --- test_monsters() --- test_combat() --- test_rewards() --- test_give() --- test_write() - --- test_sail() --- write_game("../testg.txt") --- read_game("../testg.txt") - -if 0==1 then - run_scripts() - process_orders() - write_reports() -end - --- test_moving() -if 0==1 then - read_game("530") - -- read_orders("../game/orders.530") - plan_monsters() - process_orders() - write_game("531") -else - read_game("531") - plan_monsters() - process_orders() - write_game("532") -end diff --git a/src/scripts/tests/common.lua b/src/scripts/tests/common.lua index a5d70aa31..c08e1717a 100644 --- a/src/scripts/tests/common.lua +++ b/src/scripts/tests/common.lua @@ -567,3 +567,7 @@ function test_building_other() assert_not_equal(10, b.size) end +function test_config() + assert_not_equal(nil, config.basepath) + assert_not_equal(nil, config.locales) +end diff --git a/src/scripts/tests/eressea.lua b/src/scripts/tests/eressea.lua index 3ee59561a..a213d3257 100644 --- a/src/scripts/tests/eressea.lua +++ b/src/scripts/tests/eressea.lua @@ -101,3 +101,19 @@ function test_levitate() process_orders() assert_equal(0, u.ship.flags) end + +function test_terrains() + local terrains = { "hell", "wall1", "corridor1" } + for k,v in ipairs(terrains) do + local r = region.create(k, k, v) + assert_not_equal(nil, r) + end +end + +function test_races() + local races = { "wolf", "orc", "human", "demon" } + for k,v in ipairs(races) do + local f = faction.create("noreply@eressea.de", "human", "de") + assert_not_equal(nil, f) + end +end