#include "gamemath/TextureAtlas.h" #include "unittest/TestSuite.h" static void verifyInit(TextureAtlas * object, unsigned int pixelWidth, unsigned int pixelHeight, unsigned int line) { TestCase_assert(object->vtable == &TextureAtlas_class, "Expected %p but got %p (line %u)", &TextureAtlas_class, object->vtable, line); TestCase_assert(object->vtable->dispose == TextureAtlas_dispose, "Expected %p but got %p (line %u)", TextureAtlas_dispose, object->vtable->dispose, line); TestCase_assert(object->pixelWidth = pixelWidth, "Expected %u but got %u (line %u)", pixelWidth, object->pixelWidth, line); TestCase_assert(object->pixelHeight = pixelHeight, "Expected %u but got %u (line %u)", pixelHeight, object->pixelHeight, line); size_t keyCount = TextureAtlas_getKeyCount(object); TestCase_assert(keyCount == 0, "Expected 0 but got " SIZE_T_FORMAT " (line %u)", keyCount, line); } static void testInit(void) { TextureAtlas object, * objectPtr; bool success; memset(&object, 0x00, sizeof(object)); stemobject_assign_vtable(object, TextureAtlas); success = TextureAtlas_init(&object, 1, 1); TestCase_assertBoolTrue(success); verifyInit(&object, 1, 1, __LINE__); TextureAtlas_dispose(&object); memset(&object, 0xFF, sizeof(object)); stemobject_assign_vtable(object, TextureAtlas); success = TextureAtlas_init(&object, 2, 3); TestCase_assertBoolTrue(success); verifyInit(&object, 2, 3, __LINE__); TextureAtlas_dispose(&object); objectPtr = TextureAtlas_create(2, 3); TestCase_assertPointerNonNULL(objectPtr); verifyInit(objectPtr, 4, 5, __LINE__); TextureAtlas_dispose(objectPtr); } static void testSetPixelDimensions(void) { TextureAtlas * atlas = TextureAtlas_create(1, 1); TestCase_assertUIntEqual(atlas->pixelWidth, 1); TestCase_assertUIntEqual(atlas->pixelHeight, 1); TextureAtlas_setPixelDimensions(atlas, 2, 3); TestCase_assertUIntEqual(atlas->pixelWidth, 2); TestCase_assertUIntEqual(atlas->pixelHeight, 3); TextureAtlas_setPixelDimensions(atlas, 6, 2); TestCase_assertUIntEqual(atlas->pixelWidth, 6); TestCase_assertUIntEqual(atlas->pixelHeight, 2); } #define assertRect4fEqual(rect, expected_x_min, expected_x_max, expected_y_min, expected_y_max) \ TestCase_assert((rect).xMin == (expected_x_min) && (rect).xMax == (expected_x_max) && (rect).yMin == (expected_y_min) && (rect).yMax == (expected_y_max), "Expected {%f, %f, %f, %f} but got {%f, %f, %f, %f}", (expected_x_min), (expected_x_max), (expected_y_min), (expected_y_max), (rect).xMin, (rect).xMax, (rect).yMin, (rect).yMax); static void testGetSetKeys(void) { TextureAtlas * atlas = TextureAtlas_create(1, 1); Rect4f entry = TextureAtlas_lookup(atlas, HashTable_uint32Key(0)); assertRect4fEqual(entry, 0.0f, 0.0f, 0.0f, 0.0f); TestCase_assertBoolFalse(TextureAtlas_hasKey(atlas, HashTable_uint32Key(0))); TextureAtlas_setEntry(atlas, HashTable_uint32Key(0), RECT4f(0.0f, 1.0f, 2.0f, 3.0f)); entry = TextureAtlas_lookup(atlas, HashTable_uint32Key(0)); assertRect4fEqual(entry, 0.0f, 1.0f, 2.0f, 3.0f); TestCase_assertBoolTrue(TextureAtlas_hasKey(atlas, HashTable_uint32Key(0))); entry = TextureAtlas_lookup(atlas, HashTable_uint32Key(1)); assertRect4fEqual(entry, 0.0f, 0.0f, 0.0f, 0.0f); TextureAtlas_setEntry(atlas, HashTable_uint32Key(1), RECT4f(4.0f, 5.0f, 6.0f, 7.0f)); entry = TextureAtlas_lookup(atlas, HashTable_uint32Key(0)); assertRect4fEqual(entry, 0.0f, 1.0f, 2.0f, 3.0f); entry = TextureAtlas_lookup(atlas, HashTable_uint32Key(1)); assertRect4fEqual(entry, 4.0f, 5.0f, 6.0f, 7.0f); TextureAtlas_setEntry(atlas, HashTable_uint32Key(0), RECT4f(8.0f, 9.0f, 10.0f, 11.0f)); entry = TextureAtlas_lookup(atlas, HashTable_uint32Key(0)); assertRect4fEqual(entry, 8.0f, 9.0f, 10.0f, 11.0f); entry = TextureAtlas_lookup(atlas, HashTable_uint32Key(1)); assertRect4fEqual(entry, 4.0f, 5.0f, 6.0f, 7.0f); TestCase_assertBoolTrue(TextureAtlas_hasKey(atlas, HashTable_uint32Key(0))); bool success = TextureAtlas_removeEntry(atlas, HashTable_uint32Key(0)); TestCase_assertBoolTrue(success); entry = TextureAtlas_lookup(atlas, HashTable_uint32Key(0)); assertRect4fEqual(entry, 0.0f, 0.0f, 0.0f, 0.0f); TestCase_assertBoolFalse(TextureAtlas_hasKey(atlas, HashTable_uint32Key(0))); entry = TextureAtlas_lookup(atlas, HashTable_uint32Key(1)); assertRect4fEqual(entry, 4.0f, 5.0f, 6.0f, 7.0f); TestCase_assertBoolTrue(TextureAtlas_hasKey(atlas, HashTable_uint32Key(1))); } static void testListKeys(void) { TextureAtlas * atlas = TextureAtlas_create(1, 1); TextureAtlas_setEntry(atlas, HashTable_uint32Key(0), RECT4f(0.0f, 1.0f, 0.0f, 1.0f)); TextureAtlas_setEntry(atlas, HashTable_stringKey("a"), RECT4f(0.0f, 1.0f, 0.0f, 1.0f)); TextureAtlas_setEntry(atlas, HashTable_byteKey("b", 1), RECT4f(0.0f, 1.0f, 0.0f, 1.0f)); TextureAtlas_setEntry(atlas, HashTable_pointerKey((void *) 0x2), RECT4f(0.0f, 1.0f, 0.0f, 1.0f)); TestCase_assertSizeEqual(TextureAtlas_getKeyCount(atlas), 4); size_t keyCount = 0; HashTable_key * keyList = TextureAtlas_listKeys(atlas, &keyCount); TestCase_assertSizeEqual(keyCount, 4); HashTable_key * uintKey = NULL, * stringKey = NULL, * byteKey = NULL, * pointerKey = NULL; for (unsigned int keyIndex = 0; keyIndex < 4; keyIndex++) { switch (keyList[keyIndex].type) { case HASH_KEY_TYPE_STRING: stringKey = &keyList[keyIndex]; break; case HASH_KEY_TYPE_BYTES: byteKey = &keyList[keyIndex]; break; case HASH_KEY_TYPE_UINT32: uintKey = &keyList[keyIndex]; break; case HASH_KEY_TYPE_POINTER: pointerKey = &keyList[keyIndex]; break; case HASH_KEY_TYPE_UINT32_PAIR: case HASH_KEY_TYPE_OBJECT: break; } } TestCase_assertPointerNonNULL(stringKey); TestCase_assertPointerNonNULL(byteKey); TestCase_assertPointerNonNULL(uintKey); TestCase_assertPointerNonNULL(pointerKey); TestCase_assertUIntEqual(uintKey->data.uint32, 0); TestCase_assertStringEqual(stringKey->data.string.characters, "a"); TestCase_assertSizeEqual(stringKey->data.string.length, 1); TestCase_assertIntEqual(*((char *) byteKey->data.bytes.data), 'b'); TestCase_assertSizeEqual(byteKey->data.bytes.length, 1); TestCase_assertPointerEqual(pointerKey->data.pointer, (void *) 0x2); free(keyList); HashTable_key * keys[4]; keys[0] = TextureAtlas_keyAtIndex(atlas, 0); keys[1] = TextureAtlas_keyAtIndex(atlas, 1); keys[2] = TextureAtlas_keyAtIndex(atlas, 2); keys[3] = TextureAtlas_keyAtIndex(atlas, 3); TestCase_assertPointerNULL(TextureAtlas_keyAtIndex(atlas, 4)); uintKey = stringKey = byteKey = pointerKey = NULL; for (unsigned int keyIndex = 0; keyIndex < 4; keyIndex++) { switch (keys[keyIndex]->type) { case HASH_KEY_TYPE_STRING: stringKey = keys[keyIndex]; break; case HASH_KEY_TYPE_BYTES: byteKey = keys[keyIndex]; break; case HASH_KEY_TYPE_UINT32: uintKey = keys[keyIndex]; break; case HASH_KEY_TYPE_POINTER: pointerKey = keys[keyIndex]; break; case HASH_KEY_TYPE_UINT32_PAIR: case HASH_KEY_TYPE_OBJECT: break; } } TestCase_assertPointerNonNULL(stringKey); TestCase_assertPointerNonNULL(byteKey); TestCase_assertPointerNonNULL(uintKey); TestCase_assertPointerNonNULL(pointerKey); TestCase_assertUIntEqual(uintKey->data.uint32, 0); TestCase_assertStringEqual(stringKey->data.string.characters, "a"); TestCase_assertSizeEqual(stringKey->data.string.length, 1); TestCase_assertIntEqual(*((char *) byteKey->data.bytes.data), 'b'); TestCase_assertSizeEqual(byteKey->data.bytes.length, 1); TestCase_assertPointerEqual(pointerKey->data.pointer, (void *) 0x2); TextureAtlas_dispose(atlas); } #define assertVector2fEqual(vector, expected_x, expected_y) \ TestCase_assert((vector).x == (expected_x) && (vector).y == (expected_y), "Expected {%f, %f} but got {%f, %f}", (expected_x), (expected_y), (vector).x, (vector).y); static void testGetEntryPixelDimensions(void) { TextureAtlas * atlas = TextureAtlas_create(4, 8); Vector2f dimensions = TextureAtlas_getEntryPixelDimensions(atlas, RECT4f(0.0f, 0.25f, 0.5f, 0.75f)); assertVector2fEqual(dimensions, 1.0f, 2.0f); dimensions = TextureAtlas_getEntryPixelDimensions(atlas, RECT4f(0.5f, 1.0f, 0.125f, 0.25f)); assertVector2fEqual(dimensions, 2.0f, 1.0f); TextureAtlas_setPixelDimensions(atlas, 8, 4); dimensions = TextureAtlas_getEntryPixelDimensions(atlas, RECT4f(0.0f, 0.25f, 0.5f, 0.75f)); assertVector2fEqual(dimensions, 2.0f, 1.0f); dimensions = TextureAtlas_getEntryPixelDimensions(atlas, RECT4f(0.5f, 1.0f, 0.125f, 0.25f)); assertVector2fEqual(dimensions, 4.0f, 0.5f); } TEST_SUITE(TextureAtlasTest, testInit, testSetPixelDimensions, testGetSetKeys, testListKeys, testGetEntryPixelDimensions)