DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testLabels.cpp
1 
29 //#define TRACE_BITS
30 
31 #include <cstdio>
32 #include <cmath>
33 #include <iostream>
34 #include <algorithm>
35 #include <vector>
36 #include <bitset>
37 #include "DGtal/base/Common.h"
38 #include "DGtal/base/Labels.h"
39 
40 using namespace DGtal;
41 using namespace std;
42 
43 template <typename Container1, typename Container2>
44 bool
45 isEqual( Container1 & c1, Container2 & c2 )
46 {
47  if ( c1.size() == c2.size() )
48  {
49  for ( unsigned int i = 0; i < c1.size(); ++i )
50  {
51  if ( c1.test( i ) != c2.test( i ) )
52  return false;
53  }
54  return true;
55  }
56  return false;
57 }
58 
59 template <typename VContainer1, typename LContainer2>
60 void insert( VContainer1 & c1, LContainer2 & c2, unsigned int idx )
61 {
62  c1.set( idx );
63  c2.set( idx );
64 }
65 
66 template <typename VContainer1, typename LContainer2>
67 bool
68 checkInsert( VContainer1 & v, LContainer2 & l,
69  unsigned int nb )
70 {
71  for ( unsigned int i = 0; i < nb; ++i )
72  {
73  unsigned int idx = random() % ( l.size() );
74  insert( v, l, idx );
75  }
76  return isEqual( v, l );
77 }
78 
79 template <typename VContainer1, typename LContainer2>
80 void erase( VContainer1 & c1, LContainer2 & c2, unsigned int idx )
81 {
82  c1.reset( idx );
83  c2.reset( idx );
84 }
85 
86 template <typename VContainer1, typename LContainer2>
87 bool
88 checkErase( VContainer1 & v, LContainer2 & l,
89  unsigned int nb )
90 {
91  for ( unsigned int i = 0; i < nb; ++i )
92  {
93  unsigned int idx = random() % ( l.size() );
94  erase( v, l, idx );
95  }
96  return isEqual( v, l );
97 }
98 
99 
100 int main()
101 {
102  typedef Labels<80, uint32_t> MyLabels;
103  typedef MyLabels::ConstIterator LabelsConstIterator;
104  typedef bitset<80> MyBitset;
105 
106  BOOST_CONCEPT_ASSERT(( boost::ForwardIterator< LabelsConstIterator > ));
107 
108  unsigned int nb = 0;
109  unsigned int nbok = 0;
110  trace.beginBlock ( "Testing Labels" );
111  MyLabels l;
112  MyBitset v;
113  ++nb, nbok += isEqual( v, l ) ? 1 : 0;
114  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
115  insert( v, l, 15 );
116  insert( v, l, 4 );
117  ++nb, nbok += isEqual( v, l ) ? 1 : 0;
118  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
119  insert( v, l, 62 );
120  insert( v, l, 4 );
121  insert( v, l, 78 );
122  insert( v, l, 31 );
123  insert( v, l, 32 );
124  ++nb, nbok += isEqual( v, l ) ? 1 : 0;
125  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
126  checkInsert( v, l, 40 );
127  ++nb, nbok += isEqual( v, l ) ? 1 : 0;
128  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
129  checkErase( v, l, 200 );
130  ++nb, nbok += isEqual( v, l ) ? 1 : 0;
131  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
132  for ( LabelsConstIterator it = l.begin(), it_end = l.end();
133  it != it_end; ++it )
134  std::cout << " " << *it;
135  std::cout << std::endl;
136  trace.endBlock();
137  return ( nb == nbok ) ? 0 : 1;
138 }