Commit d8e897c376bbf5b9f49a9031f5b3e9b0a812713f

Authored by Georg Hopp
1 parent 1fb326bc

optimized the class definition stuff, add instanceOf and isObject methods and ad…

…d assertion macros to use these checks
  1 +2011-11-17 13:00:11 +0100 Georg Hopp
  2 +
  3 + * optimized the class definition stuff, add instanceOf and isObject methods and add assertion macros to use these checks (HEAD, master)
  4 +
  5 +2011-11-17 08:33:56 +0100 Georg Hopp
  6 +
  7 + * remove obsolete non conformant tests for crypt
  8 +
  9 +2011-11-17 08:31:24 +0100 Georg Hopp
  10 +
  11 + * always run checks under valgrind now. @TODO Let configure set the environment variable only if valgrind is installed.
  12 +
  13 +2011-11-17 08:29:25 +0100 Georg Hopp
  14 +
  15 + * add tests form crypt class @TODO: add assertion on length
  16 +
  17 +2011-11-17 08:27:29 +0100 Georg Hopp
  18 +
  19 + * add new assertion macros
  20 +
1 21 2011-11-16 19:32:44 +0100 Georg Hopp
2 22
3   - * i had to remove the cool plugin stuff for my tests, as i found no working way to use this with autotools. Now all works. (HEAD, master)
  23 + * i had to remove the cool plugin stuff for my tests, as i found no working way to use this with autotools. Now all works. (origin/master, origin/HEAD)
4 24
5 25 2011-11-16 18:14:51 +0100 Georg Hopp
6 26
... ... @@ -8,7 +28,7 @@
8 28
9 29 2011-11-16 09:09:32 +0100 Georg Hopp
10 30
11   - * work on test <<framework>> (origin/master, origin/HEAD)
  31 + * work on test <<framework>>
12 32
13 33 2011-11-15 21:26:08 +0100 Georg Hopp
14 34
... ...
... ... @@ -22,6 +22,20 @@
22 22 #include <sys/types.h>
23 23 #include <json/json.h>
24 24
  25 +#define CCLASS_MAGIC 0xFEFE
  26 +
  27 +#define INIT_CCLASS(name, jsonConst, toJson) \
  28 + static const struct CCLASS _##name = { \
  29 + CCLASS_MAGIC, \
  30 + sizeof(struct name), \
  31 + (ctor)__construct, \
  32 + (jCtor)jsonConst, \
  33 + (dtor)__destruct, \
  34 + (jTo)toJson \
  35 + }; const struct CCLASS * const name = &_##name
  36 +
  37 +
  38 +
25 39 typedef void (* ctor)(void *, va_list *);
26 40 typedef void (* dtor)(void *);
27 41 typedef void (* jCtor)(void *, struct json_object *);
... ... @@ -29,24 +43,22 @@ typedef struct json_object * (* jTo)(void *);
29 43
30 44
31 45 struct CCLASS {
  46 + const int magic;
32 47 size_t size;
33 48 void (* __construct)(void * _this, va_list * params);
34 49 void (* __jsonConst)(void * _this, struct json_object * json);
35 50 void (* __destruct)(void * _this);
36 51 struct json_object * (* __toJson)(void * _this);
37 52 };
  53 +#define CCLASS_PTR_SIZE sizeof(struct CCLASS *)
  54 +#define CCLASS_SIZE sizeof(struct CCLASS)
38 55
39   -void *
40   -new(const void * _class, ...);
41   -
42   -void *
43   -newFromJson(const void * _class, struct json_object * json);
44   -
45   -void
46   -delete(void * _object);
47   -
48   -struct json_object *
49   -toJson(void * _object);
  56 +void * new(const void * _class, ...);
  57 +void * newFromJson(const void * _class, struct json_object * json);
  58 +void delete(void * _object);
  59 +struct json_object * toJson(void * _object);
  60 +int isObject(void * _object);
  61 +int instanceOf(const void * _class, void * _object);
