Browse Source

<<improve>> class definition

master
Georg Hopp 14 years ago
parent
commit
ae5751fe25
  1. 14
      ChangeLog
  2. 27
      createToken.c
  3. 31
      include/token/cclass.h
  4. 28
      include/token/crypt.h
  5. 13
      include/token/dyntype.h
  6. 20
      include/token/dyntype/hash.h
  7. 28
      include/token/packet.h
  8. 10
      src/cclass.c
  9. 12
      src/crypt.c
  10. 12
      src/dyntype.c
  11. 34
      src/dyntype/hash.c
  12. 24
      src/packet.c
  13. 2
      tests/cclassTest.c
  14. 2
      tests/cryptTest.c
  15. 12
      tests/mock/class.c
  16. 11
      tests/mock/class.h
  17. 23
      tests/packet.c
  18. 2
      tests/packetTest.c
  19. 2
      tests/runtest.h

14
ChangeLog

@ -1,6 +1,18 @@
2011-11-18 00:01:31 +0100 Georg Hopp
* <<improve>> class definition (HEAD, master)
2011-11-17 14:33:33 +0100 Georg Hopp
* Merge branch 'master' of 192.168.10.10:/var/lib/git/token (origin/master, origin/HEAD)
2011-11-17 14:32:08 +0100 Georg Hopp
* add extra dist files to Makefile.am
2011-11-17 14:32:08 +0100 Georg Hopp 2011-11-17 14:32:08 +0100 Georg Hopp
* add extra dist files to Makefile.am (HEAD, origin/master, origin/HEAD, master)
* add extra dist files to Makefile.am
2011-11-17 13:32:46 +0100 Georg Hopp 2011-11-17 13:32:46 +0100 Georg Hopp

27
createToken.c

@ -10,27 +10,27 @@
void void
setHashString(struct DYNTYPE_HASH * hash, const char * const key, const char * value)
setHashString(DYNTYPE_HASH hash, const char * const key, const char * value)
{ {
struct DYNTYPE * dyn;
DYNTYPE dyn;
dyn = new(DYNTYPE, DYNTYPE_TYPE_STRING, strlen(value), value); dyn = new(DYNTYPE, DYNTYPE_TYPE_STRING, strlen(value), value);
dyntype_hash_set(hash, key, dyn); dyntype_hash_set(hash, key, dyn);
} }
void void
setHashInt(struct DYNTYPE_HASH * hash, const char * const key, const int value)
setHashInt(DYNTYPE_HASH hash, const char * const key, const int value)
{ {
struct DYNTYPE * dyn;
DYNTYPE dyn;
dyn = new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), value); dyn = new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), value);
dyntype_hash_set(hash, key, dyn); dyntype_hash_set(hash, key, dyn);
} }
void void
deleteHashValue(struct DYNTYPE_HASH * hash, const char * const key)
deleteHashValue(DYNTYPE_HASH hash, const char * const key)
{ {
struct DYNTYPE * dyn = dyntype_hash_get(hash, key);
DYNTYPE dyn = dyntype_hash_get(hash, key);
delete(&dyn); delete(&dyn);
} }
@ -38,10 +38,11 @@ deleteHashValue(struct DYNTYPE_HASH * hash, const char * const key)
int int
main(int argc, char * argv[]) main(int argc, char * argv[])
{ {
struct CRYPT * crypt;
struct PACKET * packet;
struct DYNTYPE_HASH * data;
struct DYNTYPE * _clear;
CRYPT crypt;
PACKET packet;
DYNTYPE_HASH data;
DYNTYPE _clear;
struct json_object * json; struct json_object * json;
const char * json_str; const char * json_str;
char * encrypted; char * encrypted;
@ -49,13 +50,13 @@ main(int argc, char * argv[])
char pass[] = "1234"; char pass[] = "1234";
size_t length; size_t length;
packet = new(PACKET);
packet = new(PACKET, NULL);
packet_setHeader( packet_setHeader(
packet, packet,
new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), time(NULL))); new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), time(NULL)));
data = new(DYNTYPE_HASH);
data = new(DYNTYPE_HASH, NULL);
setHashString(data, "#C#", "this comes from C"); setHashString(data, "#C#", "this comes from C");
setHashString(data, "usr", "ppohg"); setHashString(data, "usr", "ppohg");
@ -66,7 +67,7 @@ main(int argc, char * argv[])
packet, packet,
new(DYNTYPE, new(DYNTYPE,
DYNTYPE_TYPE_HASH, DYNTYPE_TYPE_HASH,
sizeof(struct DYNTYPE_HASH *),
sizeof(DYNTYPE_HASH),
data)); data));
json = toJson(packet); json = toJson(packet);

