Skip to content

Commit

Permalink
Clean up tests a little and C++11 style initializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
PetterS committed Nov 3, 2013
1 parent d10bc88 commit 090a0b9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
8 changes: 4 additions & 4 deletions source/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ class Function::Implementation
Function* interface;
};

Function::Function() :
impl(new Function::Implementation(this))
Function::Function()
: impl{new Function::Implementation{this}}
{ }

Function::Function(const Function& org)
: impl(new Function::Implementation(this))
: impl{new Function::Implementation{this}}
{
*this = org;
}

Function::Implementation::Implementation(Function* function_interface)
: interface(function_interface)
: interface{function_interface}
{
clear();
}
Expand Down
4 changes: 2 additions & 2 deletions source/term_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class TermFactory::Implementation
map<string, TermCreator> creators;
};

TermFactory::TermFactory() :
impl(new TermFactory::Implementation)
TermFactory::TermFactory()
: impl{new TermFactory::Implementation}
{
}

Expand Down
34 changes: 17 additions & 17 deletions tests/test_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ TEST(Function, variable_not_found_is_added)
{
Function f;
double x[2] = {0};
f.add_term(std::make_shared<AutoDiffTerm<Term1, 2>>(), x);
f.add_term<AutoDiffTerm<Term1, 2>>(x);
CHECK(f.get_number_of_terms() == 1);
CHECK(f.get_number_of_variables() == 1);
CHECK(f.get_number_of_scalars() == 2);
Expand All @@ -68,7 +68,7 @@ TEST(Function, term_variable_mismatch)
Function f;
double x[5] = {0};
f.add_variable(x, 5);
EXPECT_THROW(f.add_term(std::make_shared<AutoDiffTerm<Term1, 4>>(), x), std::runtime_error);
EXPECT_THROW((f.add_term<AutoDiffTerm<Term1, 4>>(x)), std::runtime_error);
}

class DestructorTerm :
Expand Down Expand Up @@ -117,8 +117,8 @@ TEST(Function, calls_term_destructor)
int counter2 = 0;

