From 090a0b9dea18dbfff3627d3210cb1471ed9dad30 Mon Sep 17 00:00:00 2001 From: Petter Strandmark Date: Sun, 3 Nov 2013 20:16:40 +0100 Subject: [PATCH] Clean up tests a little and C++11 style initializers. --- source/function.cpp | 8 ++++---- source/term_factory.cpp | 4 ++-- tests/test_function.cpp | 34 +++++++++++++++++----------------- tests/test_solver.cpp | 36 +++++++++++++++--------------------- 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/source/function.cpp b/source/function.cpp index b0870a1..5d334ae 100644 --- a/source/function.cpp +++ b/source/function.cpp @@ -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(); } diff --git a/source/term_factory.cpp b/source/term_factory.cpp index 1a523e8..9fc0efd 100644 --- a/source/term_factory.cpp +++ b/source/term_factory.cpp @@ -16,8 +16,8 @@ class TermFactory::Implementation map creators; }; -TermFactory::TermFactory() : - impl(new TermFactory::Implementation) +TermFactory::TermFactory() + : impl{new TermFactory::Implementation} { } diff --git a/tests/test_function.cpp b/tests/test_function.cpp index ba677cc..cbcb29d 100644 --- a/tests/test_function.cpp +++ b/tests/test_function.cpp @@ -57,7 +57,7 @@ TEST(Function, variable_not_found_is_added) { Function f; double x[2] = {0}; - f.add_term(std::make_shared>(), x); + f.add_term>(x); CHECK(f.get_number_of_terms() == 1); CHECK(f.get_number_of_variables() == 1); CHECK(f.get_number_of_scalars() == 2); @@ -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>(), x), std::runtime_error); + EXPECT_THROW((f.add_term>(x)), std::runtime_error); } class DestructorTerm : @@ -117,8 +117,8 @@ TEST(Function, calls_term_destructor) int counter2 = 0; { - auto term1 = std::shared_ptr(new DestructorTerm(&counter1)); - auto term2 = std::shared_ptr(new DestructorTerm(&counter2)); + auto term1 = std::make_shared(&counter1); + auto term2 = std::make_shared(&counter2); function->add_term(term1, x); function->add_term(term1, x); @@ -142,8 +142,8 @@ TEST(Function, copy_constructor) int counter2 = 0; { - auto term1 = std::shared_ptr(new DestructorTerm(&counter1)); - auto term2 = std::shared_ptr(new DestructorTerm(&counter2)); + auto term1 = std::make_shared(&counter1); + auto term2 = std::make_shared(&counter2); function->add_term(term1, x); function->add_term(term1, x); @@ -255,8 +255,8 @@ TEST(Function, add_constant) f.add_variable(y, 1); f.add_variable(z, 1); - f.add_term(std::make_shared>(), x); - f.add_term(std::make_shared>(), y, z); + f.add_term>(x); + f.add_term>(y, z); double fval = f.evaluate(); f += 1.0; @@ -276,8 +276,8 @@ TEST(Function, add_functions) f.add_variable(y, 1); f.add_variable(z, 1); - f.add_term(std::make_shared>(), x); - f.add_term(std::make_shared>(), y, z); + f.add_term>(x); + f.add_term>(y, z); f += 7.0; Function f2; @@ -301,8 +301,8 @@ TEST(Function, evaluate_x) f.add_variable(y, 1); f.add_variable(z, 1); - f.add_term(std::make_shared>(), x); - f.add_term(std::make_shared>(), y, z); + f.add_term>(x); + f.add_term>(y, z); Eigen::VectorXd xg(4); xg[0] = 6.0; @@ -326,13 +326,13 @@ TEST(Function, copy_and_assignment) auto f2 = new Function; { - auto destructor_term = std::shared_ptr(new DestructorTerm(&counter)); + auto destructor_term = std::make_shared(&counter); f1->add_variable(x, 2); f1->add_variable(y, 1); f1->add_variable(z, 1); - f1->add_term(std::make_shared>(), x); - f1->add_term(std::make_shared>(), y, z); + f1->add_term>(x); + f1->add_term>(y, z); f1->add_term(destructor_term, y); REQUIRE(counter == 0); @@ -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()); diff --git a/tests/test_solver.cpp b/tests/test_solver.cpp index 1d68741..aceed1c 100644 --- a/tests/test_solver.cpp +++ b/tests/test_solver.cpp @@ -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); @@ -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); } @@ -87,7 +81,7 @@ TEST(Solver, function_tolerance) f.add_term(std::make_shared>(), 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; @@ -105,7 +99,7 @@ TEST(Solver, argument_improvement_tolerance) f.add_term(std::make_shared>(), 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; @@ -123,7 +117,7 @@ TEST(Solver, gradient_tolerance) f.add_term(std::make_shared>(), 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; @@ -161,7 +155,7 @@ TEST(Solver, inf_nan) f_inf.add_term(std::make_shared>(), x); NewtonSolver solver; - solver.log_function = no_log_function; + solver.log_function = nullptr; SolverResults results; solver.solve(f_nan, &results); @@ -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]; @@ -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); @@ -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; @@ -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); @@ -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); @@ -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;