Test harness macros extended. Need to implement bodies of primitive parsers.

This commit is contained in:
Meredith L. Patterson 2012-05-11 21:05:49 +01:00
parent 990a19e18d
commit 6e087ac758
2 changed files with 34 additions and 17 deletions

View file

@ -441,10 +441,10 @@ parse_result_t* parse(const parser_t* parser, const uint8_t* input, size_t lengt
#include "test_suite.h"
static void test_token(void) {
//uint8_t test[3] = { '9', '5', 0xa2 };
//const parser_t *token_ = token(test , 3);
//parse_result_t *ret = parse(token_, test, 3);
// need a g_check_cmpstr function for this :P
uint8_t test[3] = { '9', '5', 0xa2 };
const parser_t *token_ = token(test , 3);
parse_result_t *ret = parse(token_, test, 3);
g_check_bytes((ret->ast)[0].bytes.len, (ret->ast)[0].bytes.token, ==, test);
}
static void test_ch(void) {
@ -465,28 +465,28 @@ static void test_int64(void) {
uint8_t test[8] = { 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00 };
const parser_t *int64_ = int64();
parse_result_t *ret = parse(int64_, test, 8);
g_check_cmpint((long long int)(ret->ast)[0].sint, ==, -8589934592L);
g_check_cmplong((ret->ast)[0].sint, ==, -8589934592L);
}
static void test_int32(void) {
uint8_t test[4] = { 0xff, 0xfe, 0x00, 0x00 };
const parser_t *int32_ = int32();
parse_result_t *ret = parse(int32_, test, 4);
g_check_cmpint((long long int)(ret->ast)[0].sint, ==, -131072);
g_check_cmpint((ret->ast)[0].sint, ==, -131072);
}
static void test_int16(void) {
uint8_t test[2] = { 0xfe, 0x00 };
const parser_t *int16_ = int16();
parse_result_t *ret = parse(int16_, test, 2);
g_check_cmpint((long long int)(ret->ast)[0].sint, ==, -512);
g_check_cmpint((ret->ast)[0].sint, ==, -512);
}
static void test_int8(void) {
uint8_t test[1] = { 0x88 };
const parser_t *int8_ = int8();
parse_result_t *ret = parse(int8_, test, 1);
g_check_cmpint((long long int)(ret->ast)[0].sint, ==, -120);
g_check_cmpint((ret->ast)[0].sint, ==, -120);
}
static void test_uint64(void) {

View file

@ -2,16 +2,33 @@
#define HAMMER_TEST_SUITE__H
// Equivalent to g_assert_*, but not using g_assert...
#define g_check_cmpint(n1, op, n2) { \
typeof (n1) _n1 = (n1); \
typeof (n2) _n2 = (n2); \
#define g_check_inttype(fmt, typ, n1, op, n2) { \
typ _n1 = (n1); \
typ _n2 = (n2); \
if (!(_n1 op _n2)) { \
g_test_message("Check failed: (%s): (%lld %s %d)", \
g_test_message("Check failed: (%s): (" fmt " %s " fmt ")", \
#n1 " " #op " " #n2, \
_n1, #op, _n2); \
g_test_fail(); \
} \
}
#define g_check_bytes(len, n1, op, n2) { \
const uint8_t *_n1 = (n1); \
uint8_t *_n2 = (n2); \
if (!(memcmp(_n1, _n2, len) op 0)) { \
g_test_message("Check failed: (%s)", \
#n1 " " #op " " #n2); \
g_test_fail(); \
} \
}
#define g_check_cmpint(n1, op, n2) g_check_inttype("%d", int, n1, op, n2)
#define g_check_cmplong(n1, op, n2) g_check_inttype("%ld", long, n1, op, n2)
#define g_check_cmplonglong(n1, op, n2) g_check_inttype("%lld", long long, n1, op, n2)
#define g_check_cmpuint(n1, op, n2) g_check_inttype("%u", unsigned int, n1, op, n2)
#define g_check_cmpulong(n1, op, n2) g_check_inttype("%lu", unsigned long, n1, op, n2)
#define g_check_cmpulonglong(n1, op, n2) g_check_inttype("%llu", unsigned long long, n1, op, n2)
#endif // #ifndef HAMMER_TEST_SUITE__H