DGtal  1.0.0
Functions
testLabelledMap.cpp File Reference
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include "DGtal/base/Common.h"
#include "DGtal/base/LabelledMap.h"
Include dependency graph for testLabelledMap.cpp:

Go to the source code of this file.

Functions

template<typename Container1 , typename Container2 >
bool isEqual (Container1 &c1, Container2 &c2)
 
template<typename VContainer1 , typename LContainer2 >
void insert (VContainer1 &c1, LContainer2 &c2, unsigned int idx, double v)
 
template<typename VContainer1 , typename LContainer2 >
bool checkInsert (VContainer1 &v, LContainer2 &l, unsigned int nb)
 
template<typename VContainer1 , typename LContainer2 >
void erase (VContainer1 &c1, LContainer2 &c2, unsigned int idx)
 
template<typename VContainer1 , typename LContainer2 >
bool checkErase (VContainer1 &v, LContainer2 &l, unsigned int nb)
 
template<typename AContainer >
void display (ostream &out, const AContainer &C)
 
int main ()
 

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 5807), University of Savoie, France
Date
2012/07/02

This file is part of the DGtal library

Definition in file testLabelledMap.cpp.

Function Documentation

◆ checkErase()

template<typename VContainer1 , typename LContainer2 >
bool checkErase ( VContainer1 &  v,
LContainer2 &  l,
unsigned int  nb 
)

Definition at line 81 of file testLabelledMap.cpp.

83 {
84  for ( unsigned int i = 0; i < nb; ++i )
85  {
86  unsigned int idx = rand() % ( l.max_size() );
87  erase( v, l, idx );
88  //std::cout << " (" << i << "/" << nb << ") l=" << l << std::endl;
89  }
90  return isEqual( v, l );
91 }
void erase(VContainer1 &c1, LContainer2 &c2, unsigned int idx)
bool isEqual(Container1 &c1, Container2 &c2)

References erase(), and isEqual().

Referenced by main().

◆ checkInsert()

template<typename VContainer1 , typename LContainer2 >
bool checkInsert ( VContainer1 &  v,
LContainer2 &  l,
unsigned int  nb 
)

Definition at line 60 of file testLabelledMap.cpp.

62 {
63  for ( unsigned int i = 0; i < nb; ++i )
64  {
65  unsigned int idx = rand() % ( l.max_size() );
66  double val = ( (double)rand() ) / RAND_MAX;
67  insert( v, l, idx, val );
68  }
69  return isEqual( v, l );
70 }
void insert(VContainer1 &c1, LContainer2 &c2, unsigned int idx, double v)
bool isEqual(Container1 &c1, Container2 &c2)

References insert(), and isEqual().

Referenced by main().

◆ display()

template<typename AContainer >
void display ( ostream &  out,
const AContainer &  C 
)

Definition at line 94 of file testLabelledMap.cpp.

95 {
96  out << "C = ";
97  for ( typename AContainer::const_iterator it = C.begin(), it_end = C.end();
98  it != it_end; ++it )
99  {
100  out << " (" << (*it).first << "," << (*it).second << ")";
101  }
102  out << std::endl;
103 }

Referenced by main(), testInitFraction(), and testReducedFraction().

◆ erase()

template<typename VContainer1 , typename LContainer2 >
void erase ( VContainer1 &  c1,
LContainer2 &  c2,
unsigned int  idx 
)

Definition at line 73 of file testLabelledMap.cpp.

74 {
75  c1.erase( idx );
76  c2.erase( idx );
77 }

Referenced by checkErase().

◆ insert()

template<typename VContainer1 , typename LContainer2 >
void insert ( VContainer1 &  c1,
LContainer2 &  c2,
unsigned int  idx,
double  v 
)

Definition at line 52 of file testLabelledMap.cpp.

53 {
54  c1.insert( std::make_pair(idx, v) );
55  c2.insert( std::make_pair(idx, v) );
56 }

Referenced by checkInsert(), and main().

◆ isEqual()

template<typename Container1 , typename Container2 >
bool isEqual ( Container1 &  c1,
Container2 &  c2 
)

Definition at line 45 of file testLabelledMap.cpp.

46 {
47  return ( c1.size() == c2.size() )
48  && std::equal( c1.begin(), c1.end(), c2.begin() );
49 }

Referenced by checkErase(), checkInsert(), and main().

◆ main()

int main ( )

Definition at line 104 of file testLabelledMap.cpp.

