97 lines
4.3 KiB
C
97 lines
4.3 KiB
C
#include <assert.h>
|
|
#include <stdio.h>
|
|
|
|
#include "maze32_game.h"
|
|
|
|
static void initialize_game(maze32_maze_t* maze, maze32_maze_workspace_t* workspace,
|
|
maze32_game_t* game) {
|
|
maze32_maze_config_t maze_config = maze32_maze_default_config();
|
|
maze32_maze_report_t report;
|
|
assert(maze32_maze_generate(maze, workspace, &maze_config, UINT32_C(0x5a320009), &report) ==
|
|
MAZE32_MAZE_OK);
|
|
maze32_game_config_t game_config = maze32_game_default_config();
|
|
game_config.movement_cooldown_ms = 0U;
|
|
assert(maze32_game_init(game, maze, &game_config) == MAZE32_GAME_RESULT_ACCEPTED);
|
|
maze32_game_set_phase(game, MAZE32_GAME_PHASE_ACTIVE);
|
|
}
|
|
|
|
static void test_player_pause_and_reconnect(void) {
|
|
static maze32_maze_t maze;
|
|
static maze32_maze_workspace_t workspace;
|
|
static maze32_game_t game;
|
|
initialize_game(&maze, &workspace, &game);
|
|
game.players[0].vision_moves_remaining = 7U;
|
|
maze32_cell_t position = game.players[0].position;
|
|
|
|
assert(maze32_game_set_player_connected(&game, 1U, 0, 1000U) == MAZE32_GAME_RESULT_ACCEPTED);
|
|
assert(game.phase == MAZE32_GAME_PHASE_PAUSED);
|
|
assert(game.players[1].reconnect_deadline_ms == 61000U);
|
|
uint32_t sequence = 0U;
|
|
assert(maze32_game_enqueue_move(&game, MAZE32_GAME_ROLE_PLAYER_ONE, MAZE32_GAME_DIRECTION_NORTH,
|
|
2000U, &sequence) == MAZE32_GAME_RESULT_ACCEPTED);
|
|
assert(maze32_game_process_next(&game).result == MAZE32_GAME_RESULT_PREMATURE);
|
|
assert(game.players[0].position.x == position.x && game.players[0].position.y == position.y);
|
|
assert(game.players[0].vision_moves_remaining == 7U);
|
|
|
|
assert(maze32_game_set_player_connected(&game, 1U, 1, 60999U) == MAZE32_GAME_RESULT_ACCEPTED);
|
|
assert(game.phase == MAZE32_GAME_PHASE_ACTIVE);
|
|
assert(game.players[1].reconnect_deadline_ms == 0U);
|
|
assert(game.players[0].vision_moves_remaining == 7U);
|
|
}
|
|
|
|
static void test_timeout_draw_and_observer_read_only(void) {
|
|
static maze32_maze_t maze;
|
|
static maze32_maze_workspace_t workspace;
|
|
static maze32_game_t game;
|
|
static maze32_view_t view;
|
|
initialize_game(&maze, &workspace, &game);
|
|
|
|
uint32_t sequence = 0U;
|
|
assert(maze32_game_enqueue_move(&game, MAZE32_GAME_ROLE_OBSERVER, MAZE32_GAME_DIRECTION_EAST,
|
|
1U, &sequence) == MAZE32_GAME_RESULT_READ_ONLY);
|
|
assert(game.state_sequence == 0U);
|
|
assert(maze32_game_project_view(&game, MAZE32_GAME_ROLE_OBSERVER, &view) ==
|
|
MAZE32_GAME_RESULT_ACCEPTED);
|
|
assert(view.cell_count == maze.config.width * maze.config.height);
|
|
assert(view.observer_connected[0] && view.observer_connected[1]);
|
|
|
|
assert(maze32_game_set_player_connected(&game, 0U, 0, 5000U) == MAZE32_GAME_RESULT_ACCEPTED);
|
|
assert(maze32_game_project_view(&game, MAZE32_GAME_ROLE_OBSERVER, &view) ==
|
|
MAZE32_GAME_RESULT_ACCEPTED);
|
|
assert(!view.observer_connected[0] && view.observer_connected[1]);
|
|
assert(!maze32_game_advance_time(&game, 64999U));
|
|
assert(maze32_game_advance_time(&game, 65000U));
|
|
assert(game.phase == MAZE32_GAME_PHASE_FINISHED);
|
|
assert(game.final_result == MAZE32_GAME_FINAL_DRAW);
|
|
assert(maze32_game_set_player_connected(&game, 0U, 1, 65000U) ==
|
|
MAZE32_GAME_RESULT_MATCH_FINISHED);
|
|
}
|
|
|
|
static void test_finish_window_is_frozen_while_paused(void) {
|
|
static maze32_maze_t maze;
|
|
static maze32_maze_workspace_t workspace;
|
|
static maze32_game_t game;
|
|
initialize_game(&maze, &workspace, &game);
|
|
game.phase = MAZE32_GAME_PHASE_FINISH_PENDING;
|
|
game.first_exited_player = 0U;
|
|
game.has_exit_time[0] = 1U;
|
|
game.exit_times_ms[0] = 1000U;
|
|
|
|
assert(maze32_game_set_player_connected(&game, 1U, 0, 1100U) == MAZE32_GAME_RESULT_ACCEPTED);
|
|
assert(game.phase == MAZE32_GAME_PHASE_PAUSED);
|
|
assert(maze32_game_set_player_connected(&game, 1U, 1, 60100U) == MAZE32_GAME_RESULT_ACCEPTED);
|
|
assert(game.phase == MAZE32_GAME_PHASE_FINISH_PENDING);
|
|
assert(game.exit_times_ms[0] == 60000U);
|
|
assert(!maze32_game_advance_time(&game, 60200U));
|
|
assert(maze32_game_advance_time(&game, 60201U));
|
|
assert(game.final_result == MAZE32_GAME_FINAL_PLAYER_ONE);
|
|
}
|
|
|
|
int main(void) {
|
|
test_player_pause_and_reconnect();
|
|
test_timeout_draw_and_observer_read_only();
|
|
test_finish_window_is_frozen_while_paused();
|
|
puts("disconnect recovery tests: PASS");
|
|
return 0;
|
|
}
|