Files
pokemon-gba/tests/test_support.c
T

141 lines
4.4 KiB
C

#include "test_support.h"
u16 test_pixels[WIDTH * HEIGHT + 2];
unsigned char test_save[32];
void save_from_menu(void)
{
struct Location location;
default_location(&location);
save_at_menu(&location, SAVE_MENU_SAVED);
}
int menu_input(unsigned keys, int can_open)
{
struct Location location;
default_location(&location);
return save_menu_input(keys, can_open, &location);
}
void save_at_menu(const struct Location *location, enum SaveMenuPhase expected)
{
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(START, 1, location) && save_menu.phase == SAVE_MENU_OPTIONS);
for (int i = 0; i < MENU_SAVE; ++i) {
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(DOWN, 1, location));
}
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(A, 1, location) && save_menu.phase == expected);
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(B, 1, location) && save_menu.phase == SAVE_MENU_CLOSED);
assert(!save_menu_input(0, 1, location));
}
void checksum_fixture(void)
{
if (test_save[4] == 6) {
unsigned crc = save_crc(test_save, 17);
test_save[17] = (unsigned char)(crc >> 8);
test_save[18] = (unsigned char)crc;
} else {
unsigned checksum = 0xa5 ^ test_save[4] ^ test_save[5] ^ test_save[6];
if (test_save[4] >= 3) {
checksum ^= test_save[7];
for (int i = 0; i < CAUGHT_CAPACITY; ++i) checksum ^= test_save[8 + i];
}
test_save[test_save[4] == 2 ? 7 : 13] = (unsigned char)checksum;
test_save[test_save[4] == 2 ? 8 : 14] = 0x5a;
}
}
/* Convert a fixture, never player data, retaining only fields supported by that version. */
void legacy_fixture(int version)
{
test_save[4] = (unsigned char)version;
test_save[5] &= version == 5 ? 63 : version == 4 ? 7 : 3;
if (version == 1) {
test_save[5] = 1; test_save[6] = 0xa5; test_save[7] = 0x5a;
} else checksum_fixture();
}
void render_scene(int x, int y)
{
draw_scene(x, y);
}
int reachable(int start_x, int start_y, int end_x, int end_y)
{
static unsigned char visited[HEIGHT][WIDTH];
static int queue[WIDTH * HEIGHT][2];
static const int directions[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
memset(visited, 0, sizeof(visited));
int head = 0, tail = 1;
queue[0][0] = start_x;
queue[0][1] = start_y;
visited[start_y][start_x] = 1;
while (head < tail) {
int x = queue[head][0], y = queue[head][1];
++head;
if (x == end_x && y == end_y) return 1;
for (int direction = 0; direction < 4; ++direction) {
int nx = x + directions[direction][0], ny = y + directions[direction][1];
if (nx < 0 || nx >= WIDTH || ny < 0 || ny >= HEIGHT) continue;
if (visited[ny][nx] || !can_walk(nx, ny)) continue;
visited[ny][nx] = 1;
queue[tail][0] = nx;
queue[tail][1] = ny;
++tail;
}
}
return 0;
}
void check_art(const char *const *art, int rows, int colors)
{
for (int row = 0; row < rows; ++row) {
assert(strlen(art[row]) == 16);
for (const char *p = art[row]; *p; ++p) {
if (*p == '.') continue;
int index = *p <= '9' ? *p - '0' : *p - 'A' + 10;
assert(index > 0 && index < colors);
}
}
}
void write_preview(const char *path)
{
FILE *file = fopen(path, "wb");
assert(file);
fprintf(file, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
for (int i = 0; i < WIDTH * HEIGHT; ++i) {
u16 color = SCREEN[i];
fputc((color & 31) * 255 / 31, file);
fputc(((color >> 5) & 31) * 255 / 31, file);
fputc(((color >> 10) & 31) * 255 / 31, file);
}
assert(fclose(file) == 0);
}
/* Simulate the same row copies as hardware DMA, into a separate display. */
int pending_pixels(void)
{
int total = 0;
for (int y = 0; y < HEIGHT; ++y) {
assert(dirty_first[y] <= WIDTH && dirty_end[y] <= WIDTH);
if (dirty_first[y] < dirty_end[y]) total += dirty_end[y] - dirty_first[y];
}
return total;
}
void copy_pending_rows(u16 *visible)
{
for (int y = 0; y < HEIGHT; ++y) {
int first = dirty_first[y], end = dirty_end[y];
if (first < end)
memcpy(&visible[y * WIDTH + first], &SCREEN[y * WIDTH + first],
(size_t)(end - first) * sizeof(u16));
}
reset_dirty_rows();
}