Added unit tests

This commit is contained in:
2026-05-26 13:38:15 +02:00
parent 3232e4f44f
commit 4da59836be
7 changed files with 409 additions and 11 deletions
+1
View File
@@ -1,2 +1,3 @@
main main
save.dat save.dat
munit/
+9 -1
View File
@@ -4,6 +4,8 @@ LIBS = -lraylib -lm -lpthread -ldl
TARGET = main TARGET = main
SRC = main.c game.c ui.c SRC = main.c game.c ui.c
TEST_TARGET = munit_tests
TEST_SRC = munit.c game.c munit/munit.c
all: $(TARGET) all: $(TARGET)
@@ -16,4 +18,10 @@ clean:
run: $(TARGET) run: $(TARGET)
./$(TARGET) ./$(TARGET)
.PHONY: all clean run test: $(TEST_TARGET)
./$(TEST_TARGET)
$(TEST_TARGET): $(TEST_SRC)
$(CC) $(CFLAGS) $(TEST_SRC) -o $(TEST_TARGET) $(LIBS)
.PHONY: all clean run test
+12 -3
View File
@@ -25,12 +25,21 @@ int main() {
SetConfigFlags(FLAG_WINDOW_HIGHDPI); SetConfigFlags(FLAG_WINDOW_HIGHDPI);
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "2048"); InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "2048");
SetTargetFPS(30); // So my laptop doesn't burn SetTargetFPS(90); // We have event blocking so we can affort higher FPS
EnableEventWaiting();
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
ClearBackground(RAYWHITE);
BeginDrawing(); BeginDrawing();
render(&game_state); ClearBackground(RAYWHITE);
// If we are not animating, we can wait for input events instead of continuously rendering
bool animating = render(&game_state);
if (animating) {
DisableEventWaiting();
} else {
EnableEventWaiting();
}
EndDrawing(); EndDrawing();
handle_mouse_input(&game_state); handle_mouse_input(&game_state);
handle_keyboard_input(&game_state); handle_keyboard_input(&game_state);
+370
View File
@@ -0,0 +1,370 @@
#include "munit/munit.h"
#include "game.h"
#include <stdlib.h>
static void clear_tiles(GameState_t *state) {
for (int y = 0; y < BOARD_HEIGHT; y++) {
for (int x = 0; x < BOARD_WIDTH; x++) {
state->board[y][x] = 0;
}
}
}
static void *test_setup(const MunitParameter params[], void *user_data) {
(void)params;
(void)user_data;
GameState_t *state = (GameState_t *)malloc(sizeof(GameState_t));
munit_assert_not_null(state);
reset_board(state);
clear_tiles(state);
return state;
}
static void test_tear_down(void *fixture) {
free(fixture);
}
static MunitResult test_merge_left_simple(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][0] = 2;
state->board[0][1] = 2;
munit_assert_true(move(state, LEFT));
munit_assert_int(state->board[0][0], ==, 4);
munit_assert_int(state->board[0][1], ==, 0);
munit_assert_uint(state->score, ==, 4);
munit_assert_uint(state->moves_made, ==, 1);
return MUNIT_OK;
}
static MunitResult test_merge_left_through_gap(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][0] = 2;
state->board[0][2] = 2;
munit_assert_true(move(state, LEFT));
munit_assert_int(state->board[0][0], ==, 4);
munit_assert_int(state->board[0][1], ==, 0);
munit_assert_int(state->board[0][2], ==, 0);
munit_assert_int(state->board[0][3], ==, 0);
return MUNIT_OK;
}
static MunitResult test_merge_left_double_pair(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][0] = 2;
state->board[0][1] = 2;
state->board[0][2] = 4;
state->board[0][3] = 4;
munit_assert_true(move(state, LEFT));
munit_assert_int(state->board[0][0], ==, 4);
munit_assert_int(state->board[0][1], ==, 8);
munit_assert_int(state->board[0][2], ==, 0);
munit_assert_int(state->board[0][3], ==, 0);
munit_assert_uint(state->score, ==, 12);
return MUNIT_OK;
}
static MunitResult test_merge_left_no_double_merge_single_move(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][0] = 2;
state->board[0][1] = 2;
state->board[0][2] = 2;
munit_assert_true(move(state, LEFT));
munit_assert_int(state->board[0][0], ==, 4);
munit_assert_int(state->board[0][1], ==, 2);
munit_assert_int(state->board[0][2], ==, 0);
munit_assert_int(state->board[0][3], ==, 0);
munit_assert_uint(state->score, ==, 4);
return MUNIT_OK;
}
static MunitResult test_move_left_no_merge_when_different_values(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][1] = 2;
state->board[0][2] = 4;
munit_assert_true(move(state, LEFT));
munit_assert_int(state->board[0][0], ==, 2);
munit_assert_int(state->board[0][1], ==, 4);
munit_assert_int(state->board[0][2], ==, 0);
munit_assert_int(state->board[0][3], ==, 0);
munit_assert_uint(state->score, ==, 0);
return MUNIT_OK;
}
static MunitResult test_move_returns_false_when_nothing_changes(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][0] = 2;
state->board[0][1] = 4;
state->board[0][2] = 8;
state->board[0][3] = 16;
munit_assert_false(move(state, LEFT));
munit_assert_uint(state->moves_made, ==, 0);
munit_assert_uint(state->score, ==, 0);
return MUNIT_OK;
}
static MunitResult test_merge_right_simple(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[1][2] = 2;
state->board[1][3] = 2;
munit_assert_true(move(state, RIGHT));
munit_assert_int(state->board[1][0], ==, 0);
munit_assert_int(state->board[1][1], ==, 0);
munit_assert_int(state->board[1][2], ==, 0);
munit_assert_int(state->board[1][3], ==, 4);
return MUNIT_OK;
}
static MunitResult test_merge_right_through_gap(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[1][1] = 2;
state->board[1][3] = 2;
munit_assert_true(move(state, RIGHT));
munit_assert_int(state->board[1][0], ==, 0);
munit_assert_int(state->board[1][1], ==, 0);
munit_assert_int(state->board[1][2], ==, 0);
munit_assert_int(state->board[1][3], ==, 4);
return MUNIT_OK;
}
static MunitResult test_merge_right_double_pair(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[1][0] = 2;
state->board[1][1] = 2;
state->board[1][2] = 4;
state->board[1][3] = 4;
munit_assert_true(move(state, RIGHT));
munit_assert_int(state->board[1][0], ==, 0);
munit_assert_int(state->board[1][1], ==, 0);
munit_assert_int(state->board[1][2], ==, 4);
munit_assert_int(state->board[1][3], ==, 8);
munit_assert_uint(state->score, ==, 12);
return MUNIT_OK;
}
static MunitResult test_merge_right_no_double_merge_single_move(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[1][1] = 2;
state->board[1][2] = 2;
state->board[1][3] = 2;
munit_assert_true(move(state, RIGHT));
munit_assert_int(state->board[1][0], ==, 0);
munit_assert_int(state->board[1][1], ==, 0);
munit_assert_int(state->board[1][2], ==, 2);
munit_assert_int(state->board[1][3], ==, 4);
munit_assert_uint(state->score, ==, 4);
return MUNIT_OK;
}
static MunitResult test_merge_up_simple(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][0] = 2;
state->board[1][0] = 2;
munit_assert_true(move(state, UP));
munit_assert_int(state->board[0][0], ==, 4);
munit_assert_int(state->board[1][0], ==, 0);
munit_assert_int(state->board[2][0], ==, 0);
munit_assert_int(state->board[3][0], ==, 0);
return MUNIT_OK;
}
static MunitResult test_merge_up_through_gap(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][1] = 2;
state->board[2][1] = 2;
munit_assert_true(move(state, UP));
munit_assert_int(state->board[0][1], ==, 4);
munit_assert_int(state->board[1][1], ==, 0);
munit_assert_int(state->board[2][1], ==, 0);
munit_assert_int(state->board[3][1], ==, 0);
return MUNIT_OK;
}
static MunitResult test_merge_up_double_pair(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][1] = 2;
state->board[1][1] = 2;
state->board[2][1] = 4;
state->board[3][1] = 4;
munit_assert_true(move(state, UP));
munit_assert_int(state->board[0][1], ==, 4);
munit_assert_int(state->board[1][1], ==, 8);
munit_assert_int(state->board[2][1], ==, 0);
munit_assert_int(state->board[3][1], ==, 0);
munit_assert_uint(state->score, ==, 12);
return MUNIT_OK;
}
static MunitResult test_merge_up_no_double_merge_single_move(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][1] = 2;
state->board[1][1] = 2;
state->board[2][1] = 2;
munit_assert_true(move(state, UP));
munit_assert_int(state->board[0][1], ==, 4);
munit_assert_int(state->board[1][1], ==, 2);
munit_assert_int(state->board[2][1], ==, 0);
munit_assert_int(state->board[3][1], ==, 0);
munit_assert_uint(state->score, ==, 4);
return MUNIT_OK;
}
static MunitResult test_merge_down_simple(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[1][2] = 2;
state->board[2][2] = 2;
munit_assert_true(move(state, DOWN));
munit_assert_int(state->board[0][2], ==, 0);
munit_assert_int(state->board[1][2], ==, 0);
munit_assert_int(state->board[2][2], ==, 0);
munit_assert_int(state->board[3][2], ==, 4);
return MUNIT_OK;
}
static MunitResult test_merge_down_through_gap(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][2] = 2;
state->board[2][2] = 2;
munit_assert_true(move(state, DOWN));
munit_assert_int(state->board[0][2], ==, 0);
munit_assert_int(state->board[1][2], ==, 0);
munit_assert_int(state->board[2][2], ==, 0);
munit_assert_int(state->board[3][2], ==, 4);
return MUNIT_OK;
}
static MunitResult test_merge_down_double_pair(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[0][2] = 2;
state->board[1][2] = 2;
state->board[2][2] = 4;
state->board[3][2] = 4;
munit_assert_true(move(state, DOWN));
munit_assert_int(state->board[0][2], ==, 0);
munit_assert_int(state->board[1][2], ==, 0);
munit_assert_int(state->board[2][2], ==, 4);
munit_assert_int(state->board[3][2], ==, 8);
munit_assert_uint(state->score, ==, 12);
return MUNIT_OK;
}
static MunitResult test_merge_down_no_double_merge_single_move(const MunitParameter params[], void *fixture) {
(void)params;
GameState_t *state = (GameState_t *)fixture;
state->board[1][2] = 2;
state->board[2][2] = 2;
state->board[3][2] = 2;
munit_assert_true(move(state, DOWN));
munit_assert_int(state->board[0][2], ==, 0);
munit_assert_int(state->board[1][2], ==, 0);
munit_assert_int(state->board[2][2], ==, 2);
munit_assert_int(state->board[3][2], ==, 4);
munit_assert_uint(state->score, ==, 4);
return MUNIT_OK;
}
static MunitTest tests[] = {
{(char *)"/merge/left/simple", test_merge_left_simple, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/left/through-gap", test_merge_left_through_gap, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/left/double-pair", test_merge_left_double_pair, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/left/no-double-merge", test_merge_left_no_double_merge_single_move, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/left/different-values-no-merge", test_move_left_no_merge_when_different_values, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/move/no-change", test_move_returns_false_when_nothing_changes, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/right/simple", test_merge_right_simple, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/right/through-gap", test_merge_right_through_gap, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/right/double-pair", test_merge_right_double_pair, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/right/no-double-merge", test_merge_right_no_double_merge_single_move, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/up/simple", test_merge_up_simple, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/up/through-gap", test_merge_up_through_gap, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/up/double-pair", test_merge_up_double_pair, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/up/no-double-merge", test_merge_up_no_double_merge_single_move, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/down/simple", test_merge_down_simple, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/down/through-gap", test_merge_down_through_gap, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/down/double-pair", test_merge_down_double_pair, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{(char *)"/merge/down/no-double-merge", test_merge_down_no_double_merge_single_move, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
{NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}};
static const MunitSuite suite = {
(char *)"",
tests,
NULL,
1,
MUNIT_SUITE_OPTION_NONE,
};
int main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)]) {
return munit_suite_main(&suite, NULL, argc, argv);
}
Executable
BIN
View File
Binary file not shown.
+16 -6
View File
@@ -30,6 +30,16 @@ static bool is_animating;
static Vector2 new_tile = {-1, 1}; static Vector2 new_tile = {-1, 1};
static float new_tile_animation_progress; static float new_tile_animation_progress;
typedef struct {
Vector2 from;
Vector2 to;
// We need to store this to know when to (show the) merge, if merging
Vector2 position;
int value;
} animated_tile_t;
static animated_tile_t animated_tiles[BOARD_WIDTH * BOARD_HEIGHT];
// Common logic shared between both input handlers // Common logic shared between both input handlers
static void after_input(GameState_t *game_state) { static void after_input(GameState_t *game_state) {
spawn_tile(game_state, &new_tile); spawn_tile(game_state, &new_tile);
@@ -132,21 +142,20 @@ void handle_keyboard_input(GameState_t *game_state) {
} }
} }
void animate_new_tile() { static void animate_new_tile() {
if (new_tile.x == -1 && new_tile.y == -1) if (new_tile.x == -1 && new_tile.y == -1)
return; return;
// Simple pop-in animation that scales the tile from 0 to full size new_tile_animation_progress += GetFrameTime() * 5;
new_tile_animation_progress += GetFrameTime() * 5; // Adjust speed as needed
if (new_tile_animation_progress >= 1.0f) { if (new_tile_animation_progress >= 1.0f) {
new_tile_animation_progress = 0.3f; new_tile_animation_progress = 0.0f;
new_tile = (Vector2){-1, -1}; // Reset after animation completes new_tile = (Vector2){-1, -1}; // Reset after animation completes
is_animating = false; is_animating = false;
} }
} }
// Main render loop, responsible ONLY for redering // Main render loop, responsible ONLY for redering
void render(GameState_t *game_state) { bool render(GameState_t *game_state) {
if (is_animating) { if (is_animating) {
animate_new_tile(); animate_new_tile();
} }
@@ -163,7 +172,7 @@ void render(GameState_t *game_state) {
} }
if (is_animating && new_tile.x == x && new_tile.y == y) { if (is_animating && new_tile.x == x && new_tile.y == y) {
float scale = new_tile_animation_progress; // Scale from 0 to 1 float scale = 1.0 - 0.2 * sin(new_tile_animation_progress * PI);
int scaled_size = (int)(TILE_SIZE * scale); int scaled_size = (int)(TILE_SIZE * scale);
int offset = (TILE_SIZE - scaled_size) / 2; int offset = (TILE_SIZE - scaled_size) / 2;
DrawRectangle(pos_x + offset, pos_y + offset, scaled_size, scaled_size, tile_colors[color_index]); DrawRectangle(pos_x + offset, pos_y + offset, scaled_size, scaled_size, tile_colors[color_index]);
@@ -207,4 +216,5 @@ void render(GameState_t *game_state) {
DrawRectangle(400 - 100, 450, 200, 40, RED); DrawRectangle(400 - 100, 450, 200, 40, RED);
DrawText("Try again", 400 - MeasureText("Try again", 20) / 2, 455 + 4, 20, WHITE); DrawText("Try again", 400 - MeasureText("Try again", 20) / 2, 455 + 4, 20, WHITE);
} }
return is_animating;
} }
+1 -1
View File
@@ -9,6 +9,6 @@
#define WINDOW_WIDTH 800 #define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600 #define WINDOW_HEIGHT 600
void render(GameState_t *game_state); bool render(GameState_t *game_state);
void handle_mouse_input(GameState_t *game_state); void handle_mouse_input(GameState_t *game_state);
void handle_keyboard_input(GameState_t *game_state); void handle_keyboard_input(GameState_t *game_state);