Working on BipGraph unit tests
[libdai.git] / tests / unit / bipgraph.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2010 Joris Mooij [joris dot mooij at libdai dot org]
8 */
9
10
11 #define BOOST_TEST_DYN_LINK
12
13
14 #include <dai/bipgraph.h>
15 #include <vector>
16 #include <strstream>
17
18
19 using namespace dai;
20
21
22 #define BOOST_TEST_MODULE BipartiteGraphTest
23
24
25 #include <boost/test/unit_test.hpp>
26
27
28 BOOST_AUTO_TEST_CASE( ConstructorsTest ) {
29 // check constructors
30 typedef BipartiteGraph::Edge Edge;
31
32 BipartiteGraph G;
33 BOOST_CHECK_EQUAL( G.nrNodes1(), 0 );
34 BOOST_CHECK_EQUAL( G.nrNodes2(), 0 );
35 BOOST_CHECK_EQUAL( G.nrEdges(), 0 );
36 BOOST_CHECK( G.isConnected() );
37 BOOST_CHECK( G.isTree() );
38 G.checkConsistency();
39
40 std::vector<Edge> edges;
41 edges.push_back( Edge(0, 0) );
42 edges.push_back( Edge(0, 1) );
43 edges.push_back( Edge(1, 1) );
44 edges.push_back( Edge(1, 2) );
45 edges.push_back( Edge(1, 2) );
46 BipartiteGraph G2( 2, 3, edges.begin(), edges.end() );
47 BOOST_CHECK_EQUAL( G2.nrNodes1(), 2 );
48 BOOST_CHECK_EQUAL( G2.nrNodes2(), 3 );
49 BOOST_CHECK_EQUAL( G2.nrEdges(), 4 );
50 BOOST_CHECK( G2.isConnected() );
51 BOOST_CHECK( G2.isTree() );
52 G2.checkConsistency();
53
54 edges.push_back( Edge(1, 0) );
55 BipartiteGraph G3( 2, 3, edges.begin(), edges.end() );
56 BOOST_CHECK_EQUAL( G3.nrNodes1(), 2 );
57 BOOST_CHECK_EQUAL( G3.nrNodes2(), 3 );
58 BOOST_CHECK_EQUAL( G3.nrEdges(), 5 );
59 BOOST_CHECK( G3.isConnected() );
60 BOOST_CHECK( !G3.isTree() );
61 G3.checkConsistency();
62
63 BipartiteGraph G4( 3, 3, edges.begin(), edges.end() );
64 BOOST_CHECK_EQUAL( G4.nrNodes1(), 3 );
65 BOOST_CHECK_EQUAL( G4.nrNodes2(), 3 );
66 BOOST_CHECK_EQUAL( G4.nrEdges(), 5 );
67 BOOST_CHECK( !G4.isConnected() );
68 BOOST_CHECK( !G4.isTree() );
69 G4.checkConsistency();
70
71 G.construct( 3, 3, edges.begin(), edges.end() );
72 BOOST_CHECK_EQUAL( G.nrNodes1(), 3 );
73 BOOST_CHECK_EQUAL( G.nrNodes2(), 3 );
74 BOOST_CHECK_EQUAL( G.nrEdges(), 5 );
75 BOOST_CHECK( !G.isConnected() );
76 BOOST_CHECK( !G.isTree() );
77 G.checkConsistency();
78 }
79
80
81 BOOST_AUTO_TEST_CASE( NeighborTest ) {
82 // TODO
83 }
84
85
86 BOOST_AUTO_TEST_CASE( AddEraseTest ) {
87 // TODO
88 }
89
90
91 BOOST_AUTO_TEST_CASE( QueriesTest ) {
92 // TODO
93 }
94
95
96 BOOST_AUTO_TEST_CASE( StreamTest ) {
97 // check printDot
98 typedef BipartiteGraph::Edge Edge;
99 std::vector<Edge> edges;
100 edges.push_back( Edge(0, 0) );
101 edges.push_back( Edge(0, 1) );
102 edges.push_back( Edge(1, 1) );
103 edges.push_back( Edge(1, 2) );
104 BipartiteGraph G( 2, 3, edges.begin(), edges.end() );
105
106 std::stringstream ss;
107 G.printDot( ss );
108
109 std::string s;
110 std::getline( ss, s );
111 BOOST_CHECK_EQUAL( s, "graph G {" );
112 std::getline( ss, s );
113 BOOST_CHECK_EQUAL( s, "node[shape=circle,width=0.4,fixedsize=true];" );
114 std::getline( ss, s );
115 BOOST_CHECK_EQUAL( s, "\tx0;" );
116 std::getline( ss, s );
117 BOOST_CHECK_EQUAL( s, "\tx1;" );
118 std::getline( ss, s );
119 BOOST_CHECK_EQUAL( s, "node[shape=box,width=0.3,height=0.3,fixedsize=true];" );
120 std::getline( ss, s );
121 BOOST_CHECK_EQUAL( s, "\ty0;" );
122 std::getline( ss, s );
123 BOOST_CHECK_EQUAL( s, "\ty1;" );
124 std::getline( ss, s );
125 BOOST_CHECK_EQUAL( s, "\ty2;" );
126 std::getline( ss, s );
127 BOOST_CHECK_EQUAL( s, "\tx0 -- y0;" );
128 std::getline( ss, s );
129 BOOST_CHECK_EQUAL( s, "\tx0 -- y1;" );
130 std::getline( ss, s );
131 BOOST_CHECK_EQUAL( s, "\tx1 -- y1;" );
132 std::getline( ss, s );
133 BOOST_CHECK_EQUAL( s, "\tx1 -- y2;" );
134 std::getline( ss, s );
135 BOOST_CHECK_EQUAL( s, "}" );
136 }