DGtal 1.3.0
Loading...
Searching...
No Matches
testLabels.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 <bitset>
37#include "DGtal/base/Common.h"
38#include "DGtal/base/Labels.h"
39
40using namespace DGtal;
41
42
43template <typename Container1, typename Container2>
44bool
45isEqual( 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
59template <typename VContainer1, typename LContainer2>
60void insert( VContainer1 & c1, LContainer2 & c2, unsigned int idx )
61{
62 c1.set( idx );
63 c2.set( idx );
64}
65
66template <typename VContainer1, typename LContainer2>
67bool
68checkInsert( VContainer1 & v, LContainer2 & l,
69 unsigned int nb )
70{
71 for ( unsigned int i = 0; i < nb; ++i )
72 {
73 unsigned int idx = rand() % ( l.size() );
74 insert( v, l, idx );
75 }
76 return isEqual( v, l );
77}
78
79template <typename VContainer1, typename LContainer2>
80void erase( VContainer1 & c1, LContainer2 & c2, unsigned int idx )
81{
82 c1.reset( idx );
83 c2.reset( idx );
84}
85
86template <typename VContainer1, typename LContainer2>
87bool
88checkErase( VContainer1 & v, LContainer2 & l,
89 unsigned int nb )
90{
91 for ( unsigned int i = 0; i < nb; ++i )
92 {
93 unsigned int idx = rand() % ( l.size() );
94 erase( v, l, idx );
95 }
96 return isEqual( v, l );
97}
98
99
100int main()
101{
102 typedef Labels<80, DGtal::uint32_t> MyLabels;
103 typedef MyLabels::ConstIterator LabelsConstIterator;
104 typedef std::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
137 trace.endBlock();
138
139 // Test related to pull request #971 & #972
140 typedef Labels<32, DGtal::uint32_t> MySmallLabels;
141 typedef MySmallLabels::ConstIterator SmallLabelsConstIterator;
142 typedef std::bitset<32> MySmallBitset;
143
144 trace.beginBlock ( "Testing one word long Labels" );
145 MySmallLabels ll;
146 MySmallBitset vv;
147
148 ++nb; nbok += isEqual( vv, ll ) ? 1 : 0;
149 std::cout << "(" << nbok << "/" << nb << ") small_l=" << ll << std::endl;
150
151 insert( vv, ll, 15 );
152 insert( vv, ll, 4 );
153 insert( vv, ll, 31 );
154 ++nb; nbok += isEqual( vv, ll ) ? 1 : 0;
155 std::cout << "(" << nbok << "/" << nb << ") small_l=" << ll << std::endl;
156
157 erase( vv, ll, 15 );
158 ++nb; nbok += isEqual( vv, ll ) ? 1 : 0;
159 std::cout << "(" << nbok << "/" << nb << ") small_l=" << ll << std::endl;
160
161 // Check insertion at index 0
162 insert( vv, ll, 0 );
163 ++nb; nbok += isEqual( vv, ll ) ? 1 : 0;
164 std::cout << "(" << nbok << "/" << nb << ") small_l=" << ll << std::endl;
165
166 // Check bit count computation
167 ++nb; nbok += ll.count() == 3 ? 1 : 0;
168 std::cout << "(" << nbok << "/" << nb << ") small_l.count()=" << ll.count() << std::endl;
169
170 // Compare with size computed with iterators
171 unsigned int cnt = 0;
172 for ( SmallLabelsConstIterator it = ll.begin(), it_end = ll.end(); it != it_end; ++cnt, ++it) {}
173 ++nb; nbok += cnt == 3 ? 1 : 0;
174 std::cout << "(" << nbok << "/" << nb << ") small_l bit count with iterators=" << cnt << std::endl;
175
176 trace.endBlock();
177
178 return ( nb == nbok ) ? 0 : 1;
179}
Aim: Stores a set of labels in {O..L-1} as a sequence of bits.
Definition: Labels.h:72
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:154
Go to http://www.sgi.com/tech/stl/ForwardIterator.html.
Definition: Boost.dox:40
void erase(VContainer1 &c1, LContainer2 &c2, unsigned int idx)
Definition: testLabels.cpp:80
void insert(VContainer1 &c1, LContainer2 &c2, unsigned int idx)
Definition: testLabels.cpp:60
bool isEqual(Container1 &c1, Container2 &c2)
Definition: testLabels.cpp:45
bool checkInsert(VContainer1 &v, LContainer2 &l, unsigned int nb)
Definition: testLabels.cpp:68
int main()
Definition: testLabels.cpp:100
bool checkErase(VContainer1 &v, LContainer2 &l, unsigned int nb)
Definition: testLabels.cpp:88