DGtal  0.9.3
Functions
testPattern.cpp File Reference
#include <cstdlib>
#include <iostream>
#include <map>
#include "DGtal/base/Common.h"
#include "DGtal/kernel/CPointPredicate.h"
#include "DGtal/arithmetic/IntegerComputer.h"
#include "DGtal/arithmetic/SternBrocot.h"
#include "DGtal/arithmetic/LightSternBrocot.h"
#include "DGtal/arithmetic/LighterSternBrocot.h"
#include "DGtal/arithmetic/Pattern.h"
#include <boost/version.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_smallint.hpp>
#include <boost/random/variate_generator.hpp>
Include dependency graph for testPattern.cpp:

Go to the source code of this file.

Functions

template<typename Fraction1 , typename Fraction2 >
bool testPattern (Fraction1 f1)
 
template<typename Fraction1 , typename Fraction2 >
bool testPatterns ()
 
int main (int, char **)
 

Detailed Description

This program 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 of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2012/03/05

Functions for testing class LightSternBrocot.

This file is part of the DGtal library.

Definition in file testPattern.cpp.

Function Documentation

◆ main()

int main ( int  ,
char **   
)

Definition at line 127 of file testPattern.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), and DGtal::trace.

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
std::ostream & emphase()
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

◆ testPattern()

template<typename Fraction1 , typename Fraction2 >
bool testPattern ( Fraction1  f1)

Definition at line 57 of file testPattern.cpp.

References DGtal::Pattern< TFraction >::selfDisplay().

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 }
void selfDisplay(std::ostream &out) const
Aim: This class represents a pattern, i.e. the path between two consecutive upper leaning points on a...
Definition: Pattern.h:78

◆ testPatterns()

template<typename Fraction1 , typename Fraction2 >
bool testPatterns ( )

Definition at line 84 of file testPattern.cpp.

References DGtal::IntegerComputer< TInteger >::gcd().

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 }
Aim: This class gathers several types and methods to make computation with integers.
Integer gcd(IntegerParamType a, IntegerParamType b) const