transfered from codeberg

This commit is contained in:
2026-03-26 14:46:39 -05:00
parent 630f28bb7e
commit 5ed2173793
136 changed files with 14932 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
#include "test_block.h"
#include "../block.h"
#include "texture_atlas_mapper.h"
#include "unity.h"
void test_get_block_data_bounds_checking(void)
{
// Test way too high
block_render_data_struct data = get_block_data(999);
TEST_ASSERT_TRUE(data.is_transparent);
TEST_ASSERT_EQUAL_INT(0, data.texture.top);
// Test negative
data = get_block_data(-1);
TEST_ASSERT_TRUE(data.is_transparent);
}
/* Verify that Grass has different textures for top and bottom */
void test_grass_block_texture_indices(void)
{
block_render_data_struct grass = get_block_data(block_grass);
// Assert that the top is grass, but the bottom is dirt
TEST_ASSERT_EQUAL_INT(grass_top_texture, grass.texture.top);
TEST_ASSERT_EQUAL_INT(dirt_texture, grass.texture.bottom);
TEST_ASSERT_EQUAL_INT(grass_side_texture, grass.texture.front);
}
void test_get_block_face_texture_coordinates_math(void)
{
float uvs[4][2];
// Let's test Dirt.
// If dirt_texture is index 2 in your texture_atlas_mapper.h:
// atlas_column = 2 % 16 = 2
// horizontal_start = 2 * (1.0/16.0) = 0.125
get_block_face_texture_coordinates(block_dirt, face_front, uvs);
float expected_start_x = (float)(dirt_texture % 16) * (1.0f / 16.0f);
// Top-Left X should be the start of the tile
TEST_ASSERT_FLOAT_WITHIN(0.0001f, expected_start_x, uvs[0][0]);
// Top-Right X should be start + 1/16th
TEST_ASSERT_FLOAT_WITHIN(0.0001f, expected_start_x + 0.0625f, uvs[1][0]);
}
void test_block_library_integrity(void)
{
for (int i = 0; i < block_type_count; i++)
{
block_render_data_struct data = get_block_data(i);
if (i == block_air)
{
TEST_ASSERT_TRUE_MESSAGE(data.is_transparent,
"Air must be transparent");
continue;
}
// Ensure solid blocks have full opacity
if (!data.is_transparent)
{
TEST_ASSERT_EQUAL_UINT8_MESSAGE(255, data.color.a,
"Solid block has invalid alpha");
}
// Ensure texture indices are within a reasonable range (e.g., 256 for a 16x16 atlas)
TEST_ASSERT_LESS_THAN_INT_MESSAGE(256, data.texture.top,
"Texture index out of atlas range");
}
}
void test_get_block_face_uv_winding_and_size(void)
{
float uvs[4][2];
get_block_face_texture_coordinates(block_dirt, face_front, uvs);
// Verify the width of the UV quad is exactly 1/16th (0.0625)
float width = uvs[1][0] - uvs[0][0]; // TopRight.x - TopLeft.x
TEST_ASSERT_FLOAT_WITHIN(0.0001f, 0.0625f, width);
// Verify the height of the UV quad is exactly 1/16th
// Note: vertical_maximum - vertical_minimum
float height = uvs[0][1] - uvs[3][1]; // TopLeft.y - BottomLeft.y
TEST_ASSERT_FLOAT_WITHIN(0.0001f, 0.0625f, height);
// Verify Winding: Top should be Y-greater than Bottom
TEST_ASSERT_MESSAGE(uvs[0][1] > uvs[3][1], "UV mapping is upside down!");
// Verify Winding: Right should be X-greater than Left
TEST_ASSERT_MESSAGE(uvs[1][0] > uvs[0][0], "UV mapping is mirrored horizontally!");
}
void test_stone_faces_are_consistent(void)
{
float top_uvs[4][2];
float front_uvs[4][2];
get_block_face_texture_coordinates(block_stone, face_top, top_uvs);
get_block_face_texture_coordinates(block_stone, face_front, front_uvs);
for(int i = 0; i < 4; i++)
{
TEST_ASSERT_EQUAL_FLOAT(top_uvs[i][0], front_uvs[i][0]);
TEST_ASSERT_EQUAL_FLOAT(top_uvs[i][1], front_uvs[i][1]);
}
}
void run_block_tests(void)
{
RUN_TEST(test_get_block_data_bounds_checking);
RUN_TEST(test_grass_block_texture_indices);
RUN_TEST(test_get_block_face_texture_coordinates_math);
RUN_TEST(test_block_library_integrity);
RUN_TEST(test_get_block_face_uv_winding_and_size);
RUN_TEST(test_stone_faces_are_consistent);
}

View File

@@ -0,0 +1,18 @@
#ifndef test_block_h
#define test_block_h
void test_get_block_data_bounds_checking(void);
void test_grass_block_texture_indices(void);
void test_get_block_face_texture_coordinates_math(void);
void test_block_library_integrity(void);
void test_get_block_face_uv_winding_and_size(void);
void test_stone_faces_are_consistent(void);
void run_block_tests(void);
#endif