COIN-OR::LEMON - Graph Library

Ticket #402: test_lemon_map_default_value-verbose_class.cpp

File test_lemon_map_default_value-verbose_class.cpp, 2.4 KB (added by Charles Wilcox, 10 years ago)

Test .cpp file with verbose output and a custom class to output construction, assignment and destruction events.

Line 
1#include <lemon/list_graph.h> // ListDigraph
2
3#include <limits> // numeric_limits
4#include <iostream> // cout, endl
5
6class MyClass
7{
8public:
9  typedef size_t Id;
10  MyClass( const Id id = 0 )
11    : mId( id )
12  {
13    std::cout << "MyClass::MyClass() called." << std::endl;
14  }
15  MyClass( const MyClass& rOther )
16    : mId( rOther.mId )
17  {
18    std::cout << "MyClass::MyClass( const MyClass& ) called." << std::endl;
19  }
20  MyClass& operator=( const MyClass& rOther )
21  {
22    std::cout << "MyClass& MyClass::operator=( const MyClass& ) called." << std::endl;
23    mId = rOther.mId;
24    return *this;
25  }
26  ~MyClass()
27  {
28    std::cout << "MyClass::~MyClass() called." << std::endl;
29  }
30  friend bool operator==( const MyClass& rLhs, const MyClass& rRhs );
31  friend std::ostream& operator<<( std::ostream& rOs, const MyClass& rThis );
32private:
33  size_t mId;
34}; // class MyClass
35
36inline bool operator==( const MyClass& rLhs, const MyClass& rRhs )
37{
38  return rLhs.mId == rRhs.mId;
39}
40
41inline std::ostream& operator<<( std::ostream& rOs, const MyClass& rThis )
42{
43  rOs << "{ mId: " << rThis.mId << " }";
44  return rOs;
45}
46
47
48int main( int argc, char* argv[] )
49{
50  std::cout << "Creating the digraph." << std::endl;
51  lemon::ListDigraph digraph;
52  std::cout << "Adding a node to the digraph." << std::endl;
53  digraph.addNode();
54  std::cout << "Adding a node to the digraph." << std::endl;
55  digraph.addNode();
56
57  std::cout << "Constructing my initial-value." << std::endl;
58  const MyClass initial_map_value = MyClass( -1 );
59  std::cout << "Creating the map." << std::endl;
60  lemon::ListDigraph::NodeMap< MyClass > node_to_value_map( digraph,
61                                                            initial_map_value );
62
63  std::cout << "Adding a node to the digraph." << std::endl;
64  digraph.addNode();
65  std::cout << "Adding a node to the digraph." << std::endl;
66  digraph.addNode();
67
68  for( lemon::ListDigraph::NodeIt iter( digraph );
69       iter != lemon::INVALID; ++iter )
70  {
71    const MyClass& expected_map_value = initial_map_value;
72    const bool b_matched = node_to_value_map[ iter ] == expected_map_value;
73    std::cout << "For Digraph Node ID: " << digraph.id( iter ) << ", "
74              << "the expected map value is: " << expected_map_value << ", "
75              << ( b_matched ? "and " : "but " )
76              << "the observed map value is: " << node_to_value_map[ iter ]
77              << "." << std::endl;
78  }
79
80  std::cout << "About to destroy all local-scoped variables." << std::endl;
81  return 0;
82}