50 62
51 63 #endif//__CCLASS_H__
52 64
... ...
... ... @@ -82,4 +82,20 @@ toJson(void * _object)
82 82 return NULL;
83 83 }
84 84
  85 +int
  86 +isObject(void * _object)
  87 +{
  88 + const struct CCLASS ** class = _object;
  89 +
  90 + return (_object && (*class) && CCLASS_MAGIC == (*class)->magic);
  91 +}
  92 +
  93 +int
  94 +instanceOf(const void * _class, void * _object)
  95 +{
  96 + const struct CCLASS ** class = _object;
  97 +
  98 + return (_class == *class);
  99 +}
  100 +
85 101 // vim: set et ts=4 sw=4:
... ...
... ... @@ -56,16 +56,7 @@ __destruct(struct CRYPT * _this)
56 56 mcrypt_module_close(_this->mcrypt);
57 57 }
58 58
59   -static const
60   -struct CCLASS _crypt = {
61   - sizeof(struct CRYPT),
62   - (ctor)__construct,
63   - NULL,
64   - (dtor)__destruct,
65   - NULL
66   -};
67   -
68   -const struct CCLASS * const CRYPT = &_crypt;
  59 +INIT_CCLASS(CRYPT, NULL, NULL);
69 60
70 61 void *
71 62 crypt_createIv(struct CRYPT * _this)
... ...
... ... @@ -132,15 +132,6 @@ __toJson(struct DYNTYPE * _this)
132 132 return json;
133 133 }
134 134
135   -static const
136   -struct CCLASS _dyntype = {
137   - sizeof(struct DYNTYPE),
138   - (ctor)__construct,
139   - (jCtor)__jsonConst,
140   - (dtor)__destruct,
141   - (jTo)__toJson
142   -};
143   -
144   -const struct CCLASS * const DYNTYPE = &_dyntype;
  135 +INIT_CCLASS(DYNTYPE, __jsonConst, __toJson);
145 136
146 137 // vim: set et ts=4 sw=4:
... ...
... ... @@ -84,16 +84,7 @@ __toJson(struct DYNTYPE_HASH * _this)
84 84 return json;
85 85 }
86 86
87   -static const
88   -struct CCLASS _dyntype_hash = {
89   - sizeof(struct DYNTYPE_HASH),
90   - (ctor)__construct,
91   - (jCtor)__jsonConst,
92   - (dtor)__destruct,
93   - (jTo)__toJson
94   -};
95   -
96   -const struct CCLASS * const DYNTYPE_HASH = &_dyntype_hash;
  87 +INIT_CCLASS(DYNTYPE_HASH, __jsonConst, __toJson);
97 88
98 89 static
99 90 void
... ...
... ... @@ -69,16 +69,7 @@ __toJson(struct PACKET * _this)
69 69 return json;
70 70 }
71 71
72   -static const
73   -struct CCLASS _packet = {
74   - sizeof(struct PACKET),
75   - (ctor)__construct,
76   - (jCtor)__jsonConst,
77   - (dtor)__destruct,
78   - (jTo)__toJson
79   -};
80   -
81   -const struct CCLASS * const PACKET = &_packet;
  72 +INIT_CCLASS(PACKET, __jsonConst, __toJson);