31
include/token/cclass.h

@ -22,17 +22,26 @@
#include <sys/types.h> #include <sys/types.h>
#include <json/json.h> #include <json/json.h>
#define SWAP(a, b) a ^= b; b ^= a; a ^= b;
#define CCLASS_MAGIC 0xFEFE #define CCLASS_MAGIC 0xFEFE
#define INIT_CCLASS(name, jsonConst, toJson) \
static const struct CCLASS _##name = { \
#define CLASS(_class) \
typedef struct _##_class { \
const _CCLASS const class;
#define ENDC(_class) } * _class; \
extern const _CCLASS const __##_class;
#define INIT_CCLASS(class, jsonConst, toJson) \
static const struct CCLASS _class = { \
CCLASS_MAGIC, \ CCLASS_MAGIC, \
sizeof(struct name), \
sizeof(struct _##class), \
(ctor)__construct, \ (ctor)__construct, \
(jCtor)jsonConst, \ (jCtor)jsonConst, \
(dtor)__destruct, \ (dtor)__destruct, \
(jTo)toJson \ (jTo)toJson \
}; const struct CCLASS * const name = &_##name
}; const _CCLASS const __##class = &_class
@ -42,23 +51,27 @@ typedef void (* jCtor)(void *, struct json_object *);
typedef struct json_object * (* jTo)(void *); typedef struct json_object * (* jTo)(void *);
struct CCLASS {
typedef struct CCLASS {
const int magic; const int magic;
size_t size; size_t size;
void (* __construct)(void * _this, va_list * params); void (* __construct)(void * _this, va_list * params);
void (* __jsonConst)(void * _this, struct json_object * json); void (* __jsonConst)(void * _this, struct json_object * json);
void (* __destruct)(void * _this); void (* __destruct)(void * _this);
struct json_object * (* __toJson)(void * _this); struct json_object * (* __toJson)(void * _this);
};
} * _CCLASS;
#define CCLASS_PTR_SIZE sizeof(struct CCLASS *) #define CCLASS_PTR_SIZE sizeof(struct CCLASS *)
#define CCLASS_SIZE sizeof(struct CCLASS) #define CCLASS_SIZE sizeof(struct CCLASS)
void * new(const void * _class, ...);
void * newFromJson(const void * _class, struct json_object * json);
void * _new(const void * _class, ...);
void * _newFromJson(const void * _class, struct json_object * json);
void delete(void * _object); void delete(void * _object);
struct json_object * toJson(void * _object); struct json_object * toJson(void * _object);
int isObject(void * _object); int isObject(void * _object);
int instanceOf(const void * _class, void * _object);
int _instanceOf(const void * _class, void * _object);
#define new(class, ...) _new((__##class), __VA_ARGS__)
#define newFromJson(class, json) _newFromJson((__##class), (json))
#define instanceOf(class, object) _instanceOf((__##class), (object))
#endif//__CCLASS_H__ #endif//__CCLASS_H__

28
include/token/crypt.h

