pvjc26/a3/test_snake.c
2026-05-08 14:16:31 +02:00

74 lines
2.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "snake.h"
int main() {
printf("=== Testing Snake Game Library ===\n\n");
// Test 1: Add snake parts
printf("Test 1: Adding snake parts\n");
struct snake* snake = NULL;
snake = add_snake(snake, 5, 5);
snake = add_snake(snake, 5, 6);
snake = add_snake(snake, 5, 7);
printf("Added 3 snake parts\n");
// Test 2: Check if coordinates are part of snake
printf("\nTest 2: Checking if coordinates are part of snake\n");
printf("Is (5,5) part of snake? %d (expected: 1)\n", is_snake(snake, 5, 5));
printf("Is (5,6) part of snake? %d (expected: 1)\n", is_snake(snake, 5, 6));
printf("Is (10,10) part of snake? %d (expected: 0)\n", is_snake(snake, 10, 10));
// Test 3: Count snake parts
printf("\nTest 3: Counting snake parts\n");
int count = 0;
struct snake* curr = snake;
while (curr != NULL) {
count++;
curr = curr->next;
}
printf("Snake has %d parts (expected: 3)\n", count);
// Test 4: Remove snake tail
printf("\nTest 4: Removing snake tail\n");
snake = remove_snake(snake);
count = 0;
curr = snake;
while (curr != NULL) {
count++;
curr = curr->next;
}
printf("After removal, snake has %d parts (expected: 2)\n", count);
// Test 5: Test game state step
printf("\nTest 5: Testing game state\n");
struct state state;
state.snake = snake;
state.width = 20;
state.height = 20;
state.sx = 1; // Move right
state.sy = 0;
// Initialize food
for (int i = 0; i < FOOD_COUNT; i++) {
state.foodx[i] = 10 + i;
state.foody[i] = 10;
}
int result = step_state(&state);
printf("Step result: %d (0=CONTINUE, 1=WALL, 2=SNAKE, 3=FOOD, 4=USER)\n", result);
// Test 6: Test wall collision
printf("\nTest 6: Testing wall collision\n");
state.sx = 20; // Move far to the right (should hit wall)
state.sy = 0;
result = step_state(&state);
printf("After moving right 20, result: %d (expected: 1=WALL)\n", result);
// Cleanup
free_snake(state.snake);
printf("\n=== All tests completed ===\n");
return 0;
}