graph_test.cpp (3053B)
1 #include "../headers/graph.hpp" 2 3 #include <catch2/catch_test_macros.hpp> 4 #include <cstdlib> 5 #include <unordered_set> 6 7 TEST_CASE( "Graph traversal invariants", "[graph traversal]" ) { 8 int vertCount = 10; 9 int edgeCount = 10; 10 float xMax = 10; 11 float yMax = 10; 12 auto g = Graph(edgeCount,vertCount,xMax,yMax); 13 int count = 0; 14 for(int i = 0; i < vertCount; ++i) { 15 g.traverseVertexIdx(i); 16 count += 1; 17 } 18 for(int i = 0; i < vertCount; ++i) { 19 REQUIRE(g.getVertex(i).visited); 20 } 21 22 bool error = false; 23 24 try { 25 g.getVertex(vertCount + 1); 26 } catch (std::invalid_argument e) { 27 error = true; 28 } 29 30 REQUIRE(error); 31 } 32 33 void createGraph(int vertCount, int edgeCount) { 34 auto g = Graph(edgeCount,vertCount,10,10); 35 } 36 37 TEST_CASE( "Graph gracefully handles stupid fucking inputs", "[graph bad inputs]" ) { 38 int vertCount = 0; 39 int edgeCount = 0; // some trivial and stupid graph 40 41 bool error = false; 42 43 try { 44 createGraph(vertCount, edgeCount); 45 } catch (std::invalid_argument e) { 46 error = true; 47 } 48 REQUIRE(!error); 49 50 vertCount = 0; 51 edgeCount = 1; // bad 52 53 error = false; 54 try { 55 createGraph(vertCount, edgeCount); 56 } catch (std::invalid_argument e) { 57 error = true; 58 } 59 60 REQUIRE(error); 61 62 } 63 64 TEST_CASE( "Graph respects max x and max y values", "[graph max values]" ) { 65 int vertCount = 10; 66 int edgeCount = 10; 67 68 for(int z = 2; z < 100; ++z) { 69 70 float xMax = 0; 71 float yMax = 0; 72 73 while (xMax == 0 || yMax == 0) { 74 xMax = rand() % z; 75 yMax = rand() % z; 76 } 77 78 auto g = Graph(edgeCount,vertCount,xMax,yMax); 79 80 for(int i = 0; i < vertCount; ++i) { 81 REQUIRE(g.getVertex(i).position.x <= xMax); 82 } 83 for(int i = 0; i < vertCount; ++i) { 84 REQUIRE(g.getVertex(i).position.y <= yMax); 85 } 86 } 87 auto g = Graph(edgeCount,vertCount,UINT32_MAX,UINT32_MAX); 88 89 for(int i = 0; i < vertCount; ++i) { 90 REQUIRE(g.getVertex(i).position.x <= UINT32_MAX); 91 } 92 for(int i = 0; i < vertCount; ++i) { 93 REQUIRE(g.getVertex(i).position.y <= UINT32_MAX); 94 } 95 } 96 97 98 std::size_t countEdges(Graph g) { 99 std::size_t vertexCount = g.getVertexCount(); 100 std::unordered_set<std::size_t> unique {}; 101 102 for(std::size_t i = 0; i < vertexCount; ++i) { 103 auto edges = g.getEdgesOfVertexIdx(i); 104 for(auto edge: edges) { 105 unique.insert(edge.identifier); 106 } 107 } 108 return unique.size(); 109 } 110 111 TEST_CASE( "Graph vertex and edge counts", "[graph counts]" ) { 112 113 for(int i = 2; i < 100; ++i) { 114 for(int x = 1; x < 10; ++x) { 115 int vertCount = i; 116 int edgeCount = x; 117 float xMax = 10; 118 float yMax = 10; 119 120 auto g = Graph(edgeCount,vertCount,xMax,yMax); 121 122 REQUIRE(g.getVertexCount() == vertCount); 123 REQUIRE(countEdges(g) == edgeCount); 124 } 125 } 126 127 }