Commit 76f113a2becbe164f3edb6d6147f3a4dd9994f95

Authored by Georg Hopp
1 parent 2283bbaf

finished tests for packet

  1 +2011-11-20 02:39:21 +0100 Georg Hopp
  2 +
  3 + * finished tests for packet (HEAD, master)
  4 +
  5 +2011-11-20 00:41:23 +0100 Georg Hopp
  6 +
  7 + * update ChangeLog (origin/master, origin/HEAD)
  8 +
1 9 2011-11-20 00:22:49 +0100 Georg Hopp
2 10
3   - * added a polymorphic clear method, give messages if an assertion in an test fails (HEAD, origin/master, origin/HEAD, master)
  11 + * added a polymorphic clear method, give messages if an assertion in an test fails
4 12
5 13 2011-11-18 20:43:26 +0100 Georg Hopp
6 14
... ...
... ... @@ -48,7 +48,12 @@ __jsonConst(PACKET)
48 48 struct json_object * header = NULL;
49 49 struct json_object * data = NULL;
50 50
51   - if (! json_type_array == json_object_get_type(json)) {
  51 + if (json_type_array != json_object_get_type(json)) {
  52 + packet_set_default_content(_this);
  53 + return;
  54 + }
  55 +
  56 + if (2 != json_object_array_length(json)) {
52 57 packet_set_default_content(_this);
53 58 return;
54 59 }
... ... @@ -83,7 +88,7 @@ __destruct(PACKET)
83 88
84 89 __toJson(PACKET)
85 90 {
86   - struct json_object * value;
  91 + struct json_object * value = NULL;
87 92
88 93 *json = json_object_new_array();
89 94
... ...
1 1 #include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <json/json.h>
2 4
3 5 #include "runtest.h"
4 6 #include "token/cclass.h"
... ... @@ -67,22 +69,127 @@ static
67 69 int
68 70 testParamInit2()
69 71 {
70   - DYNTYPE header, data;
71   -
72 72 __tearDown();
73   -
74 73 packet = new(PACKET, dyntype_newInt(123), dyntype_newInt(321));
75 74
76 75 ASSERT_INSTANCE_OF(PACKET, packet);
77 76
78   - header = packet_getHeader(packet);
79   - data = packet_getData(packet);
  77 + ASSERT_INSTANCE_OF(DYNTYPE, packet_getHeader(packet));
  78 + ASSERT_INSTANCE_OF(DYNTYPE, packet_getData(packet));
  79 +
  80 + ASSERT_EQUAL(123, dyntype_getInt(packet_getHeader(packet)));
  81 + ASSERT_EQUAL(321, dyntype_getInt(packet_getData(packet)));
  82 +
  83 + return TEST_OK;
  84 +}
  85 +
  86 +static
  87 +int
  88 +testSetter()
  89 +{
  90 + packet_setHeader(packet, dyntype_newInt(123));
  91 + packet_setData(packet, dyntype_newInt(321));
  92 +
  93 + ASSERT_INSTANCE_OF(DYNTYPE, packet_getHeader(packet));
  94 +
  95 + ASSERT_INSTANCE_OF(DYNTYPE, packet_getHeader(packet));
  96 + ASSERT_INSTANCE_OF(DYNTYPE, packet_getData(packet));
  97 +
  98 + ASSERT_EQUAL(123, dyntype_getInt(packet_getHeader(packet)));
  99 + ASSERT_EQUAL(321, dyntype_getInt(packet_getData(packet)));
  100 +
  101 + return TEST_OK;
  102 +}
  103 +
  104 +static
  105 +int
  106 +testNewFromJson() {
  107 + struct json_object * json = json_tokener_parse("[123, 321]");
  108 +
  109 + __tearDown();
  110 + packet = newFromJson(PACKET, json);
  111 + json_object_put(json);
  112 +
  113 + ASSERT_INSTANCE_OF(DYNTYPE, packet_getHeader(packet));
  114 + ASSERT_INSTANCE_OF(DYNTYPE, packet_getData(packet));
  115 +
  116 + ASSERT_EQUAL(123, dyntype_getInt(packet_getHeader(packet)));
  117 + ASSERT_EQUAL(321, dyntype_getInt(packet_getData(packet)));
  118 +
  119 + return TEST_OK;
  120 +}
  121 +
  122 +static
  123 +int
  124 +testNewFromJsonFail1() {
  125 + struct json_object * json = json_tokener_parse("[123]");
  126 +
  127 + __tearDown();
  128 + packet = newFromJson(PACKET, json);
  129 + json_object_put(json);
  130 +
  131 + ASSERT_NULL(packet_getHeader(packet));
  132 + ASSERT_NULL(packet_getData(packet));
  133 +
  134 + return TEST_OK;
  135 +}
  136 +
  137 +static
  138 +int
  139 +testNewFromJsonFail2() {
  140 + struct json_object * json = json_tokener_parse("123");
  141 +
  142 + __tearDown();
  143 + packet = newFromJson(PACKET, json);
  144 + json_object_put(json);
  145 +
  146 + ASSERT_NULL(packet_getHeader(packet));
  147 + ASSERT_NULL(packet_getData(packet));
  148 +
  149 + return TEST_OK;
  150 +}
  151 +
  152 +static
  153 +int
  154 +testToJson1() {
  155 + struct json_object * json;
  156 + char * json_str;
  157 +
  158 + toJson(packet, &json);
  159 + json_str = calloc(
  160 + strlen(json_object_to_json_string(json)) + 1,
  161 + sizeof(char));
  162 + memcpy(json_str,
  163 + json_object_to_json_string(json),
  164 + strlen(json_object_to_json_string(json)));
  165 +
  166 + ASSERT_STRING_EQUAL("[ null, null ]", json_str);
  167 +
  168 + json_object_put(json);
  169 + free(json_str);
  170 +
  171 + return TEST_OK;
  172 +}
  173 +
  174 +static
  175 +int
  176 +testToJson2() {
  177 + struct json_object * json;
  178 + char * json_str;
  179 +
  180 + testNewFromJson();
  181 + toJson(packet, &json);
  182 + json_str = calloc(
  183 + strlen(json_object_to_json_string(json)) + 1,
  184 + sizeof(char));
  185 + memcpy(json_str,
  186 + json_object_to_json_string(json),
  187 + strlen(json_object_to_json_string(json)));
80 188
81   - ASSERT_INSTANCE_OF(DYNTYPE, header);
82   - ASSERT_INSTANCE_OF(DYNTYPE, data);
  189 + ASSERT_STRING_EQUAL("[ 123, 321 ]", json_str);
83 190
84   - ASSERT_EQUAL(123, dyntype_getInt(header));
85   - ASSERT_EQUAL(321, dyntype_getInt(data));
  191 + json_object_put(json);
  192 + free(json_str);
86 193
87 194 return TEST_OK;
88 195 }
... ... @@ -90,7 +197,13 @@ testParamInit2()
90 197 const testfunc tests[] = {
91 198 testDefaultInit,
92 199 testParamInit1,
93   - testParamInit2
  200 + testParamInit2,
  201 + testSetter,
  202 + testNewFromJson,
  203 + testNewFromJsonFail1,
  204 + testNewFromJsonFail2,
  205 + testToJson1,
  206 + testToJson2
94 207 };
95 208 const size_t count = FUNCS_COUNT(tests);
96 209
... ...
... ... @@ -23,10 +23,11 @@ isObjectNull(void * _object)
23 23 {
24 24 const struct CCLASS ** class = _object;
25 25
26   - ASSERT_OBJECT(_object);
27   - ASSERT_MEM_NULL(_object + _CCLASS_SIZE, (*class)->size - _CCLASS_SIZE);
  26 + if (! isObject(_object)) {
  27 + return 0;
  28 + }
28 29
29   - return TEST_OK;
  30 + return isMemNull(_object + _CCLASS_SIZE, (*class)->size - _CCLASS_SIZE);
30 31 }
31 32
32 33 int
... ... @@ -34,13 +35,13 @@ isMemNull(void * _mem, size_t size)
34 35 {
35 36 size_t index;
36 37
37   - ASSERT_NOT_NULL(_mem);
  38 + if (NULL == _mem) {
  39 + return 0;
  40 + }
38 41
39 42 for(index=0; index<size && 0 == ((char *)_mem)[index]; index++);
40 43
41   - ASSERT_EQUAL(size, index);
42   -
43   - return TEST_OK;
  44 + return (size == index);
44 45 }
45 46
46 47 int
... ...
... ... @@ -25,25 +25,25 @@ enum RESULT_TYPES {
25 25
26 26 #define ASSERT_EQUAL(val1,val2) \
27 27 if ((val1) != (val2)) { \
28   - printf("%s[%d]: Assertion failed that %s equals %s\n", \
  28 + printf("%s[%d]: Assertion failed that %s EQUALS %s\n", \
29 29 __FILE__, __LINE__, #val1, #val2); \
30 30 return TEST_FAILED; }
31 31
32 32 #define ASSERT_NOT_EQUAL(val1,val2) \
33 33 if ((val1) == (val2)) { \
34   - printf("%s[%d]: Assertion failed that %s not equals %2\n", \
  34 + printf("%s[%d]: Assertion failed that %s NOT EQUALS %s\n", \
35 35 __FILE__, __LINE__, #val1, #val2); \
36 36 return TEST_FAILED; }
37 37
38 38 #define ASSERT_MEM_EQUAL(val1,val2,size) \
39 39 if(0 != memcmp((val1), (val2), (size))) { \
40   - printf("%s[%d]: Assertion failed that memory at %s equals %s for %lu bytes\n", \
  40 + printf("%s[%d]: Assertion failed that memory at %s EQUALS %s for %lu bytes\n", \
41 41 __FILE__, __LINE__, #val1, #val2, size); \
42 42 return TEST_FAILED; }
43 43
44 44 #define ASSERT_MEM_NOT_EQUAL(val1,val2,size) \
45 45 if(0 == memcmp((val1), (val2), (size))) { \
46   - printf("%s[%d]: Assertion failed that memory at %s not equals %s for %lu bytes\n", \
  46 + printf("%s[%d]: Assertion failed that memory at %s NOT EQUALS %s for %lu bytes\n", \
47 47 __FILE__, __LINE__, #val1, #val2, size); \
48 48 return TEST_FAILED; }
49 49
... ... @@ -60,13 +60,34 @@ enum RESULT_TYPES {
60 60 return TEST_FAILED; }
61 61
62 62 #define ASSERT_STRING_EQUAL(val1,val2) \
63   - if(0 != strcmp((val1), (val2))) return TEST_FAILED
  63 + if(0 != strcmp((val1), (val2))) { \
  64 + printf("%s[%d]: Assertion failed that string %s EQUALS %s\n", \
  65 + __FILE__, __LINE__, val1, val2); \
  66 + return TEST_FAILED; }
  67 +
64 68 #define ASSERT_STRING_NOT_EQUAL(val1,val2) \
65   - if(0 == strcmp((val1), (val2))) return TEST_FAILED
  69 + if(0 == strcmp((val1), (val2))) { \
  70 + printf("%s[%d]: Assertion failed that string %s NOT EQUALS %s\n", \
  71 + __FILE__, __LINE__, val1, val2); \
  72 + return TEST_FAILED; }
  73 +
  74 +#define ASSERT_OBJECT(val) \
  75 + if (! isObject((val))) { \
  76 + printf("%s[%d]: Assertion failed that %s IS an object\n", \
  77 + __FILE__, __LINE__, #val); \
  78 + return TEST_FAILED; }
  79 +
  80 +#define ASSERT_OBJECT_NULL(val) \
  81 + if (! isObjectNull((val))) { \
  82 + printf("%s[%d]: Assertion failed that %s IS an UNINITIALIZED object\n", \
  83 + __FILE__, __LINE__, #val); \
  84 + return TEST_FAILED; }
66 85
67   -#define ASSERT_OBJECT(val) if (! isObject((val))) return TEST_FAILED
68   -#define ASSERT_OBJECT_NULL(val) if (! isObjectNull((val))) return TEST_FAILED
69   -#define ASSERT_OBJECT_NOT_NULL(val) if (isObjectNull((val))) return TEST_FAILED
  86 +#define ASSERT_OBJECT_NOT_NULL(val) \
  87 + if (isObjectNull((val))) { \
  88 + printf("%s[%d]: Assertion failed that %s IS an INITIALIZED object\n", \
  89 + __FILE__, __LINE__, #val); \
  90 + return TEST_FAILED; }
70 91
71 92 #define ASSERT_INSTANCE_OF(class, val) \
72 93 if (! instanceOf(class, val)) { \
... ...
Please register or login to post a comment