@ -23,44 +23,32 @@
#include "token/cclass.h" #include "token/cclass.h"
struct CRYPT {
const struct CCLASS * const class;
CLASS(CRYPT)
const char * algorithm; const char * algorithm;
const char * mode; const char * mode;
MCRYPT mcrypt; MCRYPT mcrypt;
size_t ivsize; size_t ivsize;
size_t keysize; size_t keysize;
void * iv; void * iv;
};
ENDC(CRYPT)
extern const struct CCLASS * const CRYPT;
void * crypt_encrypt( void * crypt_encrypt(
struct CRYPT * _this,
CRYPT _this,
const void * const data, const void * const data,
const char * const password, const char * const password,
size_t * length); size_t * length);
void * crypt_decrypt( void * crypt_decrypt(
struct CRYPT * _this,
CRYPT _this,
const void * const data, const void * const data,
const char * const password, const char * const password,
size_t * length); size_t * length);
void * crypt_createIv(
struct CRYPT * _this);
void crypt_setIv(
struct CRYPT * _this,
const void * const iv);
void crypt_setIvsize(
struct CRYPT * _this,
const size_t size);
void crypt_setKeysize(
struct CRYPT * _this,
const size_t size);
void * crypt_createIv(CRYPT _this);
void crypt_setIv(CRYPT _this, const void * const iv);
void crypt_setIvsize(CRYPT _this, const size_t size);
void crypt_setKeysize(CRYPT _this, const size_t size);
#endif//__CRYPT_H__ #endif//__CRYPT_H__
// vim: set et ts=4 sw=4: // vim: set et ts=4 sw=4:

13
include/token/dyntype.h

@ -22,7 +22,7 @@
#include "token/cclass.h" #include "token/cclass.h"
struct DYNTYPE;
struct _DYNTYPE;
#include "token/dyntype/hash.h" #include "token/dyntype/hash.h"
@ -38,8 +38,7 @@ enum DYNTYPE_TYPES {
}; };
struct DYNTYPE {
const struct CCLASS * const class;
CLASS(DYNTYPE)
enum DYNTYPE_TYPES type; enum DYNTYPE_TYPES type;
size_t size; size_t size;
union _data { union _data {
@ -47,12 +46,10 @@ struct DYNTYPE {
int _int; int _int;
double _float; double _float;
char * _string; char * _string;
struct DYNTYPE ** _array;
struct DYNTYPE_HASH * _hash;
struct _DYNTYPE ** _array;
DYNTYPE_HASH _hash;
} data; } data;
};
extern const struct CCLASS * const DYNTYPE;
ENDC(DYNTYPE)
#endif//__DYNTYPE_H__ #endif//__DYNTYPE_H__

20
include/token/dyntype/hash.h

@ -24,25 +24,17 @@
#include "token/dyntype.h" #include "token/dyntype.h"
struct DYNTYPE_HASH {
const struct CCLASS * const class;
CLASS(DYNTYPE_HASH)
char ** keys; char ** keys;
struct DYNTYPE ** values;
struct _DYNTYPE ** values;
size_t size; size_t size;
size_t used; size_t used;
};
ENDC(DYNTYPE_HASH)
extern const struct CCLASS * const DYNTYPE_HASH;
void dyntype_hash_set(struct DYNTYPE_HASH * _this,
const char * key,
struct DYNTYPE * value);
struct DYNTYPE *
dyntype_hash_get(struct DYNTYPE_HASH * _this, const char * key);
struct DYNTYPE *
dyntype_hash_del(struct DYNTYPE_HASH * _this, const char * key);
void dyntype_hash_set(DYNTYPE_HASH _this, const char * key, struct _DYNTYPE * value);
struct _DYNTYPE * dyntype_hash_get(DYNTYPE_HASH _this, const char * key);
struct _DYNTYPE * dyntype_hash_del(DYNTYPE_HASH _this, const char * key);
#endif//__DYNTYPE_HASH_H__ #endif//__DYNTYPE_HASH_H__

28
include/token/packet.h

@ -27,29 +27,17 @@ enum PACKET_CONTENT_KEYS {
PACKET_DATA PACKET_DATA
}; };
struct PACKET {
const struct CCLASS * const class;
struct DYNTYPE * content[2];
};
extern const struct CCLASS * const PACKET;
struct DYNTYPE * packet_getHeader(
struct PACKET * _this);
struct DYNTYPE * packet_getData(
struct PACKET * _this);
void packet_setHeader(
struct PACKET * _this,
struct DYNTYPE * header);
CLASS(PACKET)
DYNTYPE content[2];
ENDC(PACKET)
void packet_setData(
struct PACKET * _this,
struct DYNTYPE * data);
void packet_set_default_content(
struct PACKET * _this);
DYNTYPE packet_getHeader(PACKET _this);
DYNTYPE packet_getData(PACKET _this);
void packet_setHeader(PACKET _this, DYNTYPE header);
void packet_setData(PACKET _this, DYNTYPE data);
void packet_set_default_content(PACKET _this);
#endif//__PACKET_H__ #endif//__PACKET_H__

