hammer/src/test_suite.h

176 lines
6.4 KiB
C
Raw Normal View History

/* Test suite for Hammer.
* Copyright (C) 2012 Meredith L. Patterson, Dan "TQ" Hirsch
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef HAMMER_TEST_SUITE__H
#define HAMMER_TEST_SUITE__H
#include <stdlib.h>
// Equivalent to g_assert_*, but not using g_assert...
2012-05-13 01:01:26 +01:00
#define g_check_inttype(fmt, typ, n1, op, n2) do { \
typ _n1 = (n1); \
typ _n2 = (n2); \
if (!(_n1 op _n2)) { \
g_test_message("Check failed: (%s): (" fmt " %s " fmt ")", \
#n1 " " #op " " #n2, \
_n1, #op, _n2); \
g_test_fail(); \
} \
2012-05-13 01:01:26 +01:00
} while(0)
2012-05-13 01:01:26 +01:00
#define g_check_bytes(len, n1, op, n2) do { \
const uint8_t *_n1 = (n1); \
const uint8_t *_n2 = (n2); \
if (!(memcmp(_n1, _n2, len) op 0)) { \
g_test_message("Check failed: (%s)", \
#n1 " " #op " " #n2); \
g_test_fail(); \
} \
2012-05-13 01:01:26 +01:00
} while(0)
2012-05-13 01:01:26 +01:00
#define g_check_string(n1, op, n2) do { \
const char *_n1 = (n1); \
const char *_n2 = (n2); \
2012-05-13 01:01:26 +01:00
if (!(strcmp(_n1, _n2) op 0)) { \
g_test_message("Check failed: (%s) (%s %s %s)", \
#n1 " " #op " " #n2, \
_n1, #op, _n2); \
g_test_fail(); \
} \
2012-05-13 01:01:26 +01:00
} while(0)
2013-01-13 17:01:10 +01:00
#define g_check_regular(lang) do { \
if (!lang->isValidRegular(lang->env)) { \
g_test_message("Language is not regular"); \
g_test_fail(); \
} \
} while(0)
#define g_check_contextfree(lang) do { \
if (!lang->isValidCF(lang->env)) { \
g_test_message("Language is not context-free"); \
g_test_fail(); \
} \
} while(0)
#define g_check_compilable(lang, backend, params) do { \
if (!h_compile(lang, backend, params)) { \
g_test_message("Language is not %s(%s)", #backend, params); \
g_test_fail(); \
} \
} while(0)
// TODO: replace uses of this with g_check_parse_failed
2012-05-13 01:01:26 +01:00
#define g_check_failed(res) do { \
2012-05-26 13:01:23 +02:00
const HParseResult *result = (res); \
2012-05-13 01:01:26 +01:00
if (NULL != result) { \
g_test_message("Check failed: shouldn't have succeeded, but did"); \
2012-05-13 01:01:26 +01:00
g_test_fail(); \
} \
} while(0)
2012-05-13 01:01:26 +01:00
#define g_check_parse_failed(parser, input, inp_len) do { \
2012-05-26 14:06:52 +02:00
const HParseResult *result = h_parse(parser, (const uint8_t*)input, inp_len); \
if (NULL != result) { \
g_test_message("Check failed: shouldn't have succeeded, but did"); \
g_test_fail(); \
} \
2012-05-13 01:01:26 +01:00
} while(0)
2012-05-13 01:01:26 +01:00
#define g_check_parse_ok(parser, input, inp_len, result) do { \
2012-05-26 14:06:52 +02:00
HParseResult *res = h_parse(parser, (const uint8_t*)input, inp_len); \
if (!res) { \
g_test_message("Parse failed on line %d", __LINE__); \
g_test_fail(); \
} else { \
2012-05-26 14:06:52 +02:00
char* cres = h_write_result_unamb(res->ast); \
g_check_string(cres, ==, result); \
2013-04-26 20:36:54 -07:00
system_allocator.free(&system_allocator, cres); \
2012-05-26 13:01:23 +02:00
HArenaStats stats; \
2012-05-26 14:06:52 +02:00
h_allocator_stats(res->arena, &stats); \
g_test_message("Parse used %zd bytes, wasted %zd bytes. " \
"Inefficiency: %5f%%", \
stats.used, stats.wasted, \
stats.wasted * 100. / (stats.used+stats.wasted)); \
2013-04-26 20:36:54 -07:00
h_delete_arena(res->arena); \
} \
2012-05-13 01:01:26 +01:00
} while(0)
#define g_check_hashtable_present(table, key) do { \
if(!h_hashtable_present(table, key)) { \
g_test_message("Check failed: key should have been in table, but wasn't"); \
g_test_fail(); \
} \
} while(0)
#define g_check_hashtable_absent(table, key) do { \
if(h_hashtable_present(table, key)) { \
g_test_message("Check failed: key shouldn't have been in table, but was"); \
g_test_fail(); \
} \
} while(0)
#define g_check_hashtable_size(table, n) do { \
size_t expected = n; \
size_t actual = (table)->used; \
if(actual != expected) { \
g_test_message("Check failed: table size should have been %lu, but was %lu", \
expected, actual); \
g_test_fail(); \
} \
} while(0)
#define g_check_terminal(grammar, parser) \
g_check_hashtable_absent(grammar->nts, h_desugar(&system_allocator, parser))
#define g_check_nonterminal(grammar, parser) \
g_check_hashtable_present(grammar->nts, h_desugar(&system_allocator, parser))
#define g_check_derives_epsilon(grammar, parser) \
g_check_hashtable_present(grammar->geneps, h_desugar(&system_allocator, parser))
#define g_check_derives_epsilon_not(grammar, parser) \
g_check_hashtable_absent(grammar->geneps, h_desugar(&system_allocator, parser))
#define g_check_firstset_present(grammar, parser, token) \
g_check_hashtable_present(h_first_symbol(grammar, h_desugar(&system_allocator, parser)), (void *)token)
#define g_check_firstset_absent(grammar, parser, token) \
g_check_hashtable_absent(h_first_symbol(grammar, h_desugar(&system_allocator, parser)), (void *)token)
#define g_check_followset_present(grammar, parser, token) \
g_check_hashtable_present(h_follow(grammar, h_desugar(&system_allocator, parser)), (void *)token)
#define g_check_followset_absent(grammar, parser, token) \
g_check_hashtable_absent(h_follow(grammar, h_desugar(&system_allocator, parser)), (void *)token)
#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)
2012-07-27 15:47:38 -07:00
#define g_check_cmpulonglong(n1, op, n2) g_check_inttype("%llu", unsigned long long, n1, op, n2)
#define g_check_cmpfloat(n1, op, n2) g_check_inttype("%g", float, n1, op, n2)
#define g_check_cmpdouble(n1, op, n2) g_check_inttype("%g", double, n1, op, n2)
2013-01-13 17:01:10 +01:00
#endif // #ifndef HAMMER_TEST_SUITE__H