Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test rset... behaviour #441

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/alchemy.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void herbsearch(unit * u, int max)
herbsfound = ntimespprob(effsk * u->number,
(double)rherbs(r) / 100.0F, -0.01F);
herbsfound = _min(herbsfound, max);
rsetherbs(r, (short) (rherbs(r) - herbsfound));
rsetherbs(r, rherbs(r) - herbsfound);

if (herbsfound) {
produceexp(u, SK_HERBALISM, u->number);
Expand Down
3 changes: 2 additions & 1 deletion src/economy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,8 @@ static void plant(unit * u, int raw)
/* Alles ok. Abziehen. */
use_pooled(u, rt_water, GET_DEFAULT, 1);
use_pooled(u, itype->rtype, GET_DEFAULT, n);
rsetherbs(r, (short) (rherbs(r) + planted));
/* TODO should there a region maximum for herbs? */
rsetherbs(r, rherbs(r) + planted);
ADDMSG(&u->faction->msgs, msg_message("plant", "unit region amount herb",
u, r, planted, itype->rtype));
}
Expand Down
1 change: 1 addition & 0 deletions src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build.test.c
config.test.c
group.test.c
faction.test.c
region.test.c
unit.test.c
save.test.c
ship.test.c
Expand Down
35 changes: 19 additions & 16 deletions src/kernel/region.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,51 +598,55 @@ int rpeasants(const region * r)
return r->land ? r->land->peasants : 0;
}

void rsetpeasants(region * r, int value)
int rsetpeasants(const region * r, int value)
{
assert(value >= 0);
if (r->land) {
assert(value >= 0);
r->land->peasants = value;
return r->land->peasants = value;
}
return 0;
}

int rmoney(const region * r)
{
return r->land ? r->land->money : 0;
}

void rsethorses(const region * r, int value)
int rsethorses(const region * r, int value)
{
assert(value >= 0);
if (r->land) {
assert(value >= 0);
r->land->horses = value;
return r->land->horses = value;
}
return 0;
}

int rhorses(const region * r)
{
return r->land ? r->land->horses : 0;
}

void rsetmoney(region * r, int value)
int rsetmoney(const region * r, int value)
{
assert(value >= 0);
if (r->land) {
assert(value >= 0);
r->land->money = value;
return r->land->money = value;
}
return 0;
}

int rherbs(const struct region *r)
{
return r->land?r->land->herbs:0;
}

void rsetherbs(const struct region *r, int value)
int rsetherbs(const struct region *r, int value)
{
assert(value >= 0 && value < (1 << 15));
if (r->land) {
assert(value >= 0);
r->land->herbs = (short)(value);
return r->land->herbs = (short)(value);
}
return 0;
}


Expand Down Expand Up @@ -704,12 +708,11 @@ int rtrees(const region * r, int ageclass)

int rsettrees(const region * r, int ageclass, int value)
{
if (!r->land)
assert(value == 0);
else {
if (r->land) {
assert(value >= 0);
return r->land->trees[ageclass] = value;
}
assert(value == 0);
return 0;
}

Expand Down Expand Up @@ -1124,7 +1127,7 @@ void terraform_region(region * r, const terrain_type * terrain)
}
if (itype != NULL) {
rsetherbtype(r, itype);
rsetherbs(r, (short)(50 + rng_int() % 31));
rsetherbs(r, 50 + rng_int() % 31);
}
else {
rsetherbtype(r, NULL);
Expand Down
8 changes: 4 additions & 4 deletions src/kernel/region.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ extern "C" {
int rsettrees(const struct region *r, int ageclass, int value);

int rpeasants(const struct region *r);
void rsetpeasants(struct region *r, int value);
int rsetpeasants(const struct region *r, int value);
int rmoney(const struct region *r);
void rsetmoney(struct region *r, int value);
int rsetmoney(const struct region *r, int value);
int rhorses(const struct region *r);
void rsethorses(const struct region *r, int value);
int rsethorses(const struct region *r, int value);

int rherbs(const struct region *r);
void rsetherbs(const struct region *r, int value);
int rsetherbs(const struct region *r, int value);

#define rbuildings(r) ((r)->buildings)

Expand Down
56 changes: 56 additions & 0 deletions src/kernel/region.test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <platform.h>
#include <kernel/config.h>
#include <kernel/terrain.h>
#include "region.h"

#include <CuTest.h>
#include <tests.h>

#include <assert.h>

static void assert_setter(CuTest *tc, int (*setter)(const region *, int),
int (*getter)(const region *), region *land, region *ocean, bool throwaway) {
CuAssertIntEquals(tc, 0, setter(land, 0));
CuAssertIntEquals(tc, 0, getter(land));
CuAssertIntEquals(tc, 42, setter(land, 42));
CuAssertIntEquals(tc, 42, getter(land));
CuAssertIntEquals(tc, 0, setter(ocean, 0));
CuAssertIntEquals(tc, 0, getter(ocean));
if (throwaway) {
CuAssertIntEquals(tc, 0, setter(ocean, 42));
CuAssertIntEquals(tc, 0, getter(ocean));
}
}

static int t_settrees0(const region *r, int trees) {
return rsettrees(r, 0, trees);
}

static int t_trees0(const region *r) {
return rtrees(r, 0);
}

static void test_setters(CuTest *tc) {
region *land, *ocean;

test_cleanup();
test_create_world();
land = findregion(0, 0);
ocean = findregion(-1, 0);
assert(fval(ocean->terrain, SEA_REGION) && fval(land->terrain, LAND_REGION));

assert_setter(tc, rsetherbs, rherbs, land, ocean, true);
assert_setter(tc, rsethorses, rhorses, land, ocean, true);
assert_setter(tc, rsetmoney, rmoney, land, ocean, true);
assert_setter(tc, rsetpeasants, rpeasants, land, ocean, true);
assert_setter(tc, t_settrees0, t_trees0, land, ocean, false);

test_cleanup();
}

CuSuite *get_region_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_setters);
return suite;
}
2 changes: 1 addition & 1 deletion src/kernel/save.c
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ static region *readregion(struct gamedata *data, int x, int y)
rsetherbtype(r, NULL);
}
READ_INT(data->store, &n);
rsetherbs(r, (short)n);
rsetherbs(r, n);
READ_INT(data->store, &n);
rsetpeasants(r, n);
READ_INT(data->store, &n);
Expand Down
2 changes: 2 additions & 0 deletions src/kernel/terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extern "C" {
#define SWIM_INTO (1<<8) /* man darf hierhin schwimmen */
#define WALK_INTO (1<<9) /* man darf hierhin laufen */

struct region;

typedef struct production_rule {
char *name;
const struct resource_type *rtype;
Expand Down
2 changes: 1 addition & 1 deletion src/laws.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ growing_herbs(region * r, const int current_season, const int last_weeks_season)
int i;
for (i = rherbs(r); i > 0; i--) {
if (rng_int() % 100 < (100 - rherbs(r)))
rsetherbs(r, (short)(rherbs(r) + 1));
rsetherbs(r, rherbs(r) + 1);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test_eressea.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ int RunAllTests(int argc, char *argv[])
ADD_SUITE(item);
ADD_SUITE(magic);
ADD_SUITE(alchemy);
ADD_SUITE(region);
ADD_SUITE(reports);
ADD_SUITE(save);
ADD_SUITE(ship);
Expand Down