#include <stdio.h>
#include <math.h>
#include "exp_rs.h"
#include "common_allocator.h"
int main() {
init_memory_tracking();
printf("=== Expression Function Test ===\n\n");
ExprContext* ctx = expr_context_new();
if (!ctx) {
printf("Failed to create context\n");
return 1;
}
ExprBatch* batch = expr_batch_new(8192);
printf("1. Adding expression functions:\n");
int result = expr_batch_add_expression_function(batch, "distance", "x1,y1,x2,y2",
"sqrt((x2-x1)^2 + (y2-y1)^2)");
printf(" - Added 'distance' function: %s\n", result == 0 ? "success" : "failed");
result = expr_batch_add_expression_function(batch, "avg", "a,b", "(a+b)/2");
printf(" - Added 'avg' function: %s\n", result == 0 ? "success" : "failed");
result = expr_batch_add_expression_function(batch, "square", "x", "x*x");
printf(" - Added 'square' function: %s\n", result == 0 ? "success" : "failed");
printf("\n2. Using expression functions:\n");
expr_batch_add_expression(batch, "distance(0, 0, 3, 4)");
expr_batch_evaluate(batch, ctx);
Real dist = expr_batch_get_result(batch, 0);
printf(" - distance(0, 0, 3, 4) = %.1f (expected 5.0)\n", dist);
expr_batch_add_expression(batch, "avg(10, 20)");
expr_batch_evaluate(batch, ctx);
Real average = expr_batch_get_result(batch, 1);
printf(" - avg(10, 20) = %.1f (expected 15.0)\n", average);
expr_batch_add_expression(batch, "square(7)");
expr_batch_evaluate(batch, ctx);
Real sq = expr_batch_get_result(batch, 2);
printf(" - square(7) = %.1f (expected 49.0)\n", sq);
printf("\n3. Nested expression functions:\n");
result = expr_batch_add_expression_function(batch, "dist_squared", "x1,y1,x2,y2",
"square(distance(x1,y1,x2,y2))");
printf(" - Added 'dist_squared' function: %s\n", result == 0 ? "success" : "failed");
expr_batch_add_expression(batch, "dist_squared(0, 0, 3, 4)");
expr_batch_evaluate(batch, ctx);
Real dist_sq = expr_batch_get_result(batch, 3);
printf(" - dist_squared(0, 0, 3, 4) = %.1f (expected 25.0)\n", dist_sq);
printf("\n4. Removing expression functions:\n");
result = expr_batch_remove_expression_function(batch, "avg");
printf(" - Removed 'avg' function: %s (result=%d)\n",
result == 1 ? "found and removed" : "not found", result);
result = expr_batch_remove_expression_function(batch, "avg");
printf(" - Try to remove 'avg' again: %s (result=%d)\n",
result == 0 ? "not found" : "error", result);
result = expr_batch_remove_expression_function(batch, "nonexistent");
printf(" - Remove non-existent function: %s (result=%d)\n",
result == 0 ? "not found" : "error", result);
printf("\n5. Error handling:\n");
result = expr_batch_add_expression_function(NULL, "test", "x", "x");
printf(" - Add to NULL context: %s (result=%d)\n",
result < 0 ? "error" : "unexpected", result);
result = expr_batch_add_expression_function(batch, NULL, "x", "x");
printf(" - Add with NULL name: %s (result=%d)\n",
result < 0 ? "error" : "unexpected", result);
result = expr_batch_add_expression_function(batch, "test", NULL, "x");
printf(" - Add with NULL params: %s (result=%d)\n",
result < 0 ? "error" : "unexpected", result);
result = expr_batch_add_expression_function(batch, "test", "x", NULL);
printf(" - Add with NULL expression: %s (result=%d)\n",
result < 0 ? "error" : "unexpected", result);
printf("\n6. Expression functions with variables:\n");
result = expr_batch_add_expression_function(batch, "scale", "x,factor", "x*factor");
expr_batch_add_variable(batch, "my_factor", 2.5);
expr_batch_add_expression(batch, "scale(10, my_factor)");
expr_batch_evaluate(batch, ctx);
Real scaled = expr_batch_get_result(batch, 4);
printf(" - scale(10, my_factor) where my_factor=2.5 = %.1f (expected 25.0)\n", scaled);
printf("\n7. Batch-local expression functions:\n");
result = expr_batch_add_expression_function(batch, "double", "x", "x*2");
printf(" - Added context function 'double': %s\n", result == 0 ? "success" : "failed");
result = expr_batch_add_expression_function(batch, "double", "x", "x*3");
printf(" - Added batch function 'double' (overrides context): %s\n", result == 0 ? "success" : "failed");
result = expr_batch_add_expression_function(batch, "triple", "x", "x*3");
printf(" - Added batch function 'triple': %s\n", result == 0 ? "success" : "failed");
expr_batch_add_expression(batch, "double(5)");
expr_batch_evaluate(batch, ctx);
Real doubled = expr_batch_get_result(batch, 5);
printf(" - double(5) = %.1f (expected 15.0, batch overrides context)\n", doubled);
expr_batch_add_expression(batch, "triple(4)");
expr_batch_evaluate(batch, ctx);
Real tripled = expr_batch_get_result(batch, 6);
printf(" - triple(4) = %.1f (expected 12.0)\n", tripled);
printf("\n8. Multiple batches with different functions:\n");
ExprBatch* batch2 = expr_batch_new(8192);
result = expr_batch_add_expression_function(batch2, "quadruple", "x", "x*4");
printf(" - Added 'quadruple' to batch2: %s\n", result == 0 ? "success" : "failed");
expr_batch_add_expression(batch2, "double(5)");
expr_batch_evaluate(batch2, ctx);
Real ctx_double = expr_batch_get_result(batch2, 0);
printf(" - batch2: double(5) = %.1f (expected 10.0, uses context function)\n", ctx_double);
expr_batch_add_expression(batch2, "quadruple(5)");
expr_batch_evaluate(batch2, ctx);
Real quad = expr_batch_get_result(batch2, 1);
printf(" - batch2: quadruple(5) = %.1f (expected 20.0)\n", quad);
printf("\n9. Removing batch-local functions:\n");
result = expr_batch_remove_expression_function(batch, "double");
printf(" - Removed batch function 'double': %s (result=%d)\n",
result == 1 ? "found and removed" : "not found", result);
expr_batch_add_expression(batch, "double(5)");
expr_batch_evaluate(batch, ctx);
Real ctx_double2 = expr_batch_get_result(batch, 7);
printf(" - double(5) after removal = %.1f (expected 10.0, back to context)\n", ctx_double2);
result = expr_batch_remove_expression_function(batch, "nonexistent");
printf(" - Remove non-existent batch function: %s (result=%d)\n",
result == 0 ? "not found" : "error", result);
printf("\n10. Batch function error handling:\n");
result = expr_batch_add_expression_function(NULL, "test", "x", "x");
printf(" - Add to NULL batch: %s (result=%d)\n",
result < 0 ? "error" : "unexpected", result);
result = expr_batch_add_expression_function(batch, NULL, "x", "x");
printf(" - Add with NULL name: %s (result=%d)\n",
result < 0 ? "error" : "unexpected", result);
result = expr_batch_add_expression_function(batch, "test", NULL, "x");
printf(" - Add with NULL params: %s (result=%d)\n",
result < 0 ? "error" : "unexpected", result);
result = expr_batch_add_expression_function(batch, "test", "x", NULL);
printf(" - Add with NULL expression: %s (result=%d)\n",
result < 0 ? "error" : "unexpected", result);
expr_batch_free(batch2);
expr_batch_free(batch);
expr_context_free(ctx);
printf("\n=== All Expression Function Tests Completed ===\n");
return 0;
}