cleanup test_math_functions
authorJeff Donahue <jeff.donahue@gmail.com>
Tue, 8 Apr 2014 02:05:59 +0000 (19:05 -0700)
committerJeff Donahue <jeff.donahue@gmail.com>
Wed, 9 Apr 2014 03:17:17 +0000 (20:17 -0700)
src/caffe/test/test_math_functions.cpp

index 7e2c71d..2c2c062 100644 (file)
@@ -39,34 +39,32 @@ class MathFunctionsTest : public ::testing::Test {
     delete blob_bottom_;
     delete blob_top_;
   }
+
   // http://en.wikipedia.org/wiki/Hamming_distance
-  int ReferenceHammingDistance(const int n, const Dtype* x, const Dtype* y);
+  int ReferenceHammingDistance(const int n, const Dtype* x, const Dtype* y) {
+    int dist = 0;
+    uint64_t val;
+    for (int i = 0; i < n; ++i) {
+      if (sizeof(Dtype) == 8) {
+        val = static_cast<uint64_t>(x[i]) ^ static_cast<uint64_t>(y[i]);
+      } else if (sizeof(Dtype) == 4) {
+        val = static_cast<uint32_t>(x[i]) ^ static_cast<uint32_t>(y[i]);
+      } else {
+        LOG(FATAL) << "Unrecognized Dtype size: " << sizeof(Dtype);
+      }
+      // Count the number of set bits
+      while (val) {
+        ++dist;
+        val &= val - 1;
+      }
+    }
+    return dist;
+  }
 
   Blob<Dtype>* const blob_bottom_;
   Blob<Dtype>* const blob_top_;
 };
 
-#define REF_HAMMING_DIST(float_type, int_type) \
-template<> \
-int MathFunctionsTest<float_type>::ReferenceHammingDistance(const int n, \
-                                                       const float_type* x, \
-                                                       const float_type* y) { \
-  int dist = 0; \
-  int_type val; \
-  for (int i = 0; i < n; ++i) { \
-    val = static_cast<int_type>(x[i]) ^ static_cast<int_type>(y[i]); \
-    /* Count the number of set bits */ \
-    while (val) { \
-      ++dist; \
-      val &= val - 1; \
-    } \
-  } \
-  return dist; \
-}
-
-REF_HAMMING_DIST(float, uint32_t);
-REF_HAMMING_DIST(double, uint64_t);
-
 typedef ::testing::Types<float, double> Dtypes;
 TYPED_TEST_CASE(MathFunctionsTest, Dtypes);
 
