2012-05-12 22:26:59 +01:00
|
|
|
/* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-04-29 01:45:52 +01:00
|
|
|
#ifndef HAMMER_TEST_SUITE__H
|
|
|
|
|
#define HAMMER_TEST_SUITE__H
|
2012-11-10 23:34:08 -05:00
|
|
|
#include <stdlib.h>
|
2012-04-29 01:45:52 +01:00
|
|
|
|
|
|
|
|
// 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 { \
|
2012-05-11 21:05:49 +01:00
|
|
|
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-04-29 01:45:52 +01:00
|
|
|
|
2012-05-13 01:01:26 +01:00
|
|
|
#define g_check_bytes(len, n1, op, n2) do { \
|
2012-05-11 21:05:49 +01:00
|
|
|
const uint8_t *_n1 = (n1); \
|
2012-05-12 00:40:54 +01:00
|
|
|
const uint8_t *_n2 = (n2); \
|
2012-05-11 21:05:49 +01:00
|
|
|
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-11 21:05:49 +01:00
|
|
|
|
2012-05-13 01:01:26 +01:00
|
|
|
#define g_check_string(n1, op, n2) do { \
|
2012-05-12 00:40:54 +01:00
|
|
|
const char *_n1 = (n1); \
|
|
|
|
|
const char *_n2 = (n2); \
|
2012-05-13 01:01:26 +01:00
|
|
|
if (!(strcmp(_n1, _n2) op 0)) { \
|
2012-05-12 00:40:54 +01:00
|
|
|
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)
|
2012-05-12 00:40:54 +01:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2012-05-12 00:40:54 +01:00
|
|
|
// 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) { \
|
2012-05-11 23:38:45 +01:00
|
|
|
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-11 23:38:45 +01:00
|
|
|
|
2013-05-22 23:50:48 -07:00
|
|
|
#define g_check_parse_failed(parser, backend, input, inp_len) do { \
|
|
|
|
|
int skip = h_compile((HParser *)(parser), (HParserBackend)backend, NULL); \
|
2013-05-22 20:38:36 +02:00
|
|
|
if(skip != 0) { \
|
|
|
|
|
g_test_message("Backend not applicable, skipping test"); \
|
|
|
|
|
break; \
|
|
|
|
|
} \
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParseResult *result = h_parse(parser, (const uint8_t*)input, inp_len); \
|
2012-05-12 00:40:54 +01:00
|
|
|
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-12 00:40:54 +01:00
|
|
|
|
2013-05-22 23:50:48 -07:00
|
|
|
#define g_check_parse_ok(parser, backend, input, inp_len, result) do { \
|
|
|
|
|
int skip = h_compile((HParser *)(parser), (HParserBackend) backend, NULL); \
|
|
|
|
|
if(skip) { \
|
2013-05-22 20:38:36 +02:00
|
|
|
g_test_message("Backend not applicable, skipping test"); \
|
|
|
|
|
break; \
|
|
|
|
|
} \
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *res = h_parse(parser, (const uint8_t*)input, inp_len); \
|
2012-05-12 00:40:54 +01:00
|
|
|
if (!res) { \
|
2012-05-12 23:04:54 +01:00
|
|
|
g_test_message("Parse failed on line %d", __LINE__); \
|
2012-05-12 00:40:54 +01:00
|
|
|
g_test_fail(); \
|
|
|
|
|
} else { \
|
2012-05-26 14:06:52 +02:00
|
|
|
char* cres = h_write_result_unamb(res->ast); \
|
2012-05-12 00:40:54 +01:00
|
|
|
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); \
|
2012-05-12 23:04:54 +01:00
|
|
|
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-12 00:40:54 +01:00
|
|
|
} \
|
2012-05-13 01:01:26 +01:00
|
|
|
} while(0)
|
2012-05-12 00:40:54 +01:00
|
|
|
|
2013-05-08 00:48:46 +02:00
|
|
|
#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)
|
|
|
|
|
|
2013-05-22 20:38:36 +02:00
|
|
|
#define g_check_stringmap_present(table, key) do { \
|
|
|
|
|
bool end = (key[strlen(key)-1] == '$'); \
|
|
|
|
|
if(!h_stringmap_present(table, (uint8_t *)key, strlen(key), end)) { \
|
|
|
|
|
g_test_message("Check failed: \"%s\" should have been in map, but wasn't", key); \
|
|
|
|
|
g_test_fail(); \
|
|
|
|
|
} \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define g_check_stringmap_absent(table, key) do { \
|
|
|
|
|
bool end = (key[strlen(key)-2] == '$'); \
|
|
|
|
|
if(h_stringmap_present(table, (uint8_t *)key, strlen(key), end)) { \
|
|
|
|
|
g_test_message("Check failed: \"%s\" shouldn't have been in map, but was", key); \
|
|
|
|
|
g_test_fail(); \
|
|
|
|
|
} \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
|
2013-05-08 00:48:46 +02:00
|
|
|
#define g_check_terminal(grammar, parser) \
|
2013-05-25 03:35:42 +02:00
|
|
|
g_check_hashtable_absent(grammar->nts, h_desugar(&system_allocator, NULL, parser))
|
2013-05-08 00:48:46 +02:00
|
|
|
|
|
|
|
|
#define g_check_nonterminal(grammar, parser) \
|
2013-05-25 03:35:42 +02:00
|
|
|
g_check_hashtable_present(grammar->nts, h_desugar(&system_allocator, NULL, parser))
|
2013-05-08 00:48:46 +02:00
|
|
|
|
|
|
|
|
#define g_check_derives_epsilon(grammar, parser) \
|
2013-05-25 03:35:42 +02:00
|
|
|
g_check_hashtable_present(grammar->geneps, h_desugar(&system_allocator, NULL, parser))
|
2013-05-08 00:48:46 +02:00
|
|
|
|
|
|
|
|
#define g_check_derives_epsilon_not(grammar, parser) \
|
2013-05-25 03:35:42 +02:00
|
|
|
g_check_hashtable_absent(grammar->geneps, h_desugar(&system_allocator, NULL, parser))
|
2013-05-08 00:48:46 +02:00
|
|
|
|
2013-05-22 20:38:36 +02:00
|
|
|
#define g_check_firstset_present(k, grammar, parser, str) \
|
2013-05-25 03:35:42 +02:00
|
|
|
g_check_stringmap_present(h_first(k, grammar, h_desugar(&system_allocator, NULL, parser)), str)
|
2013-05-08 00:48:46 +02:00
|
|
|
|
2013-05-22 20:38:36 +02:00
|
|
|
#define g_check_firstset_absent(k, grammar, parser, str) \
|
2013-05-25 03:35:42 +02:00
|
|
|
g_check_stringmap_absent(h_first(k, grammar, h_desugar(&system_allocator, NULL, parser)), str)
|
2013-05-08 00:48:46 +02:00
|
|
|
|
2013-05-22 20:38:36 +02:00
|
|
|
#define g_check_followset_present(k, grammar, parser, str) \
|
2013-05-25 03:35:42 +02:00
|
|
|
g_check_stringmap_present(h_follow(k, grammar, h_desugar(&system_allocator, NULL, parser)), str)
|
2013-05-08 00:48:46 +02:00
|
|
|
|
2013-05-22 20:38:36 +02:00
|
|
|
#define g_check_followset_absent(k, grammar, parser, str) \
|
2013-05-25 03:35:42 +02:00
|
|
|
g_check_stringmap_absent(h_follow(k, grammar, h_desugar(&system_allocator, NULL, parser)), str)
|
2013-05-08 00:48:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-12 00:40:54 +01:00
|
|
|
|
2012-05-11 21:05:49 +01:00
|
|
|
#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)
|
2012-05-11 23:38:45 +01:00
|
|
|
#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)
|
2012-05-11 21:05:49 +01:00
|
|
|
|
2012-04-29 01:45:52 +01:00
|
|
|
|
2013-01-13 17:01:10 +01:00
|
|
|
|
2012-04-29 01:45:52 +01:00
|
|
|
#endif // #ifndef HAMMER_TEST_SUITE__H
|