DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testImageContainerByHashTree.cpp
1 
30 
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 ImageContainerByHashTree<Domain , T, DGtal::uint64_t >::dim
41 #define defHashKey typename 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 >
52 bool test_setVal (ImageContainerByHashTree<Domain , T, DGtal::uint64_t >& container, bool checkAfterEachSet )
53 {
54  srand ( (unsigned int)time ( NULL ) );
55 
56  //phase 1
57  cerr << "Test: set" <<endl;
58  cerr << "phase 1..." <<endl;
59  //unsigned DepthMask = container.ROOT_KEY << container.getDepth()*dim;
60  unsigned DepthMask = 100000;
61  //cerr << "DepthMask = " << DepthMask << 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  cerr << "test_set: failure in phase 1" << endl
75  << "at key " << Bits::bitString ( key ) << endl;
76  return false;
77  }
78  else
79  cerr << "ok"<<endl;
80  }
81  }
82  cerr << "checking the container's validity..." << endl;
83  if ( !container.checkIntegrity() )
84  {
85 
86  cerr << "test_set: failure in phase 1" << endl;
87  return false;
88  }
89 
90  //phase 2
91  cerr << "phase 2..." <<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  cerr << "test_set: failure in phase 2" << endl
103  << "at key " << Bits::bitString ( key ) << endl;
104  return false;
105  }
106  }
107 
108  if ( !container.checkIntegrity() )
109  {
110  cerr << "test_set: failure in phase 2" << endl;
111  return false;
112  }
113 
114  cerr << "test_set: success !" << endl;
115  return true;
116 }
117 
118 
119 template<typename Domain, typename T >
120 bool test_get ( ImageContainerByHashTree<Domain, T, DGtal::uint64_t >& container, bool )
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  //cerr << "DepthMask "<< DepthMask << endl;
127  for ( unsigned key = DepthMask; key > container.ROOT_KEY; --key )
128  {
129  if ( !container.isKeyValid ( key ) )
130  {
131  //cerr << "invalid key "<< Bits::bitString(key) << endl;
132  continue;
133  }
134  ++count;
135  T val = iRand ( 0, 100 );
136  //cerr << "plop1" << endl;
137  //cerr << "___________________________ set: " << Bits::bitString(key, 16) << 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  cerr << "test_get: failure" << endl
148  << "at key " << Bits::bitString ( key2 ) << endl;
149  return false;
150  }
151  //cerr << "check " << Bits::bitString(key2) << " ok." << endl;
152  }
153  key2 <<=dim;
154  }
155  cerr << "test_get: success !" << endl
156  << "tested with " << count << " keys" << 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() << endl;
169 
170  typedef SpaceND<5> Space;
171  typedef Space::Point Point;
172  typedef HyperRectDomain<Space> Dom;
174  Tree tree ( 12,5,1 );
175  // Do not pass concept.
176  //BOOST_CONCEPT_ASSERT((CDrawableWithBoard2D<Tree>));
177 
178  //tree.printInternalState(cerr, 12);
179  Dom::Point p1, p2, p3;
180  cerr << "azertyuiop" << endl;
181  p1[0] = -32;
182  p1[1] = -10;
183  p1[2] = -30;
184  p1[3] = 20;
185  p1[4] = 0;
186 
187  p2[0] = 32;
188  p2[1] = 32;
189  p2[2] = 20;
190  p2[3] = 64;
191  p2[4] = 120;
192 
193  p3[0] = 1;
194  p3[1] = 1;
195  p3[2] = 1;
196  p3[3] = 1;
197  p3[4] = 1;
198  cerr << "azertyuiop" << endl;
200  cerr << "azertyuiop" << endl;
201  cerr << "coord get " << tree2.get ( p1 ) << endl;
202  cerr << "_-_-_-_-_-_-_-_-_-_-_-_-" << endl;
203  cerr << "coord get " << tree2.get ( p3 ) << endl;
204  cerr << "coord get " << tree2.get ( p1+=p3 ) << endl;
205  cerr << "coord get " << tree2.get ( p1+=p3 ) << endl;
206  cerr << "coord get " << tree2.get ( p1+=p3 ) << endl;
207  cerr << "coord get " << tree2.get ( p1+=p3 ) << endl;
208 
209  // check that the iterator stuff compiles as it should
211  HashTreeIterator it = tree.begin();
212  for ( it = tree.begin(); it != tree.end(); ++it )
213  tree ( *it );
214 
215 
216 
217  bool res =
218  (
219  test_setVal ( tree, false ) &&
220  test_get ( tree, false )
221  );
222 
223 
224 
225  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
226  trace.endBlock();
227  return res ? 0 : 1;
228 }