Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 65767ad

Browse files
committedOct 13, 2016
Added: Test some discrete values for CubicMinimum
1 parent 3f41864 commit 65767ad

File tree

1 file changed

+30
-40
lines changed

1 file changed

+30
-40
lines changed
 

‎Test/TestCubicMinimum.C

+30-40
Original file line numberDiff line numberDiff line change
@@ -13,63 +13,39 @@
1313
#include "CubicMinimum.h"
1414

1515
#include "gtest/gtest.h"
16-
#include <cmath>
1716

1817

1918
class CubicFunction {
2019
public:
21-
static double value(double alpha)
22-
{
23-
return pow(alpha, 3.0) - pow(alpha, 2.0);
24-
}
25-
26-
static double tangent(double alpha)
27-
{
28-
return 3.0*pow(alpha,2.0) - 2.0*alpha;
29-
}
20+
static double value(double x) { return (x*x - x)*x; }
21+
static double tangent(double x) { return (3.0*x - 2.0)*x; }
3022
};
3123

3224

3325
class LinearFunction {
3426
public:
35-
static double value(double alpha)
36-
{
37-
return alpha;
38-
}
39-
40-
static double tangent(double alpha)
41-
{
42-
return 1.0;
43-
}
27+
static double value(double x) { return x; }
28+
static double tangent(double) { return 1.0; }
4429
};
4530

4631

4732
class QuadraticFunction {
4833
public:
49-
static double value(double alpha)
50-
{
51-
return (alpha+0.25)*(alpha+0.75);
52-
}
53-
54-
static double tangent(double alpha)
55-
{
56-
return 2*alpha + 1;
57-
}
34+
static double value(double x) { return (x+0.25)*(x+0.75); }
35+
static double tangent(double x) { return x+x+1.0; }
5836
};
5937

6038

6139
TEST(TestCubicMinimum, CubicFunction)
6240
{
6341
double alpha;
64-
std::vector<double> params(10);
65-
std::vector<double> vals(10);
66-
std::vector<double> tgts(10);
42+
std::vector<double> params(10), vals(10), tgts(10);
43+
6744
for (size_t i = 0; i < 10; ++i) {
6845
params[i] = -1.0 + 2.0/9.0 * i;
6946
vals[i] = CubicFunction::value(params[i]);
7047
tgts[i] = CubicFunction::tangent(params[i]);
7148
}
72-
7349
ASSERT_TRUE(CubicMinimum::Find(alpha, params, vals, tgts));
7450
ASSERT_FLOAT_EQ(alpha, 2.0/3.0);
7551

@@ -78,7 +54,6 @@ TEST(TestCubicMinimum, CubicFunction)
7854
vals[i] = CubicFunction::value(params[i]);
7955
tgts[i] = CubicFunction::tangent(params[i]);
8056
}
81-
8257
ASSERT_TRUE(CubicMinimum::Find(alpha, params, vals, tgts));
8358
ASSERT_FLOAT_EQ(alpha, 2.0/3.0);
8459
}
@@ -87,9 +62,8 @@ TEST(TestCubicMinimum, CubicFunction)
8762
TEST(TestCubicMinimum, QuadraticFunction)
8863
{
8964
double alpha;
90-
std::vector<double> params(10);
91-
std::vector<double> vals(10);
92-
std::vector<double> tgts(10);
65+
std::vector<double> params(10), vals(10), tgts(10);
66+
9367
for (size_t i = 0; i < 10; ++i) {
9468
params[i] = -1.0 + 2.0/9.0 * i;
9569
vals[i] = QuadraticFunction::value(params[i]);
@@ -103,17 +77,15 @@ TEST(TestCubicMinimum, QuadraticFunction)
10377
vals[i] = QuadraticFunction::value(params[i]);
10478
tgts[i] = QuadraticFunction::tangent(params[i]);
10579
}
106-
10780
ASSERT_FALSE(CubicMinimum::Find(alpha, params, vals, tgts));
10881
}
10982

11083

11184
TEST(TestCubicMinimum, LinearFunction)
11285
{
11386
double alpha;
114-
std::vector<double> params(10);
115-
std::vector<double> vals(10);
116-
std::vector<double> tgts(10);
87+
std::vector<double> params(10), vals(10), tgts(10);
88+
11789
for (size_t i = 0; i < 10; ++i) {
11890
params[i] = -1.0 + 2.0/9.0 * i;
11991
vals[i] = LinearFunction::value(params[i]);
@@ -128,3 +100,21 @@ TEST(TestCubicMinimum, LinearFunction)
128100
}
129101
ASSERT_FALSE(CubicMinimum::Find(alpha, params, vals, tgts));
130102
}
103+
104+
105+
TEST(TestCubicMinimum, DiscreteValues)
106+
{
107+
double alpha;
108+
std::vector<double> params(10);
109+
for (size_t i = 0; i < 10; ++i)
110+
params[i] = -1.0 + 2.0/9.0 * i;
111+
112+
std::vector<double> vals({ 0.668273, 0.668279, 0.668284, 0.66829, 0.668296,
113+
0.668301, 0.668307, 0.668311, 0.668306, 0.6683 });
114+
115+
std::vector<double> tgts({ 1.15625e-06, 1.00442e-06, 8.52527e-07, 7.00576e-07, 5.48567e-07,
116+
3.96498e-07, 2.44365e-07, 8.6514e-08, -1.15779e-07, -3.17837e-07 });
117+
118+
ASSERT_TRUE(CubicMinimum::Find(alpha, params, vals, tgts));
119+
ASSERT_NEAR(alpha, -1.0, 1.0e-8);
120+
}

0 commit comments

Comments
 (0)
Please sign in to comment.