@@ -79,18 +77,19 @@ TYPED_TEST(MathFunctionsTest, TestHammingDistanceCPU) {
   int n = this->blob_bottom_->count();
   const TypeParam* x = this->blob_bottom_->cpu_data();
   const TypeParam* y = this->blob_top_->cpu_data();
-  CHECK_EQ(this->ReferenceHammingDistance(n, x, y),
-           caffe_cpu_hamming_distance<TypeParam>(n, x, y));
+  EXPECT_EQ(this->ReferenceHammingDistance(n, x, y),
+            caffe_cpu_hamming_distance<TypeParam>(n, x, y));
 }
 
 TYPED_TEST(MathFunctionsTest, TestHammingDistanceGPU) {
   int n = this->blob_bottom_->count();
-  const TypeParam* x = this->blob_bottom_->gpu_data();
-  const TypeParam* y = this->blob_top_->gpu_data();
-  const TypeParam* cpu_x = this->blob_bottom_->cpu_data();
-  const TypeParam* cpu_y = this->blob_top_->cpu_data();
-  CHECK_EQ(this->ReferenceHammingDistance(n, cpu_x, cpu_y),
-           caffe_gpu_hamming_distance<TypeParam>(n, x, y));
+  const TypeParam* x = this->blob_bottom_->cpu_data();
+  const TypeParam* y = this->blob_top_->cpu_data();
+  int reference_distance = this->ReferenceHammingDistance(n, x, y);
+  x = this->blob_bottom_->gpu_data();
+  y = this->blob_top_->gpu_data();
+  int computed_distance = caffe_gpu_hamming_distance<TypeParam>(n, x, y);
+  EXPECT_EQ(reference_distance, computed_distance);
 }
 
 TYPED_TEST(MathFunctionsTest, TestAsumCPU) {
@@ -101,7 +100,7 @@ TYPED_TEST(MathFunctionsTest, TestAsumCPU) {
     std_asum += std::fabs(x[i]);
   }
   TypeParam cpu_asum = caffe_cpu_asum<TypeParam>(n, x);
-  CHECK_LT((cpu_asum - std_asum) / std_asum, 1e-2);
+  EXPECT_LT((cpu_asum - std_asum) / std_asum, 1e-2);
 }
 
 TYPED_TEST(MathFunctionsTest, TestAsumGPU) {
@@ -113,7 +112,7 @@ TYPED_TEST(MathFunctionsTest, TestAsumGPU) {
   }
   TypeParam gpu_asum;
   caffe_gpu_asum<TypeParam>(n, this->blob_bottom_->gpu_data(), &gpu_asum);
-  CHECK_LT((gpu_asum - std_asum) / std_asum, 1e-2);
+  EXPECT_LT((gpu_asum - std_asum) / std_asum, 1e-2);
 }
 
 TYPED_TEST(MathFunctionsTest, TestSignCPU) {
@@ -122,7 +121,7 @@ TYPED_TEST(MathFunctionsTest, TestSignCPU) {
   caffe_cpu_sign<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
   const TypeParam* signs = this->blob_bottom_->cpu_diff();
   for (int i = 0; i < n; ++i) {
-    CHECK_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
+    EXPECT_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
   }
 }
 
@@ -133,7 +132,7 @@ TYPED_TEST(MathFunctionsTest, TestSignGPU) {
   const TypeParam* signs = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
   for (int i = 0; i < n; ++i) {
-    CHECK_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
+    EXPECT_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
   }
 }
 
@@ -143,7 +142,7 @@ TYPED_TEST(MathFunctionsTest, TestSgnbitCPU) {
   caffe_cpu_sgnbit<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
   const TypeParam* signbits = this->blob_bottom_->cpu_diff();
   for (int i = 0; i < n; ++i) {
-    CHECK_EQ(signbits[i], x[i] < 0 ? 1 : 0);
+    EXPECT_EQ(signbits[i], x[i] < 0 ? 1 : 0);
   }
 }
 
@@ -154,7 +153,7 @@ TYPED_TEST(MathFunctionsTest, TestSgnbitGPU) {
   const TypeParam* signbits = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
   for (int i = 0; i < n; ++i) {
-    CHECK_EQ(signbits[i], x[i] < 0 ? 1 : 0);
+    EXPECT_EQ(signbits[i], x[i] < 0 ? 1 : 0);
   }
 }
 
@@ -164,7 +163,7 @@ TYPED_TEST(MathFunctionsTest, TestFabsCPU) {
   caffe_cpu_fabs<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
   const TypeParam* abs_val = this->blob_bottom_->cpu_diff();
   for (int i = 0; i < n; ++i) {
-    CHECK_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
+    EXPECT_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
   }
 }
 
@@ -175,7 +174,7 @@ TYPED_TEST(MathFunctionsTest, TestFabsGPU) {
   const TypeParam* abs_val = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
   for (int i = 0; i < n; ++i) {
-    CHECK_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
+    EXPECT_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
   }
 }
 
@@ -189,7 +188,7 @@ TYPED_TEST(MathFunctionsTest, TestScaleCPU) {
   const TypeParam* scaled = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
   for (int i = 0; i < n; ++i) {
-    CHECK_EQ(scaled[i], x[i] * alpha);
+    EXPECT_EQ(scaled[i], x[i] * alpha);
   }
 }
 
@@ -203,7 +202,7 @@ TYPED_TEST(MathFunctionsTest, TestScaleGPU) {
   const TypeParam* scaled = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
   for (int i = 0; i < n; ++i) {
-    CHECK_EQ(scaled[i], x[i] * alpha);
+    EXPECT_EQ(scaled[i], x[i] * alpha);
   }
 }