test
This commit is contained in:
@@ -71,6 +71,32 @@ static void test_entityAddGetDisposeComponent(void **state) {
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entitySetGetFindByName(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t a = entityManagerAdd(&mgr);
|
||||
entityid_t b = entityManagerAdd(&mgr);
|
||||
|
||||
assert_string_equal(entityGetName(&mgr, a), "");
|
||||
assert_int_equal(entityFindByName(&mgr, "camera"), ENTITY_ID_INVALID);
|
||||
|
||||
entitySetName(&mgr, a, "camera");
|
||||
assert_string_equal(entityGetName(&mgr, a), "camera");
|
||||
assert_int_equal(entityFindByName(&mgr, "camera"), a);
|
||||
assert_int_equal(entityFindByName(&mgr, "nonexistent"), ENTITY_ID_INVALID);
|
||||
|
||||
// Renaming doesn't leave the old name findable, and other entities stay
|
||||
// unaffected.
|
||||
entitySetName(&mgr, a, "player");
|
||||
assert_int_equal(entityFindByName(&mgr, "camera"), ENTITY_ID_INVALID);
|
||||
assert_int_equal(entityFindByName(&mgr, "player"), a);
|
||||
assert_string_equal(entityGetName(&mgr, b), "");
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entityAddDuplicateComponentAsserts(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
@@ -90,6 +116,7 @@ int main(int argc, char **argv) {
|
||||
cmocka_unit_test(test_entityManagerAddIsolatesEntities),
|
||||
cmocka_unit_test(test_entityManagerFullAsserts),
|
||||
cmocka_unit_test(test_entityAddGetDisposeComponent),
|
||||
cmocka_unit_test(test_entitySetGetFindByName),
|
||||
cmocka_unit_test(test_entityAddDuplicateComponentAsserts),
|
||||
};
|
||||
|
||||
|
||||
@@ -94,6 +94,50 @@ static void test_entityPositionParentChildWorldTransform(void **state) {
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entityPositionDeserializeLookAt(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
|
||||
entityid_t entity = entityManagerAdd(&mgr);
|
||||
componentid_t comp = entityAddComponent(
|
||||
&mgr, entity, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
|
||||
const char_t *json =
|
||||
"{ \"x\": 0, \"y\": 5, \"z\": 10, "
|
||||
"\"lookAt\": { \"x\": 0, \"y\": 0, \"z\": 0 } }";
|
||||
yyjson_doc *doc = yyjson_read(json, strlen(json), YYJSON_READ_NOFLAG);
|
||||
assert_non_null(doc);
|
||||
|
||||
errorret_t ret = entityPositionDeserialize(
|
||||
&mgr, entity, comp, yyjson_doc_get_root(doc)
|
||||
);
|
||||
yyjson_doc_free(doc);
|
||||
assert_true(errorIsOk(ret));
|
||||
|
||||
// The eye point decoded back out of localTransform should match "x"/"y"/"z".
|
||||
vec3 eye;
|
||||
entityPositionGetLocalPosition(&mgr, entity, comp, eye);
|
||||
assert_float_equal(eye[0], 0.0f, 0.0001f);
|
||||
assert_float_equal(eye[1], 5.0f, 0.0001f);
|
||||
assert_float_equal(eye[2], 10.0f, 0.0001f);
|
||||
|
||||
// Forward (-local Z, see entityCameraGetForward's convention) should
|
||||
// point from eye toward the lookAt target (the origin here).
|
||||
mat4 transform;
|
||||
entityPositionGetLocalTransform(&mgr, entity, comp, transform);
|
||||
vec3 forward = { -transform[2][0], -transform[2][1], -transform[2][2] };
|
||||
glm_vec3_normalize(forward);
|
||||
|
||||
vec3 toTarget = { -eye[0], -eye[1], -eye[2] };
|
||||
glm_vec3_normalize(toTarget);
|
||||
|
||||
assert_float_equal(glm_vec3_dot(forward, toTarget), 1.0f, 0.001f);
|
||||
|
||||
entityManagerDispose(&mgr);
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_entityPositionDisposeDeep(void **state) {
|
||||
entitymanager_t mgr;
|
||||
entityManagerInit(&mgr);
|
||||
@@ -123,6 +167,7 @@ int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_entityPositionLocalGetSet),
|
||||
cmocka_unit_test(test_entityPositionParentChildWorldTransform),
|
||||
cmocka_unit_test(test_entityPositionDeserializeLookAt),
|
||||
cmocka_unit_test(test_entityPositionDisposeDeep),
|
||||
};
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include "time/time.h"
|
||||
#include "scene/scene.h"
|
||||
#include "entity/entitymanager.h"
|
||||
#include "entity/component/display/entityposition.h"
|
||||
#include "yyjson.h"
|
||||
|
||||
static void countingUpdateCallback(
|
||||
entitymanager_t *mgr,
|
||||
@@ -137,6 +139,82 @@ static void test_sceneUpdateOnlyRunsFixedUpdateOnNonDynamicFrames(void **state)
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_sceneSerializeDeserializeParentLink(void **state) {
|
||||
sceneInit();
|
||||
|
||||
sceneid_t sceneA = sceneCreate();
|
||||
entitymanager_t *mgrA = sceneGetEntities(sceneA);
|
||||
|
||||
entityid_t parent = entityManagerAdd(mgrA);
|
||||
componentid_t parentPos = entityAddComponent(
|
||||
mgrA, parent, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
entitySetName(mgrA, parent, "root");
|
||||
entityPositionSetLocalPosition(mgrA, parent, parentPos, (vec3){ 10, 0, 0 });
|
||||
|
||||
entityid_t child = entityManagerAdd(mgrA);
|
||||
componentid_t childPos = entityAddComponent(
|
||||
mgrA, child, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
entityPositionSetLocalPosition(mgrA, child, childPos, (vec3){ 1, 0, 0 });
|
||||
entityPositionSetParent(mgrA, child, childPos, parent, parentPos);
|
||||
|
||||
vec3 childWorldBefore;
|
||||
entityPositionGetWorldPosition(mgrA, child, childPos, childWorldBefore);
|
||||
assert_float_equal(childWorldBefore[0], 11.0f, 0.0001f);
|
||||
|
||||
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
|
||||
yyjson_mut_val *root = yyjson_mut_obj(doc);
|
||||
yyjson_mut_doc_set_root(doc, root);
|
||||
errorret_t ret = sceneSerialize(sceneA, doc, root);
|
||||
assert_true(errorIsOk(ret));
|
||||
|
||||
size_t len = 0;
|
||||
char_t *jsonStr = yyjson_mut_write(doc, 0, &len);
|
||||
assert_non_null(jsonStr);
|
||||
yyjson_mut_doc_free(doc);
|
||||
|
||||
yyjson_doc *readDoc = yyjson_read(jsonStr, len, 0);
|
||||
free(jsonStr);
|
||||
assert_non_null(readDoc);
|
||||
|
||||
sceneid_t sceneB = sceneCreate();
|
||||
entitymanager_t *mgrB = sceneGetEntities(sceneB);
|
||||
ret = sceneDeserialize(sceneB, yyjson_doc_get_root(readDoc));
|
||||
yyjson_doc_free(readDoc);
|
||||
assert_true(errorIsOk(ret));
|
||||
|
||||
entityid_t reloadedParent = entityFindByName(mgrB, "root");
|
||||
assert_true(reloadedParent != ENTITY_ID_INVALID);
|
||||
|
||||
entityid_t reloadedChild = ENTITY_ID_INVALID;
|
||||
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
|
||||
if(!(mgrB->entities[i].state & ENTITY_STATE_ACTIVE)) continue;
|
||||
if(i == reloadedParent) continue;
|
||||
reloadedChild = i;
|
||||
break;
|
||||
}
|
||||
assert_true(reloadedChild != ENTITY_ID_INVALID);
|
||||
|
||||
componentid_t reloadedChildPos = entityGetComponent(
|
||||
mgrB, reloadedChild, COMPONENT_TYPE_POSITION
|
||||
);
|
||||
assert_true(reloadedChildPos != COMPONENT_ID_INVALID);
|
||||
assert_int_equal(
|
||||
entityPositionGetParent(mgrB, reloadedChild, reloadedChildPos),
|
||||
reloadedParent
|
||||
);
|
||||
|
||||
vec3 childWorldAfter;
|
||||
entityPositionGetWorldPosition(
|
||||
mgrB, reloadedChild, reloadedChildPos, childWorldAfter
|
||||
);
|
||||
assert_float_equal(childWorldAfter[0], childWorldBefore[0], 0.0001f);
|
||||
|
||||
sceneDispose();
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_sceneDestroyClearsActive(void **state) {
|
||||
sceneInit();
|
||||
|
||||
@@ -157,6 +235,7 @@ int main(int argc, char **argv) {
|
||||
cmocka_unit_test(test_sceneEntitiesAreIsolatedPerScene),
|
||||
cmocka_unit_test(test_sceneFixedUpdateOnlyTicksActiveScene),
|
||||
cmocka_unit_test(test_sceneUpdateOnlyRunsFixedUpdateOnNonDynamicFrames),
|
||||
cmocka_unit_test(test_sceneSerializeDeserializeParentLink),
|
||||
cmocka_unit_test(test_sceneDestroyClearsActive),
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user