DGtal  0.9.2
testPattern.cpp
1 
30 #include <cstdlib>
32 #include <iostream>
33 #include <map>
34 #include "DGtal/base/Common.h"
35 #include "DGtal/kernel/CPointPredicate.h"
36 #include "DGtal/arithmetic/IntegerComputer.h"
37 #include "DGtal/arithmetic/SternBrocot.h"
38 #include "DGtal/arithmetic/LightSternBrocot.h"
39 #include "DGtal/arithmetic/LighterSternBrocot.h"
40 #include "DGtal/arithmetic/Pattern.h"
41 
42 #include <boost/version.hpp>
43 #include <boost/random/mersenne_twister.hpp>
44 #include <boost/random/uniform_smallint.hpp>
45 #include <boost/random/variate_generator.hpp>
47 
48 using namespace std;
49 using namespace DGtal;
50 
52 // Functions for testing class LightSternBrocot.
54 
55 template <typename Fraction1, typename Fraction2>
56 bool
57 testPattern( Fraction1 f1 )
58 {
59  typedef Pattern<Fraction1> Pattern1;
60  typedef Pattern<Fraction2> Pattern2;
61  Fraction2 f2( f1.p(), f1.q() );
62 
63  std::cerr << " f1 =";
64  f1.selfDisplay( std::cerr );
65  std::cerr << std::endl;
66  std::cerr << "r(1)=";
67  f1.reduced(1).selfDisplay( std::cerr );
68  std::cerr << std::endl;
69  std::cerr << "r(2)=";
70  f1.reduced(2).selfDisplay( std::cerr );
71  std::cerr << std::endl;
72 
73  Pattern1 p1( f1 );
74  Pattern2 p2( f2 );
75  std::cerr << "p1=" << p1.rE() << std::endl;
76  std::cerr << "p2=" << p2.rE() << std::endl;
77  std::cerr << "p1=" << p1.rEs( "(|)" ) << std::endl;
78  std::cerr << "p2=" << p2.rEs( "(|)" ) << std::endl;
79  return p1.rE() == p2.rE();
80 }
81 
82 template <typename Fraction1, typename Fraction2>
83 bool
84 testPatterns()
85 {
86  unsigned int nbtests = 100;
87  unsigned int nb = 0;
88  unsigned int nbok = 0;
89  Fraction1 f1;
90  f1 = Fraction1( 5, 8 );
91  ++nb, nbok += testPattern<Fraction1,Fraction2>( f1 ) ? 1 : 0;
92  std::cerr << "(" << nbok << "/" << nb << ") f1=";
93  f1.selfDisplay( std::cerr );
94  std::cerr << endl;
95  f1 = Fraction1( 31, 24 );
96  ++nb, nbok += testPattern<Fraction1,Fraction2>( f1 ) ? 1 : 0;
97  std::cerr << "(" << nbok << "/" << nb << ") f1=";
98  f1.selfDisplay( std::cerr );
99  std::cerr << endl;
100  f1 = Fraction1( 5, 13 );
101  ++nb, nbok += testPattern<Fraction1,Fraction2>( f1 ) ? 1 : 0;
102  std::cerr << "(" << nbok << "/" << nb << ") f1=";
103  f1.selfDisplay( std::cerr );
104  std::cerr << endl;
106 
107  for ( unsigned int i = 0; i < nbtests; ++i )
108  {
109  unsigned int p = std::rand() % 999+1;
110  unsigned int q = std::rand() % 999+1;
111  unsigned int g = ic.gcd( p, q );
112  p /= g; q /= g;
113  std::cerr << "*- p / q = " << p << "/" << q << std::endl;
114  f1 = Fraction1( p, q );
115  ++nb;
116  nbok += testPattern<Fraction1,Fraction2>( f1 ) ? 1 : 0;
117  std::cerr << "(" << nbok << "/" << nb << ") f1=";
118  f1.selfDisplay( std::cerr );
119  std::cerr << endl;
120  }
121  return nbok == nb;
122 }
123 
124 
126 // Standard services - public :
127 int main( int , char** )
128 {
132  typedef SB::Fraction Fraction;
133  typedef LSB::Fraction LFraction;
134  typedef LLSB::Fraction LLFraction;
135  trace.beginBlock ( "Testing class Pattern" );
136  bool res = testPatterns<Fraction, LFraction>()
137  && testPatterns<Fraction, LLFraction>()
138  && testPatterns<LLFraction, LFraction>();
139  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
140  trace.endBlock();
141 
142  return res ? 0 : 1;
143 }
144 // //
146 
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:130
STL namespace.
double endBlock()
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Aim: This class represents a pattern, i.e. the path between two consecutive upper leaning points on a...
Definition: Pattern.h:78
Aim: This class gathers several types and methods to make computation with integers.
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
Integer gcd(IntegerParamType a, IntegerParamType b) const
void selfDisplay(std::ostream &out) const
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Definition: SternBrocot.h:77