| 
									
										
										
										
											2013-09-23 22:41:58 +01:00
										 |  |  | #include "ptest.h"
 | 
					
						
							|  |  |  | #include "../mpc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-30 20:55:57 +01:00
										 |  |  | static int int_eq(void* x, void* y) { return (*(int*)x == *(int*)y); } | 
					
						
							| 
									
										
										
										
											2013-09-23 22:41:58 +01:00
										 |  |  | static void int_print(void* x) { printf("'%i'", *((int*)x)); } | 
					
						
							| 
									
										
										
										
											2013-09-30 20:55:57 +01:00
										 |  |  | static int string_eq(void* x, void* y) { return (strcmp(x, y) == 0); } | 
					
						
							| 
									
										
										
										
											2013-09-23 22:41:58 +01:00
										 |  |  | static void string_print(void* x) { printf("'%s'", (char*)x); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void test_ident(void) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ^[a-zA-Z_][a-zA-Z0-9_]*$ */ | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   PT_ASSERT(mpc_match(Ident, "test", "test", string_eq, free, string_print)); | 
					
						
							|  |  |  |   PT_ASSERT(mpc_unmatch(Ident, "  blah", "", string_eq, free, string_print)); | 
					
						
							|  |  |  |   PT_ASSERT(mpc_match(Ident, "anoth21er", "anoth21er", string_eq, free, string_print)); | 
					
						
							|  |  |  |   PT_ASSERT(mpc_match(Ident, "du__de", "du__de", string_eq, free, string_print)); | 
					
						
							|  |  |  |   PT_ASSERT(mpc_unmatch(Ident, "some spaces", "", string_eq, free, string_print)); | 
					
						
							|  |  |  |   PT_ASSERT(mpc_unmatch(Ident, "", "", string_eq, free, string_print)); | 
					
						
							|  |  |  |   PT_ASSERT(mpc_unmatch(Ident, "18nums", "", string_eq, free, string_print)); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   mpc_delete(Ident); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void test_maths(void) { | 
					
						
							| 
									
										
										
										
											2013-09-30 20:55:57 +01:00
										 |  |  |    | 
					
						
							|  |  |  |   mpc_parser_t *Expr, *Factor, *Term, *Maths;  | 
					
						
							|  |  |  |   int r0 = 1; | 
					
						
							|  |  |  |   int r1 = 5; | 
					
						
							|  |  |  |   int r2 = 13; | 
					
						
							|  |  |  |   int r3 = 0; | 
					
						
							|  |  |  |   int r4 = 2; | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Expr   = mpc_new("expr"); | 
					
						
							|  |  |  |   Factor = mpc_new("factor"); | 
					
						
							|  |  |  |   Term   = mpc_new("term"); | 
					
						
							|  |  |  |   Maths  = mpc_new("maths"); | 
					
						
							| 
									
										
										
										
											2013-09-23 22:41:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-10 12:42:47 +00: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 | 
					
						
							|  |  |  |   )); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2013-11-10 12:42:47 +00: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 | 
					
						
							|  |  |  |   )); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2013-11-10 12:42:47 +00:00
										 |  |  |   mpc_define(Term, mpc_or(2,  | 
					
						
							| 
									
										
										
										
											2013-09-23 22:41:58 +01:00
										 |  |  |     mpc_int(), | 
					
						
							|  |  |  |     mpc_parens(Expr, free) | 
					
						
							|  |  |  |   )); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2014-01-26 11:25:50 +00:00
										 |  |  |   mpc_define(Maths, mpc_whole(Expr, free)); | 
					
						
							| 
									
										
										
										
											2013-09-23 22:41:58 +01:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2013-09-30 20:55:57 +01:00
										 |  |  |   PT_ASSERT(mpc_match(Maths, "1", &r0, int_eq, free, int_print)); | 
					
						
							|  |  |  |   PT_ASSERT(mpc_match(Maths, "(5)", &r1, int_eq, free, int_print)); | 
					
						
							|  |  |  |   PT_ASSERT(mpc_match(Maths, "(4*2)+5", &r2, int_eq, free, int_print)); | 
					
						
							|  |  |  |   PT_ASSERT(mpc_unmatch(Maths, "a", &r3, int_eq, free, int_print)); | 
					
						
							|  |  |  |   PT_ASSERT(mpc_unmatch(Maths, "2b+4", &r4, int_eq, free, int_print)); | 
					
						
							| 
									
										
										
										
											2013-09-23 22:41:58 +01:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2013-09-26 13:15:00 +01:00
										 |  |  |   mpc_cleanup(4, Expr, Factor, Term, Maths); | 
					
						
							| 
									
										
										
										
											2013-09-23 22:41:58 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void suite_core(void) { | 
					
						
							|  |  |  |   pt_add_test(test_ident, "Test Ident", "Suite Core"); | 
					
						
							|  |  |  |   pt_add_test(test_maths, "Test Maths", "Suite Core"); | 
					
						
							|  |  |  | } |