[mlpack-svn] r15768 - mlpack/trunk/src/mlpack/tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Sep 12 14:30:29 EDT 2013


Author: rcurtin
Date: Thu Sep 12 14:30:28 2013
New Revision: 15768

Log:
Add a test for the KernelTraits class, and a long rant which explains why this
checkin didn't happen two hours ago.


Added:
   mlpack/trunk/src/mlpack/tests/kernel_traits_test.cpp
Modified:
   mlpack/trunk/src/mlpack/tests/CMakeLists.txt

Modified: mlpack/trunk/src/mlpack/tests/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/tests/CMakeLists.txt	(original)
+++ mlpack/trunk/src/mlpack/tests/CMakeLists.txt	Thu Sep 12 14:30:28 2013
@@ -15,6 +15,7 @@
   hmm_test.cpp
   kernel_test.cpp
   kernel_pca_test.cpp
+  kernel_traits_test.cpp
   kmeans_test.cpp
   lars_test.cpp
   lbfgs_test.cpp

Added: mlpack/trunk/src/mlpack/tests/kernel_traits_test.cpp
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/tests/kernel_traits_test.cpp	Thu Sep 12 14:30:28 2013
@@ -0,0 +1,59 @@
+/**
+ * @file kernel_traits_test.cpp
+ * @author Ryan Curtin
+ *
+ * Test the KernelTraits class.  Because all of the values are known at compile
+ * time, this test is meant to ensure that uses of KernelTraits still compile
+ * okay and react as expected.
+ */
+#include <mlpack/core.hpp>
+
+#include <boost/test/unit_test.hpp>
+#include "old_boost_test_definitions.hpp"
+
+using namespace mlpack;
+using namespace mlpack::kernel;
+
+BOOST_AUTO_TEST_SUITE(KernelTraitsTest);
+
+BOOST_AUTO_TEST_CASE(IsNormalizedTest)
+{
+  // Reason number ten billion why macros are bad:
+  //
+  // The Boost unit test framework is built on macros.  When I write
+  // BOOST_REQUIRE_EQUAL(KernelTraits<int>::IsNormalized, false), what actually
+  // happens (in gcc at least) is that the 'false' gets implicitly converted to
+  // an int; then, the compiler goes looking for an int IsNormalized variable in
+  // KernelTraits.  But this doesn't exist, so we get this error at linker time:
+  //
+  // kernel_traits_test.cpp:(.text+0xb86): undefined reference to
+  // `mlpack::kernel::KernelTraits<mlpack::kernel::LinearKernel>::IsNormalized'
+  //
+  // and this actually tells us nothing about the error.  When you dig deep
+  // enough or get frustrated enough, you end up realizing it's a macro problem
+  // and now have to *explicitly* cast to bool.  Yes, really; try it if you
+  // don't believe me.
+
+  // Test each kernel individually.
+  // If the type is not a valid kernel, it should be false (default value).
+  BOOST_REQUIRE_EQUAL((bool) KernelTraits<int>::IsNormalized, false);
+
+  // Normalized kernels.
+  BOOST_REQUIRE_EQUAL((bool) KernelTraits<CosineDistance>::IsNormalized, true);
+  BOOST_REQUIRE_EQUAL((bool) KernelTraits<EpanechnikovKernel>::IsNormalized,
+      true);
+  BOOST_REQUIRE_EQUAL((bool) KernelTraits<GaussianKernel>::IsNormalized, true);
+  BOOST_REQUIRE_EQUAL((bool) KernelTraits<LaplacianKernel>::IsNormalized, true);
+  BOOST_REQUIRE_EQUAL((bool) KernelTraits<SphericalKernel>::IsNormalized, true);
+  BOOST_REQUIRE_EQUAL((bool) KernelTraits<TriangularKernel>::IsNormalized,
+      true);
+
+  // Unnormalized kernels.
+  BOOST_REQUIRE_EQUAL((bool) KernelTraits<LinearKernel>::IsNormalized, false);
+  BOOST_REQUIRE_EQUAL((bool) KernelTraits<PolynomialKernel>::IsNormalized,
+      false);
+  BOOST_REQUIRE_EQUAL((bool) KernelTraits<PSpectrumStringKernel>::IsNormalized,
+      false);
+}
+
+BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-svn mailing list