generic_fixture.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include "interop/util/assert.h"
12 #include "interop/util/math.h"
13 
14 namespace illumina{ namespace interop { namespace unittest {
15 
18  template<class T>
22  template<class T>
24  {
27  public:
34  abstract_generator(const int test_modifier=0) : m_test_modifier(test_modifier){}
36  virtual ~abstract_generator(){}
44  virtual ::testing::AssertionResult generate(T& expected, T& actual, bool* skip_test)const=0;
49  virtual bool advance()
50  {
51  return true;
52  }
53 
58  virtual parent_type clone()const=0;
63  int test_modifier()const{return m_test_modifier;}
68  virtual void write(std::ostream& out)const=0;
73  std::string info()const
74  {
75  std::ostringstream oss;
76  write(oss);
77  return oss.str();
78  }
85  friend std::ostream& operator<<(std::ostream& out, const abstract_generator<T>& gen)
86  {
87  gen.write(out);
88  return out;
89  }
90 
91  private:
92  int m_test_modifier;
93  };
94 
99  template<class T, class Fixture>
101  {
102  typedef typename Fixture::parameter_type parameter_type;
104  public:
109  standard_parameter_generator(const parameter_type parameter) : m_fixture(parameter)
110  {
111  }
117  base_type operator()(const parameter_type& parameter)const
118  {
119  return new standard_parameter_generator(parameter);
120  }
121 
127  ::testing::AssertionResult generate(T& expected, T &actual, bool*) const
128  {
129  return m_fixture.generate(expected, actual);
130  }
131 
136  base_type clone() const
137  {
138  return new standard_parameter_generator(*this);
139  }
140 
145  void write(std::ostream &out) const
146  {
147  m_fixture.write(out);
148  }
149 
150  protected:
152  Fixture m_fixture;
153  };
158  template<class T, class Fixture>
160  {
161  typedef typename Fixture::parameter_type parameter_type;
162  public:
167  iterator_parameter_generator(const parameter_type parameter) :
168  standard_parameter_generator<T, Fixture>(parameter)
169  {
170  }
175  bool advance()
176  {
178  }
179  };
180 
185  template<class T>
186  class generator_ptr
187  {
188  public:
193  generator_ptr(abstract_generator<T>* ptr=0) : m_ptr(ptr){}
198  generator_ptr(const generator_ptr<T>& other) : m_ptr(0)
199  {
200  if(other.m_ptr != 0)
201  {
202  generator_ptr<T> tmp = other.m_ptr->clone();
203  m_ptr = tmp.m_ptr;
204  tmp.m_ptr=0;
205  }
206  }
213  {
214  delete m_ptr;
215  INTEROP_ASSERT(other.m_ptr != 0);
216 
217  if(other.m_ptr != 0)
218  {
219  generator_ptr<T> tmp = other.m_ptr->clone();
220  m_ptr = tmp.m_ptr;
221  tmp.m_ptr=0;
222  }
223  return *this;
224  }
231  {
232  delete m_ptr;
233  if(ptr != 0) m_ptr = ptr;
234  return *this;
235  }
237  virtual ~generator_ptr(){delete m_ptr;}
238 
244  {
245  INTEROP_ASSERT(m_ptr != 0);
246  return *m_ptr;
247  }
253  {
254  INTEROP_ASSERT(m_ptr != 0);
255  return *m_ptr;
256  }
262  {
263  INTEROP_ASSERT(m_ptr != 0);
264  return m_ptr;
265  }
271  {
272  INTEROP_ASSERT(m_ptr != 0);
273  return m_ptr;
274  }
281  {
282  return m_ptr == other;
283  }
290  {
291  return m_ptr != other;
292  }
299  friend std::ostream& operator<<(std::ostream& out, const generator_ptr<T>& ptr)
300  {
301  INTEROP_ASSERT(ptr.m_ptr != 0);
302  if(ptr.m_ptr == 0) return out;
303  ptr.m_ptr->write(out);
304  return out;
305  }
306  private:
307  abstract_generator<T>* m_ptr;
308  };
314  template<class T>
316  {
317  return generator_ptr<T>(ptr);
318  }
319 
320 
326  template<class T>
327  struct generic_test_fixture : public ::testing::TestWithParam< generator_ptr<T> >
328  {
329  public:
331  typedef ::testing::TestWithParam< generator_ptr<T> > parent_type;
332  public:
333  //typedef abstract_generator<T>* generator_type;
336  typedef T value_type;
337 
341  fixture_test_result(parent_type::GetParam()->generate(expected, actual, &skip_test))
342  {
344  std::cout << fixture_test_result.message() << std::endl;
345  test_modifier = parent_type::GetParam()->test_modifier();
346  }
352  bool skip_test;
353  ::testing::Message msg;
357  const ::testing::AssertionResult fixture_test_result;
358  };
359 
367  inline bool is_float_near(const float expected, const float actual, const float tol)
368  {
369  if(std::isnan(expected) && std::isnan(actual)) return true;
370  if(std::isnan(expected) || std::isnan(actual)) return false;
371  if(std::isinf(expected) && std::isinf(actual)) return true;
372  if(std::isinf(expected) || std::isinf(actual)) return false;
373  return std::abs(expected-actual) < tol;
374  }
375 
385  inline ::testing::AssertionResult AreFloatsNear(const float expected, const float actual, const float tol)
386  {
387  if(is_float_near(expected, actual, tol)) return ::testing::AssertionSuccess();
388  return ::testing::AssertionFailure() << "Abs(" << expected << " - " << actual << ") >= " << tol;
389  }
390 
400  inline ::testing::AssertionResult AreValuesNear(const std::vector<float>& expected,
401  const std::vector<float>& actual,
402  const float tol)
403  {
404  ::testing::Message msg;
405  bool test_failed = false;
406  if( expected.size() != actual.size() )
407  {
408  return ::testing::AssertionFailure() << "Expected size: " << expected.size() << " != actual: " << actual.size();
409  }
410  for(size_t i=0;i<expected.size();++i)
411  {
412  if(!is_float_near(expected[i], actual[i], tol))
413  {
414  if(test_failed) msg << " | ";
415  msg << "Value("<< i << ") Expected: " << expected[i] << " == Actual: " << actual[i];
416  test_failed=true;
417  }
418  }
419  if(test_failed) return ::testing::AssertionFailure(msg << " Tol: " << tol);
420  return ::testing::AssertionSuccess();
421  }
422 
423 
424 }}}
425 #define INTEROP_ASSERT_NEAR(EXPECTED, ACTUAL, TOL) ASSERT_TRUE(::illumina::interop::unittest::AreFloatsNear(EXPECTED, ACTUAL, TOL))
426 #define INTEROP_EXPECT_NEAR(EXPECTED, ACTUAL, TOL) EXPECT_TRUE(::illumina::interop::unittest::AreFloatsNear(EXPECTED, ACTUAL, TOL))
427 #define INTEROP_EXPECT_ARRAY_NEAR(EXPECTED, ACTUAL, TOL) EXPECT_TRUE(::illumina::interop::unittest::AreValuesNear(EXPECTED, ACTUAL, TOL))
428 
generator_ptr(abstract_generator< T > *ptr=0)
Definition: generic_fixture.h:193
standard_parameter_generator(const parameter_type parameter)
Definition: generic_fixture.h:109
Definition: generic_fixture.h:327
generic_test_fixture()
Definition: generic_fixture.h:340
base_type clone() const
Definition: generic_fixture.h:136
bool operator==(const abstract_generator< T > *other)
Definition: generic_fixture.h:280
void write(std::ostream &out) const
Definition: generic_fixture.h:145
Definition: enum_description.h:15
generator_ptr & operator=(const generator_ptr< T > &other)
Definition: generic_fixture.h:212
virtual parent_type clone() const =0
abstract_generator(const int test_modifier=0)
Definition: generic_fixture.h:34
virtual void write(std::ostream &out) const =0
T expected
Definition: generic_fixture.h:348
virtual bool advance()
Definition: generic_fixture.h:49
Definition: generic_fixture.h:23
Fixture m_fixture
Definition: generic_fixture.h:152
const ::testing::AssertionResult fixture_test_result
Definition: generic_fixture.h:357
::testing::Message msg
Definition: generic_fixture.h:353
inline::testing::AssertionResult AreValuesNear(const std::vector< float > &expected, const std::vector< float > &actual, const float tol)
Definition: generic_fixture.h:400
T value_type
Definition: generic_fixture.h:336
base_type operator()(const parameter_type &parameter) const
Definition: generic_fixture.h:117
T actual
Definition: generic_fixture.h:350
virtual ~generator_ptr()
Definition: generic_fixture.h:237
const abstract_generator< T > & operator*() const
Definition: generic_fixture.h:243
virtual ~abstract_generator()
Definition: generic_fixture.h:36
::testing::AssertionResult generate(T &expected, T &actual, bool *) const
Definition: generic_fixture.h:127
generator_ptr(const generator_ptr< T > &other)
Definition: generic_fixture.h:198
inline::testing::AssertionResult AreFloatsNear(const float expected, const float actual, const float tol)
Definition: generic_fixture.h:385
virtual ::testing::AssertionResult generate(T &expected, T &actual, bool *skip_test) const =0
abstract_generator< T > * operator->()
Definition: generic_fixture.h:270
#define INTEROP_ASSERT(TST)
Definition: assert.h:21
generator_ptr< T > generator_type
Definition: generic_fixture.h:334
generator_ptr< T > parent_type
Definition: generic_fixture.h:29
generator_ptr< T > wrap(abstract_generator< T > *ptr)
Definition: generic_fixture.h:315
int test_modifier() const
Definition: generic_fixture.h:63
bool operator!=(const abstract_generator< T > *other)
Definition: generic_fixture.h:289
const abstract_generator< T > * operator->() const
Definition: generic_fixture.h:261
Definition: enums.h:301
Definition: generic_fixture.h:19
abstract_generator< T > & operator*()
Definition: generic_fixture.h:252
std::string info() const
Definition: generic_fixture.h:73
::testing::TestWithParam< generator_ptr< T > > parent_type
Definition: generic_fixture.h:331
int test_modifier
Definition: generic_fixture.h:355
iterator_parameter_generator(const parameter_type parameter)
Definition: generic_fixture.h:167
bool is_float_near(const float expected, const float actual, const float tol)
Definition: generic_fixture.h:367
bool skip_test
Definition: generic_fixture.h:352
bool advance()
Definition: generic_fixture.h:175
generator_ptr & operator=(abstract_generator< T > *ptr)
Definition: generic_fixture.h:230