transfered from codeberg
This commit is contained in:
120
source_code/terrain_module/terrain/tests/test_terrain.c
Normal file
120
source_code/terrain_module/terrain/tests/test_terrain.c
Normal file
@@ -0,0 +1,120 @@
|
||||
#include "test_terrain.h"
|
||||
|
||||
#include "unity.h"
|
||||
#include "../terrain.h"
|
||||
#include "../_internal/private_terrain.h"
|
||||
#include "block.h"
|
||||
#include "chunk.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
//#define terrain_c_test_debug
|
||||
|
||||
/* Definition of the missing global the scheduler is looking for */
|
||||
struct world_struct *world;
|
||||
|
||||
/* Checks to see if _get_terrain_hight_at_coordinates is consistant. */
|
||||
void test_terrain_height_is_consistent(void)
|
||||
{
|
||||
float height_one = _get_terrain_height_at_coordinates(10, 10);
|
||||
float height_two = _get_terrain_height_at_coordinates(10, 10);
|
||||
|
||||
TEST_ASSERT_EQUAL_FLOAT(height_one, height_two);
|
||||
}
|
||||
|
||||
void test_cave_probability_at_high_altitude(void)
|
||||
{
|
||||
bool has_cave = _is_cave_present_at_coordinates(0, 100, 0, 10);
|
||||
|
||||
TEST_ASSERT_FALSE(has_cave);
|
||||
}
|
||||
|
||||
/* Step 1: initialize a chunk with location 0, 0, 0.
|
||||
* Step 2: run the terrain generator over the chunk.
|
||||
* Step 3: verify that the surface of the chunk IS NOT made of air.
|
||||
* Step 4: veridy that blocks 1 unit above the surface (within the same chunk) IS air.
|
||||
* Step 5: clean up.
|
||||
*/
|
||||
void test_generate_terrain_fuzzer(void)
|
||||
{
|
||||
/* Step 1: Set up logging and randomness */
|
||||
unsigned int seed = (unsigned int)time(NULL);
|
||||
|
||||
FILE *log = fopen("test_logs/last_test_random_vars.txt", "w");
|
||||
fprintf(log, "Seed: %u\n", seed);
|
||||
srand(seed);
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
/* Step 2: Generate a random world position aligned to 16x grid */
|
||||
// We multiply by 16 to ensure the chunk is grid-aligned
|
||||
chunk_position_struct world_pos = {
|
||||
.x = (rand() % 2000 - 1000) * chunk_size,
|
||||
.y = (rand() % 10 - 5) * chunk_size, // Focus near sea level
|
||||
.z = (rand() % 2000 - 1000) * chunk_size
|
||||
};
|
||||
|
||||
fprintf(log, "Chunk %d: x:%" PRId32 " y:%" PRId32 " z:%" PRId32 "\n",
|
||||
i,
|
||||
(int32_t)world_pos.x,
|
||||
(int32_t)world_pos.y,
|
||||
(int32_t)world_pos.z);
|
||||
|
||||
struct chunk_struct *chunk = initialize_chunk(world_pos);
|
||||
generate_terrain(chunk, &world_pos);
|
||||
|
||||
/* Step 3: Verify the internal data */
|
||||
for (uint8_t x = 0; x < chunk_size; x++)
|
||||
{
|
||||
for (uint8_t z = 0; z < chunk_size; z++)
|
||||
{
|
||||
int32_t world_x = (int32_t)world_pos.x + x;
|
||||
int32_t world_z = (int32_t)world_pos.z + z;
|
||||
|
||||
int32_t surface_y =
|
||||
(int32_t)floorf(
|
||||
_get_terrain_height_at_coordinates(world_x,
|
||||
world_z));
|
||||
|
||||
// Convert world surface to local chunk space
|
||||
int32_t local_y = surface_y - (int32_t)world_pos.y;
|
||||
|
||||
if (local_y >= 0 && local_y < chunk_size)
|
||||
{
|
||||
uint16_t block =
|
||||
chunk->block_data[index_chunk(x, local_y, z)];
|
||||
|
||||
if (block == block_air)
|
||||
{
|
||||
bool is_cave =
|
||||
_is_cave_present_at_coordinates
|
||||
(world_x,
|
||||
local_y + (int32_t)world_pos.y,
|
||||
world_z,
|
||||
surface_y
|
||||
);
|
||||
if (!is_cave)
|
||||
{
|
||||
fclose(log);
|
||||
TEST_FAIL_MESSAGE("Found AIR at s"
|
||||
"urface where no "
|
||||
"cave exists!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
destroy_chunk(chunk);
|
||||
}
|
||||
fclose(log);
|
||||
}
|
||||
|
||||
void run_terrain_tests(void)
|
||||
{
|
||||
RUN_TEST(test_terrain_height_is_consistent);
|
||||
RUN_TEST(test_cave_probability_at_high_altitude);
|
||||
RUN_TEST(test_generate_terrain_fuzzer);
|
||||
}
|
||||
13
source_code/terrain_module/terrain/tests/test_terrain.h
Normal file
13
source_code/terrain_module/terrain/tests/test_terrain.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef test_terrain_h
|
||||
#define test_terrain_h
|
||||
|
||||
void test_terrain_height_is_consistent(void);
|
||||
|
||||
void test_cave_probability_at_high_altitude(void);
|
||||
|
||||
void test_generate_terrain_fuzzer(void);
|
||||
|
||||
void run_terrain_tests(void);
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user