{
auto term1 = std::shared_ptr<const Term>(new DestructorTerm(&counter1));
auto term2 = std::shared_ptr<const Term>(new DestructorTerm(&counter2));
auto term1 = std::make_shared<DestructorTerm>(&counter1);
auto term2 = std::make_shared<DestructorTerm>(&counter2);

function->add_term(term1, x);
function->add_term(term1, x);
Expand All @@ -142,8 +142,8 @@ TEST(Function, copy_constructor)
int counter2 = 0;

{
auto term1 = std::shared_ptr<const Term>(new DestructorTerm(&counter1));
auto term2 = std::shared_ptr<const Term>(new DestructorTerm(&counter2));
auto term1 = std::make_shared<DestructorTerm>(&counter1);
auto term2 = std::make_shared<DestructorTerm>(&counter2);

function->add_term(term1, x);
function->add_term(term1, x);
Expand Down Expand Up @@ -255,8 +255,8 @@ TEST(Function, add_constant)
f.add_variable(y, 1);
f.add_variable(z, 1);

f.add_term(std::make_shared<AutoDiffTerm<Term1, 2>>(), x);
f.add_term(std::make_shared<AutoDiffTerm<Term2, 1, 1>>(), y, z);
f.add_term<AutoDiffTerm<Term1, 2>>(x);
f.add_term<AutoDiffTerm<Term2, 1, 1>>(y, z);

double fval = f.evaluate();
f += 1.0;
Expand All @@ -276,8 +276,8 @@ TEST(Function, add_functions)
f.add_variable(y, 1);
f.add_variable(z, 1);

f.add_term(std::make_shared<AutoDiffTerm<Term1, 2>>(), x);
f.add_term(std::make_shared<AutoDiffTerm<Term2, 1, 1>>(), y, z);
f.add_term<AutoDiffTerm<Term1, 2>>(x);
f.add_term<AutoDiffTerm<Term2, 1, 1>>(y, z);
f += 7.0;

Function f2;
Expand All @@ -301,8 +301,8 @@ TEST(Function, evaluate_x)
f.add_variable(y, 1);
f.add_variable(z, 1);

f.add_term(std::make_shared<AutoDiffTerm<Term1, 2>>(), x);
f.add_term(std::make_shared<AutoDiffTerm<Term2, 1, 1>>(), y, z);
f.add_term<AutoDiffTerm<Term1, 2>>(x);
f.add_term<AutoDiffTerm<Term2, 1, 1>>(y, z);

Eigen::VectorXd xg(4);
xg[0] = 6.0;
Expand All @@ -326,13 +326,13 @@ TEST(Function, copy_and_assignment)
auto f2 = new Function;

{
auto destructor_term = std::shared_ptr<const Term>(new DestructorTerm(&counter));
auto destructor_term = std::make_shared<DestructorTerm>(&counter);

f1->add_variable(x, 2);
f1->add_variable(y, 1);
f1->add_variable(z, 1);
f1->add_term(std::make_shared<AutoDiffTerm<Term1, 2>>(), x);
f1->add_term(std::make_shared<AutoDiffTerm<Term2, 1, 1>>(), y, z);
f1->add_term<AutoDiffTerm<Term1, 2>>(x);
f1->add_term<AutoDiffTerm<Term2, 1, 1>>(y, z);
f1->add_term(destructor_term, y);
REQUIRE(counter == 0);

Expand All @@ -345,10 +345,10 @@ TEST(Function, copy_and_assignment)
}
REQUIRE(counter == 0);

auto f3 = new Function(*f1);
auto f3 = new Function{*f1};
REQUIRE(counter == 0);

auto f4 = new Function(*f2);
auto f4 = new Function{*f2};
REQUIRE(counter == 0);

CHECK(f3->get_number_of_scalars() == f1->get_number_of_scalars());
Expand Down
36 changes: 15 additions & 21 deletions tests/test_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,24 @@ void test_method(const Solver& solver)
EXPECT_LT( std::fabs(f.evaluate()), 1e-9);
}

// nullptr would have been better, but is not present
// in gcc 4.5.
namespace {
auto no_log_function = [](const std::string&) { };
}

TEST(Solver, NEWTON)
{
NewtonSolver solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
test_method(solver);
}

TEST(Solver, LBFGS)
{
LBFGSSolver solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
test_method(solver);
}

TEST(Solver, NELDER_MEAD)
{
NelderMeadSolver solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
solver.maximum_iterations = 10000;
solver.area_tolerance = 1e-40;
test_method(solver);
Expand All @@ -74,7 +68,7 @@ TEST(Solver, NELDER_MEAD)
TEST(Solver, PATTERN_SEARCH)
{
PatternSolver solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
solver.maximum_iterations = 100000;
test_method(solver);
}
Expand All @@ -87,7 +81,7 @@ TEST(Solver, function_tolerance)
f.add_term(std::make_shared<AutoDiffTerm<Rosenbrock, 2>>(), x);

NewtonSolver solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
solver.maximum_iterations = 50;
solver.gradient_tolerance = 0;
solver.argument_improvement_tolerance = 0;
Expand All @@ -105,7 +99,7 @@ TEST(Solver, argument_improvement_tolerance)
f.add_term(std::make_shared<AutoDiffTerm<Rosenbrock, 2>>(), x);

NewtonSolver solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
solver.maximum_iterations = 50;
solver.gradient_tolerance = 0;
solver.function_improvement_tolerance = 0;
Expand All @@ -123,7 +117,7 @@ TEST(Solver, gradient_tolerance)
f.add_term(std::make_shared<AutoDiffTerm<Rosenbrock, 2>>(), x);

NewtonSolver solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
solver.maximum_iterations = 50;
solver.function_improvement_tolerance = 0;
solver.argument_improvement_tolerance = 0;
Expand Down Expand Up @@ -161,7 +155,7 @@ TEST(Solver, inf_nan)
f_inf.add_term(std::make_shared<AutoDiffTerm<InfFunctor, 1>>(), x);

NewtonSolver solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
SolverResults results;

solver.solve(f_nan, &results);
Expand Down Expand Up @@ -224,7 +218,7 @@ TEST(Solver, L_GBFS_exact)

LBFGSSolver solver;
SolverResults results;
solver.log_function = no_log_function;
solver.log_function = nullptr;
solver.lbfgs_history_size = 10;
solver.maximum_iterations = iters[i];

Expand Down Expand Up @@ -275,7 +269,7 @@ TEST(Solver, Newton_exact)

NewtonSolver solver;
SolverResults results;
solver.log_function = no_log_function;
solver.log_function = nullptr;
solver.maximum_iterations = iters[i];

solver.solve(f, &results);
Expand Down Expand Up @@ -361,10 +355,10 @@ TEST(Solver, SimpleConstraints)
t);

NelderMeadSolver nm_solver;
nm_solver.log_function = no_log_function;
nm_solver.log_function = nullptr;

LBFGSSolver lbfgs_solver;
lbfgs_solver.log_function = no_log_function;
lbfgs_solver.log_function = nullptr;

SolverResults results;
results.exit_condition = SolverResults::NA;
Expand Down Expand Up @@ -403,7 +397,7 @@ TEST(Solver, PositiveConstraint)
x);

LBFGSSolver solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
SolverResults results;
solver.solve(function, &results);

Expand All @@ -423,7 +417,7 @@ TEST(Solver, BoxConstraint)
x);

LBFGSSolver solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
SolverResults results;
solver.solve(function, &results);

Expand All @@ -438,7 +432,7 @@ void test_constant_variables()
// extra variables are added and held constant

SolverClass solver;
solver.log_function = no_log_function;
solver.log_function = nullptr;
SolverResults results;


Expand Down

0 comments on commit 090a0b9

Please sign in to comment.