Files

143 lines
7.3 KiB
C

#include "test_support.h"
static void same_location(const struct Location *a, const struct Location *b)
{
assert(a->room == b->room && a->x == b->x && a->y == b->y && a->facing == b->facing);
}
static void team_loaded(void)
{
assert(partner_owned && balls_given && rod_owned && ball_count == 3 &&
caught_count == 2 && active_member == 2);
assert(caught_species[0] == FERNFOX && caught_species[1] == TIDEFIN);
for (int i = 2; i < CAUGHT_CAPACITY; ++i) assert(caught_species[i] == 0xff);
}
void check_location_save(void)
{
assert(save_crc((const unsigned char *)"123456789", 9) == 0x29b1);
struct Location fallback, restored;
default_location(&fallback);
assert(location_valid(&fallback));
memset(test_save, 0xff, sizeof(test_save));
load_progress();
partner_owned = balls_given = rod_owned = 1;
ball_count = 3; caught_count = active_member = 2;
caught_species[0] = FERNFOX; caught_species[1] = TIDEFIN;
struct Location beach = {BEACH, 120, 100, FACING_RIGHT};
save_at_menu(&beach, SAVE_MENU_SAVED);
/* Golden bytes calculated independently with Python binascii.crc_hqx(..., 0xffff). */
const unsigned char golden[] = {'F','Q','S','P',6,0x17,3,2,1,4,255,255,255,5,3,120,100,0xa1,0xa4};
assert(memcmp(test_save, golden, sizeof(golden)) == 0);
unsigned char saved[sizeof(test_save)], fixture[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
const int positions[ROOM_COUNT][2][2] = {
{{108,103},{222,131}}, {{120,119},{80,75}}, {{108,60},{207,131}},
{{31,113},{177,83}}, {{120,119},{80,75}}, {{108,60},{120,103}}
};
for (int room = 0; room < ROOM_COUNT; ++room) {
current_room = (enum Room)room;
int x, y;
reset_position(&x, &y);
struct Location spawn = {room, x, y, FACING_DOWN};
assert(location_valid(&spawn));
for (int edge = 0; edge < 2; ++edge)
for (int facing = FACING_DOWN; facing <= FACING_RIGHT; ++facing) {
struct Location here = {room, positions[room][edge][0], positions[room][edge][1], facing};
assert(location_valid(&here));
save_at_menu(&here, SAVE_MENU_SAVED);
partner_owned = balls_given = rod_owned = caught_count = active_member = ball_count = 0;
load_progress();
team_loaded();
saved_location(&restored);
same_location(&here, &restored);
door_open = grass_steps = diagonal_fraction = 7;
follower_ready = 0;
enter_location(&restored, &x, &y);
assert((int)current_room == room && x == here.x && y == here.y && (int)player_facing == facing);
assert(!door_open && !grass_steps && !diagonal_fraction && follower_ready && !trail_head);
assert(follower_x == x && follower_y == y);
move_follower(x, y, x + 1, y);
assert(follower_x == x && follower_y == y);
}
}
/* Walking, turning, doors, map exits and unsaved inventory cannot replace A. */
save_at_menu(&fallback, SAVE_MENU_SAVED);
memcpy(fixture, test_save, sizeof(fixture));
int x, y;
enter_location(&fallback, &x, &y);
walk_movement(RIGHT, &x, &y, 2);
ball_count = 1; rod_owned = 0; active_member = 0;
x = 177; y = 83; assert(use_door(&x, &y));
y = 125; assert(use_door(&x, &y));
x = 108; y = 131; assert(use_map_exit(&x, &y, DOWN));
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
load_progress(); team_loaded(); saved_location(&restored); same_location(&fallback, &restored);
for (int version = 1; version <= 5; ++version) {
memcpy(test_save, saved, sizeof(saved));
legacy_fixture(version);
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored);
assert(partner_owned && balls_given == (version >= 2) && ball_count == (version >= 2 ? 3 : 0));
assert(rod_owned == (version >= 4) && caught_count == (version >= 3 ? 2 : 0));
assert(active_member == (version == 5 ? 2 : 0));
for (int i = 0; i < CAUGHT_CAPACITY; ++i)
assert(caught_species[i] == (version >= 3 && i < 2 ? saved[8 + i] : 0xff));
same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
}
for (int damage = 0; damage < SAVE_RECORD_SIZE * 2; ++damage) {
memcpy(test_save, saved, sizeof(saved));
if (damage < SAVE_RECORD_SIZE) test_save[damage] ^= 1;
else if (damage == SAVE_RECORD_SIZE) { test_save[4] = 7; checksum_fixture(); }
else {
int cut = damage - SAVE_RECORD_SIZE;
memset(test_save + cut, 0xff, sizeof(test_save) - (unsigned)cut);
}
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored);
assert(!partner_owned && !balls_given && !rod_owned && !ball_count && !caught_count && !active_member);
same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
}
const unsigned char bad_progress[][2] = {{5,0x57}, {6,100}, {7,6}, {8,SPECIES_COUNT}, {12,0}, {5,0x1f}};
for (unsigned i = 0; i < sizeof(bad_progress) / sizeof(bad_progress[0]); ++i) {
memcpy(test_save, saved, sizeof(saved));
test_save[bad_progress[i][0]] = bad_progress[i][1];
checksum_fixture();
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored);
assert(!partner_owned && !caught_count && !ball_count);
same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
}
const struct Location invalid[] = {
{ROOM_COUNT,108,103,0}, {-1,108,103,0}, {OUTSIDE,108,103,4}, {OUTSIDE,108,103,-1},
{256,108,103,0}, {OUTSIDE,108,103,256},
{OUTSIDE,-1,103,0}, {OUTSIDE,256,103,0}, {OUTSIDE,108,-1,0}, {OUTSIDE,108,256,0},
{OUTSIDE,255,103,0}, {OUTSIDE,108,255,0}, {COTTAGE,25,120,0}, {BEACH,120,130,0},
{ROUTE,180,60,0}, {OUTSIDE,NPC_X,NPC_Y,0}, {MELLON_HOUSE,PROF_X,PROF_Y,0}
};
for (unsigned i = 0; i < sizeof(invalid) / sizeof(invalid[0]); ++i) {
assert(!location_valid(&invalid[i]));
memcpy(test_save, saved, sizeof(saved));
test_save[13] = (unsigned char)invalid[i].room; test_save[14] = (unsigned char)invalid[i].facing;
test_save[15] = (unsigned char)invalid[i].x; test_save[16] = (unsigned char)invalid[i].y;
checksum_fixture();
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored); team_loaded(); same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
memcpy(test_save, saved, sizeof(saved));
enter_location(&beach, &x, &y);
struct Location bad = invalid[i];
save_at_menu(&bad, SAVE_MENU_FAILED);
same_location(&bad, &invalid[i]); team_loaded();
assert(current_room == BEACH && player_facing == FACING_RIGHT && x == beach.x && y == beach.y);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
}
memset(test_save, 0xff, sizeof(test_save));
load_progress(); enter_location(&fallback, &x, &y);
puts("Location saves: v6 CRC, all rooms/facings, legacy reads, fallback and no-autosave checks passed");
}