This commit is contained in:
Rorikstr | Rust Dev 2025-10-24 12:06:07 +03:00
parent c473bf3f7e
commit 2bda91134e
5 changed files with 13 additions and 71 deletions

View file

@ -5,7 +5,6 @@ void do_attaching(void) {
long long current_time = get_current_time_ms();
if (!state->attach_completed) {
// Первый вход в Attaching
if (state->attach_start_time == 0) {
place_figure();
clear_lines();
@ -13,12 +12,10 @@ void do_attaching(void) {
state->attach_completed = 0;
}
// Проверяем, прошло ли 350мс
if (current_time - state->attach_start_time >= ATTACH_DELAY_MS) {
state->attach_completed = 1;
state->attach_start_time = 0; // Сбрасываем таймер
state->attach_start_time = 0;
// Проверяем game over и переходим
int game_over = is_game_over();
if (game_over) {
@ -27,9 +24,8 @@ void do_attaching(void) {
state->state = Spawn;
}
state->attach_completed = 0; // Сбрасываем флаг для следующего attach
state->attach_completed = 0;
}
// Иначе остаёмся в Attaching и ждём
}
}

View file

@ -6,7 +6,7 @@ START_TEST(test_collision_bottom_boundary) {
state->curr.y = FIELD_HEIGHT - 1;
state->curr.x = 5;
state->curr.mtrx[0][0] = 1;
state->curr.y++; // Выходим за границу
state->curr.y++;
ck_assert_int_eq(check_collision(), 1);
}
END_TEST
@ -21,7 +21,7 @@ END_TEST
START_TEST(test_collision_with_placed_block) {
GameState_t* state = get_game_state();
state->field[10][5] = 2; // Размещённый блок
state->field[10][5] = 2;
state->curr.y = 10;
state->curr.x = 5;
state->curr.mtrx[0][0] = 1;
@ -40,7 +40,7 @@ END_TEST
START_TEST(test_game_over_detection) {
GameState_t* state = get_game_state();
state->field[0][5] = 2; // Блок в верхней строке
state->field[0][5] = 2;
ck_assert_int_eq(is_game_over(), 1);
}
END_TEST

View file

@ -21,12 +21,10 @@ START_TEST(test_pause_toggle) {
userInput(Start, false);
updateCurrentState();
// Включаем паузу
userInput(Pause, false);
GameInfo_t state = updateCurrentState();
ck_assert_int_eq(state.pause, 1);
// Выключаем паузу
userInput(Pause, false);
state = updateCurrentState();
ck_assert_int_eq(state.pause, 0);
@ -37,7 +35,6 @@ START_TEST(test_field_initialization) {
userInput(Start, false);
GameInfo_t state = updateCurrentState();
// Поле должно быть чистым в начале
int non_zero_count = 0;
for (int i = 0; i < FIELD_HEIGHT; i++) {
for (int j = 0; j < FIELD_WIDTH; j++) {
@ -47,7 +44,6 @@ START_TEST(test_field_initialization) {
}
}
// Только активная фигура (максимум 4 блока)
ck_assert_int_le(non_zero_count, 4);
}
END_TEST
@ -56,7 +52,6 @@ START_TEST(test_next_figure_exists) {
userInput(Start, false);
GameInfo_t state = updateCurrentState();
// Должна быть следующая фигура
int has_blocks = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
@ -73,11 +68,9 @@ START_TEST(test_movement_left) {
userInput(Start, false);
updateCurrentState();
// Двигаем влево (движение происходит мгновенно через Moving)
userInput(Left, false);
GameInfo_t state = updateCurrentState();
// Проверяем что состояние изменилось (игра не в паузе)
ck_assert_int_eq(state.pause, 0);
}
END_TEST
@ -86,11 +79,9 @@ START_TEST(test_movement_right) {
userInput(Start, false);
updateCurrentState();
// Двигаем вправо
userInput(Right, false);
GameInfo_t state = updateCurrentState();
// Проверяем что состояние валидное
ck_assert_int_eq(state.pause, 0);
ck_assert_int_ge(state.level, 1);
}
@ -100,11 +91,9 @@ START_TEST(test_rotation) {
userInput(Start, false);
updateCurrentState();
// Вращаем фигуру
userInput(Action, false);
GameInfo_t state = updateCurrentState();
// Фигура должна остаться на поле
int has_figure = 0;
for (int i = 0; i < FIELD_HEIGHT; i++) {
for (int j = 0; j < FIELD_WIDTH; j++) {
@ -118,7 +107,6 @@ START_TEST(test_rotation) {
END_TEST
START_TEST(test_user_input_actions) {
// Просто проверяем что userInput не крашит
userInput(Start, false);
userInput(Left, false);
userInput(Right, false);
@ -126,7 +114,6 @@ START_TEST(test_user_input_actions) {
userInput(Pause, false);
userInput(Terminate, false);
// Если дошли сюда - всё ок
ck_assert_int_eq(1, 1);
}
END_TEST
@ -134,7 +121,6 @@ END_TEST
START_TEST(test_multiple_updates) {
userInput(Start, false);
// Многократный вызов updateCurrentState не должен крашить
for (int i = 0; i < 10; i++) {
updateCurrentState();
}
@ -144,22 +130,15 @@ START_TEST(test_multiple_updates) {
}
END_TEST
// ============================================================================
// Тесты для повышения покрытия
// ============================================================================
START_TEST(test_instant_drop_down_key) {
userInput(Start, false);
updateCurrentState();
// Устанавливаем флаг что клавиша была отжата
GameState_t* state = get_game_state();
state->down_key_was_released = 1;
// Нажимаем Down (instant drop)
userInput(Down, false);
// Проверяем что флаг сброшен
ck_assert_int_eq(state->down_key_was_released, 0);
}
END_TEST
@ -171,11 +150,9 @@ START_TEST(test_up_key_release) {
GameState_t* state = get_game_state();
state->down_key_was_released = 0;
// Нажимаем Up (release)
userInput(Up, false);
updateCurrentState();
// Флаг должен установиться
ck_assert_int_eq(state->down_key_was_released, 1);
}
END_TEST
@ -186,12 +163,10 @@ START_TEST(test_terminate_with_high_score) {
GameState_t* state = get_game_state();
state->info->high_score = 100;
state->info->score = 500; // Больше рекорда
state->info->score = 500;
// Terminate должен сохранить рекорд
userInput(Terminate, false);
// Проверяем что файл сохранён (загружаем обратно)
int loaded = load_high_score();
ck_assert_int_ge(loaded, 500);
}
@ -203,18 +178,15 @@ START_TEST(test_game_over_state) {
GameState_t* state = get_game_state();
// Заполняем верхние две строки почти полностью
for (int i = 0; i < 2; i++) {
for (int j = 0; j < FIELD_WIDTH; j++) {
state->field[i][j] = 2;
}
}
// Вызываем spawn, который должен обнаружить game over
state->state = Spawn;
updateCurrentState();
// Состояние должно перейти в GameOver
ck_assert_int_eq(state->state, GameOver);
}
END_TEST
@ -225,7 +197,6 @@ START_TEST(test_do_gameover_clears_next) {
GameState_t* state = get_game_state();
// Заполняем поле для trigger GameOver
for (int i = 0; i < 2; i++) {
for (int j = 0; j < FIELD_WIDTH; j++) {
state->field[i][j] = 2;
@ -235,7 +206,6 @@ START_TEST(test_do_gameover_clears_next) {
state->state = Spawn;
updateCurrentState();
// После GameOver next должна быть пустой
state->state = GameOver;
updateCurrentState();
@ -254,12 +224,10 @@ END_TEST
START_TEST(test_place_figure_directly) {
GameState_t* state = get_game_state();
// Очищаем поле
for (int i = 0; i < FIELD_HEIGHT; i++)
for (int j = 0; j < FIELD_WIDTH; j++)
state->field[i][j] = 0;
// Устанавливаем фигуру
state->curr.x = 5;
state->curr.y = 10;
for (int i = 0; i < 4; i++)
@ -268,10 +236,8 @@ START_TEST(test_place_figure_directly) {
state->curr.mtrx[0][0] = 1;
state->curr.mtrx[0][1] = 1;
// Размещаем
place_figure();
// Проверяем что блоки размещены
ck_assert_int_eq(state->field[10][5], 2);
ck_assert_int_eq(state->field[10][6], 2);
}
@ -283,15 +249,12 @@ START_TEST(test_attach_state_transition) {
GameState_t* state = get_game_state();
// Принудительно переводим в Attaching
state->state = Attaching;
state->attach_start_time = 0;
state->attach_completed = 0;
// Первый вызов должен разместить фигуру
updateCurrentState();
// Проверяем что таймер запущен
ck_assert_int_gt(state->attach_start_time, 0);
}
END_TEST
@ -302,29 +265,22 @@ START_TEST(test_moving_to_down) {
GameState_t* state = get_game_state();
// Устанавливаем фигуру с валидной матрицей
state->curr.y = 5;
state->curr.x = 5;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
state->curr.mtrx[i][j] = 0;
state->curr.mtrx[0][0] = 1; // Один блок
state->curr.mtrx[0][0] = 1;
int initial_y = state->curr.y;
// Вызываем handle_to_down_move через Moving
state->state = Moving;
state->moving_type = ToDown;
// ВАЖНО: updateCurrentState вызовет do_moving -> handle_to_down_move
// Защита от бесконечного цикла: ограничиваем время
long long start = get_current_time_ms();
updateCurrentState();
long long elapsed = get_current_time_ms() - start;
// Проверяем что:
// 1. Фигура упала ниже (или перешли в Attaching)
// 2. Не было timeout (< 1 секунды)
ck_assert(state->curr.y > initial_y || state->state == Attaching);
ck_assert_int_lt(elapsed, 1000);
}
@ -337,12 +293,10 @@ START_TEST(test_moving_do_nothing) {
GameState_t* state = get_game_state();
// Вызываем DoNothing
state->state = Moving;
state->moving_type = DoNothing;
updateCurrentState();
// Должны перейти в Move
ck_assert_int_eq(state->state, Move);
}
END_TEST
@ -353,7 +307,6 @@ START_TEST(test_automatic_falling) {
GameState_t* state = get_game_state();
// Устанавливаем фигуру
state->curr.y = 5;
state->curr.x = 5;
for (int i = 0; i < 4; i++)
@ -361,17 +314,13 @@ START_TEST(test_automatic_falling) {
state->curr.mtrx[i][j] = 0;
state->curr.mtrx[0][0] = 1;
// Принудительно переводим в Move с обнулённым таймером
state->state = Move;
state->last_move_time = 0; // Обнуляем время последнего движения
state->last_move_time = 0;
int initial_y = state->curr.y;
// Вызываем updateCurrentState, который должен вызвать do_move()
// Поскольку last_move_time = 0, условие should_move сработает
updateCurrentState();
// Проверяем что фигура упала или перешла в Attaching
ck_assert(state->curr.y > initial_y || state->state == Attaching);
}
END_TEST
@ -381,7 +330,6 @@ Suite* fsm_suite(void) {
Suite* s = suite_create("FSM");
TCase* tc = tcase_create("Core");
// Существующие тесты
tcase_add_test(tc, test_game_start);
tcase_add_test(tc, test_pause_toggle);
tcase_add_test(tc, test_field_initialization);

View file

@ -1,18 +1,17 @@
#include <check.h>
// Объявления Suite из других файлов
Suite* collision_suite(void);
Suite* lines_suite(void);
Suite* figures_suite(void);
Suite* score_suite(void);
Suite* fsm_suite(void); // <- Эта строка уже есть?
Suite* fsm_suite(void);
int main(void) {
SRunner* sr = srunner_create(collision_suite());
srunner_add_suite(sr, lines_suite());
srunner_add_suite(sr, figures_suite());
srunner_add_suite(sr, score_suite());
srunner_add_suite(sr, fsm_suite()); // <- Эта строка добавлена?
srunner_add_suite(sr, fsm_suite());
srunner_run_all(sr, CK_VERBOSE);
int failed = srunner_ntests_failed(sr);

View file

@ -4,17 +4,16 @@ START_TEST(test_level_up) {
test_setup();
GameState_t* state = get_game_state();
// Набираем 700 очков
fill_line(FIELD_HEIGHT - 1);
clear_lines(); // +100
clear_lines();
fill_line(FIELD_HEIGHT - 1);
fill_line(FIELD_HEIGHT - 2);
clear_lines(); // +300 = 400
clear_lines();
fill_line(FIELD_HEIGHT - 1);
fill_line(FIELD_HEIGHT - 2);
clear_lines(); // +300 = 700
clear_lines();
ck_assert_int_eq(state->info->level, 2);
}