From 2d98d2ac9a1d95c89f76bd601c77c99135c2c46c Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Wed, 3 Feb 2021 01:03:11 +0100 Subject: [PATCH] Test file is no no longer used. --- src/mlpack/tests/function_test.cpp | 681 ----------------------------- 1 file changed, 681 deletions(-) delete mode 100644 src/mlpack/tests/function_test.cpp diff --git a/src/mlpack/tests/function_test.cpp b/src/mlpack/tests/function_test.cpp deleted file mode 100644 index 5486ac1e87..0000000000 --- a/src/mlpack/tests/function_test.cpp +++ /dev/null @@ -1,681 +0,0 @@ -/** - * @file tests/function_test.cpp - * @author Ryan Curtin - * @author Shikhar Bhardwaj - * - * Test the Function<> class to see that it properly adds functionality. - * - * mlpack is free software; you may redistribute it and/or modify it under the - * terms of the 3-clause BSD license. You should have received a copy of the - * 3-clause BSD license along with mlpack. If not, see - * http://www.opensource.org/licenses/BSD-3-Clause for more information. - */ -#include -#include -#include -#include -#include -#include - -#include -#include "test_tools.hpp" - -using namespace mlpack; -using namespace mlpack::optimization; -using namespace ens::traits; // For some SFINAE checks. -using namespace mlpack::regression; - -/** - * Utility class with no functions. - */ -class EmptyTestFunction { }; - -/** - * Utility class with Evaluate() but no Evaluate(). - */ -class EvaluateTestFunction -{ - public: - double Evaluate(const arma::mat& coordinates) - { - return arma::accu(coordinates); - } - - double Evaluate(const arma::mat& coordinates, - const size_t begin, - const size_t batchSize) - { - return arma::accu(coordinates) + begin + batchSize; - } -}; - -/** - * Utility class with Gradient() but no Evaluate(). - */ -class GradientTestFunction -{ - public: - void Gradient(const arma::mat& coordinates, arma::mat& gradient) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - } - - void Gradient(const arma::mat& coordinates, - const size_t /* begin */, - arma::mat& gradient, - const size_t /* batchSize */) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - } -}; - -/** - * Utility class with Gradient() and Evaluate(). - */ -class EvaluateGradientTestFunction -{ - public: - double Evaluate(const arma::mat& coordinates) - { - return arma::accu(coordinates); - } - - double Evaluate(const arma::mat& coordinates, - const size_t /* begin */, - const size_t /* batchSize */) - { - return arma::accu(coordinates); - } - - void Gradient(const arma::mat& coordinates, arma::mat& gradient) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - } - - void Gradient(const arma::mat& coordinates, - const size_t /* begin */, - arma::mat& gradient, - const size_t /* batchSize */) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - } -}; - -/** - * Utility class with EvaluateWithGradient(). - */ -class EvaluateWithGradientTestFunction -{ - public: - double EvaluateWithGradient(const arma::mat& coordinates, arma::mat& gradient) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - return arma::accu(coordinates); - } - - double EvaluateWithGradient(const arma::mat& coordinates, - const size_t /* begin */, - arma::mat& gradient, - const size_t /* batchSize */) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - return arma::accu(coordinates); - } -}; - -/** - * Utility class with all three functions. - */ -class EvaluateAndWithGradientTestFunction -{ - public: - double Evaluate(const arma::mat& coordinates) - { - return arma::accu(coordinates); - } - - double Evaluate(const arma::mat& coordinates, - const size_t begin, - const size_t batchSize) - { - return arma::accu(coordinates) + batchSize + begin; - } - - void Gradient(const arma::mat& coordinates, arma::mat& gradient) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - } - - void Gradient(const arma::mat& coordinates, - const size_t /* begin */, - arma::mat& gradient, - const size_t /* batchSize */) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - } - - double EvaluateWithGradient(const arma::mat& coordinates, arma::mat& gradient) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - return arma::accu(coordinates); - } - - double EvaluateWithGradient(const arma::mat& coordinates, - const size_t /* begin */, - arma::mat& gradient, - const size_t /* batchSize */) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - return arma::accu(coordinates); - } -}; - -/** - * Utility class with const Evaluate() and non-const Gradient(). - */ -class EvaluateAndNonConstGradientTestFunction -{ - public: - double Evaluate(const arma::mat& coordinates) const - { - return arma::accu(coordinates); - } - - void Gradient(const arma::mat& coordinates, arma::mat& gradient) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - } -}; - -/** - * Utility class with const Evaluate() and non-const Gradient(). - */ -class EvaluateAndStaticGradientTestFunction -{ - public: - double Evaluate(const arma::mat& coordinates) const - { - return arma::accu(coordinates); - } - - static void Gradient(const arma::mat& coordinates, arma::mat& gradient) - { - gradient.ones(coordinates.n_rows, coordinates.n_cols); - } -}; - -BOOST_AUTO_TEST_SUITE(FunctionTest); - -/** - * Make sure that an empty class doesn't have any methods added to it. - */ -BOOST_AUTO_TEST_CASE(AddEvaluateWithGradientEmptyTest) -{ - const bool hasEvaluate = HasEvaluate, - EvaluateForm>::value; - const bool hasGradient = HasGradient, - GradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - EvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, false); - BOOST_REQUIRE_EQUAL(hasGradient, false); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, false); -} - -/** - * Make sure we don't add any functions if we only have Evaluate(). - */ -BOOST_AUTO_TEST_CASE(AddEvaluateWithGradientEvaluateOnlyTest) -{ - const bool hasEvaluate = HasEvaluate, - EvaluateForm>::value; - const bool hasGradient = HasGradient, - GradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - EvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, false); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, false); -} - -/** - * Make sure we don't add any functions if we only have Gradient(). - */ -BOOST_AUTO_TEST_CASE(AddEvaluateWithGradientGradientOnlyTest) -{ - const bool hasEvaluate = HasEvaluate, - EvaluateForm>::value; - const bool hasGradient = HasGradient, - GradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - EvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, false); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, false); -} - -/** - * Make sure we add EvaluateWithGradient() when we have both Evaluate() and - * Gradient(). - */ -BOOST_AUTO_TEST_CASE(AddEvaluateWithGradientBothTest) -{ - const bool hasEvaluate = - HasEvaluate, - EvaluateForm>::value; - const bool hasGradient = - HasGradient, - GradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - EvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, true); -} - -/** - * Make sure we add Evaluate() and Gradient() when we have only - * EvaluateWithGradient(). - */ -BOOST_AUTO_TEST_CASE(AddEvaluateWithGradientEvaluateWithGradientTest) -{ - const bool hasEvaluate = - HasEvaluate, - EvaluateForm>::value; - const bool hasGradient = - HasGradient, - GradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - EvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, true); -} - -/** - * Make sure we add no methods when we already have all three. - */ -BOOST_AUTO_TEST_CASE(AddEvaluateWithGradientAllThreeTest) -{ - const bool hasEvaluate = - HasEvaluate, - EvaluateForm>::value; - const bool hasGradient = - HasGradient, - GradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - EvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, true); -} - -BOOST_AUTO_TEST_CASE(LogisticRegressionEvaluateWithGradientTest) -{ - const bool hasEvaluate = - HasEvaluate>, - EvaluateConstForm>::value; - const bool hasGradient = - HasGradient>, - GradientConstForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient>, - EvaluateWithGradientConstForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, true); -} - -BOOST_AUTO_TEST_CASE(SDPTest) -{ - typedef AugLagrangianFunction>> FunctionType; - - const bool hasEvaluate = - HasEvaluate, EvaluateConstForm>::value; - const bool hasGradient = - HasGradient, GradientConstForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - EvaluateWithGradientConstForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, true); -} - -/** - * Make sure that an empty class doesn't have any methods added to it. - */ -BOOST_AUTO_TEST_CASE(AddDecomposableEvaluateWithGradientEmptyTest) -{ - const bool hasEvaluate = HasEvaluate, - DecomposableEvaluateForm>::value; - const bool hasGradient = HasGradient, - DecomposableGradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - DecomposableEvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, false); - BOOST_REQUIRE_EQUAL(hasGradient, false); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, false); -} - -/** - * Make sure we don't add any functions if we only have Evaluate(). - */ -BOOST_AUTO_TEST_CASE(AddDecomposableEvaluateWithGradientEvaluateOnlyTest) -{ - const bool hasEvaluate = HasEvaluate, - DecomposableEvaluateForm>::value; - const bool hasGradient = HasGradient, - DecomposableGradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - DecomposableEvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, false); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, false); -} - -/** - * Make sure we don't add any functions if we only have Gradient(). - */ -BOOST_AUTO_TEST_CASE(AddDecomposableEvaluateWithGradientGradientOnlyTest) -{ - const bool hasEvaluate = HasEvaluate, - DecomposableEvaluateForm>::value; - const bool hasGradient = HasGradient, - DecomposableGradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - DecomposableEvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, false); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, false); -} - -/** - * Make sure we add EvaluateWithGradient() when we have both Evaluate() and - * Gradient(). - */ -BOOST_AUTO_TEST_CASE(AddDecomposableEvaluateWithGradientBothTest) -{ - const bool hasEvaluate = - HasEvaluate, - DecomposableEvaluateForm>::value; - const bool hasGradient = - HasGradient, - DecomposableGradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - DecomposableEvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, true); -} - -/** - * Make sure we add Evaluate() and Gradient() when we have only - * EvaluateWithGradient(). - */ -BOOST_AUTO_TEST_CASE(AddDecomposableEvaluateWGradientEvaluateWithGradientTest) -{ - const bool hasEvaluate = - HasEvaluate, - DecomposableEvaluateForm>::value; - const bool hasGradient = - HasGradient, - DecomposableGradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - DecomposableEvaluateWithGradientForm>::value; - - Function f; - arma::mat coordinates(10, 10, arma::fill::ones); - arma::mat gradient; - f.Gradient(coordinates, 0, gradient, 5); - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, true); -} - -/** - * Make sure we add no methods when we already have all three. - */ -BOOST_AUTO_TEST_CASE(AddDecomposableEvaluateWithGradientAllThreeTest) -{ - const bool hasEvaluate = - HasEvaluate, - DecomposableEvaluateForm>::value; - const bool hasGradient = - HasGradient, - DecomposableGradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - DecomposableEvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, true); -} - -/** - * Make sure we can properly create EvaluateWithGradient() even when one of the - * functions is non-const. - */ -BOOST_AUTO_TEST_CASE(AddEvaluateWithGradientMixedTypesTest) -{ - const bool hasEvaluate = - HasEvaluate, - EvaluateConstForm>::value; - const bool hasGradient = - HasGradient, - GradientForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - EvaluateWithGradientForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, true); -} - -/** - * Make sure we can properly create EvaluateWithGradient() even when one of the - * functions is static. - */ -BOOST_AUTO_TEST_CASE(AddEvaluateWithGradientMixedTypesStaticTest) -{ - const bool hasEvaluate = - HasEvaluate, - EvaluateConstForm>::value; - const bool hasGradient = - HasGradient, - GradientStaticForm>::value; - const bool hasEvaluateWithGradient = - HasEvaluateWithGradient, - EvaluateWithGradientConstForm>::value; - - BOOST_REQUIRE_EQUAL(hasEvaluate, true); - BOOST_REQUIRE_EQUAL(hasGradient, true); - BOOST_REQUIRE_EQUAL(hasEvaluateWithGradient, true); -} - -class A -{ - public: - size_t NumFunctions() const; - size_t NumFeatures() const; - double Evaluate(const arma::mat&, const size_t, const size_t) const; - void Gradient(const arma::mat&, const size_t, arma::mat&, const size_t) const; - void Gradient(const arma::mat&, const size_t, arma::sp_mat&, const size_t) - const; - void PartialGradient(const arma::mat&, const size_t, arma::sp_mat&) const; -}; - -class B -{ - public: - size_t NumFunctions(); - size_t NumFeatures(); - double Evaluate(const arma::mat&, const size_t, const size_t); - void Gradient(const arma::mat&, const size_t, arma::mat&, const size_t); - void Gradient(const arma::mat&, const size_t, arma::sp_mat&, const size_t); - void PartialGradient(const arma::mat&, const size_t, arma::sp_mat&); -}; - -class C -{ - public: - size_t NumConstraints() const; - double Evaluate(const arma::mat&) const; - void Gradient(const arma::mat&, arma::mat&) const; - double EvaluateConstraint(const size_t, const arma::mat&) const; - void GradientConstraint(const size_t, const arma::mat&, arma::mat&) const; -}; - -class D -{ - public: - size_t NumConstraints(); - double Evaluate(const arma::mat&); - void Gradient(const arma::mat&, arma::mat&); - double EvaluateConstraint(const size_t, const arma::mat&); - void GradientConstraint(const size_t, const arma::mat&, arma::mat&); -}; - - -/** - * Test the correctness of the static check for DecomposableFunctionType API. - */ -BOOST_AUTO_TEST_CASE(DecomposableFunctionTypeCheckTest) -{ - static_assert(CheckNumFunctions::value, - "CheckNumFunctions static check failed."); - static_assert(CheckNumFunctions::value, - "CheckNumFunctions static check failed."); - static_assert(!CheckNumFunctions::value, - "CheckNumFunctions static check failed."); - static_assert(!CheckNumFunctions::value, - "CheckNumFunctions static check failed."); - - static_assert(CheckDecomposableEvaluate::value, - "CheckDecomposableEvaluate static check failed."); - static_assert(CheckDecomposableEvaluate::value, - "CheckDecomposableEvaluate static check failed."); - static_assert(!CheckDecomposableEvaluate::value, - "CheckDecomposableEvaluate static check failed."); - static_assert(!CheckDecomposableEvaluate::value, - "CheckDecomposableEvaluate static check failed."); - - static_assert(CheckDecomposableGradient::value, - "CheckDecomposableGradient static check failed."); - static_assert(CheckDecomposableGradient::value, - "CheckDecomposableGradient static check failed."); - static_assert(!CheckDecomposableGradient::value, - "CheckDecomposableGradient static check failed."); - static_assert(!CheckDecomposableGradient::value, - "CheckDecomposableGradient static check failed."); -} - -/** - * Test the correctness of the static check for LagrangianFunctionType API. - */ -BOOST_AUTO_TEST_CASE(LagrangianFunctionTypeCheckTest) -{ - static_assert(!CheckEvaluate::value, "CheckEvaluate static check failed."); - static_assert(!CheckEvaluate::value, "CheckEvaluate static check failed."); - static_assert(CheckEvaluate::value, "CheckEvaluate static check failed."); - static_assert(CheckEvaluate::value, "CheckEvaluate static check failed."); - - static_assert(!CheckGradient::value, "CheckGradient static check failed."); - static_assert(!CheckGradient::value, "CheckGradient static check failed."); - static_assert(CheckGradient::value, "CheckGradient static check failed."); - static_assert(CheckGradient::value, "CheckGradient static check failed."); - - static_assert(!CheckNumConstraints::value, - "CheckNumConstraints static check failed."); - static_assert(!CheckNumConstraints::value, - "CheckNumConstraints static check failed."); - static_assert(CheckNumConstraints::value, - "CheckNumConstraints static check failed."); - static_assert(CheckNumConstraints::value, - "CheckNumConstraints static check failed."); - - static_assert(!CheckEvaluateConstraint::value, - "CheckEvaluateConstraint static check failed."); - static_assert(!CheckEvaluateConstraint::value, - "CheckEvaluateConstraint static check failed."); - static_assert(CheckEvaluateConstraint::value, - "CheckEvaluateConstraint static check failed."); - static_assert(CheckEvaluateConstraint::value, - "CheckEvaluateConstraint static check failed."); - - static_assert(!CheckGradientConstraint::value, - "CheckGradientConstraint static check failed."); - static_assert(!CheckGradientConstraint::value, - "CheckGradientConstraint static check failed."); - static_assert(CheckGradientConstraint::value, - "CheckGradientConstraint static check failed."); - static_assert(CheckGradientConstraint::value, - "CheckGradientConstraint static check failed."); -} - -/** - * Test the correctness of the static check for SparseFunctionType API. - */ -BOOST_AUTO_TEST_CASE(SparseFunctionTypeCheckTest) -{ - static_assert(CheckSparseGradient::value, - "CheckSparseGradient static check failed."); - static_assert(CheckSparseGradient::value, - "CheckSparseGradient static check failed."); - static_assert(!CheckSparseGradient::value, - "CheckSparseGradient static check failed."); - static_assert(!CheckSparseGradient::value, - "CheckSparseGradient static check failed."); -} - -/** - * Test the correctness of the static check for SparseFunctionType API. - */ -BOOST_AUTO_TEST_CASE(ResolvableFunctionTypeCheckTest) -{ - static_assert(CheckNumFeatures::value, - "CheckNumFeatures static check failed."); - static_assert(CheckNumFeatures::value, - "CheckNumFeatures static check failed."); - static_assert(!CheckNumFeatures::value, - "CheckNumFeatures static check failed."); - static_assert(!CheckNumFeatures::value, - "CheckNumFeatures static check failed."); - - static_assert(CheckPartialGradient::value, - "CheckPartialGradient static check failed."); - static_assert(CheckPartialGradient::value, - "CheckPartialGradient static check failed."); - static_assert(!CheckPartialGradient::value, - "CheckPartialGradient static check failed."); - static_assert(!CheckPartialGradient::value, - "CheckPartialGradient static check failed."); -} - -BOOST_AUTO_TEST_SUITE_END();