DGtal  1.5.beta
testImageContainerByHashTree.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include <cmath>
34 #include "DGtal/kernel/SpaceND.h"
35 #include "DGtal/kernel/domains/HyperRectDomain.h"
36 #include "DGtal/images/ImageContainerByHashTree.h"
37 
39 
40 #define dim experimental::ImageContainerByHashTree<Domain , T, DGtal::uint64_t >::dim
41 #define defHashKey typename experimental::ImageContainerByHashTree<Domain , int, DGtal::uint64_t >::HashKey
42 
43 using namespace DGtal;
44 
45 int iRand ( int iMin, int iMax )
46 {
47  double f = ( double ) rand() / RAND_MAX;
48  return (int) (iMin + static_cast<double> ( f * ( iMax - iMin ) ));
49 }
50 
51 template<typename Domain, typename T >
53 {
54  srand ( (unsigned int)time ( NULL ) );
55 
56  //phase 1
57  trace.info() << "Test: set" <<std::endl;
58  trace.info() << "phase 1..." <<std::endl;
59  //unsigned DepthMask = container.ROOT_KEY << container.getDepth()*dim;
60  unsigned DepthMask = 100000;
61  //cerr << "DepthMask = " << DepthMask << std::endl;
62  for ( int key = DepthMask; key > 1; --key )
63  {
64  if ( !container.isKeyValid ( key ) )
65  {
66  continue;
67  }
68  //count ++;
69  container.setValue ( key, iRand ( 0, 100 ) );
70  if ( checkAfterEachSet )
71  {
72  if ( !container.checkIntegrity() )
73  {
74  trace.info() << "test_set: failure in phase 1" << std::endl
75  << "at key " << Bits::bitString ( key ) << std::endl;
76  return false;
77  }
78  else
79  trace.info() << "ok"<<std::endl;
80  }
81  }
82  trace.info() << "checking the container's validity..." << std::endl;
83  if ( !container.checkIntegrity() )
84  {
85 
86  trace.info() << "test_set: failure in phase 1" << std::endl;
87  return false;
88  }
89 
90  //phase 2
91  trace.info() << "phase 2..." <<std::endl;
92  for ( unsigned key = container.ROOT_KEY; key < DepthMask; ++key )
93  {
94  if ( !container.isKeyValid ( key ) )
95  {
96  continue;
97  }
98  container.setValue ( key, iRand ( 0, 100 ) );
99  if ( checkAfterEachSet )
100  if ( !container.checkIntegrity() )
101  {
102  trace.info() << "test_set: failure in phase 2" << std::endl
103  << "at key " << Bits::bitString ( key ) << std::endl;
104  return false;
105  }
106  }
107 
108  if ( !container.checkIntegrity() )
109  {
110  trace.info() << "test_set: failure in phase 2" << std::endl;
111  return false;
112  }
113 
114  trace.info() << "test_set: success !" << std::endl;
115  return true;
116 }
117 
118 
119 template<typename Domain, typename T >
121 {
122  srand ( (unsigned int)time ( NULL ) );
123  unsigned count = 0;
124  //unsigned DepthMask = container.ROOT_KEY << container.getDepth()*dim;
125  unsigned DepthMask = 100000;
126  //trace.info() << "DepthMask "<< DepthMask << std::endl;
127  for ( unsigned key = DepthMask; key > container.ROOT_KEY; --key )
128  {
129  if ( !container.isKeyValid ( key ) )
130  {
131  //trace.info() << "invalid key "<< Bits::bitString(key) << std::endl;
132  continue;
133  }
134  ++count;
135  T val = iRand ( 0, 100 );
136  //trace.info() << "plop1" << std::endl;
137  //trace.info() << "___________________________ set: " << Bits::bitString(key, 16) << std::endl;
138  container.setValue ( key, val );
139 
140 
142  while ( container.isKeyValid ( key2 ) )
143  {
144  key2 = key2 << dim;
145  if ( val != container.get ( key2 ) )
146  {
147  trace.info() << "test_get: failure" << std::endl
148  << "at key " << Bits::bitString ( key2 ) << std::endl;
149  return false;
150  }
151  //trace.info() << "check " << Bits::bitString(key2) << " ok." << std::endl;
152  }
153  //key2 <<=dim;
154  }
155  trace.info() << "test_get: success !" << std::endl
156  << "tested with " << count << " keys" << std::endl;
157  return true;
158 }
159 
160 
161 
162 int main ( int argc, char** argv )
163 {
164  trace.beginBlock ( "Testing class ImageContainerBenchmark" );
165  trace.info() << "Args:";
166  for ( int i = 0; i < argc; ++i )
167  trace.info() << " " << argv[ i ];
168  trace.info() << std::endl;
169 
170  typedef SpaceND<5> Space;
171  typedef HyperRectDomain<Space> Dom;
173  Tree tree ( 12,5,1 );
174  // Do not pass concept.
175  //BOOST_CONCEPT_ASSERT((CDrawableWithBoard2D<Tree>));
176 
177  //tree.printInternalState(cerr, 12);
178  Dom::Point p1, p2, p3;
179  trace.info() << "azertyuiop" << std::endl;
180  p1[0] = -32;
181  p1[1] = -10;
182  p1[2] = -30;
183  p1[3] = 20;
184  p1[4] = 0;
185 
186  p2[0] = 32;
187  p2[1] = 32;
188  p2[2] = 20;
189  p2[3] = 64;
190  p2[4] = 120;
191 
192  p3[0] = 1;
193  p3[1] = 1;
194  p3[2] = 1;
195  p3[3] = 1;
196  p3[4] = 1;
197  trace.info() << "azertyuiop" << std::endl;
199  trace.info() << "azertyuiop" << std::endl;
200  trace.info() << "coord get " << tree2.get ( p1 ) << std::endl;
201  trace.info() << "_-_-_-_-_-_-_-_-_-_-_-_-" << std::endl;
202  trace.info() << "coord get " << tree2.get ( p3 ) << std::endl;
203  trace.info() << "coord get " << tree2.get ( p1+=p3 ) << std::endl;
204  trace.info() << "coord get " << tree2.get ( p1+=p3 ) << std::endl;
205  trace.info() << "coord get " << tree2.get ( p1+=p3 ) << std::endl;
206  trace.info() << "coord get " << tree2.get ( p1+=p3 ) << std::endl;
207 
208  // check that the iterator stuff compiles as it should
210  HashTreeIterator it = tree.begin();
211  for ( it = tree.begin(); it != tree.end(); ++it )
212  tree ( *it );
213 
214 
215 
216  bool res =
217  (
218  test_setVal ( tree, false ) &&
219  test_get ( tree, false )
220  );
221 
222 
223 
224  trace.emphase() << ( res ? "Passed." : "Error." ) << std::endl;
225  trace.endBlock();
226  return res ? 0 : 1;
227 }
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
Built-in iterator on an HashTree. This iterator visits all node in the tree.
Model of CImageContainer implementing the association key<->Value using a hash tree....
bool checkIntegrity(HashKey key=ROOT_KEY, bool leafAbove=false) const
Value get(const HashKey key) const
void setValue(const HashKey key, const Value object)
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
static std::string bitString(T value, unsigned nbBits=0)
Bits Structs grouping all the functions of this tiny library for bitwise calculation.
Definition: Bits.h:57
MyPointD Point
Definition: testClone2.cpp:383
srand(0)
bool test_setVal(experimental::ImageContainerByHashTree< Domain, T, DGtal::uint64_t > &container, bool checkAfterEachSet)
int main(int argc, char **argv)
int iRand(int iMin, int iMax)
bool test_get(experimental::ImageContainerByHashTree< Domain, T, DGtal::uint64_t > &container, bool)