libformula-calc-test.cpp
Go to the documentation of this file.
00001 // Simple mathematical formula processing library
00002 //
00003 // Copyright 2017 Atsushi Watanabe, All rights reserved.
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //
00008 // * Redistributions of source code must retain the above copyright notice, this
00009 //   list of conditions and the following disclaimer.
00010 // * Redistributions in binary form must reproduce the above copyright notice,
00011 //   this list of conditions and the following disclaimer in the documentation
00012 //   and/or other materials provided with the distribution.
00013 //
00014 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
00015 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00016 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
00017 // EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00018 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00019 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00020 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00021 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00022 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00023 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }


yp-spur
Author(s):
autogenerated on Fri May 10 2019 02:52:19