Skip to content
Snippets Groups Projects
Commit 06d979b3 authored by Marco Biasini's avatar Marco Biasini
Browse files

added utility functions to check for equality of vecs and mats in unit tests

Thanks Andi!
parent 46b0d1bc
Branches
No related tags found
No related merge requests found
......@@ -23,6 +23,7 @@ composite_op.hh
composite2_op.hh
composite3_op.hh
aligned_cuboid.hh
vec_mat_predicates.hh
quat.hh
point_cloud.hh
......
//------------------------------------------------------------------------------
// This file is part of the OpenStructure project <www.openstructure.org>
//
// Copyright (C) 2008-2011 by the OpenStructure authors
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3.0 of the License, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//------------------------------------------------------------------------------
#ifndef OST_GEOM_VEC_MAT_PREDICATES_HH
#define OST_GEOM_VEC_MAT_PREDICATES_HH
#include <boost/test/unit_test.hpp>
#include <ost/geom/geom.hh>
/*
This file contains several predicates to check for equality of vectors and
matrices. Usage:
BOOST_CHECK(vec2_is_close(geom::Vec2(1,2), geom::Vec2(1, 2)));
*/
template<class V>
boost::test_tools::predicate_result vec_is_close(const V& v1, const V& v2,
Real tolerance,
unsigned int dim)
{
std::string labels[]={"x","y","z","w"};
bool flag=true;
boost::test_tools::predicate_result res( false );
boost::test_tools::close_at_tolerance<Real> close_test(::boost::test_tools::percent_tolerance(tolerance));
for(unsigned int i=0;i<dim;++i){
if(v1[i]==0.0){
if(!boost::test_tools::check_is_small(v2[i],tolerance)){
flag=false;
res.message() << "Value for " << labels[i] << " differs: ( " << v1[i] << " != " << v2[i] << " ). ";
}
}else if (v2[i]==0.0){
if(!boost::test_tools::check_is_small(v1[i],tolerance)){
flag=false;
res.message() << "Value for "<< labels[i] << " differs: ( " << v1[i] << " != " << v2[i] << " ). ";
}
}else{
if(!close_test(v1[i],v2[i])){
flag=false;
res.message() << "Value for " << labels[i] << " differs: ( " << v1[i] << " != " << v2[i] << " ). ";
}
}
}
if(flag){
return true;
}else{
return res;
}
}
boost::test_tools::predicate_result vec2_is_close(const geom::Vec2& v1,
const geom::Vec2& v2,
Real tolerance=geom::EPSILON)
{
return vec_is_close<geom::Vec2>(v1,v2,tolerance,2);
}
boost::test_tools::predicate_result vec3_is_close(const geom::Vec3& v1,
const geom::Vec3& v2,
Real tolerance=geom::EPSILON)
{
return vec_is_close<geom::Vec3>(v1,v2,tolerance,3);
}
boost::test_tools::predicate_result vec4_is_close(const geom::Vec4& v1,
const geom::Vec4& v2,
Real tolerance=geom::EPSILON)
{
return vec_is_close<geom::Vec4>(v1,v2,tolerance,4);
}
template<class M>
boost::test_tools::predicate_result mat_is_close(const M& m1, const M& m2,
Real tolerance,
unsigned int dim)
{
bool flag=true;
boost::test_tools::predicate_result res( false );
boost::test_tools::close_at_tolerance<Real> close_test(::boost::test_tools::percent_tolerance(tolerance));
for(unsigned int i=0;i<dim;++i){
for(unsigned int j=0;j<dim;++j){
if(m1(i,j)==0.0){
if(!boost::test_tools::check_is_small(m2(i,j),tolerance)){
flag=false;
res.message() << "Value for (" << i << "," << j << ") differs: (" << m1(i,j) << "!=" << m2(i,j) << "). ";
}
}else if (m2(i,j)==0.0){
if(!boost::test_tools::check_is_small(m1(i,j),tolerance)){
flag=false;
res.message() << "Value for (" << i << "," << j << ") differs: (" << m1(i,j) << "!=" << m2(i,j) << "). ";
}
}else{
if(!close_test(m1(i,j),m2(i,j))){
flag=false;
res.message() << "Value for (" << i << "," << j << ") differs: (" << m1(i,j) << "!=" << m2(i,j) << "). ";
}
}
}
}
if(flag){
return true;
}else{
return res;
}
}
boost::test_tools::predicate_result mat2_is_close(const geom::Mat2& m1,
const geom::Mat2& m2,
Real tolerance=geom::EPSILON)
{
return mat_is_close<geom::Mat2>(m1,m2,tolerance,2);
}
boost::test_tools::predicate_result mat3_is_close(const geom::Mat3& m1,
const geom::Mat3& m2,
Real tolerance=geom::EPSILON)
{
return mat_is_close<geom::Mat3>(m1,m2,tolerance,3);
}
boost::test_tools::predicate_result mat4_is_close(const geom::Mat4& m1,
const geom::Mat4& m2,
Real tolerance=geom::EPSILON)
{
return mat_is_close<geom::Mat4>(m1,m2,tolerance,4);
}
#endif
......@@ -20,6 +20,7 @@
* Authors: Marco Biasini, Juergen Haas
*/
#include <ost/geom/vec_mat_predicates.hh>
#include <ost/mol/chem_class.hh>
#include <ost/mol/mol.hh>
#include <cmath>
......@@ -27,20 +28,13 @@
#include <boost/test/unit_test.hpp>
#define CHECK_TRANSFORMED_ATOM_POSITION(ATOM,TARGET) \
BOOST_CHECK_SMALL(static_cast<double>(std::fabs(ATOM.GetPos()[0]-TARGET[0])),0.000001); \
BOOST_CHECK_SMALL(static_cast<double>(std::fabs(ATOM.GetPos()[1]-TARGET[1])),0.000001); \
BOOST_CHECK_SMALL(static_cast<double>(std::fabs(ATOM.GetPos()[2]-TARGET[2])),0.000001);
BOOST_CHECK(vec3_is_close(ATOM.GetPos(), TARGET))
#define CHECK_ORIGINAL_ATOM_POSITION(ATOM,TARGET) \
BOOST_CHECK_SMALL(static_cast<double>(std::fabs(ATOM.GetOriginalPos()[0]-TARGET[0])),0.000001); \
BOOST_CHECK_SMALL(static_cast<double>(std::fabs(ATOM.GetOriginalPos()[1]-TARGET[1])),0.000001); \
BOOST_CHECK_SMALL(static_cast<double>(std::fabs(ATOM.GetOriginalPos()[2]-TARGET[2])),0.000001);
BOOST_CHECK(vec3_is_close(ATOM.GetOriginalPos(), TARGET))
#define CHECK_ALTERNATE_ATOM_POSITION(ATOM,TARGET,GROUP) \
BOOST_CHECK_SMALL(static_cast<double>(std::fabs(ATOM.GetAltPos(GROUP)[0]-TARGET[0])),0.000001); \
BOOST_CHECK_SMALL(static_cast<double>(std::fabs(ATOM.GetAltPos(GROUP)[1]-TARGET[1])),0.000001); \
BOOST_CHECK_SMALL(static_cast<double>(std::fabs(ATOM.GetAltPos(GROUP)[2]-TARGET[2])),0.000001);
BOOST_CHECK(vec3_is_close(ATOM.GetAltPos(GROUP), TARGET))
using namespace ost;
using namespace ost::mol;
......
......@@ -48,7 +48,6 @@ BOOST_AUTO_TEST_CASE(test_in_sequence)
BOOST_AUTO_TEST_CASE(test_res_index_bzdng227)
{
std::cout << "HERE" << std::endl;
EntityHandle eh=CreateEntity();
XCSEditor e=eh.EditXCS();
ChainHandle ch1=e.InsertChain("A");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment