diff --git a/CMakeLists.txt b/CMakeLists.txt index 272bed7..6a19dad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,10 +6,23 @@ project(graph) set(CMAKE_CXX_COMPILER "/usr/lib/colorgcc/bin/g++") # set(CMAKE_CXX_COMPILER "/usr/bin/clang") -set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow " - "-ggdb -std=c++0x -D_GLIBCXX_PROFILE ") +set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow -ggdb -std=c++0x") add_definitions( ${CXX_FLAGS} ) -# include_directories(.) -add_executable(graph main.cpp) +find_package(CxxTest) +if(CXXTEST_FOUND) + + include_directories(${CXXTEST_INCLUDE_DIR} + ../lib) + + enable_testing() + + CXXTEST_ADD_TEST( + test_graph + generated_main.cpp + + test_graph.hpp + ) + +endif() diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 671ce1a..0000000 --- a/main.cpp +++ /dev/null @@ -1,169 +0,0 @@ -#include -#include - - -#include "graph.hpp" -// #include "dotConverter.hpp" - -int main() -{ - Graph g; - - assert(g.empty() == true); - assert(g.numberOfVertices() == 0); - assert(g.numberOfEdges() == 0); - - - int a = 2; - int b = 5; - assert(g.addEdge(a, b) == false); - - assert(g.addVertex(a) == true); - assert(g.addVertex(a) == false); - assert(g.empty() == false); - assert(g.numberOfVertices() == 1); - { - const std::vector v = g.vertices(); - assert(v.size() == 1); - assert(*(v[0]) == 2); - } - - assert(g.addEdge(a, b) == false); - - assert(g.addVertex(b) == true); - assert(g.numberOfVertices() == 2); - { - const std::vector v = g.vertices(); - assert(v.size() == 2); - assert(*(v[0]) == 2); - assert(*(v[1]) == 5); - } - - assert(g.numberOfEdges() == 0); - assert(g.addEdge(a, b) == true); - - assert(g.numberOfEdges() == 1); - { - const std::vector eb = g.weightsBetween(2, 5); - assert(eb.size() == 1); - assert(eb[0] == 0); - } - assert(g.edgesBetween(5, 2).size() == 0); - assert(g.neighboursOf(2).size() == 1); - { - const std::vector n = g.neighboursOf(2); - assert(n.size() == 1); - assert(*(n[0]) == 5); - } - - int c = 13; - int d = 1; - assert(g.numberOfVertices() == 2); - g.addVertex(c); - g.addVertex(d); - assert(g.numberOfVertices() == 4); - int vertices_array[] = {2, 5, 13, 1}; - std::vector v(vertices_array, vertices_array + sizeof(vertices_array) / sizeof(int) ); - Graph::vertex_iterator g_it2; - std::vector::iterator v_it = v.begin(); - g_it2 = g.vertex_begin(); - assert(g_it2 == g.vertex_begin()); - - for (Graph::vertex_iterator g_it = g.vertex_begin(); - g_it != g.vertex_end(); - ++g_it, ++v_it) { - std::cout << "victor: " -// << *g_it << " " - << *g_it - << std::endl; - assert(*g_it == *v_it); - } - - - assert(g.neighboursOf(5).size() == 0); - - Graph g2; - g2.addVertex(1); - g2.addVertex(2); - g2.addVertex(3); - g2.addVertex(4); - - int vertices_array2[] = {1, 2, 3, 4}; - std::vector v2(vertices_array2, vertices_array2 + sizeof(vertices_array2) / sizeof(int) ); - v_it = v2.begin(); - for (Graph::vertex_iterator g_it = g2.vertex_begin(); - g_it != g2.vertex_end(); - ++g_it, ++v_it) { - std::cout << "vector: " - << *g_it - << std::endl; - assert(*g_it == *v_it); - } - - - assert(g2.addEdge(1, 2) == true); - assert(g2.addEdge(1, 3) == true); - assert(g2.addEdge(2, 4) == true); - std::vector::Edge> e = g2.edges(); - assert(e.size() == 3); - - Graph::edge_iterator edge_it; - Graph::edge_iterator beee = g2.edge_begin(); - edge_it = beee; -// assert(edge_it == g2.edge_begin()); - int source = (*edge_it).getSource(); - int destination = (*edge_it).getDestination(); - assert (source == 1); - assert (destination == 2); - - ++edge_it; - source = (*edge_it).getSource(); - destination = (*edge_it).getDestination(); - assert (source == 1); - assert (destination == 3); - - ++edge_it; - source = (*edge_it).getSource(); - destination = (*edge_it).getDestination(); - assert (source == 2); - assert (destination == 4); - - int aasdads = 23; -// Graph::edge_iterator edge_it; - - for(/*Graph::edge_iterator*/ edge_it = g2.edge_begin(); edge_it != g2.edge_end(); ++edge_it) { - int source = (*edge_it).getSource(); - int destination = (*edge_it).getDestination(); - std::cout << "edge: " - << source << " " - << destination << " " - << (*edge_it).getWeight() << std::endl; - } - - Graph::edge_iterator edge_it1 = g2.edge_begin(); - edge_it1 += 10; - assert(edge_it1 == g2.edge_end()); - - Graph::edge_iterator edge_it2 = g2.edge_begin(); - edge_it2 += 2; - assert( (*edge_it2).getSource() == 2); - assert( (*edge_it2).getDestination() == 4); - assert((*edge_it2).getWeight() == 0); - - std::cout << "frankon vege" << std::endl; - -// toDot(g2, "itt.dot"); -// Graph read_g; -// fromDot("itt.dot", read_g); -// assert(g2 == read_g); - - - - Graph g4; - g4.addVertex(3); - Graph g5 = g4; - Graph g6; - g6 = g5; - g6.removeVertex(3); -} - diff --git a/test_graph.hpp b/test_graph.hpp new file mode 100644 index 0000000..27e2e24 --- /dev/null +++ b/test_graph.hpp @@ -0,0 +1,174 @@ +#include + +#include "graph.hpp" + + +namespace test { class TestSubsystem1DummyClassSuite; } + +class test::TestSubsystem1DummyClassSuite : public CxxTest::TestSuite +{ + +public: + + void testBasic() + { + + Graph g; + + TS_ASSERT_EQUALS(g.empty(), true); + TS_ASSERT_EQUALS(g.numberOfVertices(), 0); + TS_ASSERT_EQUALS(g.numberOfEdges(), 0); + + + int a = 2; + int b = 5; + TS_ASSERT_EQUALS(g.addEdge(a, b), false); + + TS_ASSERT_EQUALS(g.addVertex(a), true); + TS_ASSERT_EQUALS(g.addVertex(a), false); + TS_ASSERT_EQUALS(g.empty(), false); + TS_ASSERT_EQUALS(g.numberOfVertices(), 1); + { + const std::vector v = g.vertices(); + TS_ASSERT_EQUALS(v.size(), 1); + TS_ASSERT_EQUALS(*(v[0]), 2); + } + + TS_ASSERT_EQUALS(g.addEdge(a, b), false); + + TS_ASSERT_EQUALS(g.addVertex(b), true); + TS_ASSERT_EQUALS(g.numberOfVertices(), 2); + { + const std::vector v = g.vertices(); + TS_ASSERT_EQUALS(v.size(), 2); + TS_ASSERT_EQUALS(*(v[0]), 2); + TS_ASSERT_EQUALS(*(v[1]), 5); + } + + TS_ASSERT_EQUALS(g.numberOfEdges(), 0); + TS_ASSERT_EQUALS(g.addEdge(a, b), true); + + TS_ASSERT_EQUALS(g.numberOfEdges(), 1); + { + const std::vector eb = g.weightsBetween(2, 5); + TS_ASSERT_EQUALS(eb.size(), 1); + TS_ASSERT_EQUALS(eb[0], 0); + } + TS_ASSERT_EQUALS(g.weightsBetween(5, 2).size(), 0); + TS_ASSERT_EQUALS(g.neighboursOf(2).size(), 1); + { + const std::vector n = g.neighboursOf(2); + TS_ASSERT_EQUALS(n.size(), 1); + TS_ASSERT_EQUALS(*(n[0]), 5); + } + + int c = 13; + int d = 1; + TS_ASSERT_EQUALS(g.numberOfVertices(), 2); + g.addVertex(c); + g.addVertex(d); + TS_ASSERT_EQUALS(g.numberOfVertices(), 4); + int vertices_array[] = {2, 5, 13, 1}; + std::vector v(vertices_array, vertices_array + sizeof(vertices_array) / sizeof(int) ); + Graph::vertex_iterator g_it2; + std::vector::iterator v_it = v.begin(); + g_it2 = g.vertex_begin(); + TS_ASSERT_EQUALS(g_it2, g.vertex_begin()); + + for (Graph::vertex_iterator g_it = g.vertex_begin(); + g_it != g.vertex_end(); + ++g_it, ++v_it) { + std::cout << "victor: " +// << *g_it << " " + << *g_it + << std::endl; + TS_ASSERT_EQUALS(*g_it, *v_it); + } + + + TS_ASSERT_EQUALS(g.neighboursOf(5).size(), 0); + + Graph g2; + g2.addVertex(1); + g2.addVertex(2); + g2.addVertex(3); + g2.addVertex(4); + + int vertices_array2[] = {1, 2, 3, 4}; + std::vector v2(vertices_array2, vertices_array2 + sizeof(vertices_array2) / sizeof(int) ); + v_it = v2.begin(); + for (Graph::vertex_iterator g_it = g2.vertex_begin(); + g_it != g2.vertex_end(); + ++g_it, ++v_it) { +// std::cout << "vector: " +// << *g_it +// << std::endl; + TS_ASSERT_EQUALS(*g_it, *v_it); + } + + + TS_ASSERT_EQUALS(g2.addEdge(1, 2), true); + TS_ASSERT_EQUALS(g2.addEdge(1, 3), true); + TS_ASSERT_EQUALS(g2.addEdge(2, 4), true); + std::vector::Edge> e = g2.edges(); + TS_ASSERT_EQUALS(e.size(), 3); + + Graph::edge_iterator edge_it; + Graph::edge_iterator beee = g2.edge_begin(); + edge_it = beee; +// TS_ASSERT_EQUALS(edge_it, g2.edge_begin()); + int source = (*edge_it).getSource(); + int destination = (*edge_it).getDestination(); + TS_ASSERT_EQUALS (source, 1); + TS_ASSERT_EQUALS (destination, 2); + + ++edge_it; + source = (*edge_it).getSource(); + destination = (*edge_it).getDestination(); + TS_ASSERT_EQUALS (source, 1); + TS_ASSERT_EQUALS (destination, 3); + + ++edge_it; + source = (*edge_it).getSource(); + destination = (*edge_it).getDestination(); + TS_ASSERT_EQUALS (source, 2); + TS_ASSERT_EQUALS (destination, 4); + + int aasdads = 23; +// Graph::edge_iterator edge_it; + + for(/*Graph::edge_iterator*/ edge_it = g2.edge_begin(); edge_it != g2.edge_end(); ++edge_it) { + int source = (*edge_it).getSource(); + int destination = (*edge_it).getDestination(); +// std::cout << "edge: " +// << source << " " +// << destination << " " +// << (*edge_it).getWeight() << std::endl; + } + + Graph::edge_iterator edge_it1 = g2.edge_begin(); + edge_it1 += 10; + TS_ASSERT_EQUALS(edge_it1, g2.edge_end()); + + Graph::edge_iterator edge_it2 = g2.edge_begin(); + edge_it2 += 2; + TS_ASSERT_EQUALS( (*edge_it2).getSource(), 2); + TS_ASSERT_EQUALS( (*edge_it2).getDestination(), 4); + TS_ASSERT_EQUALS((*edge_it2).getWeight(), 0); + +// toDot(g2, "itt.dot"); +// Graph read_g; +// fromDot("itt.dot", read_g); +// TS_ASSERT_EQUALS(g2, read_g); + + + + Graph g4; + g4.addVertex(3); + Graph g5 = g4; + Graph g6; + g6 = g5; + g6.removeVertex(3); +} + +};