10
src/cclass.c

@ -24,10 +24,10 @@
void * void *
new(const void * _class, ...)
_new(const void * _class, ...)
{ {
const struct CCLASS * class = _class;
void * object = calloc(1, class->size);
const _CCLASS class = _class;
void * object = calloc(1, class->size);
* (const struct CCLASS **) object = class; * (const struct CCLASS **) object = class;
@ -43,7 +43,7 @@ new(const void * _class, ...)
} }
void * void *
newFromJson(const void * _class, struct json_object * json)
_newFromJson(const void * _class, struct json_object * json)
{ {
const struct CCLASS * class = _class; const struct CCLASS * class = _class;
void * object = calloc(1, class->size); void * object = calloc(1, class->size);
@ -91,7 +91,7 @@ isObject(void * _object)
} }
int int
instanceOf(const void * _class, void * _object)
_instanceOf(const void * _class, void * _object)
{ {
const struct CCLASS ** class = _object; const struct CCLASS ** class = _object;

12
src/crypt.c

@ -30,7 +30,7 @@
static static
void void
__construct(struct CRYPT * _this, va_list * params)
__construct(CRYPT _this, va_list * params)
{ {
_this->algorithm = va_arg(* params, const char * const); _this->algorithm = va_arg(* params, const char * const);
_this->mode = va_arg(* params, const char * const); _this->mode = va_arg(* params, const char * const);
@ -47,7 +47,7 @@ __construct(struct CRYPT * _this, va_list * params)
static static
void void
__destruct(struct CRYPT * _this)
__destruct(CRYPT _this)
{ {
if (_this->iv) { if (_this->iv) {
free(_this->iv); free(_this->iv);
@ -59,7 +59,7 @@ __destruct(struct CRYPT * _this)
INIT_CCLASS(CRYPT, NULL, NULL); INIT_CCLASS(CRYPT, NULL, NULL);
void * void *
crypt_createIv(struct CRYPT * _this)
crypt_createIv(CRYPT _this)
{ {
int urandom; int urandom;
size_t rsize = 0; size_t rsize = 0;
@ -80,7 +80,7 @@ crypt_createIv(struct CRYPT * _this)
static static
void * void *
createKey(struct CRYPT * _this, const char * const password)
createKey(CRYPT _this, const char * const password)
{ {
void * key = NULL; void * key = NULL;
@ -102,7 +102,7 @@ createKey(struct CRYPT * _this, const char * const password)
void * void *
crypt_encrypt( crypt_encrypt(
struct CRYPT * _this,
CRYPT _this,
const void * const data, const void * const data,
const char * const password, const char * const password,
size_t * length) size_t * length)
@ -138,7 +138,7 @@ crypt_encrypt(
void * void *
crypt_decrypt( crypt_decrypt(
struct CRYPT * _this,
CRYPT _this,
const void * const data, const void * const data,
const char * const password, const char * const password,
size_t * length) size_t * length)

12
src/dyntype.c

@ -26,7 +26,7 @@
static static
void void
__construct(struct DYNTYPE * _this, va_list * params)
__construct(DYNTYPE _this, va_list * params)
{ {
_this->type = va_arg(* params, enum DYNTYPE_TYPES); _this->type = va_arg(* params, enum DYNTYPE_TYPES);
_this->size = va_arg(* params, size_t); _this->size = va_arg(* params, size_t);
@ -42,7 +42,7 @@ __construct(struct DYNTYPE * _this, va_list * params)
break; break;
case DYNTYPE_TYPE_HASH: case DYNTYPE_TYPE_HASH:
(_this->data)._hash = va_arg(* params, struct DYNTYPE_HASH *);
(_this->data)._hash = va_arg(* params, DYNTYPE_HASH);
break; break;
default: default:
@ -52,7 +52,7 @@ __construct(struct DYNTYPE * _this, va_list * params)
static static
void void
__jsonConst(struct DYNTYPE * _this, struct json_object * json)
__jsonConst(DYNTYPE _this, struct json_object * json)
{ {
switch (json_object_get_type(json)) { switch (json_object_get_type(json)) {
case json_type_int: case json_type_int:
@ -75,7 +75,7 @@ __jsonConst(struct DYNTYPE * _this, struct json_object * json)
case json_type_object: case json_type_object:
_this->type = DYNTYPE_TYPE_HASH; _this->type = DYNTYPE_TYPE_HASH;
_this->size = sizeof(struct DYNTYPE_HASH *);
_this->size = sizeof(DYNTYPE_HASH);
(_this->data)._hash = newFromJson(DYNTYPE_HASH, json); (_this->data)._hash = newFromJson(DYNTYPE_HASH, json);
break; break;
@ -88,7 +88,7 @@ __jsonConst(struct DYNTYPE * _this, struct json_object * json)
static static
void void
__destruct(struct DYNTYPE * _this)
__destruct(DYNTYPE _this)
{ {
if (_this) { if (_this) {
switch(_this->type) { switch(_this->type) {
@ -108,7 +108,7 @@ __destruct(struct DYNTYPE * _this)
static static
struct json_object * struct json_object *
__toJson(struct DYNTYPE * _this)
__toJson(DYNTYPE _this)
{ {
struct json_object * json = NULL; struct json_object * json = NULL;

34
src/dyntype/hash.c

@ -27,11 +27,11 @@
#define HASH_ENTRY_CHUNK_SIZE 128 #define HASH_ENTRY_CHUNK_SIZE 128
static void _updateHashSize(struct DYNTYPE_HASH * _this);
static void _updateHashSize(DYNTYPE_HASH _this);
static static
void void
__construct(struct DYNTYPE_HASH * _this, va_list * params)
__construct(DYNTYPE_HASH _this, va_list * params)
{ {
_this->size = 0; _this->size = 0;
_this->used = 0; _this->used = 0;
@ -40,7 +40,7 @@ __construct(struct DYNTYPE_HASH * _this, va_list * params)
static static
void void
__jsonConst(struct DYNTYPE_HASH * _this, struct json_object * json)
__jsonConst(DYNTYPE_HASH _this, struct json_object * json)
{ {
__construct(_this, NULL); __construct(_this, NULL);
@ -55,7 +55,7 @@ __jsonConst(struct DYNTYPE_HASH * _this, struct json_object * json)
static static
void void
__destruct(struct DYNTYPE_HASH * _this)
__destruct(DYNTYPE_HASH _this)
{ {
size_t index; size_t index;
@ -69,7 +69,7 @@ __destruct(struct DYNTYPE_HASH * _this)
static static
struct json_object * struct json_object *
__toJson(struct DYNTYPE_HASH * _this)
__toJson(DYNTYPE_HASH _this)
{ {
size_t index; size_t index;
struct json_object * json = json_object_new_object(); struct json_object * json = json_object_new_object();
@ -88,7 +88,7 @@ INIT_CCLASS(DYNTYPE_HASH, __jsonConst, __toJson);
static static
void void
_updateHashSize(struct DYNTYPE_HASH * _this)
_updateHashSize(DYNTYPE_HASH _this)
{ {
if (_this->used == _this->size) { if (_this->used == _this->size) {
_this->size += HASH_ENTRY_CHUNK_SIZE; _this->size += HASH_ENTRY_CHUNK_SIZE;
@ -100,16 +100,16 @@ _updateHashSize(struct DYNTYPE_HASH * _this)
_this->values = realloc( _this->values = realloc(
_this->values, _this->values,
sizeof(struct DYNTYPE *) * _this->size);
memset(_this->values + (_this->used * sizeof(struct DYNTYPE *)),
sizeof(DYNTYPE) * _this->size);
memset(_this->values + (_this->used * sizeof(DYNTYPE)),
0, 0,
HASH_ENTRY_CHUNK_SIZE * sizeof(struct DYNTYPE *));
HASH_ENTRY_CHUNK_SIZE * sizeof(DYNTYPE));
} }
} }
static static
size_t size_t
_getHashIdx(struct DYNTYPE_HASH * _this, const char * key)
_getHashIdx(DYNTYPE_HASH _this, const char * key)
{ {
size_t index; size_t index;
@ -121,9 +121,7 @@ _getHashIdx(struct DYNTYPE_HASH * _this, const char * key)
} }
void void
dyntype_hash_set(struct DYNTYPE_HASH * _this,
const char * key,
struct DYNTYPE * value)
dyntype_hash_set(DYNTYPE_HASH _this, const char * key, DYNTYPE value)
{ {
size_t index = _getHashIdx(_this, key); size_t index = _getHashIdx(_this, key);
@ -139,8 +137,7 @@ dyntype_hash_set(struct DYNTYPE_HASH * _this,
_updateHashSize(_this); _updateHashSize(_this);
} }
struct DYNTYPE *
dyntype_hash_get(struct DYNTYPE_HASH * _this, const char * key)
DYNTYPE dyntype_hash_get(DYNTYPE_HASH _this, const char * key)
{ {
size_t index = _getHashIdx(_this, key); size_t index = _getHashIdx(_this, key);
@ -151,10 +148,9 @@ dyntype_hash_get(struct DYNTYPE_HASH * _this, const char * key)
return _this->values[index]; return _this->values[index];
} }
struct DYNTYPE *
dyntype_hash_del(struct DYNTYPE_HASH * _this, const char * key)
DYNTYPE dyntype_hash_del(DYNTYPE_HASH _this, const char * key)
{ {
struct DYNTYPE * found = NULL;
DYNTYPE found = NULL;
size_t index = _getHashIdx(_this, key); size_t index = _getHashIdx(_this, key);
if (index == _this->used) { if (index == _this->used) {
@ -169,7 +165,7 @@ dyntype_hash_del(struct DYNTYPE_HASH * _this, const char * key)
memmove(_this->values + index + 1, memmove(_this->values + index + 1,
_this->values + index, _this->values + index,
(_this->size - index) * sizeof(struct DYNTYPE *));
(_this->size - index) * sizeof(DYNTYPE));
_this->used -= 1; _this->used -= 1;
} }

24
src/packet.c

@ -22,14 +22,14 @@
static static
void void
__construct(struct PACKET * _this, va_list * params)
__construct(PACKET _this, va_list * params)
{ {
packet_set_default_content(_this); packet_set_default_content(_this);
} }
static static
void void
__jsonConst(struct PACKET * _this, struct json_object * json)
__jsonConst(PACKET _this, struct json_object * json)
{ {
struct json_object * header = NULL; struct json_object * header = NULL;
struct json_object * data = NULL; struct json_object * data = NULL;
@ -53,13 +53,13 @@ __jsonConst(struct PACKET * _this, struct json_object * json)
static static
void void
__destruct(struct PACKET * _this)
__destruct(PACKET _this)
{ {
} }
static static
struct json_object * struct json_object *
__toJson(struct PACKET * _this)
__toJson(PACKET _this)
{ {
struct json_object * json = json_object_new_array(); struct json_object * json = json_object_new_array();
@ -71,36 +71,30 @@ __toJson(struct PACKET * _this)
INIT_CCLASS(PACKET, __jsonConst, __toJson); INIT_CCLASS(PACKET, __jsonConst, __toJson);
struct DYNTYPE *
packet_getHeader(struct PACKET * _this)
DYNTYPE packet_getHeader(PACKET _this)
{ {
return _this->content[PACKET_HEADER]; return _this->content[PACKET_HEADER];
} }
struct DYNTYPE *
packet_getData(struct PACKET * _this)
DYNTYPE packet_getData(PACKET _this)
{ {
return _this->content[PACKET_DATA]; return _this->content[PACKET_DATA];
} }
void void
packet_setHeader(
struct PACKET * _this,
struct DYNTYPE * header)
packet_setHeader(PACKET _this, DYNTYPE header)
{ {
_this->content[PACKET_HEADER] = header; _this->content[PACKET_HEADER] = header;
} }
void void
packet_setData(
struct PACKET * _this,
struct DYNTYPE * data)
packet_setData(PACKET _this, DYNTYPE data)
{ {
_this->content[PACKET_DATA] = data; _this->content[PACKET_DATA] = data;
} }
void void
packet_set_default_content(struct PACKET * _this)
packet_set_default_content(PACKET _this)
{ {
_this->content[PACKET_HEADER] = NULL; _this->content[PACKET_HEADER] = NULL;
_this->content[PACKET_DATA] = NULL; _this->content[PACKET_DATA] = NULL;

2
tests/cclassTest.c

@ -7,7 +7,7 @@
const char testname[] = "cclassTest"; const char testname[] = "cclassTest";
struct MOCK_CLASS * mock = NULL;
MOCK_CLASS mock = NULL;
static static
int int

2
tests/cryptTest.c

@ -12,7 +12,7 @@
const char testname[] = "cryptTest"; const char testname[] = "cryptTest";
struct CRYPT * crypt = NULL;
CRYPT crypt = NULL;
static static

12
tests/mock/class.c

@ -15,7 +15,7 @@ _reset()
static static
void void
__construct(struct MOCK_CLASS * _this, va_list * params)
__construct(MOCK_CLASS _this, va_list * params)
{ {
_called = 1; _called = 1;
_this->value = va_arg(* params, int); _this->value = va_arg(* params, int);
@ -23,7 +23,7 @@ __construct(struct MOCK_CLASS * _this, va_list * params)
static static
void void
__jsonConst(struct MOCK_CLASS * _this, json_object * json)
__jsonConst(MOCK_CLASS _this, json_object * json)
{ {
_called = 1; _called = 1;
assert(json_type_int == json_object_get_type(json)); assert(json_type_int == json_object_get_type(json));
@ -33,14 +33,14 @@ __jsonConst(struct MOCK_CLASS * _this, json_object * json)
static static
void void
__destruct(struct MOCK_CLASS * _this)
__destruct(MOCK_CLASS _this)
{ {
_called = 1; _called = 1;
} }
static static
struct json_object * struct json_object *
__toJson(struct MOCK_CLASS * _this)
__toJson(MOCK_CLASS _this)
{ {
struct json_object * json = json_object_new_int(_this->value); struct json_object * json = json_object_new_int(_this->value);
@ -55,13 +55,13 @@ INIT_CCLASS(MOCK_CLASS, __jsonConst, __toJson);
*/ */
int int
mock_class_getValue(struct MOCK_CLASS * _this)
mock_class_getValue(MOCK_CLASS _this)
{ {
return _this->value; return _this->value;
} }
void void
mock_class_setValue(struct MOCK_CLASS * _this, int value)
mock_class_setValue(MOCK_CLASS _this, int value)
{ {
_this->value = value; _this->value = value;
} }

11
tests/mock/class.h

@ -8,19 +8,18 @@ extern char _called;
extern void inline _reset(); extern void inline _reset();
struct MOCK_CLASS {
const struct CCLASS * const class;
CLASS(MOCK_CLASS)
int value; int value;
};
ENDC(MOCK_CLASS)
extern const struct CCLASS * const MOCK_CLASS;
extern const _CCLASS const __MOCK_CLASS;
/** /**
* ~~~ method declarations ~~~~~~~~ * ~~~ method declarations ~~~~~~~~
*/ */
int mock_class_getValue(struct MOCK_CLASS * _this);
void mock_class_setValue(struct MOCK_CLASS * _this, int value);
int mock_class_getValue(MOCK_CLASS _this);
void mock_class_setValue(MOCK_CLASS _this, int value);
#endif//__MOCK_CLASS_H__ #endif//__MOCK_CLASS_H__
// vim: set et ts=4 sw=4: // vim: set et ts=4 sw=4:

23
tests/packet.c

@ -11,27 +11,27 @@
void void
setHashString(struct DYNTYPE_HASH * hash, const char * const key, const char * value)
setHashString(DYNTYPE_HASH hash, const char * const key, const char * value)
{ {
struct DYNTYPE * dyn;
DYNTYPE dyn;
dyn = new(DYNTYPE, DYNTYPE_TYPE_STRING, strlen(value), value); dyn = new(DYNTYPE, DYNTYPE_TYPE_STRING, strlen(value), value);
dyntype_hash_set(hash, key, dyn); dyntype_hash_set(hash, key, dyn);
} }
void void
setHashInt(struct DYNTYPE_HASH * hash, const char * const key, const int value)
setHashInt(DYNTYPE_HASH hash, const char * const key, const int value)
{ {
struct DYNTYPE * dyn;
DYNTYPE dyn;
dyn = new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), value); dyn = new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), value);
dyntype_hash_set(hash, key, dyn); dyntype_hash_set(hash, key, dyn);
} }
void void
deleteHashValue(struct DYNTYPE_HASH * hash, const char * const key)
deleteHashValue(DYNTYPE_HASH hash, const char * const key)
{ {
struct DYNTYPE * dyn = dyntype_hash_get(hash, key);
DYNTYPE dyn = dyntype_hash_get(hash, key);
delete(&dyn); delete(&dyn);
} }
@ -39,19 +39,20 @@ deleteHashValue(struct DYNTYPE_HASH * hash, const char * const key)
int int
main(int argc, char * argv[]) main(int argc, char * argv[])
{ {
struct PACKET * packet;
struct DYNTYPE_HASH * data;
struct DYNTYPE * _clear;
PACKET packet;
DYNTYPE_HASH data;
DYNTYPE _clear;
struct json_object * json; struct json_object * json;
char * json_str; char * json_str;
packet = new(PACKET);
packet = new(PACKET, NULL);
packet_setHeader( packet_setHeader(
packet, packet,
new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), time(NULL))); new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), time(NULL)));
data = new(DYNTYPE_HASH);
data = new(DYNTYPE_HASH, NULL);
setHashString(data, "#C#", "this comes from C"); setHashString(data, "#C#", "this comes from C");
setHashString(data, "usr", "ppohg"); setHashString(data, "usr", "ppohg");

2
tests/packetTest.c

@ -12,7 +12,7 @@ static
int int
__setUp() __setUp()
{ {
packet = new(PACKET);
packet = new(PACKET, NULL);
ASSERT_INSTANCE_OF(PACKET, packet); ASSERT_INSTANCE_OF(PACKET, packet);
return TEST_OK; return TEST_OK;

2
tests/runtest.h

@ -30,7 +30,7 @@ enum RESULT_TYPES {
#define ASSERT_OBJECT_NULL(val) if (! isObjectNull((val))) return TEST_FAILED #define ASSERT_OBJECT_NULL(val) if (! isObjectNull((val))) return TEST_FAILED
#define ASSERT_OBJECT_NOT_NULL(val) if (isObjectNull((val))) return TEST_FAILED #define ASSERT_OBJECT_NOT_NULL(val) if (isObjectNull((val))) return TEST_FAILED
#define ASSERT_INSTANCE_OF(class, val) \ #define ASSERT_INSTANCE_OF(class, val) \
if (! instanceOf((class), (val))) return TEST_FAILED
if (! instanceOf(class, val)) return TEST_FAILED
typedef int (* const testfunc)(void); typedef int (* const testfunc)(void);

Loading…
Cancel
Save