#include #include #include #include "lib.h" #include "common.h" static bool lib_test_add_wrap(void) { AL_TEST_START("lib_add_wrap"); // u32 u32 counter32 = UINT32_MAX - 5; counter32 = al_u32_add_wrap(counter32, 15, UINT32_MAX); AL_TEST_EQ(counter32, 10, u32); counter32 = UINT32_MAX; counter32 = al_u32_add_wrap(counter32, 1, UINT32_MAX); AL_TEST_EQ(counter32, 1, u32); counter32 = UINT32_MAX - 25; counter32 = al_u32_add_wrap(counter32, 20, UINT32_MAX); AL_TEST_EQ(counter32, UINT32_MAX - 5, u32); counter32 = al_u32_add_wrap(counter32, 6, UINT32_MAX); AL_TEST_EQ(counter32, 1, u32); // u16 u16 counter16 = UINT16_MAX - 5; counter16 = al_u16_add_wrap(counter16, 15, UINT16_MAX); AL_TEST_EQ(counter16, 10, u16); counter16 = UINT16_MAX; counter16 = al_u16_add_wrap(counter16, 1, UINT16_MAX); AL_TEST_EQ(counter16, 1, u16); counter16 = UINT16_MAX - 25; counter16 = al_u16_add_wrap(counter16, 20, UINT16_MAX); AL_TEST_EQ(counter16, UINT16_MAX - 5, u16); counter16 = al_u16_add_wrap(counter16, 6, UINT16_MAX); AL_TEST_EQ(counter16, 1, u16); // inc_wrap counter16 = UINT16_MAX - 4; AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), UINT16_MAX - 3, u16); AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), UINT16_MAX - 2, u16); AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), UINT16_MAX - 1, u16); AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), UINT16_MAX, u16); AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), 0, u16); AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), 1, u16); AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), 2, u16); AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), 3, u16); // From 3 to UINT16_MAX. for (u16 i = 0; i < UINT16_MAX - 3; i++) { AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), i + 4, u16); } // Wrap to 0. AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), 0, u16); AL_TEST_END(); } static bool lib_test_offset_vs_at_and_ref(void) { AL_TEST_START("lib_offset_vs_at_and_ref"); // Test that &al_array_at and al_array_offset are equivalent and that we can // have NULL entires in an array of pointers. array(struct test_t *) objects; al_array_init(objects); for (u32 i = 0; i < 10; i++) { if (i == 4) { al_array_push(objects, (struct test_t *)NULL); } else { al_array_push(objects, al_alloc_object(struct test_t)); } } AL_TEST_EQ(&al_array_at(objects, 2), al_array_offset(objects, 2), ptr); AL_TEST_EQ(&al_array_at(objects, 3), al_array_offset(objects, 3), ptr); AL_TEST_NEQ(*&al_array_at(objects, 3), NULL, ptr); AL_TEST_EQ(&al_array_at(objects, 4), al_array_offset(objects, 4), ptr); AL_TEST_EQ(*&al_array_at(objects, 4), *al_array_offset(objects, 4), ptr); AL_TEST_NEQ(*&al_array_at(objects, 4), *al_array_offset(objects, 2), ptr); AL_TEST_EQ(*&al_array_at(objects, 4), NULL, ptr); AL_TEST_EQ(&al_array_at(objects, 5), al_array_offset(objects, 5), ptr); for (u32 i = 0; i < 10; i++) { if (i != 4) { al_free(al_array_at(objects, i)); } } AL_TEST_END(); } static bool lib_test_min_max(void) { AL_TEST_START("lib_min_max"); AL_TEST_EQ(MAX((u32)4, (u32)3), 4, u32); AL_TEST_EQ(MIN((u32)4, (u32)3), 3, u32); // Compiler warnings. //AL_TEST_EQ(MAX(4, (u32)3), 4, u32); //AL_TEST_EQ(MIN(4, (u32)3), 3, u32); AL_TEST_END(); } bool lib_tests_run(void) { AL_TEST_START_GROUP("lib"); AL_TEST_RUN(lib_test_add_wrap); AL_TEST_RUN(lib_test_offset_vs_at_and_ref); AL_TEST_RUN(lib_test_min_max); AL_TEST_END_GROUP(); }