expr_eval_test.c

Go to the documentation of this file.
00001 /* 
00002  * @file
00003  *
00004  * Simple test of the expression evaluator built into the gsIDL grammar.
00005  * If executed without comand line arguments, a few standard tests are
00006  * run.  Otherwise, the expressions on the command line are evaluated.
00007  *
00008  */
00009 /* $Id: expr_eval_test.c,v 1.5 2005/09/16 21:43:45 seymour Exp $ */
00010 /* $UTK_Copyright: $ */
00011 
00012 #include <stdlib.h>
00013 #include <stdio.h>
00014 
00015 #include "problem.h"
00016 #include "utility.h"
00017 
00018 int run_standard_tests();
00019 
00020 int
00021 main(int argc, char *argv[])
00022 {
00023   extern int idl_parse();
00024   double d;
00025   int i;
00026 
00027   if(argc == 1)
00028     exit(run_standard_tests());
00029  
00030   for(i = 1; i < argc; i++) {
00031     printf("parsing '%s'.. ", argv[i]);
00032     if(gs_expr_d(argv[i], &d, NULL) < 0)
00033       printf("result = PARSE ERROR\n");
00034     else
00035       printf("result = %g\n", d);
00036   }
00037 
00038   exit(EXIT_SUCCESS);
00039 }
00040 
00045 int 
00046 run_standard_tests()
00047 {
00048   icl_hash_t *table;
00049   int i, rv;
00050   double d;
00051   double values[] = {1.2, 3.7, 4.0, 1.0, 0.0, 43.63};
00052   char *expressions[] = {
00053      "1+2",
00054      "a+b",
00055      "sqrt(c)",
00056      "1<2",
00057      "3>4",
00058      "(1<2)&&(3>4)",
00059      "3<<1",
00060      "(3<<1)+4",
00061      "6>>1",
00062      "(6>>1)-7",
00063      "3||f",
00064      "c^zero",
00065      "c^one",
00066      "c^zero|one",
00067      "c^zero|one&c",
00068      "((6>>1)-7) && (3||f)",
00069      "((6>>1)-7) && (3||f) <= (c^zero|one&c)",
00070      "((6>>1)-7) && (3||f) < (c^zero|one&c)",
00071      "((6>>1)-7) && (3||f) > (c^zero|one&c)",
00072      "3*2",
00073      "3*2/4",
00074      "3*2/4%6",
00075      "(((6>>1)-7) && (3||f) <= (c^zero|one&c)) >= (3*2/4%6)",
00076      "(((6>>1)-7) && (3||f) <= (c^zero|one&c)) > (3*2/4%6)",
00077      "3!=2",
00078      "((1<2)&&(3>4) ? (3<<1)+4 : (6>>1)-7)",
00079      "((1<2)&&(3>4) ? (3<<1)+4 : (6>>1)-7) && (3||f)",
00080      "((1<2)&&(3>4) ? (3<<1)+4 : (6>>1)-7) && (3||f) <= (c^zero|one&c)",
00081      "(((1<2)&&(3>4) ? (3<<1)+4 : (6>>1)-7) && (3||f) <= (c^zero|one&c)) >= (3*2/4%6)",
00082      "(((1<2)&&(3>4) ? (3<<1)+4 : (6>>1)-7) && (3||f) <= (c^zero|one&c)) >= (3*2/4%6) == (3!=2)",
00083      "(((1<2)&&(3>4) ? (3<<1)+4 : (6>>1)-7) && (3||f) <= (c^zero|one&c)) >= (3*2/4%6) == (3==2)",
00084      "(((1<2)&&(3>4) ? (3<<1)+4 : (6>>1)-7) && (3||f) <= (c^zero|one&c)) >= (3*2/4%6) == (3!=2) + zero",
00085      "(((1<2)&&(3>4) ? (3<<1)+4 : (6>>1)-7) && (3||f) <= (c^zero|one&c)) >= (3*2/4%6) == (3!=2) + zero - one",
00086      "(((1<2)&&(3>4) ? (3<<1)+4 : (6>>1)-7) && (3||f) <= (c^zero|one&c)) >= (3*2/4%6) == (3!=2) + zero - one - +c",
00087      "(((1<2)&&(3>4) ? (3<<1)+4 : (6>>1)-7) && (3||f) <= (c^zero|one&c)) >= (3*2/4%6) == (3!=2) + zero - one - +c - -a + ~c * !one +(int)c",
00088      "~c",
00089      "~c*!one",
00090      "b",
00091      "(int)b",
00092      "~c*!one +(int)b",
00093      "-a + ~c*!one +(int)b",
00094      "+c - -a",
00095      "+c - -a + ~c*!one +(int)b",
00096      "zero - one - +c",
00097      "zero - one - +c - -a",
00098      "zero - one - +c - -a + ~c * !one",
00099      "zero - one - +c - -a + ~c * !one +(int)b",
00100      NULL
00101   };
00102   double correct_results[] = {3.0, 4.9, 2.0, 1.0, 0.0, 0.0, 6.0, 10.0, 3.0, -4.0, 
00103     1.0, 4.0, 5.0, 5.0, 4.0, 1.0, 1.0, 1.0, 0.0, 6.0, 1.5, 1.0, 1.0, 0.0, 1.0, 
00104     -4.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, -5.0, 0.0, 3.7, 3.0, 3.0, 
00105     1.8, 5.2, 8.2, -5.0, -3.8, -3.8, -0.8};
00106 
00107   rv = EXIT_SUCCESS;
00108 
00109   table = icl_hash_create(11, NULL);
00110 
00111   icl_hash_insert(table, "a", &values[0]);
00112   icl_hash_insert(table, "b", &values[1]);
00113   icl_hash_insert(table, "c", &values[2]);
00114   icl_hash_insert(table, "one", &values[3]);
00115   icl_hash_insert(table, "zero", &values[4]);
00116   icl_hash_insert(table, "f", &values[5]);
00117 
00118   for(i=0;expressions[i];i++) {
00119     printf("parsing '%s'.. ", expressions[i]);
00120     if(gs_expr_d(expressions[i], &d, table) < 0)
00121       printf("result = PARSE ERROR\n");
00122     else {
00123       if(abs(d - correct_results[i]) > 0.05) {
00124         printf("INCORRECT result = %g (correct = %g)\n", 
00125            d, correct_results[i]);
00126         rv = EXIT_FAILURE;
00127       }
00128       else
00129         printf("result = %g\n", d);
00130     }
00131   }
00132 
00133   icl_hash_destroy(table, NULL, NULL);
00134 
00135   return rv;
00136 }