mosesdecoder/mert/PointTest.cpp
Tetsuo Kiso 0e9b5fd9d0 Add const to return values of overloaded operators.
* This commit prevents developers from doing mistakes like:

  Point p1, p2, p4;
  if (p1 + p2 = p4) { // Bang! We actually wanted to compare
                      // the result of two points: (p1 + p2 == p4).
    // do something.
  }

See, e.g., http://www.gotw.ca/gotw/006.htm for details.

* Add more test cases according to this change.
* Move a helper function to compare floating point numbers
  to Util.h.
2012-04-12 09:16:27 +09:00

61 lines
1.3 KiB
C++

#include "Point.h"
#define BOOST_TEST_MODULE MertPoint
#include <boost/test/unit_test.hpp>
#include "Optimizer.h"
#include "Util.h"
using namespace std;
BOOST_AUTO_TEST_CASE(point_operators) {
const unsigned int dim = 5;
vector<float> init(dim);
init[0] = 1.0f;
init[1] = 1.0f;
init[2] = 0.3f;
init[3] = 0.2f;
init[4] = 0.3f;
vector<float> min(dim, 0.0f);
vector<float> max(dim, 0.0f);
Point::setdim(dim);
BOOST_REQUIRE(dim == Point::getdim());
// Test operator '+'
{
Point p1(init, min, max);
Point p2(init, min, max);
Point p3 = p1 + p2;
for (size_t i = 0; i < p3.size(); ++i) {
BOOST_CHECK(IsAlmostEqual(init[i] * 2.0f, p3[i]));
}
BOOST_CHECK_EQUAL(p3.GetScore(), kMaxFloat);
}
// Test operator '+='
{
Point p1(init, min, max);
Point p2(init, min, max);
p1 += p2;
for (size_t i = 0; i < p1.size(); ++i) {
BOOST_CHECK(IsAlmostEqual(init[i] * 2.0f, p1[i]));
}
BOOST_CHECK_EQUAL(p1.GetScore(), kMaxFloat);
}
// Test operator '*'
{
Point p1(init, min, max);
const Point p2 = p1 * 2.0;
BOOST_REQUIRE(p1.size() == p2.size());
for (size_t i = 0; i < p2.size(); ++i) {
BOOST_CHECK(IsAlmostEqual(init[i] * 2.0f, p2[i]));
}
BOOST_CHECK_EQUAL(p2.GetScore(), kMaxFloat);
}
}