libformula-calc-test.cpp
Go to the documentation of this file.
1 // Simple mathematical formula processing library
2 //
3 // Copyright 2017 Atsushi Watanabe, All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
15 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 // EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28 
29 #include <gtest/gtest.h>
30 
31 #include <formula-calc.h>
32 
33 TEST(FormulaCalcTest, testFormulaCalculation)
34 {
35  int i;
36 
37  for (i = 0; i < 5; i++)
38  {
39  struct rpf_t *rpf;
40  struct rpf_t *rpf2;
41  double test = 0;
42  struct variables_t variable[2] =
43  {
44  { "TEST", &test },
45  { NULL, NULL }
46  };
47  float ret;
48 
49  ASSERT_EQ(formula("1+1", &rpf, variable), 1);
50  ret = formula_eval(rpf);
51  EXPECT_EQ(ret, 2.0);
52  rpf2 = formula_optimize(rpf);
53  ret = formula_eval(rpf2);
54  EXPECT_EQ(ret, 2.0);
55  formula_free(rpf);
56  formula_free(rpf2);
57 
58  ASSERT_EQ(formula("abc+1", &rpf, variable), 0);
59 
60  ASSERT_EQ(formula("1+2+3+4/8+2*3", &rpf, variable), 1);
61  ret = formula_eval(rpf);
62  EXPECT_EQ(ret, 12.5);
63  rpf2 = formula_optimize(rpf);
64  ret = formula_eval(rpf2);
65  EXPECT_EQ(ret, 12.5);
66  formula_free(rpf);
67  formula_free(rpf2);
68 
69  ASSERT_EQ(formula("cos(3.1416)+1", &rpf, variable), 1);
70  ret = formula_eval(rpf);
71  EXPECT_LT(fabs(ret), 1e-3);
72  rpf2 = formula_optimize(rpf);
73  ret = formula_eval(rpf2);
74  EXPECT_LT(fabs(ret), 1e-3);
75  formula_free(rpf);
76  formula_free(rpf2);
77 
78  ASSERT_EQ(formula("(TEST+1)*2-1", &rpf, variable), 1);
79 
80  for (test = 0; test < 4; test++)
81  {
82  ret = formula_eval(rpf);
83  EXPECT_EQ(ret, (test + 1) * 2.0 - 1.0);
84  }
85 
86  rpf2 = formula_optimize(rpf);
87 
88  for (test = 0; test < 4; test++)
89  {
90  ret = formula_eval(rpf2);
91  EXPECT_EQ(ret, (test + 1) * 2.0 - 1.0);
92  }
93  formula_free(rpf);
94  formula_free(rpf2);
95 
96  ASSERT_EQ(formula("(TEST=5)*0.1+10.0", &rpf, variable), 1);
97  ret = formula_eval(rpf);
98  EXPECT_NEAR(ret, 10.5, 1e-6);
99  EXPECT_NEAR(test, 5.0, 1e-6);
100  formula_free(rpf);
101 
102  ASSERT_EQ(formula("TEST=(10.0+5*0.1)", &rpf, variable), 1);
103  ret = formula_eval(rpf);
104  EXPECT_NEAR(ret, 10.5, 1e-6);
105  EXPECT_NEAR(test, 10.5, 1e-6);
106  formula_free(rpf);
107  }
108 }
109 
110 int main(int argc, char **argv)
111 {
112  testing::InitGoogleTest(&argc, argv);
113 
114  return RUN_ALL_TESTS();
115 }
TEST(FormulaCalcTest, testFormulaCalculation)
int main(int argc, char **argv)
struct rpf_t * formula_optimize(struct rpf_t *rpf)
double formula_eval(struct rpf_t *rpf)
int formula(const char *expr, struct rpf_t **rpf, const struct variables_t *variable)
void formula_free(struct rpf_t *rpf)


yp-spur
Author(s):
autogenerated on Sat May 11 2019 02:08:24