105 {
107  BOOST_CONCEPT_ASSERT(( boost::AssociativeContainer< MyLabelledMap > ));
108  BOOST_CONCEPT_ASSERT(( boost::PairAssociativeContainer< MyLabelledMap > ));
109  BOOST_CONCEPT_ASSERT(( boost::UniqueAssociativeContainer< MyLabelledMap > ));
110  // BOOST_CONCEPT_ASSERT(( boost::ForwardIterator< MyIndexedList::Iterator > ));
111  // BOOST_CONCEPT_ASSERT(( boost::ForwardIterator< MyIndexedList::ConstIterator > ));
112  unsigned int nb = 0;
113  unsigned int nbok = 0;
114  trace.beginBlock ( "Testing LabelledMap" );
115  MyLabelledMap l;
116  map<unsigned int, double> v;
117  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
118  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
119  insert( v, l, 3, 4.5 );
120  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
121  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
122  insert( v, l, 0, 10.1 );
123  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
124  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
125  insert( v, l, 1, 3.7 );
126  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
127  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
128  insert( v, l, 2, 8.4 );
129  insert( v, l, 1, 2.1 );
130  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
131  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
132  display( std::cout, v );
133  insert( v, l, 1, -3.0 );
134  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
135  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
136  insert( v, l, 15, -13.1 );
137  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
138  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
139  insert( v, l, 2, -7.1 );
140  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
141  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
142  // MyLabelledMap::Iterator it = l.insert( l.begin(), std::make_pair( 7, 4.4 ) );
143  // std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
144  // it = l.insert( it, std::make_pair( 9, 5.5 ) );
145  // l.insert( it, std::make_pair( 9, 10.5 ) );
146  // std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
147  ++nb; nbok += checkInsert( v, l, 1000 ) ? 1 : 0;
148  std::cout << "(" << nbok << "/" << nb << ") 1000 insertions l=" << l << std::endl;
149  ++nb; nbok += checkErase( v, l, 1000 ) ? 1 : 0;
150  std::cout << "(" << nbok << "/" << nb << ") 1000 deletions l=" << l << std::endl;
151  trace.endBlock();
152  trace.beginBlock ( "Testing LabelledMap" );
153  ++nb; nbok += checkInsert( v, l, 10 ) ? 1 : 0;
154  std::cout << "(" << nbok << "/" << nb << ") 10 deletions l=" << l << std::endl;
155  std::pair< MyLabelledMap::Iterator,
156  MyLabelledMap::Iterator > pair1 = l.equal_range( 7 );
157  std::cout << "Range(7)=[" << (*pair1.first).first << "," << (*pair1.second).first << ")" << std::endl;
158  ++nb; nbok += ( pair1.first == l.lower_bound( 7 ) ) ? 1 : 0;
159  ++nb; nbok += ( pair1.second == l.upper_bound( 7 ) ) ? 1 : 0;
160  std::cout << "(" << nbok << "/" << nb << ") equal_range, lower_bound." << std::endl;
161 
162  trace.endBlock();
163 
164  // Test related to pull request #973 about copy constructor & operator when using at less 3 blocks.
165  typedef LabelledMap<double, 32, DGtal::uint16_t, 2, 3> MyOtherLabelledMap;
166  trace.beginBlock ( "Testing LabelledMap copy constructor and copy operator" );
167  MyOtherLabelledMap ll;
168 
169  for ( unsigned int size = 0; size <= 2 + 3 + 2; ++size )
170  {
171  for (unsigned int i = 0; i < size; ++i)
172  ll[i] = i;
173 
174  MyOtherLabelledMap ll_ccopy(ll);
175  MyOtherLabelledMap ll_ocopy; ll_ocopy = ll;
176 
177  for (unsigned int i = 0; i < size; ++i)
178  ll[i] = 10*i+1;
179 
180  bool csuccess = true;
181  bool osuccess = true;
182  for (unsigned int i = 0; i < size; ++i)
183  {
184  csuccess &= ll_ccopy[i] == i;
185  osuccess &= ll_ocopy[i] == i;
186  }
187 
188  ++nb; nbok += csuccess ? 1 : 0;
189  std::cout << "(" << nbok << "/" << nb << ") ll_copy_constructed=" << ll_ccopy << std::endl;
190 
191  ++nb; nbok += osuccess ? 1 : 0;
192  std::cout << "(" << nbok << "/" << nb << ") ll_copied=" << ll_ocopy << std::endl;
193  }
194 
195  trace.endBlock();
196 
197  return ( nb == nbok ) ? 0 : 1;
198 }
void beginBlock(const std::string &keyword="")
void insert(VContainer1 &c1, LContainer2 &c2, unsigned int idx, double v)
Trace trace
Definition: Common.h:144
Go to http://www.sgi.com/tech/stl/PairAssociativeContainer.html.
Definition: Boost.dox:149
Aim: Represents a map label -> data, where the label is an integer between 0 and a constant L-1....
Definition: LabelledMap.h:119
double endBlock()
bool checkInsert(VContainer1 &v, LContainer2 &l, unsigned int nb)
void display(ostream &out, const AContainer &C)
bool isEqual(Container1 &c1, Container2 &c2)
bool checkErase(VContainer1 &v, LContainer2 &l, unsigned int nb)
Go to http://www.sgi.com/tech/stl/AssociativeContainer.html.
Definition: Boost.dox:137
Go to http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html.
Definition: Boost.dox:140

References DGtal::Trace::beginBlock(), checkErase(), checkInsert(), display(), DGtal::Trace::endBlock(), insert(), isEqual(), and DGtal::trace.