-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathTestLoopDetection.cpp
More file actions
168 lines (135 loc) · 4.84 KB
/
TestLoopDetection.cpp
File metadata and controls
168 lines (135 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "ApplicationSetup.hpp"
#include "TestGraphModel.hpp"
#include "TestDataFlowNodes.hpp"
#include <catch2/catch.hpp>
#include <QtNodes/DataFlowGraphModel>
#include <QtNodes/NodeDelegateModelRegistry>
using QtNodes::ConnectionId;
using QtNodes::DataFlowGraphModel;
using QtNodes::NodeDelegateModelRegistry;
using QtNodes::NodeId;
/// Test model that allows loops (default behavior)
class LoopEnabledModel : public TestGraphModel
{
public:
bool loopsEnabled() const override { return true; }
};
/// Test model that disables loops
class LoopDisabledModel : public TestGraphModel
{
public:
bool loopsEnabled() const override { return false; }
};
TEST_CASE("Loop detection configuration", "[loops]")
{
SECTION("Default AbstractGraphModel allows loops")
{
TestGraphModel model;
CHECK(model.loopsEnabled() == true);
}
SECTION("DataFlowGraphModel disables loops by default")
{
auto app = applicationSetup();
auto registry = std::make_shared<NodeDelegateModelRegistry>();
registry->registerModel<TestSourceNode>("Sources");
DataFlowGraphModel model(registry);
CHECK(model.loopsEnabled() == false);
}
SECTION("Custom model can enable loops")
{
LoopEnabledModel model;
CHECK(model.loopsEnabled() == true);
}
SECTION("Custom model can disable loops")
{
LoopDisabledModel model;
CHECK(model.loopsEnabled() == false);
}
}
TEST_CASE("Loop detection in DataFlowGraphModel", "[loops]")
{
auto app = applicationSetup();
auto registry = std::make_shared<NodeDelegateModelRegistry>();
registry->registerModel<TestSourceNode>("Sources");
registry->registerModel<TestDisplayNode>("Sinks");
DataFlowGraphModel model(registry);
SECTION("Direct self-loop is not possible")
{
NodeId node1 = model.addNode("TestSourceNode");
// Try to connect node to itself
ConnectionId selfLoop{node1, 0, node1, 0};
CHECK_FALSE(model.connectionPossible(selfLoop));
}
SECTION("Simple A->B connection is allowed")
{
NodeId node1 = model.addNode("TestSourceNode");
NodeId node2 = model.addNode("TestDisplayNode");
ConnectionId conn{node1, 0, node2, 0};
CHECK(model.connectionPossible(conn));
model.addConnection(conn);
CHECK(model.connectionExists(conn));
}
SECTION("Indirect loop A->B->A is prevented")
{
// Use TestDisplayNode which has both input and output ports
NodeId node1 = model.addNode("TestDisplayNode");
NodeId node2 = model.addNode("TestDisplayNode");
// Create A->B connection
ConnectionId conn1{node1, 0, node2, 0};
CHECK(model.connectionPossible(conn1));
model.addConnection(conn1);
// Try to create B->A connection (would form a loop)
ConnectionId conn2{node2, 0, node1, 0};
CHECK_FALSE(model.connectionPossible(conn2));
}
SECTION("Three node loop A->B->C->A is prevented")
{
// Use TestDisplayNode which has both input and output ports
NodeId node1 = model.addNode("TestDisplayNode");
NodeId node2 = model.addNode("TestDisplayNode");
NodeId node3 = model.addNode("TestDisplayNode");
// Create A->B
ConnectionId conn1{node1, 0, node2, 0};
model.addConnection(conn1);
CHECK(model.connectionExists(conn1));
// Create B->C
ConnectionId conn2{node2, 0, node3, 0};
model.addConnection(conn2);
CHECK(model.connectionExists(conn2));
// Try to create C->A (would form a loop)
ConnectionId conn3{node3, 0, node1, 0};
CHECK_FALSE(model.connectionPossible(conn3));
}
}
TEST_CASE("Loop-enabled model allows cycles", "[loops]")
{
LoopEnabledModel model;
NodeId node1 = model.addNode("Node1");
NodeId node2 = model.addNode("Node2");
// Create A->B connection
ConnectionId conn1{node1, 0, node2, 0};
model.addConnection(conn1);
CHECK(model.connectionExists(conn1));
SECTION("B->A connection is allowed when loops enabled")
{
ConnectionId conn2{node2, 0, node1, 0};
// With loops enabled, this should be possible
CHECK(model.connectionPossible(conn2));
}
}
TEST_CASE("Loop-disabled model prevents cycles", "[loops]")
{
LoopDisabledModel model;
NodeId node1 = model.addNode("Node1");
NodeId node2 = model.addNode("Node2");
// Note: TestGraphModel's connectionPossible doesn't check for loops,
// it only checks if nodes exist and aren't self-connecting.
// The loop detection is in DataFlowGraphModel's implementation.
// This test documents the expected behavior when properly implemented.
ConnectionId conn1{node1, 0, node2, 0};
model.addConnection(conn1);
SECTION("Loop-disabled model configuration")
{
CHECK(model.loopsEnabled() == false);
}
}