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