server/src/kernel/calendar.test.c

133 lines
3.5 KiB
C
Raw Normal View History

2017-05-07 13:35:59 +02:00
#include <platform.h>
#include "calendar.h"
#include <kernel/config.h>
#include <CuTest.h>
#include "tests.h"
2017-05-07 13:40:43 +02:00
static void test_calendar_config(CuTest * tc)
{
gamedate gd;
test_setup();
get_gamedate(0, &gd);
CuAssertIntEquals(tc, 0, first_turn());
config_set_int("game.start", 42);
CuAssertIntEquals(tc, 42, first_turn());
test_teardown();
2017-05-07 13:40:43 +02:00
}
2017-05-07 13:35:59 +02:00
static void test_calendar(CuTest * tc)
{
gamedate gd;
test_setup();
get_gamedate(0, &gd);
2017-05-07 13:40:43 +02:00
CuAssertIntEquals(tc, 1, gd.year);
CuAssertIntEquals(tc, 0, gd.season);
CuAssertIntEquals(tc, 0, gd.month);
CuAssertIntEquals(tc, 0, gd.week);
get_gamedate(1, &gd);
CuAssertIntEquals(tc, 1, gd.year);
CuAssertIntEquals(tc, 0, gd.season);
2017-05-07 13:35:59 +02:00
CuAssertIntEquals(tc, 0, gd.month);
2017-05-07 13:40:43 +02:00
CuAssertIntEquals(tc, 1, gd.week);
2017-05-07 15:50:19 +02:00
get_gamedate(weeks_per_month, &gd);
CuAssertIntEquals(tc, 1, gd.year);
CuAssertIntEquals(tc, 0, gd.season);
CuAssertIntEquals(tc, 1, gd.month);
CuAssertIntEquals(tc, 0, gd.week);
get_gamedate(weeks_per_month*months_per_year, &gd);
CuAssertIntEquals(tc, 2, gd.year);
CuAssertIntEquals(tc, 0, gd.season);
CuAssertIntEquals(tc, 0, gd.month);
CuAssertIntEquals(tc, 0, gd.week);
2017-05-07 13:40:43 +02:00
config_set_int("game.start", 42);
get_gamedate(42, &gd);
CuAssertIntEquals(tc, 1, gd.year);
CuAssertIntEquals(tc, 0, gd.season);
CuAssertIntEquals(tc, 0, gd.month);
CuAssertIntEquals(tc, 0, gd.week);
2017-05-07 15:50:19 +02:00
first_month = 2;
get_gamedate(42, &gd);
CuAssertIntEquals(tc, 1, gd.year);
CuAssertIntEquals(tc, 0, gd.season);
CuAssertIntEquals(tc, 2, gd.month);
CuAssertIntEquals(tc, 0, gd.week);
test_teardown();
2017-05-07 15:50:19 +02:00
}
2019-08-25 19:34:04 +02:00
static void setup_calendar(void) {
int i;
months_per_year = 4;
weeks_per_month = 2;
free(month_season);
2019-08-25 18:14:03 +02:00
month_season = calloc(months_per_year, sizeof(season_t));
for (i = 0; i != 4; ++i) {
month_season[i] = (season_t)i;
}
}
2017-05-07 15:50:19 +02:00
static void test_calendar_season(CuTest * tc)
{
test_setup();
setup_calendar();
CuAssertIntEquals(tc, SEASON_WINTER, calendar_season(0));
CuAssertIntEquals(tc, SEASON_WINTER, calendar_season(1));
CuAssertIntEquals(tc, SEASON_SPRING, calendar_season(2));
CuAssertIntEquals(tc, SEASON_SPRING, calendar_season(3));
CuAssertIntEquals(tc, SEASON_SUMMER, calendar_season(4));
CuAssertIntEquals(tc, SEASON_SUMMER, calendar_season(5));
CuAssertIntEquals(tc, SEASON_AUTUMN, calendar_season(6));
CuAssertIntEquals(tc, SEASON_AUTUMN, calendar_season(7));
CuAssertIntEquals(tc, SEASON_WINTER, calendar_season(8));
free(month_season);
month_season = NULL;
test_teardown();
}
static void test_gamedate(CuTest * tc)
2017-05-07 15:50:19 +02:00
{
gamedate gd;
test_setup();
setup_calendar();
2017-05-07 15:50:19 +02:00
get_gamedate(0, &gd);
CuAssertIntEquals(tc, 1, gd.year);
CuAssertIntEquals(tc, SEASON_WINTER, gd.season);
2017-05-07 15:50:19 +02:00
CuAssertIntEquals(tc, 0, gd.month);
CuAssertIntEquals(tc, 0, gd.week);
get_gamedate(weeks_per_month + 1, &gd);
CuAssertIntEquals(tc, 1, gd.year);
CuAssertIntEquals(tc, SEASON_SPRING, gd.season);
2017-05-07 15:50:19 +02:00
CuAssertIntEquals(tc, 1, gd.month);
CuAssertIntEquals(tc, 1, gd.week);
free(month_season);
month_season = NULL;
test_teardown();
2017-05-07 13:35:59 +02:00
}
CuSuite *get_calendar_suite(void)
{
CuSuite *suite = CuSuiteNew();
2017-05-07 13:40:43 +02:00
SUITE_ADD_TEST(suite, test_calendar_config);
2017-05-07 13:35:59 +02:00
SUITE_ADD_TEST(suite, test_calendar);
2017-05-07 15:50:19 +02:00
SUITE_ADD_TEST(suite, test_calendar_season);
SUITE_ADD_TEST(suite, test_gamedate);
2017-05-07 13:35:59 +02:00
return suite;
}