From ad57a9868a096511ba0f07f2a442e8b94a0dbbea Mon Sep 17 00:00:00 2001
From: Maxime Lalisse <maxime.lalisse@gmail.com>
Date: Tue, 9 Apr 2024 12:13:34 +0200
Subject: [PATCH] q3: decorators

---
 test/test_gnt.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/test/test_gnt.cpp b/test/test_gnt.cpp
index 3dc00d1..3ecb790 100644
--- a/test/test_gnt.cpp
+++ b/test/test_gnt.cpp
@@ -107,3 +107,80 @@ SCENARIO("Shortest paths", "[graph nt]")
         }
     }
 }
+
+class EdgeData {
+    std::string str;
+    double l;
+public:
+    void set_string(const std::string &s) { str = s; }
+    std::string get_string() const { return str; }
+    void set_length(double len) { l = len; }
+    double get_length(double len) const { return l; }       
+};
+
+
+SCENARIO("Decorators", "[graph nt]")
+{
+    GIVEN("A graph with some items") {
+        Graph<string, EdgeData> g;
+        int a = g.add_node("A");
+        int b = g.add_node("B");
+        int c = g.add_node("C");
+        int d = g.add_node("D");
+        EdgeData _ab, _ac, _bd, _cd;
+        _ab.set_string("msg-ab");
+        _ac.set_string("msg-ac");
+        _bd.set_string("msg-bd");
+        _cd.set_string("msg-cd");
+        _ab.set_length(1.0);
+        _ac.set_length(2.0);
+        _bd.set_length(3.0);
+        _cd.set_length(4.0);
+        int ab = g.add_edge(_ab, a,b);
+        int ac = g.add_edge(_ac, a,c);
+        int bd = g.add_edge(_bd, b,d);
+        int cd = g.add_edge(_cd, c,d);
+    
+        REQUIRE(g.get_node_data(a) == "A");
+        REQUIRE(g.get_node_data(b) == "B");
+        REQUIRE(g.get_node_data(c) == "C");
+        REQUIRE(g.get_node_data(d) == "D");
+        
+        REQUIRE(ab == 0);
+        REQUIRE(ac == 1);
+        
+        EdgeData __ab = g.get_edge_data(ab);
+        EdgeData __ac = g.get_edge_data(ac);
+        EdgeData __cd = g.get_edge_data(cd);
+        EdgeData __bd = g.get_edge_data(bd);
+        REQUIRE(__ab.get_string() == "msg-ab");
+        REQUIRE(__ac.get_string() == "msg-ac");
+        REQUIRE(__cd.get_string() == "msg-cd");
+        REQUIRE(__bd.get_string() == "msg-bd");
+
+        vector<int> succ_a = {b, c};
+        REQUIRE(g.get_successors(a) == succ_a);
+        vector<int> succ_b = {d};
+        REQUIRE(g.get_successors(b) == succ_b);
+        vector<int> prec_d = {b, c};
+        REQUIRE(g.get_predecessors(d) == prec_d);
+
+        WHEN("Copying the graph") {
+            Graph g1(g);
+            THEN ("We obtain the same graph, with the same indexes") {
+                REQUIRE(g1.get_predecessors(d) == prec_d);
+            }
+        }
+        WHEN("Changing the copy") {
+            Graph g2(g);
+            EdgeData _bc;
+            _bc.set_string("msg-bc");
+            _bc.set_length(5.0);
+            g2.add_edge(_bc, b,c);
+            THEN ("The original is not changed") {
+                REQUIRE(g.get_successors(b).size() == 1);
+                REQUIRE(g2.get_successors(b).size() == 2);
+            }
+        }
+    }
+}
-- 
GitLab