Skip to content

Commit

Permalink
add test code for price calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
smcameron committed Jan 28, 2014
1 parent a36a95e commit c95cd30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ test-fleet: quat.o fleet.o mathutils.o mtwist.o Makefile
test-mtwist: mtwist.o test-mtwist.c Makefile
gcc -o test-mtwist mtwist.o test-mtwist.c

test-commodities: commodities.o Makefile
gcc -DTESTCOMMODITIES=1 -c commodities.c -o test-commodities.o
gcc -DTESTCOMMODITIES=1 -o test-commodities test-commodities.o

test : test-matrix test-space-partition test-marshal test-quat test-fleet test-mtwist

clean: mostly-clean
Expand Down
34 changes: 34 additions & 0 deletions commodities.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,37 @@ float commodity_calculate_price(struct commodity *c,
return price;
}

#ifdef TESTCOMMODITIES

static void test_price(struct commodity *c, float e, float g, float t)
{
float p;

p = commodity_calculate_price(c, e, t, g);
printf("%s %0.2f %0.2f %0.2f %3.2f\n", c->name, e, t, g, p);
}

int main(int argc, char *argv[])
{
struct commodity *c;
float e, g, t;
int i, ncommodities;

if (argc < 2) {
fprintf(stderr, "usage: test-commodities commodities-file\n");
return 1;
}
c = read_commodities(argv[1], &ncommodities);
if (!c) {
fprintf(stderr, "Failed to read commodities file %s\n", argv[1]);
return 1;
}

for (i = 0; i < ncommodities; i++)
for (e = -1.0; e <= 1.0; e += 0.1)
for (g = -1.0; g <= 1.0; g += 0.1)
for (t = -1.0; t <= 1.0; t += 0.1)
test_price(&c[i], e, g, t);
return 0;
}
#endif

0 comments on commit c95cd30

Please sign in to comment.