From 76f113a2becbe164f3edb6d6147f3a4dd9994f95 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sun, 20 Nov 2011 02:39:21 +0100 Subject: [PATCH] finished tests for packet --- ChangeLog | 10 +++- src/packet.c | 9 ++- tests/packetTest.c | 133 +++++++++++++++++++++++++++++++++++++++++---- tests/runtest.c | 15 ++--- tests/runtest.h | 39 ++++++++++--- 5 files changed, 177 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index ae3b165..8e9e6a5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,14 @@ +2011-11-20 02:39:21 +0100 Georg Hopp + + * finished tests for packet (HEAD, master) + +2011-11-20 00:41:23 +0100 Georg Hopp + + * update ChangeLog (origin/master, origin/HEAD) + 2011-11-20 00:22:49 +0100 Georg Hopp - * added a polymorphic clear method, give messages if an assertion in an test fails (HEAD, origin/master, origin/HEAD, master) + * added a polymorphic clear method, give messages if an assertion in an test fails 2011-11-18 20:43:26 +0100 Georg Hopp diff --git a/src/packet.c b/src/packet.c index ef7d1ab..08748aa 100644 --- a/src/packet.c +++ b/src/packet.c @@ -48,7 +48,12 @@ __jsonConst(PACKET) struct json_object * header = NULL; struct json_object * data = NULL; - if (! json_type_array == json_object_get_type(json)) { + if (json_type_array != json_object_get_type(json)) { + packet_set_default_content(_this); + return; + } + + if (2 != json_object_array_length(json)) { packet_set_default_content(_this); return; } @@ -83,7 +88,7 @@ __destruct(PACKET) __toJson(PACKET) { - struct json_object * value; + struct json_object * value = NULL; *json = json_object_new_array(); diff --git a/tests/packetTest.c b/tests/packetTest.c index 62a89de..d894979 100644 --- a/tests/packetTest.c +++ b/tests/packetTest.c @@ -1,4 +1,6 @@ #include +#include +#include #include "runtest.h" #include "token/cclass.h" @@ -67,22 +69,127 @@ static int testParamInit2() { - DYNTYPE header, data; - __tearDown(); - packet = new(PACKET, dyntype_newInt(123), dyntype_newInt(321)); ASSERT_INSTANCE_OF(PACKET, packet); - header = packet_getHeader(packet); - data = packet_getData(packet); + ASSERT_INSTANCE_OF(DYNTYPE, packet_getHeader(packet)); + ASSERT_INSTANCE_OF(DYNTYPE, packet_getData(packet)); + + ASSERT_EQUAL(123, dyntype_getInt(packet_getHeader(packet))); + ASSERT_EQUAL(321, dyntype_getInt(packet_getData(packet))); + + return TEST_OK; +} + +static +int +testSetter() +{ + packet_setHeader(packet, dyntype_newInt(123)); + packet_setData(packet, dyntype_newInt(321)); + + ASSERT_INSTANCE_OF(DYNTYPE, packet_getHeader(packet)); + + ASSERT_INSTANCE_OF(DYNTYPE, packet_getHeader(packet)); + ASSERT_INSTANCE_OF(DYNTYPE, packet_getData(packet)); + + ASSERT_EQUAL(123, dyntype_getInt(packet_getHeader(packet))); + ASSERT_EQUAL(321, dyntype_getInt(packet_getData(packet))); + + return TEST_OK; +} + +static +int +testNewFromJson() { + struct json_object * json = json_tokener_parse("[123, 321]"); + + __tearDown(); + packet = newFromJson(PACKET, json); + json_object_put(json); + + ASSERT_INSTANCE_OF(DYNTYPE, packet_getHeader(packet)); + ASSERT_INSTANCE_OF(DYNTYPE, packet_getData(packet)); + + ASSERT_EQUAL(123, dyntype_getInt(packet_getHeader(packet))); + ASSERT_EQUAL(321, dyntype_getInt(packet_getData(packet))); + + return TEST_OK; +} + +static +int +testNewFromJsonFail1() { + struct json_object * json = json_tokener_parse("[123]"); + + __tearDown(); + packet = newFromJson(PACKET, json); + json_object_put(json); + + ASSERT_NULL(packet_getHeader(packet)); + ASSERT_NULL(packet_getData(packet)); + + return TEST_OK; +} + +static +int +testNewFromJsonFail2() { + struct json_object * json = json_tokener_parse("123"); + + __tearDown(); + packet = newFromJson(PACKET, json); + json_object_put(json); + + ASSERT_NULL(packet_getHeader(packet)); + ASSERT_NULL(packet_getData(packet)); + + return TEST_OK; +} + +static +int +testToJson1() { + struct json_object * json; + char * json_str; + + toJson(packet, &json); + json_str = calloc( + strlen(json_object_to_json_string(json)) + 1, + sizeof(char)); + memcpy(json_str, + json_object_to_json_string(json), + strlen(json_object_to_json_string(json))); + + ASSERT_STRING_EQUAL("[ null, null ]", json_str); + + json_object_put(json); + free(json_str); + + return TEST_OK; +} + +static +int +testToJson2() { + struct json_object * json; + char * json_str; + + testNewFromJson(); + toJson(packet, &json); + json_str = calloc( + strlen(json_object_to_json_string(json)) + 1, + sizeof(char)); + memcpy(json_str, + json_object_to_json_string(json), + strlen(json_object_to_json_string(json))); - ASSERT_INSTANCE_OF(DYNTYPE, header); - ASSERT_INSTANCE_OF(DYNTYPE, data); + ASSERT_STRING_EQUAL("[ 123, 321 ]", json_str); - ASSERT_EQUAL(123, dyntype_getInt(header)); - ASSERT_EQUAL(321, dyntype_getInt(data)); + json_object_put(json); + free(json_str); return TEST_OK; } @@ -90,7 +197,13 @@ testParamInit2() const testfunc tests[] = { testDefaultInit, testParamInit1, - testParamInit2 + testParamInit2, + testSetter, + testNewFromJson, + testNewFromJsonFail1, + testNewFromJsonFail2, + testToJson1, + testToJson2 }; const size_t count = FUNCS_COUNT(tests); diff --git a/tests/runtest.c b/tests/runtest.c index 69c10b0..17622f1 100644 --- a/tests/runtest.c +++ b/tests/runtest.c @@ -23,10 +23,11 @@ isObjectNull(void * _object) { const struct CCLASS ** class = _object; - ASSERT_OBJECT(_object); - ASSERT_MEM_NULL(_object + _CCLASS_SIZE, (*class)->size - _CCLASS_SIZE); + if (! isObject(_object)) { + return 0; + } - return TEST_OK; + return isMemNull(_object + _CCLASS_SIZE, (*class)->size - _CCLASS_SIZE); } int @@ -34,13 +35,13 @@ isMemNull(void * _mem, size_t size) { size_t index; - ASSERT_NOT_NULL(_mem); + if (NULL == _mem) { + return 0; + } for(index=0; index