mirror of
				https://github.com/eledio-devices/thirdparty-tinyexpr.git
				synced 2025-10-31 16:14:16 +01:00 
			
		
		
		
	Added equality tests
This commit is contained in:
		
							
								
								
									
										9
									
								
								test.c
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								test.c
									
									
									
									
									
								
							| @@ -694,6 +694,15 @@ void test_logic() { | |||||||
|             {"2 > -2", 1}, |             {"2 > -2", 1}, | ||||||
|             {"-2 < 2", 1}, |             {"-2 < 2", 1}, | ||||||
|  |  | ||||||
|  |             {"0 == 0", 1}, | ||||||
|  |             {"0 != 0", 0}, | ||||||
|  |             {"2 == 2", 1}, | ||||||
|  |             {"2 != 2", 0}, | ||||||
|  |             {"2 == 3", 0}, | ||||||
|  |             {"2 != 3", 1}, | ||||||
|  |             {"2 == 2.0001", 0}, | ||||||
|  |             {"2 != 2.0001", 1}, | ||||||
|  |  | ||||||
|             {"1 < 2 && 2 < 3", 1}, |             {"1 < 2 && 2 < 3", 1}, | ||||||
|             {"1 < 2 && 3 < 2", 0}, |             {"1 < 2 && 3 < 2", 0}, | ||||||
|             {"2 < 1 && 2 < 3", 0}, |             {"2 < 1 && 2 < 3", 0}, | ||||||
|   | |||||||
							
								
								
									
										28
									
								
								tinyexpr.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								tinyexpr.c
									
									
									
									
									
								
							| @@ -229,9 +229,11 @@ static double greater(double a, double b) {return a > b;} | |||||||
| static double greater_eq(double a, double b) {return a >= b;} | static double greater_eq(double a, double b) {return a >= b;} | ||||||
| static double lower(double a, double b) {return a < b;} | static double lower(double a, double b) {return a < b;} | ||||||
| static double lower_eq(double a, double b) {return a <= b;} | static double lower_eq(double a, double b) {return a <= b;} | ||||||
| static double logical_and(double a, double b) {return (int)(a + 0.5) == 1 && (int)(b + 0.5) == 1;} | static double equal(double a, double b) {return a == b;} | ||||||
| static double logical_or(double a, double b) {return (int)(a + 0.5) == 1 || (int)(b + 0.5) == 1;} | static double not_equal(double a, double b) {return a != b;} | ||||||
| static double logical_not(double a) {return (int)(a + 0.5) == 0;} | static double logical_and(double a, double b) {return (int)a != 0 && (int)b != 0;} | ||||||
|  | static double logical_or(double a, double b) {return (int)a != 0 || (int)b != 0;} | ||||||
|  | static double logical_not(double a) {return (int)a == 0;} | ||||||
|  |  | ||||||
|  |  | ||||||
| void next_token(state *s) { | void next_token(state *s) { | ||||||
| @@ -289,7 +291,21 @@ void next_token(state *s) { | |||||||
|                     case '/': s->type = TOK_INFIX; s->function = divide; break; |                     case '/': s->type = TOK_INFIX; s->function = divide; break; | ||||||
|                     case '^': s->type = TOK_INFIX; s->function = pow; break; |                     case '^': s->type = TOK_INFIX; s->function = pow; break; | ||||||
|                     case '%': s->type = TOK_INFIX; s->function = fmod; break; |                     case '%': s->type = TOK_INFIX; s->function = fmod; break; | ||||||
|                     case '!': s->type = TOK_INFIX; s->function = logical_not; break; |                     case '!': | ||||||
|  |                         if (s->next++[0] == '=') { | ||||||
|  |                             s->type = TOK_INFIX; s->function = not_equal; | ||||||
|  |                         } else { | ||||||
|  |                             s->next--; | ||||||
|  |                             s->type = TOK_INFIX; s->function = logical_not; | ||||||
|  |                         } | ||||||
|  |                         break; | ||||||
|  |                     case '=': | ||||||
|  |                         if (s->next++[0] == '=') { | ||||||
|  |                             s->type = TOK_INFIX; s->function = equal; | ||||||
|  |                         } else { | ||||||
|  |                             s->type = TOK_ERROR; | ||||||
|  |                         } | ||||||
|  |                         break; | ||||||
|                     case '<': |                     case '<': | ||||||
|                         if (s->next++[0] == '=') { |                         if (s->next++[0] == '=') { | ||||||
|                             s->type = TOK_INFIX; s->function = lower_eq; |                             s->type = TOK_INFIX; s->function = lower_eq; | ||||||
| @@ -554,11 +570,11 @@ static te_expr *sum_expr(state *s) { | |||||||
|  |  | ||||||
|  |  | ||||||
| static te_expr *test_expr(state *s) { | static te_expr *test_expr(state *s) { | ||||||
|     /* <expr>      =    <sum_expr> {(">" | ">=" | "<" | "<=") <sum_expr>} */ |     /* <expr>      =    <sum_expr> {(">" | ">=" | "<" | "<=" | "==" | "!=") <sum_expr>} */ | ||||||
|     te_expr *ret = sum_expr(s); |     te_expr *ret = sum_expr(s); | ||||||
|  |  | ||||||
|     while (s->type == TOK_INFIX && (s->function == greater || s->function == greater_eq || |     while (s->type == TOK_INFIX && (s->function == greater || s->function == greater_eq || | ||||||
|         s->function == lower || s->function == lower_eq)) { |         s->function == lower || s->function == lower_eq || s->function == equal || s->function == not_equal)) { | ||||||
|         te_fun2 t = s->function; |         te_fun2 t = s->function; | ||||||
|         next_token(s); |         next_token(s); | ||||||
|         ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, sum_expr(s)); |         ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, sum_expr(s)); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user