DGtal  1.5.beta
testImageContainerByHashTree.cpp File Reference
#include <iostream>
#include "DGtal/base/Common.h"
#include <cmath>
#include "DGtal/kernel/SpaceND.h"
#include "DGtal/kernel/domains/HyperRectDomain.h"
#include "DGtal/images/ImageContainerByHashTree.h"
Include dependency graph for testImageContainerByHashTree.cpp:

Go to the source code of this file.

Macros

#define dim   experimental::ImageContainerByHashTree<Domain , T, DGtal::uint64_t >::dim
 
#define defHashKey   typename experimental::ImageContainerByHashTree<Domain , int, DGtal::uint64_t >::HashKey
 

Functions

int iRand (int iMin, int iMax)
 
template<typename Domain , typename T >
bool test_setVal (experimental::ImageContainerByHashTree< Domain, T, DGtal::uint64_t > &container, bool checkAfterEachSet)
 
template<typename Domain , typename T >
bool test_get (experimental::ImageContainerByHashTree< Domain, T, DGtal::uint64_t > &container, bool)
 
int main (int argc, char **argv)
 

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
David Coeurjolly (david.nosp@m..coe.nosp@m.urjol.nosp@m.ly@l.nosp@m.iris..nosp@m.cnrs.nosp@m..fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2010/07/22

Functions for testing class ImageContainerBenchmark.

This file is part of the DGtal library.

Definition in file testImageContainerByHashTree.cpp.

Macro Definition Documentation

◆ defHashKey

#define defHashKey   typename experimental::ImageContainerByHashTree<Domain , int, DGtal::uint64_t >::HashKey

Definition at line 41 of file testImageContainerByHashTree.cpp.

◆ dim

Definition at line 40 of file testImageContainerByHashTree.cpp.

Function Documentation

◆ iRand()

int iRand ( int  iMin,
int  iMax 
)

Definition at line 45 of file testImageContainerByHashTree.cpp.

46 {
47  double f = ( double ) rand() / RAND_MAX;
48  return (int) (iMin + static_cast<double> ( f * ( iMax - iMin ) ));
49 }

Referenced by test_get(), and test_setVal().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 162 of file testImageContainerByHashTree.cpp.

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....
Trace trace
Definition: Common.h:153
MyPointD Point
Definition: testClone2.cpp:383
bool test_setVal(experimental::ImageContainerByHashTree< Domain, T, DGtal::uint64_t > &container, bool checkAfterEachSet)
bool test_get(experimental::ImageContainerByHashTree< Domain, T, DGtal::uint64_t > &container, bool)

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), DGtal::experimental::ImageContainerByHashTree< TDomain, TValue, THashKey >::get(), DGtal::Trace::info(), test_get(), test_setVal(), and DGtal::trace.

◆ test_get()

template<typename Domain , typename T >
bool test_get ( experimental::ImageContainerByHashTree< Domain, T, DGtal::uint64_t > &  container,
bool   
)

Definition at line 120 of file testImageContainerByHashTree.cpp.

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 }
Value get(const HashKey key) const
void setValue(const HashKey key, const Value object)
srand(0)
int iRand(int iMin, int iMax)

References DGtal::Bits::bitString(), dim, DGtal::experimental::ImageContainerByHashTree< TDomain, TValue, THashKey >::get(), DGtal::Trace::info(), iRand(), DGtal::experimental::ImageContainerByHashTree< TDomain, TValue, THashKey >::isKeyValid(), DGtal::experimental::ImageContainerByHashTree< TDomain, TValue, THashKey >::setValue(), srand(), and DGtal::trace.

Referenced by main().

◆ test_setVal()

template<typename Domain , typename T >
bool test_setVal ( experimental::ImageContainerByHashTree< Domain, T, DGtal::uint64_t > &  container,
bool  checkAfterEachSet 
)

Definition at line 52 of file testImageContainerByHashTree.cpp.

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 }
bool checkIntegrity(HashKey key=ROOT_KEY, bool leafAbove=false) const

References DGtal::Bits::bitString(), DGtal::experimental::ImageContainerByHashTree< TDomain, TValue, THashKey >::checkIntegrity(), DGtal::Trace::info(), iRand(), DGtal::experimental::ImageContainerByHashTree< TDomain, TValue, THashKey >::isKeyValid(), DGtal::experimental::ImageContainerByHashTree< TDomain, TValue, THashKey >::setValue(), srand(), and DGtal::trace.

Referenced by main().