2013-09-23 22:41:58 +01:00
|
|
|
#include "ptest.h"
|
|
|
|
#include "../mpc.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-10-16 15:38:27 +01:00
|
|
|
static int int_eq(const void* x, const void* y) { return (*(int*)x == *(int*)y); }
|
|
|
|
static void int_print(const void* x) { printf("'%i'", *((int*)x)); }
|
2015-03-10 10:44:29 +00:00
|
|
|
static int streq(const void* x, const void* y) { return (strcmp(x, y) == 0); }
|
|
|
|
static void strprint(const void* x) { printf("'%s'", (char*)x); }
|
2013-09-23 22:41:58 +01:00
|
|
|
|
|
|
|
void test_ident(void) {
|
|
|
|
|
|
|
|
/* ^[a-zA-Z_][a-zA-Z0-9_]*$ */
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2014-01-26 11:25:50 +00:00
|
|
|
mpc_parser_t* Ident = mpc_whole(
|
2013-11-10 14:17:32 +00:00
|
|
|
mpc_and(2, mpcf_strfold,
|
2013-11-10 12:42:47 +00:00
|
|
|
mpc_or(2, mpc_alpha(), mpc_underscore()),
|
2013-11-10 12:52:01 +00:00
|
|
|
mpc_many1(mpcf_strfold, mpc_or(3, mpc_alpha(), mpc_underscore(), mpc_digit())),
|
2013-11-10 12:42:47 +00:00
|
|
|
free),
|
2013-09-23 22:41:58 +01:00
|
|
|
free
|
|
|
|
);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-03-10 10:44:29 +00:00
|
|
|
PT_ASSERT(mpc_test_pass(Ident, "test", "test", streq, free, strprint));
|
|
|
|
PT_ASSERT(mpc_test_fail(Ident, " blah", "", streq, free, strprint));
|
|
|
|
PT_ASSERT(mpc_test_pass(Ident, "anoth21er", "anoth21er", streq, free, strprint));
|
|
|
|
PT_ASSERT(mpc_test_pass(Ident, "du__de", "du__de", streq, free, strprint));
|
|
|
|
PT_ASSERT(mpc_test_fail(Ident, "some spaces", "", streq, free, strprint));
|
|
|
|
PT_ASSERT(mpc_test_fail(Ident, "", "", streq, free, strprint));
|
|
|
|
PT_ASSERT(mpc_test_fail(Ident, "18nums", "", streq, free, strprint));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2013-09-23 22:41:58 +01:00
|
|
|
mpc_delete(Ident);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_maths(void) {
|
2020-01-08 22:07:32 -08:00
|
|
|
|
|
|
|
mpc_parser_t *Expr, *Factor, *Term, *Maths;
|
2014-04-16 19:19:25 +01:00
|
|
|
int r0 = 1, r1 = 5, r2 = 13, r3 = 0, r4 = 2;
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2013-09-30 20:55:57 +01:00
|
|
|
Expr = mpc_new("expr");
|
|
|
|
Factor = mpc_new("factor");
|
|
|
|
Term = mpc_new("term");
|
|
|
|
Maths = mpc_new("maths");
|
2013-09-23 22:41:58 +01:00
|
|
|
|
2020-01-08 22:07:32 -08:00
|
|
|
mpc_define(Expr, mpc_or(2,
|
2013-09-23 22:41:58 +01:00
|
|
|
mpc_and(3, mpcf_maths, Factor, mpc_oneof("*/"), Factor, free, free),
|
|
|
|
Factor
|
|
|
|
));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
|
|
|
mpc_define(Factor, mpc_or(2,
|
2013-09-23 22:41:58 +01:00
|
|
|
mpc_and(3, mpcf_maths, Term, mpc_oneof("+-"), Term, free, free),
|
|
|
|
Term
|
|
|
|
));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
|
|
|
mpc_define(Term, mpc_or(2,
|
2013-09-23 22:41:58 +01:00
|
|
|
mpc_int(),
|
|
|
|
mpc_parens(Expr, free)
|
|
|
|
));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2014-01-26 11:25:50 +00:00
|
|
|
mpc_define(Maths, mpc_whole(Expr, free));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2014-04-16 19:19:25 +01:00
|
|
|
PT_ASSERT(mpc_test_pass(Maths, "1", &r0, int_eq, free, int_print));
|
|
|
|
PT_ASSERT(mpc_test_pass(Maths, "(5)", &r1, int_eq, free, int_print));
|
|
|
|
PT_ASSERT(mpc_test_pass(Maths, "(4*2)+5", &r2, int_eq, free, int_print));
|
|
|
|
PT_ASSERT(mpc_test_fail(Maths, "a", &r3, int_eq, free, int_print));
|
|
|
|
PT_ASSERT(mpc_test_fail(Maths, "2b+4", &r4, int_eq, free, int_print));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2013-09-26 13:15:00 +01:00
|
|
|
mpc_cleanup(4, Expr, Factor, Term, Maths);
|
2013-09-23 22:41:58 +01:00
|
|
|
}
|
|
|
|
|
2015-03-10 10:44:29 +00:00
|
|
|
void test_strip(void) {
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-03-10 10:44:29 +00:00
|
|
|
mpc_parser_t *Stripperl = mpc_apply(mpc_many(mpcf_strfold, mpc_any()), mpcf_strtriml);
|
|
|
|
mpc_parser_t *Stripperr = mpc_apply(mpc_many(mpcf_strfold, mpc_any()), mpcf_strtrimr);
|
|
|
|
mpc_parser_t *Stripper = mpc_apply(mpc_many(mpcf_strfold, mpc_any()), mpcf_strtrim);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-03-10 10:44:29 +00:00
|
|
|
PT_ASSERT(mpc_test_pass(Stripperl, " asdmlm dasd ", "asdmlm dasd ", streq, free, strprint));
|
|
|
|
PT_ASSERT(mpc_test_pass(Stripperr, " asdmlm dasd ", " asdmlm dasd", streq, free, strprint));
|
|
|
|
PT_ASSERT(mpc_test_pass(Stripper, " asdmlm dasd ", "asdmlm dasd", streq, free, strprint));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-03-10 10:44:29 +00:00
|
|
|
mpc_delete(Stripperl);
|
|
|
|
mpc_delete(Stripperr);
|
|
|
|
mpc_delete(Stripper);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-03-10 10:44:29 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 19:20:39 +01:00
|
|
|
void test_repeat(void) {
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-08-28 19:20:39 +01:00
|
|
|
int success;
|
|
|
|
mpc_result_t r;
|
|
|
|
mpc_parser_t *p = mpc_count(3, mpcf_strfold, mpc_digit(), free);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-08-28 19:20:39 +01:00
|
|
|
success = mpc_parse("test", "046", p, &r);
|
|
|
|
PT_ASSERT(success);
|
|
|
|
PT_ASSERT_STR_EQ(r.output, "046");
|
|
|
|
free(r.output);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-08-28 19:20:39 +01:00
|
|
|
success = mpc_parse("test", "046aa", p, &r);
|
|
|
|
PT_ASSERT(success);
|
|
|
|
PT_ASSERT_STR_EQ(r.output, "046");
|
|
|
|
free(r.output);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-08-28 19:20:39 +01:00
|
|
|
success = mpc_parse("test", "04632", p, &r);
|
|
|
|
PT_ASSERT(success);
|
|
|
|
PT_ASSERT_STR_EQ(r.output, "046");
|
|
|
|
free(r.output);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-08-28 19:20:39 +01:00
|
|
|
success = mpc_parse("test", "04", p, &r);
|
|
|
|
PT_ASSERT(!success);
|
|
|
|
mpc_err_delete(r.error);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-08-28 19:20:39 +01:00
|
|
|
mpc_delete(p);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2015-08-28 19:20:39 +01:00
|
|
|
}
|
|
|
|
|
2016-03-03 10:34:52 +00:00
|
|
|
void test_copy(void) {
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2016-03-03 10:34:52 +00:00
|
|
|
int success;
|
|
|
|
mpc_result_t r;
|
|
|
|
mpc_parser_t* p = mpc_or(2, mpc_char('a'), mpc_char('b'));
|
|
|
|
mpc_parser_t* q = mpc_and(2, mpcf_strfold, p, mpc_copy(p), free);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2016-03-03 10:34:52 +00:00
|
|
|
success = mpc_parse("test", "aa", q, &r);
|
|
|
|
PT_ASSERT(success);
|
|
|
|
PT_ASSERT_STR_EQ(r.output, "aa");
|
|
|
|
free(r.output);
|
|
|
|
|
|
|
|
success = mpc_parse("test", "bb", q, &r);
|
|
|
|
PT_ASSERT(success);
|
|
|
|
PT_ASSERT_STR_EQ(r.output, "bb");
|
|
|
|
free(r.output);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2016-03-03 10:34:52 +00:00
|
|
|
success = mpc_parse("test", "ab", q, &r);
|
|
|
|
PT_ASSERT(success);
|
|
|
|
PT_ASSERT_STR_EQ(r.output, "ab");
|
|
|
|
free(r.output);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2016-03-03 10:34:52 +00:00
|
|
|
success = mpc_parse("test", "ba", q, &r);
|
|
|
|
PT_ASSERT(success);
|
|
|
|
PT_ASSERT_STR_EQ(r.output, "ba");
|
|
|
|
free(r.output);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2016-03-03 10:34:52 +00:00
|
|
|
success = mpc_parse("test", "c", p, &r);
|
|
|
|
PT_ASSERT(!success);
|
|
|
|
mpc_err_delete(r.error);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2016-03-03 10:34:52 +00:00
|
|
|
mpc_delete(mpc_copy(p));
|
|
|
|
mpc_delete(mpc_copy(q));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2016-03-03 10:34:52 +00:00
|
|
|
mpc_delete(q);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2016-03-03 10:34:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 17:35:21 -04:00
|
|
|
static int line_count = 0;
|
|
|
|
|
2018-10-14 17:20:11 -04:00
|
|
|
static mpc_val_t* read_line(mpc_val_t* line) {
|
2018-10-13 17:35:21 -04:00
|
|
|
line_count++;
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_reader(void) {
|
|
|
|
|
|
|
|
mpc_parser_t* Line = mpc_many(
|
2020-01-08 22:07:32 -08:00
|
|
|
mpcf_strfold,
|
2018-10-13 17:35:21 -04:00
|
|
|
mpc_apply(mpc_re("[^\\n]*(\\n|$)"), read_line));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-13 17:35:21 -04:00
|
|
|
line_count = 0;
|
|
|
|
|
2020-01-08 22:07:32 -08:00
|
|
|
PT_ASSERT(mpc_test_pass(Line,
|
|
|
|
"hello\nworld\n\nthis\nis\ndan",
|
2018-10-13 17:35:21 -04:00
|
|
|
"hello\nworld\n\nthis\nis\ndan", streq, free, strprint));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-13 17:35:21 -04:00
|
|
|
PT_ASSERT(line_count == 6);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-13 17:35:21 -04:00
|
|
|
line_count = 0;
|
2020-01-08 22:07:32 -08:00
|
|
|
|
|
|
|
PT_ASSERT(mpc_test_pass(Line,
|
|
|
|
"abcHVwufvyuevuy3y436782\n\n\nrehre\nrew\n-ql.;qa\neg",
|
2018-10-13 17:35:21 -04:00
|
|
|
"abcHVwufvyuevuy3y436782\n\n\nrehre\nrew\n-ql.;qa\neg", streq, free, strprint));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-13 17:35:21 -04:00
|
|
|
PT_ASSERT(line_count == 7);
|
|
|
|
|
|
|
|
mpc_delete(Line);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-14 17:20:11 -04:00
|
|
|
static int token_count = 0;
|
|
|
|
|
|
|
|
static mpc_val_t *print_token(mpc_val_t *x) {
|
2018-12-15 16:11:39 -05:00
|
|
|
/*printf("Token: '%s'\n", (char*)x);*/
|
2018-10-14 17:20:11 -04:00
|
|
|
token_count++;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_tokens(void) {
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-14 17:20:11 -04:00
|
|
|
mpc_parser_t* Tokens = mpc_many(
|
2020-01-08 22:07:32 -08:00
|
|
|
mpcf_strfold,
|
2018-10-14 17:20:11 -04:00
|
|
|
mpc_apply(mpc_strip(mpc_re("\\s*([a-zA-Z_]+|[0-9]+|,|\\.|:)")), print_token));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-14 17:20:11 -04:00
|
|
|
token_count = 0;
|
|
|
|
|
2020-01-08 22:07:32 -08:00
|
|
|
PT_ASSERT(mpc_test_pass(Tokens,
|
|
|
|
" hello 4352 , \n foo.bar \n\n test:ing ",
|
2018-10-14 17:20:11 -04:00
|
|
|
"hello4352,foo.bartest:ing", streq, free, strprint));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-14 17:20:11 -04:00
|
|
|
PT_ASSERT(token_count == 9);
|
|
|
|
|
|
|
|
mpc_delete(Tokens);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-14 17:20:11 -04:00
|
|
|
}
|
|
|
|
|
2018-10-13 18:27:42 -04:00
|
|
|
void test_eoi(void) {
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-13 18:27:42 -04:00
|
|
|
mpc_parser_t* Line = mpc_re("[^\\n]*$");
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-13 18:27:42 -04:00
|
|
|
PT_ASSERT(mpc_test_pass(Line, "blah", "blah", streq, free, strprint));
|
|
|
|
PT_ASSERT(mpc_test_pass(Line, "blah\n", "blah\n", streq, free, strprint));
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-13 18:27:42 -04:00
|
|
|
mpc_delete(Line);
|
2020-01-08 22:07:32 -08:00
|
|
|
|
2018-10-13 18:27:42 -04:00
|
|
|
}
|
|
|
|
|
2013-09-23 22:41:58 +01:00
|
|
|
void suite_core(void) {
|
2015-08-28 19:20:39 +01:00
|
|
|
pt_add_test(test_ident, "Test Ident", "Suite Core");
|
|
|
|
pt_add_test(test_maths, "Test Maths", "Suite Core");
|
|
|
|
pt_add_test(test_strip, "Test Strip", "Suite Core");
|
|
|
|
pt_add_test(test_repeat, "Test Repeat", "Suite Core");
|
2016-03-03 10:34:52 +00:00
|
|
|
pt_add_test(test_copy, "Test Copy", "Suite Core");
|
2018-10-13 17:35:21 -04:00
|
|
|
pt_add_test(test_reader, "Test Reader", "Suite Core");
|
2018-10-14 17:20:11 -04:00
|
|
|
pt_add_test(test_tokens, "Test Tokens", "Suite Core");
|
2018-10-13 18:27:42 -04:00
|
|
|
pt_add_test(test_eoi, "Test EOI", "Suite Core");
|
2013-09-23 22:41:58 +01:00
|
|
|
}
|