82 73
83 74 struct DYNTYPE *
84 75 packet_getHeader(
... ...
... ... @@ -10,23 +10,28 @@ const char testname[] = "cclassTest";
10 10 struct MOCK_CLASS * mock = NULL;
11 11
12 12 static
13   -void
  13 +int
14 14 __setUp()
15 15 {
16 16 mock = NULL;
17 17 _reset();
  18 +
  19 + return TEST_OK;
18 20 }
19   -void (* const setUp)() = __setUp;
  21 +int (* const setUp)() = __setUp;
20 22
21 23 static
22   -void
  24 +int
23 25 __tearDown()
24 26 {
25 27 if (NULL != mock) {
  28 + ASSERT_OBJECT(mock);
26 29 delete(&mock);
27 30 }
  31 +
  32 + return TEST_OK;
28 33 }
29   -void (* const tearDown)() = __tearDown;
  34 +int (* const tearDown)() = __tearDown;
30 35
31 36 static
32 37 int
... ... @@ -34,7 +39,7 @@ testNew(void)
34 39 {
35 40 mock = new(MOCK_CLASS, 123);
36 41
37   - ASSERT_NOT_NULL(mock);
  42 + ASSERT_OBJECT_NOT_NULL(mock);
38 43 ASSERT_EQUAL(1, _called);
39 44 ASSERT_EQUAL(123, mock_class_getValue(mock));
40 45
... ... @@ -50,7 +55,7 @@ testNewFromJson(void)
50 55 mock = newFromJson(MOCK_CLASS, json);
51 56 json_object_put(json);
52 57
53   - ASSERT_NOT_NULL(mock);
  58 + ASSERT_OBJECT_NOT_NULL(mock);
54 59 ASSERT_EQUAL(1, _called);
55 60 ASSERT_EQUAL(123, mock_class_getValue(mock));
56 61
... ... @@ -82,9 +87,11 @@ testToJson(void)
82 87 mock = new(MOCK_CLASS, 123);
83 88 int value;
84 89
  90 + _reset();
85 91 json = toJson(mock);
86 92
87 93 ASSERT_NOT_NULL(json);
  94 +
88 95 value = json_object_get_int(json);
89 96 json_object_put(json);
90 97
... ...
... ... @@ -16,20 +16,29 @@ struct CRYPT * crypt = NULL;
16 16
17 17
18 18 static
19   -void
  19 +int
20 20 __setUp()
21 21 {
22 22 crypt = new(CRYPT, MCRYPT_RIJNDAEL_256, MCRYPT_CFB);
  23 +
  24 + ASSERT_INSTANCE_OF(CRYPT, crypt);
  25 +
  26 + return TEST_OK;
23 27 }
24   -void (* const setUp)() = __setUp;
  28 +int (* const setUp)() = __setUp;
25 29
26 30 static
27   -void
  31 +int
28 32 __tearDown()
29 33 {
30   - delete(&crypt);
  34 + if (NULL != crypt) {
  35 + ASSERT_OBJECT(crypt);
  36 + delete(&crypt);
  37 + }
  38 +
  39 + return TEST_OK;
31 40 }
32   -void (* const tearDown)() = __tearDown;
  41 +int (* const tearDown)() = __tearDown;
33 42
34 43 static
35 44 int
... ...
... ... @@ -48,16 +48,7 @@ __toJson(struct MOCK_CLASS * _this)
48 48 return json;
49 49 }
50 50
51   -static const
52   -struct CCLASS _mock_class = {
53   - sizeof(struct MOCK_CLASS),
54   - (ctor)__construct,
55   - (jCtor)__jsonConst,
56   - (dtor)__destruct,
57   - (jTo)__toJson
58   -};
59   -
60   -const struct CCLASS * const MOCK_CLASS = &_mock_class;
  51 +INIT_CCLASS(MOCK_CLASS, __jsonConst, __toJson);
61 52
62 53 /**
63 54 * ~~~ method implementations ~~~~~~~~
... ...
... ... @@ -4,6 +4,8 @@
4 4 #include <sys/types.h>
5 5
6 6 #include "runtest.h"
  7 +#include "token/cclass.h"
  8 +
7 9
8 10 #define TEST_OK_CHAR '.'
9 11 #define TEST_FAILED_CHAR 'F'
... ... @@ -16,6 +18,30 @@ const char results[3] = {
16 18 TEST_ERROR_CHAR
17 19 };
18 20
  21 +int
  22 +isObjectNull(void * _object)
  23 +{
  24 + const struct CCLASS ** class = _object;
  25 +
  26 + ASSERT_OBJECT(_object);
  27 + ASSERT_MEM_NULL(_object + CCLASS_PTR_SIZE, (*class)->size - CCLASS_PTR_SIZE);
  28 +
  29 + return TEST_OK;
  30 +}
  31 +
  32 +int
  33 +isMemNull(void * _mem, size_t size)
  34 +{
  35 + size_t index;
  36 +
  37 + ASSERT_NOT_NULL(_mem);
  38 +
  39 + for(index=0; index<size && 0 == ((char *)_mem)[index]; index++);
  40 +
  41 + ASSERT_EQUAL(size, index);
  42 +
  43 + return TEST_OK;
  44 +}
19 45
20 46 int
21 47 main(int argc, char * argv[])
... ... @@ -29,16 +55,24 @@ main(int argc, char * argv[])
29 55 printf("running tests for %s\n", testname);
30 56
31 57 for (index=0; index<count; index++) {
32   - int result;
  58 + int result, _setUp = 0; // initialize setup to false
33 59
34 60 if (NULL != setUp) {
35   - setUp();
  61 + if (TEST_OK == (result = setUp())) {
  62 + _setUp = 1; // we successfully set up the test
  63 + }
  64 + }
  65 +
  66 + if (_setUp) {
  67 + result = tests[index]();
36 68 }
37 69
38   - result = tests[index]();
  70 + if (_setUp && NULL != tearDown) {
  71 + int _tearDown = tearDown();
39 72
40   - if (NULL != setUp) {
41   - tearDown();
  73 + if ((! TEST_OK == _tearDown) && TEST_OK == result) {
  74 + result = _tearDown;
  75 + }
42 76 }
43 77
44 78 switch (result) {
... ...
... ... @@ -19,10 +19,18 @@ enum RESULT_TYPES {
19 19 if(0 != memcmp((val1), (val2), (size))) return TEST_FAILED
20 20 #define ASSERT_MEM_NOT_EQUAL(val1,val2,size) \
21 21 if(0 == memcmp((val1), (val2), (size))) return TEST_FAILED
  22 +#define ASSERT_MEM_NULL(val, size) if (! isMemNull((val), (size))) return TEST_FAILED
  23 +#define ASSERT_MEM_NOT_NULL(val, size) \
  24 + if (isMemNull((val), (size))) return TEST_FAILED
22 25 #define ASSERT_STRING_EQUAL(val1,val2) \
23 26 if(0 != strcmp((val1), (val2))) return TEST_FAILED
24 27 #define ASSERT_STRING_NOT_EQUAL(val1,val2) \
25 28 if(0 == strcmp((val1), (val2))) return TEST_FAILED
  29 +#define ASSERT_OBJECT(val) if (! isObject((val))) return TEST_FAILED
  30 +#define ASSERT_OBJECT_NULL(val) if (! isObjectNull((val))) return TEST_FAILED
  31 +#define ASSERT_OBJECT_NOT_NULL(val) if (isObjectNull((val))) return TEST_FAILED
  32 +#define ASSERT_INSTANCE_OF(class, val) \
  33 + if (! instanceOf((class), (val))) return TEST_FAILED
26 34
27 35
28 36 typedef int (* const testfunc)(void);
... ... @@ -32,8 +40,11 @@ extern const char testname[];
32 40 extern testfunc tests[];
33 41 extern const size_t count;
34 42
35   -extern void (* const setUp)();
36   -extern void (* const tearDown)();
  43 +extern int (* const setUp)();
  44 +extern int (* const tearDown)();
  45 +
  46 +int isMemNull(void * _mem, size_t size);
  47 +int isObjectNull(void * _object);
37 48
38 49 #endif//__RUNTEST_h__
39 50 // vim: set et ts=4 sw=4:
... ...
Please register or login to post a comment