DGtal 1.4.0
Loading...
Searching...
No Matches
testLabelledMap.cpp
Go to the documentation of this file.
1
29//#define TRACE_BITS
30
31#include <cstdio>
32#include <cmath>
33#include <iostream>
34#include <algorithm>
35#include <vector>
36#include <map>
37#include "DGtal/base/Common.h"
38#include "DGtal/base/LabelledMap.h"
39
40using namespace DGtal;
41using namespace std;
42
43template <typename Container1, typename Container2>
44bool
45isEqual( Container1 & c1, Container2 & c2 )
46{
47 return ( c1.size() == c2.size() )
48 && std::equal( c1.begin(), c1.end(), c2.begin() );
49}
50
51template <typename VContainer1, typename LContainer2>
52void insert( VContainer1 & c1, LContainer2 & c2, unsigned int idx, double v )
53{
54 c1.insert( std::make_pair(idx, v) );
55 c2.insert( std::make_pair(idx, v) );
56}
57
58template <typename VContainer1, typename LContainer2>
59bool
60checkInsert( VContainer1 & v, LContainer2 & l,
61 unsigned int nb )
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}
71
72template <typename VContainer1, typename LContainer2>
73void erase( VContainer1 & c1, LContainer2 & c2, unsigned int idx )
74{
75 c1.erase( idx );
76 c2.erase( idx );
77}
78
79template <typename VContainer1, typename LContainer2>
80bool
81checkErase( VContainer1 & v, LContainer2 & l,
82 unsigned int nb )
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}
92
93template <typename AContainer>
94void display( ostream & out, const AContainer & C )
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}
104int main()
105{
107 BOOST_CONCEPT_ASSERT(( boost::AssociativeContainer< MyLabelledMap > ));
108 BOOST_CONCEPT_ASSERT(( boost::PairAssociativeContainer< 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}
Aim: Represents a map label -> data, where the label is an integer between 0 and a constant L-1....
void beginBlock(const std::string &keyword="")
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition Common.h:153
STL namespace.
Go to http://www.sgi.com/tech/stl/AssociativeContainer.html.
Definition Boost.dox:137
Go to http://www.sgi.com/tech/stl/PairAssociativeContainer.html.
Definition Boost.dox:149
Go to http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html.
Definition Boost.dox:140
void erase(VContainer1 &c1, LContainer2 &c2, unsigned int idx)
bool isEqual(Container1 &c1, Container2 &c2)
bool checkInsert(VContainer1 &v, LContainer2 &l, unsigned int nb)
void insert(VContainer1 &c1, LContainer2 &c2, unsigned int idx, double v)
void display(ostream &out, const AContainer &C)
int main()
bool checkErase(VContainer1 &v, LContainer2 &l, unsigned int nb)