DGtal  1.3.beta
testPattern.cpp
Go to the documentation of this file.
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
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;
92  nbok += testPattern<Fraction1,Fraction2>( f1 ) ? 1 : 0;
93  std::cerr << "(" << nbok << "/" << nb << ") f1=";
94  f1.selfDisplay( std::cerr );
95  std::cerr << endl;
96  f1 = Fraction1( 31, 24 );
97  ++nb;
98  nbok += testPattern<Fraction1,Fraction2>( f1 ) ? 1 : 0;
99  std::cerr << "(" << nbok << "/" << nb << ") f1=";
100  f1.selfDisplay( std::cerr );
101  std::cerr << endl;
102  f1 = Fraction1( 5, 13 );
103  ++nb;
104  nbok += testPattern<Fraction1,Fraction2>( f1 ) ? 1 : 0;
105  std::cerr << "(" << nbok << "/" << nb << ") f1=";
106  f1.selfDisplay( std::cerr );
107  std::cerr << endl;
109 
110  for ( unsigned int i = 0; i < nbtests; ++i )
111  {
112  unsigned int p = 1 + std::rand() % 999;
113  unsigned int q = 1 + std::rand() % 999;
114  unsigned int g = ic.gcd( p, q );
115  p /= g; q /= g;
116  std::cerr << "*- p / q = " << p << "/" << q << std::endl;
117  f1 = Fraction1( p, q );
118  ++nb;
119  nbok += testPattern<Fraction1,Fraction2>( f1 ) ? 1 : 0;
120  std::cerr << "(" << nbok << "/" << nb << ") f1=";
121  f1.selfDisplay( std::cerr );
122  std::cerr << endl;
123  }
124  return nbok == nb;
125 }
126 
127 
129 // Standard services - public :
130 int main( int , char** )
131 {
135  typedef SB::Fraction Fraction;
136  typedef LSB::Fraction LFraction;
137  typedef LLSB::Fraction LLFraction;
138  trace.beginBlock ( "Testing class Pattern" );
139  bool res = testPatterns<Fraction, LFraction>()
140  && testPatterns<Fraction, LLFraction>()
141  && testPatterns<LLFraction, LFraction>();
142  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
143  trace.endBlock();
144 
145  return res ? 0 : 1;
146 }
147 // //
149 
DGtal::Trace::endBlock
double endBlock()
main
int main(int, char **)
Definition: testPattern.cpp:130
DGtal::IntegerComputer
Aim: This class gathers several types and methods to make computation with integers.
Definition: IntegerComputer.h:82
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::trace
Trace trace
Definition: Common.h:154
testPatterns
bool testPatterns()
Definition: testPattern.cpp:84
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
testPattern
bool testPattern(Fraction1 f1)
Definition: testPattern.cpp:57
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::SternBrocot
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Definition: SternBrocot.h:77
DGtal::LightSternBrocot
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Definition: LightSternBrocot.h:107
DGtal::Pattern
Aim: This class represents a pattern, i.e. the path between two consecutive upper leaning points on a...
Definition: Pattern.h:78
DGtal::LighterSternBrocot
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Definition: LighterSternBrocot.h:108
DGtal::IntegerComputer::gcd
Integer gcd(IntegerParamType a, IntegerParamType b) const