#include #include "Hash.h" #include "Graph.h" #include "test_utilities.h" int main(int argc, char *argv[]) { test_init(50, argv[0]); // test basic operations Graph G; int v; G.add_vertex(1); G.add_vertex(2); v = G.add_vertex(); test(2, ! (v ==1 || v ==2), "Added vertex not new"); test(2, G.vertex_exists(1) && G.vertex_exists(2) && G.vertex_exists(v), "Added vertex fails to exist"); test(2, ! G.vertex_exists(0), "Non-added vertex exists"); G.add_edge(1,2); G.add_edge(v,1); G.add_edge(2,v); test(3, G.edge_exists(1,2) && G.edge_exists(v,1) && G.edge_exists(2,v) && G.edge_exists(2,1) && G.edge_exists(1,v) && G.edge_exists(v,2), "Added edge fails to exist"); test(3, ! (G.edge_exists(2,2) || G.edge_exists(1,1) || G.edge_exists(v,v) ), "Non-added edge exists"); Array vs = G.vertices(); test(4, vs.size() == 3, "Wrong number of vertices returned by vertices()"); int errors = 0; for (int i = 0; i < vs.size(); ++i) { int vid = vs[i]; if (G.nbrs(vs[i]).size() != 2) ++errors; } test(4, errors == 0, "Wrong number of vertices returned by nbrs()"); // copy constructor, some const correctness const Graph & H(G); Graph GG(G); Array Hvs = H.vertices(); Array GGvs = GG.vertices(); test(2, Hvs.size() == 3, "vertices() const test fails"); test(3, GGvs.size() == 3, "Graph copy constructor fails? (wrong size)"); GG.remove_edge(1,2); test(2, ! (GG.edge_exists(1,2) || GG.edge_exists(2,1)), "Removed edge still exists"); GG.remove_vertex(1); test(2, ! (GG.edge_exists(1,v) || GG.vertex_exists(1)), "Removed vertex (or edge for it) still exists"); // test clear function G.clear(); Array Gvs = G.vertices(); test(2, G.vertices().size() == 0, "G.vertices().size() != 0 after clear"); // try large binary tree errors = 0; int N = 1<<10; G.add_vertex(1); for (int i = 2; i < N; ++i) { G.add_vertex(i); G.add_edge(i/2,i); } for (int i = 2; i < N/2; ++i) { if (! (G.vertex_exists(i) && G.edge_exists(i/2,i))) { ++errors; } else { Array nbrs = G.nbrs(i); if (! nbrs.size() == 3 ) ++errors; } } test(9, errors == 0, "Binary tree test failed"); G.clear(); // try large complete graph errors = 0; N = 1<<6; for (int i = 1; i <= N; ++i) { std::cout << "."; G.add_vertex(i); for (int j = 1; j < i; ++j) G.add_edge(i,j); } std::cout << std::endl; for (int i = 1; i <= N; ++i) { std::cout << "."; for (int j = 1; j < i; ++j) { if (! (G.edge_exists(i,j) && G.edge_exists(j,i))) ++errors; } Array nbrs = G.nbrs(i); if (! nbrs.size() == N-1) ++errors; } std::cout << std::endl; test(10, errors == 0, "Complete graph test failed"); // end test_report(); }