From 2c5911c0aae5617fa5474214012a4b55013eee9e Mon Sep 17 00:00:00 2001 From: "Meredith L. Patterson" Date: Sat, 12 May 2012 00:24:56 +0100 Subject: [PATCH] wrote some more tests, waiting for tq to finish revising harness --- src/hammer.c | 54 +++++++++++++++++++++++++++++++++++++++------------- src/hammer.h | 2 +- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/hammer.c b/src/hammer.c index ec010ac..154af8f 100644 --- a/src/hammer.c +++ b/src/hammer.c @@ -319,8 +319,8 @@ static parse_result_t* parse_butnot(void *env, parse_state_t *state) { } size_t r1len = token_length(r1); size_t r2len = token_length(r2); - // if both match but p1's text is longer than p2's, fail - if (r1len > r2len) { + // if both match but p1's text is as long as or longer than p2's, fail + if (r1len >= r2len) { return NULL; } else { return r1; @@ -656,23 +656,56 @@ static void test_sequence(void) { } static void test_choice(void) { - + const parser_t *choice_ = choice(ch('a'), ch('b'), NULL); + parse_result_t *ret1 = parse(choice_, (const uint8_t*)"a", 1); + parse_result_t *ret2 = parse(choice_, (const uint8_t*)"b", 1); + parse_result_t *ret3 = parse(choice_, (const uint8_t*)"c", 1); + g_check_cmpint(ret1->ast->uint, ==, 'a'); + g_check_cmpint(ret2->ast->uint, ==, 'b'); + g_check_failed(ret3); } static void test_butnot(void) { - + const parser_t *butnot_1 = butnot(ch('a'), token((const uint8_t*)"ab", 2)); + const parser_t *butnot_2 = butnot(range('0', '9'), ch('6')); + parse_result_t *ret1 = parse(butnot_1, (const uint8_t*)"a", 1); + parse_result_t *ret2 = parse(butnot_1, (const uint8_t*)"ab", 2); + parse_result_t *ret3 = parse(butnot_1, (const uint8_t*)"aa", 2); + parse_result_t *ret4 = parse(butnot_2, (const uint8_t*)"6", 1); + g_check_cmpint(ret1->ast->uint, ==, 'a'); + g_check_failed(ret2); + g_check_cmpint(ret3->ast->uint, ==, 'a'); + g_check_failed(ret4); } static void test_difference(void) { - + const parser_t *difference_ = difference(token((const uint8_t*)"ab", 2), ch('a')); + parse_result_t *ret1 = parse(difference_, (const uint8_t*)"ab", 2); + parse_result_t *ret2 = parse(difference_, (const uint8_t*)"a", 1); + g_check_cmpint(ret1->ast->uint, ==, 'a'); + g_check_failed(ret2); } static void test_xor(void) { - + const parser_t *xor_ = xor(range('0', '6'), range('5', '9')); + parse_result_t *ret1 = parse(xor_, (const uint8_t*)"0", 1); + parse_result_t *ret2 = parse(xor_, (const uint8_t*)"9", 1); + parse_result_t *ret3 = parse(xor_, (const uint8_t*)"5", 1); + parse_result_t *ret4 = parse(xor_, (const uint8_t*)"a", 1); + g_check_cmpint(ret1->ast->uint, ==, '0'); + g_check_cmpint(ret2->ast->uint, ==, '9'); + g_check_failed(ret3); + g_check_failed(ret4); } static void test_repeat0(void) { - + const parser_t *repeat0_ = repeat0(choice(ch('a'), ch('b'), NULL)); + parse_result_t *ret1 = parse(repeat0_, (const uint8_t*)"adef", 4); + parse_result_t *ret2 = parse(repeat0_, (const uint8_t*)"bdef", 4); + parse_result_t *ret3 = parse(repeat0_, (const uint8_t*)"aabbabadef", 10); + parse_result_t *ret4 = parse(repeat0_, (const uint8_t*)"daabbabadef", 11); + g_check_cmpint(ret1->ast->uint, ==, 'a'); + g_check_cmpint(ret2->ast->uint, ==, 'b'); } static void test_repeat1(void) { @@ -687,7 +720,7 @@ static void test_optional(void) { } -static void test_expect(void) { +static void test_ignore(void) { } @@ -719,10 +752,6 @@ static void test_not(void) { } -static void test_ignore(void) { - -} - void register_parser_tests(void) { g_test_add_func("/core/parser/token", test_token); g_test_add_func("/core/parser/ch", test_ch); @@ -752,7 +781,6 @@ void register_parser_tests(void) { g_test_add_func("/core/parser/repeat1", test_repeat1); g_test_add_func("/core/parser/repeat_n", test_repeat_n); g_test_add_func("/core/parser/optional", test_optional); - g_test_add_func("/core/parser/expect", test_expect); g_test_add_func("/core/parser/chain", test_chain); g_test_add_func("/core/parser/chainl", test_chainl); g_test_add_func("/core/parser/list", test_list); diff --git a/src/hammer.h b/src/hammer.h index 1b84861..9c83338 100644 --- a/src/hammer.h +++ b/src/hammer.h @@ -155,7 +155,7 @@ const parser_t* choice(const parser_t* p_array[]); /* Given two parsers, p1 and p2, this parser succeeds in the following cases: * - if p1 succeeds and p2 fails - * - if both succeed but p1's result is shorter than p2's + * - if both succeed but p1's result is as long as or shorter than p2's */ const parser_t* butnot(const parser_t* p1, const parser_t* p2);