Commit e569955ca6136f5efc90ebab526de409ec7a5e30
1 parent
2132f4fd
added a polymorphic clear method, give messages if an assertion in an test fails
Showing
13 changed files
with
252 additions
and
31 deletions
| @@ -32,33 +32,33 @@ | @@ -32,33 +32,33 @@ | ||
| 32 | extern const _CCLASS const __##_class; \ | 32 | extern const _CCLASS const __##_class; \ |
| 33 | struct _##_class | 33 | struct _##_class |
| 34 | 34 | ||
| 35 | -#define ENDC(_class) } * _class; \ | ||
| 36 | - extern const _CCLASS const __##_class; | ||
| 37 | - | ||
| 38 | typedef void (* ctor)(void *, va_list *); | 35 | typedef void (* ctor)(void *, va_list *); |
| 36 | +typedef void (* clr)(void *); | ||
| 39 | typedef void (* dtor)(void *); | 37 | typedef void (* dtor)(void *); |
| 40 | typedef void (* jCtor)(void *, struct json_object *); | 38 | typedef void (* jCtor)(void *, struct json_object *); |
| 41 | typedef void (* jTo)(void *, struct json_object **); | 39 | typedef void (* jTo)(void *, struct json_object **); |
| 42 | 40 | ||
| 43 | - | ||
| 44 | typedef struct CCLASS { | 41 | typedef struct CCLASS { |
| 45 | const int magic; | 42 | const int magic; |
| 46 | size_t size; | 43 | size_t size; |
| 47 | void (* __construct)(void * _this, va_list * params); | 44 | void (* __construct)(void * _this, va_list * params); |
| 45 | + void (* __clear)(void * _this); | ||
| 48 | void (* __destruct)(void * _this); | 46 | void (* __destruct)(void * _this); |
| 49 | void (* __jsonConst)(void * _this, struct json_object * json); | 47 | void (* __jsonConst)(void * _this, struct json_object * json); |
| 50 | void (* __toJson)(void * _this, struct json_object ** json); | 48 | void (* __toJson)(void * _this, struct json_object ** json); |
| 51 | } * _CCLASS; | 49 | } * _CCLASS; |
| 52 | -#define CCLASS_PTR_SIZE sizeof(struct CCLASS *) | ||
| 53 | -#define CCLASS_SIZE sizeof(struct CCLASS) | 50 | +#define _CCLASS_SIZE sizeof(_CCLASS) |
| 51 | +#define CCLASS_SIZE sizeof(struct CCLASS) | ||
| 54 | 52 | ||
| 55 | #define __construct(class) static void __construct(class _this, va_list * params) | 53 | #define __construct(class) static void __construct(class _this, va_list * params) |
| 54 | +#define __clear(class) static void __clear(class _this) | ||
| 56 | #define __destruct(class) static void __destruct(class _this) | 55 | #define __destruct(class) static void __destruct(class _this) |
| 57 | #define __jsonConst(class) static void __jsonConst(class _this, struct json_object * json) | 56 | #define __jsonConst(class) static void __jsonConst(class _this, struct json_object * json) |
| 58 | #define __toJson(class) static void __toJson(class _this, struct json_object ** json) | 57 | #define __toJson(class) static void __toJson(class _this, struct json_object ** json) |
| 59 | 58 | ||
| 60 | #define INIT_CCLASS(class) \ | 59 | #define INIT_CCLASS(class) \ |
| 61 | __construct(class); \ | 60 | __construct(class); \ |
| 61 | + __clear(class); \ | ||
| 62 | __destruct(class); \ | 62 | __destruct(class); \ |
| 63 | __jsonConst(class); \ | 63 | __jsonConst(class); \ |
| 64 | __toJson(class); \ | 64 | __toJson(class); \ |
| @@ -66,6 +66,7 @@ typedef struct CCLASS { | @@ -66,6 +66,7 @@ typedef struct CCLASS { | ||
| 66 | CCLASS_MAGIC, \ | 66 | CCLASS_MAGIC, \ |
| 67 | sizeof(struct _##class), \ | 67 | sizeof(struct _##class), \ |
| 68 | (ctor)__construct, \ | 68 | (ctor)__construct, \ |
| 69 | + (clr)__clear, \ | ||
| 69 | (dtor)__destruct, \ | 70 | (dtor)__destruct, \ |
| 70 | (jCtor)__jsonConst, \ | 71 | (jCtor)__jsonConst, \ |
| 71 | (jTo)__toJson \ | 72 | (jTo)__toJson \ |
| @@ -73,6 +74,7 @@ typedef struct CCLASS { | @@ -73,6 +74,7 @@ typedef struct CCLASS { | ||
| 73 | 74 | ||
| 74 | void * _new(const _CCLASS _class, ...); | 75 | void * _new(const _CCLASS _class, ...); |
| 75 | void * _newFromJson(const _CCLASS _class, struct json_object * json); | 76 | void * _newFromJson(const _CCLASS _class, struct json_object * json); |
| 77 | +void clear(void * _object); | ||
| 76 | void delete(void * _object); | 78 | void delete(void * _object); |
| 77 | void toJson(void * _object, struct json_object ** json); | 79 | void toJson(void * _object, struct json_object ** json); |
| 78 | int isObject(void * _object); | 80 | int isObject(void * _object); |
| @@ -40,11 +40,10 @@ enum DYNTYPE_TYPES { | @@ -40,11 +40,10 @@ enum DYNTYPE_TYPES { | ||
| 40 | CLASS(DYNTYPE) { | 40 | CLASS(DYNTYPE) { |
| 41 | const _CCLASS const class; | 41 | const _CCLASS const class; |
| 42 | enum DYNTYPE_TYPES type; | 42 | enum DYNTYPE_TYPES type; |
| 43 | - size_t size; | ||
| 44 | union _data { | 43 | union _data { |
| 45 | unsigned char _boolean; | 44 | unsigned char _boolean; |
| 46 | int _int; | 45 | int _int; |
| 47 | - double _float; | 46 | + double _double; |
| 48 | char * _string; | 47 | char * _string; |
| 49 | struct _DYNTYPE ** _array; | 48 | struct _DYNTYPE ** _array; |
| 50 | DYNTYPE_HASH _hash; | 49 | DYNTYPE_HASH _hash; |
| @@ -53,6 +52,22 @@ CLASS(DYNTYPE) { | @@ -53,6 +52,22 @@ CLASS(DYNTYPE) { | ||
| 53 | 52 | ||
| 54 | #include "token/dyntype/hash.h" | 53 | #include "token/dyntype/hash.h" |
| 55 | 54 | ||
| 55 | +#define dyntype_newBoolean(value) new(DYNTYPE, DYNTYPE_TYPE_BOOLEAN, value) | ||
| 56 | +#define dyntype_newInt(value) new(DYNTYPE, DYNTYPE_TYPE_INT, value) | ||
| 57 | +#define dyntype_newDouble(value) new(DYNTYPE, DYNTYPE_TYPE_DOUBLE, value) | ||
| 58 | +#define dyntype_newString(value) new(DYNTYPE, DYNTYPE_TYPE_STRING, value) | ||
| 59 | +#define dyntype_newArray(value) new(DYNTYPE, DYNTYPE_TYPE_ARRAY, value) | ||
| 60 | +#define dyntype_newHash(value) new(DYNTYPE, DYNTYPE_TYPE_HASH, value) | ||
| 61 | + | ||
| 62 | +enum DYNTYPE_TYPES dyntype_getType(DYNTYPE _this); | ||
| 63 | +size_t dyntype_getSize(DYNTYPE _this); | ||
| 64 | +unsigned char dyntype_getBoolean(DYNTYPE _this); | ||
| 65 | +int dyntype_getInt(DYNTYPE _this); | ||
| 66 | +double dyntype_getDouble(DYNTYPE _this); | ||
| 67 | +char * dyntype_getString(DYNTYPE _this); | ||
| 68 | +DYNTYPE * dyntype_getArray(DYNTYPE _this); | ||
| 69 | +DYNTYPE_HASH dyntype_getHash(DYNTYPE _this); | ||
| 70 | + | ||
| 56 | #endif//__DYNTYPE_H__ | 71 | #endif//__DYNTYPE_H__ |
| 57 | 72 | ||
| 58 | // vim: set et ts=4 sw=4: | 73 | // vim: set et ts=4 sw=4: |
| @@ -23,6 +23,7 @@ | @@ -23,6 +23,7 @@ | ||
| 23 | #include "token/cclass.h" | 23 | #include "token/cclass.h" |
| 24 | 24 | ||
| 25 | #undef __construct | 25 | #undef __construct |
| 26 | +#undef __clear | ||
| 26 | #undef __destruct | 27 | #undef __destruct |
| 27 | #undef __jsonConst | 28 | #undef __jsonConst |
| 28 | #undef __toJson | 29 | #undef __toJson |
| @@ -62,6 +63,16 @@ _newFromJson(const _CCLASS _class, struct json_object * json) | @@ -62,6 +63,16 @@ _newFromJson(const _CCLASS _class, struct json_object * json) | ||
| 62 | } | 63 | } |
| 63 | 64 | ||
| 64 | void | 65 | void |
| 66 | +clear(void * _object) | ||
| 67 | +{ | ||
| 68 | + const struct CCLASS ** class = _object; | ||
| 69 | + | ||
| 70 | + if (_object && *class && (*class)->__clear) { | ||
| 71 | + (*class)->__clear(_object); | ||
| 72 | + } | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +void | ||
| 65 | delete(void * _object) | 76 | delete(void * _object) |
| 66 | { | 77 | { |
| 67 | const struct CCLASS ** class = *(void**)_object; | 78 | const struct CCLASS ** class = *(void**)_object; |
| @@ -97,7 +108,7 @@ _instanceOf(const _CCLASS _class, void * _object) | @@ -97,7 +108,7 @@ _instanceOf(const _CCLASS _class, void * _object) | ||
| 97 | { | 108 | { |
| 98 | const struct CCLASS ** class = _object; | 109 | const struct CCLASS ** class = _object; |
| 99 | 110 | ||
| 100 | - return (_class == *class); | 111 | + return (class && _class == *class); |
| 101 | } | 112 | } |
| 102 | 113 | ||
| 103 | // vim: set et ts=4 sw=4: | 114 | // vim: set et ts=4 sw=4: |
| @@ -23,13 +23,22 @@ | @@ -23,13 +23,22 @@ | ||
| 23 | #include "token/dyntype.h" | 23 | #include "token/dyntype.h" |
| 24 | #include "token/dyntype/hash.h" | 24 | #include "token/dyntype/hash.h" |
| 25 | 25 | ||
| 26 | +static | ||
| 27 | +size_t _dyntype_sizes[] = { | ||
| 28 | + 0, | ||
| 29 | + sizeof(unsigned char), | ||
| 30 | + sizeof(int), | ||
| 31 | + sizeof(double), | ||
| 32 | + sizeof(char *), | ||
| 33 | + sizeof(DYNTYPE *), | ||
| 34 | + sizeof(DYNTYPE_HASH) | ||
| 35 | +}; | ||
| 26 | 36 | ||
| 27 | INIT_CCLASS(DYNTYPE); | 37 | INIT_CCLASS(DYNTYPE); |
| 28 | 38 | ||
| 29 | __construct(DYNTYPE) | 39 | __construct(DYNTYPE) |
| 30 | { | 40 | { |
| 31 | _this->type = va_arg(* params, enum DYNTYPE_TYPES); | 41 | _this->type = va_arg(* params, enum DYNTYPE_TYPES); |
| 32 | - _this->size = va_arg(* params, size_t); | ||
| 33 | 42 | ||
| 34 | switch(_this->type) { | 43 | switch(_this->type) { |
| 35 | case DYNTYPE_TYPE_INT: | 44 | case DYNTYPE_TYPE_INT: |
| @@ -37,8 +46,13 @@ __construct(DYNTYPE) | @@ -37,8 +46,13 @@ __construct(DYNTYPE) | ||
| 37 | break; | 46 | break; |
| 38 | 47 | ||
| 39 | case DYNTYPE_TYPE_STRING: | 48 | case DYNTYPE_TYPE_STRING: |
| 40 | - (_this->data)._string = calloc(_this->size + 1, sizeof(char)); | ||
| 41 | - memcpy((_this->data)._string, va_arg(* params, const char *), _this->size); | 49 | + { |
| 50 | + const char * string = va_arg(* params, const char *); | ||
| 51 | + const size_t length = strlen(string); | ||
| 52 | + | ||
| 53 | + (_this->data)._string = calloc(length + 1, sizeof(char)); | ||
| 54 | + memcpy((_this->data)._string, string, length); | ||
| 55 | + } | ||
| 42 | break; | 56 | break; |
| 43 | 57 | ||
| 44 | case DYNTYPE_TYPE_HASH: | 58 | case DYNTYPE_TYPE_HASH: |
| @@ -55,35 +69,34 @@ __jsonConst(DYNTYPE) | @@ -55,35 +69,34 @@ __jsonConst(DYNTYPE) | ||
| 55 | switch (json_object_get_type(json)) { | 69 | switch (json_object_get_type(json)) { |
| 56 | case json_type_int: | 70 | case json_type_int: |
| 57 | _this->type = DYNTYPE_TYPE_INT; | 71 | _this->type = DYNTYPE_TYPE_INT; |
| 58 | - _this->size = sizeof(int); | ||
| 59 | (_this->data)._int = (int)json_object_get_int(json); | 72 | (_this->data)._int = (int)json_object_get_int(json); |
| 60 | break; | 73 | break; |
| 61 | 74 | ||
| 62 | case json_type_string: | 75 | case json_type_string: |
| 63 | { | 76 | { |
| 64 | const char * string = json_object_get_string(json); | 77 | const char * string = json_object_get_string(json); |
| 78 | + const size_t length = strlen(string); | ||
| 65 | 79 | ||
| 66 | _this->type = DYNTYPE_TYPE_STRING; | 80 | _this->type = DYNTYPE_TYPE_STRING; |
| 67 | - _this->size = strlen(string); | ||
| 68 | - (_this->data)._string = calloc(_this->size + 1, sizeof(char)); | ||
| 69 | - memcpy((_this->data)._string, string, _this->size); | 81 | + (_this->data)._string = calloc(length + 1, sizeof(char)); |
| 82 | + memcpy((_this->data)._string, string, length); | ||
| 70 | // the json object handles the memory for string.... | 83 | // the json object handles the memory for string.... |
| 71 | } | 84 | } |
| 72 | break; | 85 | break; |
| 73 | 86 | ||
| 74 | case json_type_object: | 87 | case json_type_object: |
| 75 | _this->type = DYNTYPE_TYPE_HASH; | 88 | _this->type = DYNTYPE_TYPE_HASH; |
| 76 | - _this->size = sizeof(DYNTYPE_HASH); | ||
| 77 | (_this->data)._hash = newFromJson(DYNTYPE_HASH, json); | 89 | (_this->data)._hash = newFromJson(DYNTYPE_HASH, json); |
| 78 | break; | 90 | break; |
| 79 | 91 | ||
| 80 | default: | 92 | default: |
| 81 | _this->type = DYNTYPE_TYPE_NULL; | 93 | _this->type = DYNTYPE_TYPE_NULL; |
| 82 | - _this->size = 0; | ||
| 83 | (_this->data)._hash = NULL; | 94 | (_this->data)._hash = NULL; |
| 84 | } | 95 | } |
| 85 | } | 96 | } |
| 86 | 97 | ||
| 98 | +__clear(DYNTYPE) {} | ||
| 99 | + | ||
| 87 | __destruct(DYNTYPE) | 100 | __destruct(DYNTYPE) |
| 88 | { | 101 | { |
| 89 | if (_this) { | 102 | if (_this) { |
| @@ -122,4 +135,53 @@ __toJson(DYNTYPE) | @@ -122,4 +135,53 @@ __toJson(DYNTYPE) | ||
| 122 | } | 135 | } |
| 123 | } | 136 | } |
| 124 | 137 | ||
| 138 | +enum DYNTYPE_TYPES | ||
| 139 | +dyntype_getType(DYNTYPE _this) | ||
| 140 | +{ | ||
| 141 | + return _this->type; | ||
| 142 | +} | ||
| 143 | + | ||
| 144 | +size_t | ||
| 145 | +dyntype_getSize(DYNTYPE _this) | ||
| 146 | +{ | ||
| 147 | + return _dyntype_sizes[_this->type]; | ||
| 148 | +} | ||
| 149 | + | ||
| 150 | +unsigned char | ||
| 151 | +dyntype_getBoolean(DYNTYPE _this) | ||
| 152 | +{ | ||
| 153 | + return (_this->data)._boolean; | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +int | ||
| 157 | +dyntype_getInt(DYNTYPE _this) | ||
| 158 | +{ | ||
| 159 | + return (_this->data)._int; | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +double | ||
| 163 | +dyntype_getDouble(DYNTYPE _this) | ||
| 164 | +{ | ||
| 165 | + return (_this->data)._double; | ||
| 166 | +} | ||
| 167 | + | ||
| 168 | +char * | ||
| 169 | +dyntype_getString(DYNTYPE _this) | ||
| 170 | +{ | ||
| 171 | + return (_this->data)._string; | ||
| 172 | +} | ||
| 173 | + | ||
| 174 | +DYNTYPE * | ||
| 175 | +dyntype_getArray(DYNTYPE _this) | ||
| 176 | +{ | ||
| 177 | + return (_this->data)._array; | ||
| 178 | +} | ||
| 179 | + | ||
| 180 | +DYNTYPE_HASH | ||
| 181 | +dyntype_getHash(DYNTYPE _this) | ||
| 182 | +{ | ||
| 183 | + return (_this->data)._hash; | ||
| 184 | +} | ||
| 185 | + | ||
| 186 | + | ||
| 125 | // vim: set et ts=4 sw=4: | 187 | // vim: set et ts=4 sw=4: |
| @@ -37,6 +37,7 @@ __construct(DYNTYPE_HASH) | @@ -37,6 +37,7 @@ __construct(DYNTYPE_HASH) | ||
| 37 | _this->used = 0; | 37 | _this->used = 0; |
| 38 | _updateHashSize(_this); | 38 | _updateHashSize(_this); |
| 39 | } | 39 | } |
| 40 | + | ||
| 40 | #undef __construct | 41 | #undef __construct |
| 41 | 42 | ||
| 42 | __jsonConst(DYNTYPE_HASH) | 43 | __jsonConst(DYNTYPE_HASH) |
| @@ -52,16 +53,22 @@ __jsonConst(DYNTYPE_HASH) | @@ -52,16 +53,22 @@ __jsonConst(DYNTYPE_HASH) | ||
| 52 | } | 53 | } |
| 53 | } | 54 | } |
| 54 | 55 | ||
| 56 | +__clear(DYNTYPE_HASH) {} | ||
| 57 | + | ||
| 55 | __destruct(DYNTYPE_HASH) | 58 | __destruct(DYNTYPE_HASH) |
| 56 | { | 59 | { |
| 57 | size_t index; | 60 | size_t index; |
| 58 | 61 | ||
| 59 | for (index = 0; index < _this->used; index ++) { | 62 | for (index = 0; index < _this->used; index ++) { |
| 60 | free(_this->keys[index]); | 63 | free(_this->keys[index]); |
| 64 | + delete(_this->values[index]); | ||
| 61 | } | 65 | } |
| 62 | 66 | ||
| 63 | free(_this->keys); | 67 | free(_this->keys); |
| 64 | free(_this->values); | 68 | free(_this->values); |
| 69 | + | ||
| 70 | + _this->size = _this->used = 0; | ||
| 71 | + _updateHashSize(_this); | ||
| 65 | } | 72 | } |
| 66 | 73 | ||
| 67 | __toJson(DYNTYPE_HASH) | 74 | __toJson(DYNTYPE_HASH) |
| @@ -24,7 +24,23 @@ INIT_CCLASS(PACKET); | @@ -24,7 +24,23 @@ INIT_CCLASS(PACKET); | ||
| 24 | 24 | ||
| 25 | __construct(PACKET) | 25 | __construct(PACKET) |
| 26 | { | 26 | { |
| 27 | - packet_set_default_content(_this); | 27 | + DYNTYPE header = va_arg(* params, DYNTYPE); |
| 28 | + DYNTYPE data = NULL; | ||
| 29 | + | ||
| 30 | + if (instanceOf(DYNTYPE, header)) { | ||
| 31 | + data = va_arg(* params, DYNTYPE); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + if (instanceOf(DYNTYPE, data)) { | ||
| 35 | + | ||
| 36 | + packet_setHeader(_this, header); | ||
| 37 | + packet_setData(_this, data); | ||
| 38 | + | ||
| 39 | + } else { | ||
| 40 | + | ||
| 41 | + packet_set_default_content(_this); | ||
| 42 | + | ||
| 43 | + } | ||
| 28 | } | 44 | } |
| 29 | 45 | ||
| 30 | __jsonConst(PACKET) | 46 | __jsonConst(PACKET) |
| @@ -49,7 +65,21 @@ __jsonConst(PACKET) | @@ -49,7 +65,21 @@ __jsonConst(PACKET) | ||
| 49 | packet_setData(_this, newFromJson(DYNTYPE, data)); | 65 | packet_setData(_this, newFromJson(DYNTYPE, data)); |
| 50 | } | 66 | } |
| 51 | 67 | ||
| 52 | -__destruct(PACKET) {} | 68 | +__clear(PACKET) {} |
| 69 | + | ||
| 70 | +__destruct(PACKET) | ||
| 71 | +{ | ||
| 72 | + DYNTYPE header = packet_getHeader(_this); | ||
| 73 | + DYNTYPE data = packet_getData(_this); | ||
| 74 | + | ||
| 75 | + if (NULL != header) { | ||
| 76 | + delete(&header); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + if (NULL != data) { | ||
| 80 | + delete(&data); | ||
| 81 | + } | ||
| 82 | +} | ||
| 53 | 83 | ||
| 54 | __toJson(PACKET) | 84 | __toJson(PACKET) |
| 55 | { | 85 | { |
| @@ -22,6 +22,8 @@ __jsonConst(MOCK_CLASS) | @@ -22,6 +22,8 @@ __jsonConst(MOCK_CLASS) | ||
| 22 | _this->value = json_object_get_int(json); | 22 | _this->value = json_object_get_int(json); |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | +__clear(MOCK_CLASS) {} | ||
| 26 | + | ||
| 25 | __destruct(MOCK_CLASS) | 27 | __destruct(MOCK_CLASS) |
| 26 | { | 28 | { |
| 27 | _called = 1; | 29 | _called = 1; |
| 1 | +#include <stdio.h> | ||
| 2 | + | ||
| 1 | #include "runtest.h" | 3 | #include "runtest.h" |
| 2 | #include "token/cclass.h" | 4 | #include "token/cclass.h" |
| 3 | #include "token/packet.h" | 5 | #include "token/packet.h" |
| @@ -36,14 +38,59 @@ static | @@ -36,14 +38,59 @@ static | ||
| 36 | int | 38 | int |
| 37 | testDefaultInit() | 39 | testDefaultInit() |
| 38 | { | 40 | { |
| 41 | + ASSERT_INSTANCE_OF(PACKET, packet); | ||
| 39 | ASSERT_NULL(packet_getHeader(packet)); | 42 | ASSERT_NULL(packet_getHeader(packet)); |
| 40 | ASSERT_NULL(packet_getData(packet)); | 43 | ASSERT_NULL(packet_getData(packet)); |
| 41 | 44 | ||
| 42 | return TEST_OK; | 45 | return TEST_OK; |
| 43 | } | 46 | } |
| 44 | 47 | ||
| 48 | +static | ||
| 49 | +int | ||
| 50 | +testParamInit1() | ||
| 51 | +{ | ||
| 52 | + __tearDown(); | ||
| 53 | + | ||
| 54 | + DYNTYPE header = dyntype_newInt(123); | ||
| 55 | + packet = new(PACKET, header, NULL); | ||
| 56 | + | ||
| 57 | + ASSERT_INSTANCE_OF(PACKET, packet); | ||
| 58 | + ASSERT_NULL(packet_getHeader(packet)); | ||
| 59 | + ASSERT_NULL(packet_getData(packet)); | ||
| 60 | + | ||
| 61 | + delete(&header); | ||
| 62 | + | ||
| 63 | + return TEST_OK; | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +static | ||
| 67 | +int | ||
| 68 | +testParamInit2() | ||
| 69 | +{ | ||
| 70 | + DYNTYPE header, data; | ||
| 71 | + | ||
| 72 | + __tearDown(); | ||
| 73 | + | ||
| 74 | + packet = new(PACKET, dyntype_newInt(123), dyntype_newInt(321)); | ||
| 75 | + | ||
| 76 | + ASSERT_INSTANCE_OF(PACKET, packet); | ||
| 77 | + | ||
| 78 | + header = packet_getHeader(packet); | ||
| 79 | + data = packet_getData(packet); | ||
| 80 | + | ||
| 81 | + ASSERT_INSTANCE_OF(DYNTYPE, header); | ||
| 82 | + ASSERT_INSTANCE_OF(DYNTYPE, data); | ||
| 83 | + | ||
| 84 | + ASSERT_EQUAL(123, dyntype_getInt(header)); | ||
| 85 | + ASSERT_EQUAL(321, dyntype_getInt(data)); | ||
| 86 | + | ||
| 87 | + return TEST_OK; | ||
| 88 | +} | ||
| 89 | + | ||
| 45 | const testfunc tests[] = { | 90 | const testfunc tests[] = { |
| 46 | - testDefaultInit | 91 | + testDefaultInit, |
| 92 | + testParamInit1, | ||
| 93 | + testParamInit2 | ||
| 47 | }; | 94 | }; |
| 48 | const size_t count = FUNCS_COUNT(tests); | 95 | const size_t count = FUNCS_COUNT(tests); |
| 49 | 96 |
| @@ -24,7 +24,7 @@ isObjectNull(void * _object) | @@ -24,7 +24,7 @@ isObjectNull(void * _object) | ||
| 24 | const struct CCLASS ** class = _object; | 24 | const struct CCLASS ** class = _object; |
| 25 | 25 | ||
| 26 | ASSERT_OBJECT(_object); | 26 | ASSERT_OBJECT(_object); |
| 27 | - ASSERT_MEM_NULL(_object + CCLASS_PTR_SIZE, (*class)->size - CCLASS_PTR_SIZE); | 27 | + ASSERT_MEM_NULL(_object + _CCLASS_SIZE, (*class)->size - _CCLASS_SIZE); |
| 28 | 28 | ||
| 29 | return TEST_OK; | 29 | return TEST_OK; |
| 30 | } | 30 | } |
| @@ -11,26 +11,68 @@ enum RESULT_TYPES { | @@ -11,26 +11,68 @@ enum RESULT_TYPES { | ||
| 11 | TEST_ERROR | 11 | TEST_ERROR |
| 12 | }; | 12 | }; |
| 13 | 13 | ||
| 14 | -#define ASSERT_NULL(value) if (NULL != (value)) return TEST_FAILED | ||
| 15 | -#define ASSERT_NOT_NULL(value) if (NULL == (value)) return TEST_FAILED | ||
| 16 | -#define ASSERT_EQUAL(val1,val2) if ((val1) != (val2)) return TEST_FAILED | ||
| 17 | -#define ASSERT_NOT_EQUAL(val1,val2) if ((val1) == (val2)) return TEST_FAILED | 14 | +#define ASSERT_NULL(value) \ |
| 15 | + if (NULL != (value)) { \ | ||
| 16 | + printf("%s[%d]: Assertion failed that %s is NULL\n", \ | ||
| 17 | + __FILE__, __LINE__, #value); \ | ||
| 18 | + return TEST_FAILED; } | ||
| 19 | + | ||
| 20 | +#define ASSERT_NOT_NULL(value) \ | ||
| 21 | + if (NULL == (value)) { \ | ||
| 22 | + printf("%s[%d]: Assertion failed that %s is NOT NULL\n", \ | ||
| 23 | + __FILE__, __LINE__, #value); \ | ||
| 24 | + return TEST_FAILED; } | ||
| 25 | + | ||
| 26 | +#define ASSERT_EQUAL(val1,val2) \ | ||
| 27 | + if ((val1) != (val2)) { \ | ||
| 28 | + printf("%s[%d]: Assertion failed that %s equals %s\n", \ | ||
| 29 | + __FILE__, __LINE__, #val1, #val2); \ | ||
| 30 | + return TEST_FAILED; } | ||
| 31 | + | ||
| 32 | +#define ASSERT_NOT_EQUAL(val1,val2) \ | ||
| 33 | + if ((val1) == (val2)) { \ | ||
| 34 | + printf("%s[%d]: Assertion failed that %s not equals %2\n", \ | ||
| 35 | + __FILE__, __LINE__, #val1, #val2); \ | ||
| 36 | + return TEST_FAILED; } | ||
| 37 | + | ||
| 18 | #define ASSERT_MEM_EQUAL(val1,val2,size) \ | 38 | #define ASSERT_MEM_EQUAL(val1,val2,size) \ |
| 19 | - if(0 != memcmp((val1), (val2), (size))) return TEST_FAILED | 39 | + if(0 != memcmp((val1), (val2), (size))) { \ |
| 40 | + printf("%s[%d]: Assertion failed that memory at %s equals %s for %lu bytes\n", \ | ||
| 41 | + __FILE__, __LINE__, #val1, #val2, size); \ | ||
| 42 | + return TEST_FAILED; } | ||
| 43 | + | ||
| 20 | #define ASSERT_MEM_NOT_EQUAL(val1,val2,size) \ | 44 | #define ASSERT_MEM_NOT_EQUAL(val1,val2,size) \ |
| 21 | - if(0 == memcmp((val1), (val2), (size))) return TEST_FAILED | ||
| 22 | -#define ASSERT_MEM_NULL(val, size) if (! isMemNull((val), (size))) return TEST_FAILED | 45 | + if(0 == memcmp((val1), (val2), (size))) { \ |
| 46 | + printf("%s[%d]: Assertion failed that memory at %s not equals %s for %lu bytes\n", \ | ||
| 47 | + __FILE__, __LINE__, #val1, #val2, size); \ | ||
| 48 | + return TEST_FAILED; } | ||
| 49 | + | ||
| 50 | +#define ASSERT_MEM_NULL(val, size) \ | ||
| 51 | + if (! isMemNull((val), (size))) { \ | ||
| 52 | + printf("%s[%d]: Assertion failed that memory at %s is NULL for %lu bytes\n", \ | ||
| 53 | + __FILE__, __LINE__, #val, size); \ | ||
| 54 | + return TEST_FAILED; } | ||
| 55 | + | ||
| 23 | #define ASSERT_MEM_NOT_NULL(val, size) \ | 56 | #define ASSERT_MEM_NOT_NULL(val, size) \ |
| 24 | - if (isMemNull((val), (size))) return TEST_FAILED | 57 | + if (isMemNull((val), (size))) { \ |
| 58 | + printf("%s[%d]: Assertion failed that memory at %s is NOT NULL for %lu bytes\n", \ | ||
| 59 | + __FILE__, __LINE__, #val, size); \ | ||
| 60 | + return TEST_FAILED; } | ||
| 61 | + | ||
| 25 | #define ASSERT_STRING_EQUAL(val1,val2) \ | 62 | #define ASSERT_STRING_EQUAL(val1,val2) \ |
| 26 | if(0 != strcmp((val1), (val2))) return TEST_FAILED | 63 | if(0 != strcmp((val1), (val2))) return TEST_FAILED |
| 27 | #define ASSERT_STRING_NOT_EQUAL(val1,val2) \ | 64 | #define ASSERT_STRING_NOT_EQUAL(val1,val2) \ |
| 28 | if(0 == strcmp((val1), (val2))) return TEST_FAILED | 65 | if(0 == strcmp((val1), (val2))) return TEST_FAILED |
| 66 | + | ||
| 29 | #define ASSERT_OBJECT(val) if (! isObject((val))) return TEST_FAILED | 67 | #define ASSERT_OBJECT(val) if (! isObject((val))) return TEST_FAILED |
| 30 | #define ASSERT_OBJECT_NULL(val) if (! isObjectNull((val))) return TEST_FAILED | 68 | #define ASSERT_OBJECT_NULL(val) if (! isObjectNull((val))) return TEST_FAILED |
| 31 | #define ASSERT_OBJECT_NOT_NULL(val) if (isObjectNull((val))) return TEST_FAILED | 69 | #define ASSERT_OBJECT_NOT_NULL(val) if (isObjectNull((val))) return TEST_FAILED |
| 70 | + | ||
| 32 | #define ASSERT_INSTANCE_OF(class, val) \ | 71 | #define ASSERT_INSTANCE_OF(class, val) \ |
| 33 | - if (! instanceOf(class, val)) return TEST_FAILED | 72 | + if (! instanceOf(class, val)) { \ |
| 73 | + printf("%s[%d]: Assertion failed that %s is instance of %s\n", \ | ||
| 74 | + __FILE__, __LINE__, #val, #class); \ | ||
| 75 | + return TEST_FAILED; } | ||
| 34 | 76 | ||
| 35 | 77 | ||
| 36 | typedef int (* const testfunc)(void); | 78 | typedef int (* const testfunc)(void); |
Please
register
or
login
to post a comment