Bridge++  Ver. 1.1.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_Rational_Approx.cpp
Go to the documentation of this file.
1 
14 #include "parameterManager_YAML.h"
15 
16 #include "bridgeIO.h"
17 using Bridge::vout;
18 
19 #include "math_Rational.h"
20 
21 #ifdef USE_TEST
22 #include "test.h"
23 #endif
24 
25 #ifdef USE_TESTMANAGER_AUTOREGISTER
26 #include "testManager.h"
27 #endif
28 
29 //====================================================================
31 
38 namespace Test_Rational {
39  //- test-private parameters
40  namespace {
41  const std::string filename_input = "test_Rational_Approx.yaml";
42  const std::string filename_output = "stdout";
43 
44  class Parameters_Test_Rational : public Parameters {
45  public:
46  Parameters_Test_Rational()
47  {
48  Register_string("verbose_level", "NULL");
49 
50  Register_double("expected_result", 0.0);
51  }
52  };
53  }
54 
55  //- prototype declaration
56  int approx(void);
57 
58 #ifdef USE_TESTMANAGER_AUTOREGISTER
59  namespace {
60  static const bool is_registered = TestManager::RegisterTest(
61  "Rational.Approx",
62  approx
63  );
64  }
65 #endif
66 
67  //====================================================================
68  int approx(void)
69  {
70  // #### parameter setup ####
71  Parameters_Test_Rational params_test;
72  Parameters_Math_Rational params_rational;
73 
74  Parameters params_all;
75 
76  params_all.Register_Parameters("Test_Rational", &params_test);
77  params_all.Register_Parameters("Math_Rational", &params_rational);
78 
79  ParameterManager_YAML params_manager;
80  params_manager.read_params(filename_input, &params_all);
81 
82  const string str_vlevel = params_test.get_string("verbose_level");
83 #ifdef USE_TEST
84  const double expected_result = params_test.get_double("expected_result");
85 #endif
86 
87  const int n_exp = params_rational.get_int("exponent_numerator");
88  const int d_exp = params_rational.get_int("exponent_denominator");
89  const double x_min = params_rational.get_double("lower_bound");
90  const double x_max = params_rational.get_double("upper_bound");
91  const int Npoint = params_rational.get_int("number_of_partitions");
92 
94 
95  //- print input parameters
96  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
97  vout.general(vl, " n_exp = %d\n", n_exp);
98  vout.general(vl, " d_exp = %d\n", d_exp);
99  vout.general(vl, " x_min = %12.8f\n", x_min);
100  vout.general(vl, " x_max = %12.8f\n", x_max);
101  vout.general(vl, " Npoint = %d\n", Npoint);
102  vout.general(vl, "\n");
103 
104 
105  // #### object setup ####
106  Math_Rational rational;
107  rational.set_parameters(params_rational);
108 
109 
110  // #### Execution main part ####
111  double x = x_min;
112  double Dx = (x_max - x_min) / Npoint;
113  double p_exp = ((double)n_exp) / ((double)d_exp);
114 
115  for (int j = 0; j < Npoint + 1; ++j) {
116  double y1 = rational.func(x);
117  double y2 = pow(x, p_exp);
118 
119  vout.general(vl, "x,rational,x^(n/d) = %12.8f %12.8f %12.8f \n", x, y1, y2);
120 
121  x += Dx;
122  }
123 
124  double result = x;
125 
126 
127 #ifdef USE_TEST
128  return Test::verify(expected_result, result);
129 
130 #else
131  return EXIT_SUCCESS;
132 #endif
133  }
134 } // namespace Test_Rational