commit 4206dceba3c31cb5e99338f6639b749a2761cd24
parent 10d4cfe3526254a5e5b526ca8010f624db7cb4ea
Author: Andrew Laack <andrew@laack.co>
Date: Wed, 16 Sep 2026 00:51:08 -0500
Started adding snapshot tests
Diffstat:
7 files changed, 89 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -4,7 +4,14 @@ install: build
cp abg.out /usr/local/bin/abg
clean:
rm abg.out
+update-snapshot:
+ g++ tests/snapshot_update.cpp src/prim.cpp src/graph.cpp src/edge.cpp src/utils.cpp src/vertex.cpp src/background.cpp -lCatch2Main -lCatch2 -lraylib -lX11 -o snapshot_update
+ ./snapshot_update
+ rm snapshot_update
test:
+ g++ tests/snapshot_test.cpp src/prim.cpp src/graph.cpp src/edge.cpp src/utils.cpp src/vertex.cpp src/background.cpp -lCatch2Main -lCatch2 -lraylib -lX11 -o snapshot_tests
+ ./snapshot_tests
+ rm snapshot_tests
g++ tests/graph_test.cpp src/prim.cpp src/graph.cpp src/edge.cpp src/utils.cpp src/vertex.cpp src/background.cpp -lCatch2Main -lCatch2 -lraylib -lX11 -o graph_tests
./graph_tests
rm graph_tests
diff --git a/src/edge.cpp b/src/edge.cpp
@@ -1,11 +1,12 @@
#include "../include/edge.hpp"
#include <cstddef>
+#include <string>
Edge::Edge(std::size_t v1, std::size_t v2, double length2, std::size_t identifier)
: v1Index(v1), v2Index(v2), length2(length2), identifier(identifier) {}
std::string Edge::toString() const noexcept {
- return "(" + std::to_string(this->v1Index) + ", " + std::to_string(this->v2Index) + ")";
+ return "(v1: " + std::to_string(this->v1Index) + ", v2: " + std::to_string(this->v2Index) + + ", traversed: " + std::to_string(this->traversed) + ")";
}
bool Edge::operator<(const Edge& other) const {
diff --git a/src/vertex.cpp b/src/vertex.cpp
@@ -7,7 +7,7 @@ Vertex::Vertex(Vector2 position, float drawSize)
std::string Vertex::toString() {
- std::string result = "(" + std::to_string(this->position.x) + ", " + std::to_string(this->position.y) + ")";
+ std::string result = "(x: " + std::to_string(this->position.x) + ", y: " + std::to_string(this->position.y) + ", visited: "+ std::to_string(this->visited) + ")";
return result;
}
diff --git a/tests/snapshot/basicGraph.out b/tests/snapshot/basicGraph.out
@@ -0,0 +1,2 @@
+edges: {(v1: 5, v2: 7, traversed: 0)(v1: 1, v2: 0, traversed: 0)(v1: 1, v2: 6, traversed: 0)(v1: 6, v2: 4, traversed: 0)(v1: 1, v2: 6, traversed: 0)(v1: 4, v2: 1, traversed: 0)(v1: 1, v2: 0, traversed: 0)(v1: 9, v2: 4, traversed: 0)(v1: 6, v2: 4, traversed: 0)(v1: 4, v2: 1, traversed: 0)(v1: 9, v2: 3, traversed: 0)(v1: 3, v2: 7, traversed: 0)(v1: 9, v2: 3, traversed: 0)(v1: 7, v2: 9, traversed: 0)(v1: 9, v2: 4, traversed: 0)(v1: 7, v2: 2, traversed: 0)(v1: 7, v2: 2, traversed: 0)(v1: 7, v2: 9, traversed: 0)(v1: 3, v2: 7, traversed: 0)(v1: 5, v2: 7, traversed: 0)}
+vertices: {(x: 6.000000, y: 0.000000, visited: 0)(x: 1.000000, y: 1.000000, visited: 0)(x: 2.000000, y: 8.000000, visited: 0)(x: 1.000000, y: 0.000000, visited: 0)(x: 5.000000, y: 3.000000, visited: 0)(x: 4.000000, y: 3.000000, visited: 0)(x: 7.000000, y: 4.000000, visited: 0)(x: 6.000000, y: 2.000000, visited: 0)(x: 2.000000, y: 8.000000, visited: 0)(x: 8.000000, y: 9.000000, visited: 0)}+
\ No newline at end of file
diff --git a/tests/snapshot/traversedGraph.out b/tests/snapshot/traversedGraph.out
@@ -0,0 +1,2 @@
+edges: {(v1: 1, v2: 0, traversed: 1)(v1: 1, v2: 0, traversed: 1)}
+vertices: {(x: 1606.000000, y: 420.000000, visited: 1)(x: 3121.000000, y: 1161.000000, visited: 1)}+
\ No newline at end of file
diff --git a/tests/snapshot_test.cpp b/tests/snapshot_test.cpp
@@ -0,0 +1,13 @@
+#include "../include/graph.hpp"
+#include <catch2/catch_test_macros.hpp>
+#include <iostream>
+
+TEST_CASE( "Basic graph serialization snapshot", "[basic graph serialization snapshot]" ) {
+ srand(42);
+ int vertCount = 10;
+ int edgeCount = 10;
+ float xMax = 10;
+ float yMax = 10;
+ auto g = Graph(edgeCount,vertCount,xMax,yMax);
+ std::cout << g.toString() << std::endl;
+}
diff --git a/tests/snapshot_update.cpp b/tests/snapshot_update.cpp
@@ -0,0 +1,60 @@
+#include "../include/graph.hpp"
+#include "../include/prim.hpp"
+#include <fstream>
+#include <filesystem>
+#include <queue>
+#include <unordered_set>
+
+
+void snapshotGraph(std::string testName, Graph g) {
+
+ std::filesystem::path path{ "tests/snapshot" };
+ path /= testName + ".out";
+ std::filesystem::create_directories(path.parent_path());
+ std::ofstream ofs(path);
+ ofs << g.toString();
+
+}
+
+void updateBasicGraphSerialization() {
+ srand(42);
+ int vertCount = 10;
+ int edgeCount = 10;
+ float xMax = 10;
+ float yMax = 10;
+ auto g = Graph(edgeCount,vertCount,xMax,yMax);
+ snapshotGraph("basicGraph",g);
+}
+
+
+void updateFullTraversalSerialization() {
+ srand(42);
+ std::size_t edgeCount = 1;
+ std::size_t vertCount = 2;
+
+ float xMax = 5120;
+ float yMax = 1440;
+
+ Graph g = Graph(edgeCount, vertCount, xMax,yMax);
+ std::unordered_set<std::size_t> visitedIndices {};
+ std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> toVisit {};
+ std::vector<Edge> edges = g.getEdgesOfVertexIdx(0);
+
+ for(auto edge: edges) {
+ toVisit.push(edge);
+ }
+
+ g.traverseVertexIdx(0);
+ visitedIndices.insert(0);
+
+ while (toVisit.size() != 0) {
+ oneStepPrim(toVisit, visitedIndices, g);
+ }
+
+ snapshotGraph("traversedGraph",g);
+}
+
+int main() {
+ updateBasicGraphSerialization();
+ updateFullTraversalSerialization();
+}