First commit

This commit is contained in:
Daniel Holden
2013-09-19 20:57:40 +01:00
commit c2f936bcb7
9 changed files with 1884 additions and 0 deletions

37
tests/ident.c Normal file
View File

@@ -0,0 +1,37 @@
#include "../mpc.h"
#include <stdlib.h>
#include <string.h>
static bool string_eq(void* x, void* y) { return (strcmp(x, y) == 0); }
static void string_print(void* x) { printf("'%s'", (char*)x); }
bool suite_ident(void) {
/* ^[a-zA-Z_][a-zA-Z0-9_]*$ */
mpc_parser_t* Ident = mpc_new();
mpc_define(Ident, mpc_ends(
mpc_also(
mpc_either(mpc_alpha(), mpc_underscore()),
mpc_many1(mpc_or(3, mpc_alpha(), mpc_underscore(), mpc_digit()), mpcf_strfold),
free, mpcf_strfold
),
free)
);
mpc_print(Ident);
mpc_test(Ident, "test", "test", string_eq, free, string_print);
mpc_test(Ident, " blah", "", string_eq, free, string_print);
mpc_test(Ident, "anoth21er", "anoth21er", string_eq, free, string_print);
mpc_test(Ident, "du__de", "du__de", string_eq, free, string_print);
mpc_test(Ident, "some spaces", "", string_eq, free, string_print);
mpc_test(Ident, "", "", string_eq, free, string_print);
mpc_test(Ident, "18nums", "", string_eq, free, string_print);
mpc_delete(Ident);
return true;
}

56
tests/math.c Normal file
View File

@@ -0,0 +1,56 @@
#include "../mpc.h"
#include <stdlib.h>
#include <string.h>
static bool int_eq(void* x, void* y) {
return (*(int*)x == *(int*)y);
}
static void int_print(void* x) {
printf("'%i'", *((int*)x));
}
bool suite_math(void) {
mpc_parser_t* Expr = mpc_new();
mpc_parser_t* Factor = mpc_new();
mpc_parser_t* Term = mpc_new();
mpc_parser_t* Maths = mpc_new();
mpc_define(Expr, mpc_either(
mpc_and(3, mpcf_maths, Factor, mpc_oneof("*/"), Factor, free, free),
Factor
));
mpc_define(Factor, mpc_either(
mpc_and(3, mpcf_maths, Term, mpc_oneof("+-"), Term, free, free),
Term
));
mpc_define(Term, mpc_either(
mpc_int(),
mpc_parens(Expr, free)
));
mpc_define(Maths, mpc_ends(Expr, free));
mpc_print(Expr);
mpc_print(Factor);
mpc_print(Term);
mpc_print(Maths);
mpc_test(Maths, "1", (int[]){ 1 }, int_eq, free, int_print);
mpc_test(Maths, "(5)", (int[]){ 5 }, int_eq, free, int_print);
mpc_test(Maths, "(4*2)+5", (int[]){ 13 }, int_eq, free, int_print);
mpc_test(Maths, "a", (int[]){ 0 }, int_eq, free, int_print);
mpc_test(Maths, "2b+4", (int[]){ 2 }, int_eq, free, int_print);
mpc_delete(Expr);
mpc_delete(Factor);
mpc_delete(Term);
mpc_delete(Maths);
return true;
}

34
tests/regex.c Normal file
View File

@@ -0,0 +1,34 @@
#include "../mpc.h"
#include <string.h>
/*
static bool string_eq(void* x, void* y) { return (strcmp(x, y) == 0); }
static void string_print(void* x) { printf("'%s'", (char*)x); }
*/
bool suite_regex(void) {
mpc_parser_t* re0 = mpc_re("abc|bcd");
mpc_parser_t* re1 = mpc_re("abc|bcd|e");
mpc_parser_t* re2 = mpc_re("abc(ab)*");
mpc_parser_t* re3 = mpc_re("abc(abdd)?");
mpc_parser_t* re4 = mpc_re("ab|c(abdd)?");
mpc_parser_t* re5 = mpc_re("abc(ab|dd)+g$");
mpc_print(re0);
mpc_print(re1);
mpc_print(re2);
mpc_print(re3);
mpc_print(re4);
mpc_print(re5);
mpc_delete(re0);
mpc_delete(re1);
mpc_delete(re2);
mpc_delete(re3);
mpc_delete(re4);
mpc_delete(re5);
return true;
}

15
tests/test.c Normal file
View File

@@ -0,0 +1,15 @@
#include <stdbool.h>
bool suite_ident(void);
bool suite_math(void);
bool suite_regex(void);
int main(int argc, char** argv) {
suite_ident();
suite_math();
suite_regex();
return 0;
}