DGtal 1.3.0
Loading...
Searching...
No Matches
Functions
testLabels.cpp File Reference
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <bitset>
#include "DGtal/base/Common.h"
#include "DGtal/base/Labels.h"

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)
 
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)
 
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 testLabels.cpp.

Function Documentation

◆ checkErase()

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

Definition at line 88 of file testLabels.cpp.

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}
void erase(VContainer1 &c1, LContainer2 &c2, unsigned int idx)
Definition: testLabels.cpp:80
bool isEqual(Container1 &c1, Container2 &c2)
Definition: testLabels.cpp:45

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 68 of file testLabels.cpp.

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}
void insert(VContainer1 &c1, LContainer2 &c2, unsigned int idx)
Definition: testLabels.cpp:60

References insert(), and isEqual().

Referenced by main().

◆ erase()

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

Definition at line 80 of file testLabels.cpp.

81{
82 c1.reset( idx );
83 c2.reset( idx );
84}

Referenced by checkErase(), and main().

◆ insert()

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

Definition at line 60 of file testLabels.cpp.

61{
62 c1.set( idx );
63 c2.set( idx );
64}

Referenced by checkInsert(), and main().

◆ isEqual()

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

Definition at line 45 of file testLabels.cpp.

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}

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

◆ main()

int main ( void  )

Definition at line 100 of file testLabels.cpp.

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()
Trace trace
Definition: Common.h:154
Go to http://www.sgi.com/tech/stl/ForwardIterator.html.
Definition: Boost.dox:40
bool checkInsert(VContainer1 &v, LContainer2 &l, unsigned int nb)
Definition: testLabels.cpp:68
bool checkErase(VContainer1 &v, LContainer2 &l, unsigned int nb)
Definition: testLabels.cpp:88

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