DGtal  1.5.beta
testOutputIteratorAdapter.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include <map>
33 #include <vector>
34 
35 #include "DGtal/base/Common.h"
36 #include "DGtal/base/BasicFunctors.h"
37 #include "DGtal/base/OutputIteratorAdapter.h"
38 #include "DGtal/base/IteratorAdapter.h"
39 
40 
42 
43 using namespace std;
44 using namespace DGtal;
45 
47 // Functions for testing class OutputIteratorAdapter.
49 
53 bool testPairs()
54 {
55  int nb = 0;
56  int nbok = 0;
57 
58  typedef map<string,string> Map;
59  typedef pair<string,string> Pair;
60 
61  //input
62  Map m;
63  m.insert( Pair("Proudhon", "Pierre-Joseph" ) );
64  m.insert( Pair("Reclus", "Élisée" ) );
65  m.insert( Pair("Zamenhof", "Ludwik Lejzer" ) );
66 
67  typedef vector<string> Vec;
68  Vec v;
69  v.push_back( "P-J." );
70  v.push_back( "E." );
71  v.push_back( "L. L." );
72 
73  //result
74  Map m2;
75  m2.insert( Pair("Proudhon", "P-J." ) );
76  m2.insert( Pair("Reclus", "E.") );
77  m2.insert( Pair("Zamenhof", "L. L." ) );
78 
79  {//test output iterator
80  trace.beginBlock ( "OutputIteratorAdapter..." );
81 
83  BOOST_CONCEPT_ASSERT(( boost::OutputIterator<Adapter, string> ));
85 
86  Adapter a( m.begin(), f );
87 
88  copy(v.begin(), v.end(), a);
89 
90  bool flag;
91  flag = std::equal( m.begin(), m.end(), m2.begin() );
92  nbok += (flag)?1:0;
93  nb++;
94 
95  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
96  trace.endBlock();
97  }
98 
99  {//test iterator
100  trace.beginBlock ( "IteratorAdapter..." );
101 
103 
105  BOOST_CONCEPT_ASSERT(( boost::ForwardIterator<Adapter> ));
106 
107  //writting
108  Adapter a( m.begin(), f );
109  copy(v.begin(), v.end(), a);
110  bool flag1 = std::equal( m.begin(), m.end(), m2.begin() );
111 
112  //reading
113  Adapter aBegin( m.begin(), f);
114  Adapter aEnd( m.end(), f);
115  bool flag2 = std::equal( aBegin, aEnd, v.begin() );
116 
117  nbok += (flag1 && flag2)?1:0;
118  nb++;
119 
120  trace.info() << "(" << nbok << "/" << nb << ")" << std::endl;
121  trace.endBlock();
122  }
123 
124  return (nb == nbok);
125 }
126 
128 // Standard services - public :
129 
130 int main( int argc, char** argv )
131 {
132  trace.beginBlock ( "Testing class OutputIteratorAdapter" );
133  trace.info() << "Args:";
134  for ( int i = 0; i < argc; ++i )
135  trace.info() << " " << argv[ i ];
136  trace.info() << endl;
137 
138  bool res = testPairs()
139  ; // && ... other tests
140  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
141  trace.endBlock();
142  return res ? 0 : 1;
143 }
144 // //
This class adapts any lvalue iterator so that operator* returns a member on the element pointed to by...
Aim: Adapts an output iterator i with a unary functor f, both given at construction,...
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
Aim: Define a simple unary functor that returns a reference on the first member of a pair in order to...
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Go to http://www.sgi.com/tech/stl/ForwardIterator.html.
Definition: Boost.dox:40
Go to http://www.sgi.com/tech/stl/OutputIterator.html.
Definition: Boost.dox:38
std::unordered_map< Cell, CubicalCellData > Map
bool testPairs()
int main(int argc, char **argv)