Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <math.h>
00028
00029 #include <gtest/gtest.h>
00030
00031 #include <formula-calc.h>
00032
00033 TEST(FormulaCalcTest, testFormulaCalculation)
00034 {
00035 int i;
00036
00037 for (i = 0; i < 5; i++)
00038 {
00039 struct rpf_t *rpf;
00040 struct rpf_t *rpf2;
00041 double test = 0;
00042 struct variables_t variable[2] =
00043 {
00044 { "TEST", &test },
00045 { NULL, NULL }
00046 };
00047 float ret;
00048
00049 ASSERT_EQ(formula("1+1", &rpf, variable), 1);
00050 ret = formula_eval(rpf);
00051 EXPECT_EQ(ret, 2.0);
00052 rpf2 = formula_optimize(rpf);
00053 ret = formula_eval(rpf2);
00054 EXPECT_EQ(ret, 2.0);
00055 formula_free(rpf);
00056 formula_free(rpf2);
00057
00058 ASSERT_EQ(formula("abc+1", &rpf, variable), 0);
00059
00060 ASSERT_EQ(formula("1+2+3+4/8+2*3", &rpf, variable), 1);
00061 ret = formula_eval(rpf);
00062 EXPECT_EQ(ret, 12.5);
00063 rpf2 = formula_optimize(rpf);
00064 ret = formula_eval(rpf2);
00065 EXPECT_EQ(ret, 12.5);
00066 formula_free(rpf);
00067 formula_free(rpf2);
00068
00069 ASSERT_EQ(formula("cos(3.1416)+1", &rpf, variable), 1);
00070 ret = formula_eval(rpf);
00071 EXPECT_LT(fabs(ret), 1e-3);
00072 rpf2 = formula_optimize(rpf);
00073 ret = formula_eval(rpf2);
00074 EXPECT_LT(fabs(ret), 1e-3);
00075 formula_free(rpf);
00076 formula_free(rpf2);
00077
00078 ASSERT_EQ(formula("(TEST+1)*2-1", &rpf, variable), 1);
00079
00080 for (test = 0; test < 4; test++)
00081 {
00082 ret = formula_eval(rpf);
00083 EXPECT_EQ(ret, (test + 1) * 2.0 - 1.0);
00084 }
00085
00086 rpf2 = formula_optimize(rpf);
00087
00088 for (test = 0; test < 4; test++)
00089 {
00090 ret = formula_eval(rpf2);
00091 EXPECT_EQ(ret, (test + 1) * 2.0 - 1.0);
00092 }
00093 formula_free(rpf);
00094 formula_free(rpf2);
00095
00096 ASSERT_EQ(formula("(TEST=5)*0.1+10.0", &rpf, variable), 1);
00097 ret = formula_eval(rpf);
00098 EXPECT_NEAR(ret, 10.5, 1e-6);
00099 EXPECT_NEAR(test, 5.0, 1e-6);
00100 formula_free(rpf);
00101
00102 ASSERT_EQ(formula("TEST=(10.0+5*0.1)", &rpf, variable), 1);
00103 ret = formula_eval(rpf);
00104 EXPECT_NEAR(ret, 10.5, 1e-6);
00105 EXPECT_NEAR(test, 10.5, 1e-6);
00106 formula_free(rpf);
00107 }
00108 }
00109
00110 int main(int argc, char **argv)
00111 {
00112 testing::InitGoogleTest(&argc, argv);
00113
00114 return RUN_ALL_TESTS();
00115 }