Debian Codegraph @

sets_test.go

raw symbols SCIP
1 package graph
2
3 import (
4 "testing"
5 )
6
7 func TestDirectedUnion(t *testing.T) {
8 tests := map[string]struct {
9 gVertices []int
10 gVertexProperties map[int]VertexProperties
11 gEdges []Edge[int]
12 hVertices []int
13 hVertexProperties map[int]VertexProperties
14 hEdges []Edge[int]
15 expectedAdjacencyMap map[int]map[int]Edge[int]
16 }{
17 "two 3-vertices directed graphs": {
18 gVertices: []int{1, 2, 3},
19 gVertexProperties: map[int]VertexProperties{},
20 gEdges: []Edge[int]{
21 {Source: 1, Target: 2},
22 {Source: 2, Target: 3},
23 },
24 hVertices: []int{4, 5, 6},
25 hVertexProperties: map[int]VertexProperties{},
26 hEdges: []Edge[int]{
27 {Source: 4, Target: 5},
28 {Source: 5, Target: 6},
29 },
30 expectedAdjacencyMap: map[int]map[int]Edge[int]{
31 1: {
32 2: {Source: 1, Target: 2},
33 },
34 2: {
35 3: {Source: 2, Target: 3},
36 },
37 3: {},
38 4: {
39 5: {Source: 4, Target: 5},
40 },
41 5: {
42 6: {Source: 5, Target: 6},
43 },
44 6: {},
45 },
46 },
47 "vertices and edges with properties": {
48 gVertices: []int{1, 2},
49 gVertexProperties: map[int]VertexProperties{
50 1: {
51 Attributes: map[string]string{
52 "color": "red",
53 },
54 Weight: 10,
55 },
56 2: {
57 Attributes: map[string]string{},
58 Weight: 20,
59 },
60 },
61 gEdges: []Edge[int]{
62 {
63 Source: 1,
64 Target: 2,
65 Properties: EdgeProperties{
66 Attributes: map[string]string{
67 "label": "my-edge",
68 },
69 Weight: 42,
70 Data: "edge data #1",
71 },
72 },
73 },
74 hVertices: []int{3, 4},
75 hVertexProperties: map[int]VertexProperties{
76 3: {
77 Attributes: map[string]string{
78 "color": "blue",
79 },
80 Weight: 15,
81 },
82 },
83 hEdges: []Edge[int]{
84 {
85 Source: 3,
86 Target: 4,
87 Properties: EdgeProperties{
88 Attributes: map[string]string{
89 "label": "another-edge",
90 },
91 Weight: 50,
92 Data: "edge data #2",
93 },
94 },
95 },
96 expectedAdjacencyMap: map[int]map[int]Edge[int]{
97 1: {
98 2: {
99 Source: 1,
100 Target: 2,
101 Properties: EdgeProperties{
102 Attributes: map[string]string{
103 "label": "my-edge",
104 },
105 Weight: 42,
106 Data: "edge data #1",
107 },
108 },
109 },
110 2: {},
111 3: {
112 4: {
113 Source: 3,
114 Target: 4,
115 Properties: EdgeProperties{
116 Attributes: map[string]string{
117 "label": "another-edge",
118 },
119 Weight: 50,
120 Data: "edge data #2",
121 },
122 },
123 },
124 4: {},
125 },
126 },
127 }
128
129 for name, test := range tests {
130 g := New(IntHash, Directed())
131
132 for _, vertex := range test.gVertices {
133 _ = g.AddVertex(vertex, copyVertexProperties(test.gVertexProperties[vertex]))
134 }
135
136 for _, edge := range test.gEdges {
137 _ = g.AddEdge(copyEdge(edge))
138 }
139
140 h := New(IntHash, Directed())
141
142 for _, vertex := range test.hVertices {
143 _ = h.AddVertex(vertex, copyVertexProperties(test.gVertexProperties[vertex]))
144 }
145
146 for _, edge := range test.hEdges {
147 _ = h.AddEdge(copyEdge(edge))
148 }
149
150 union, err := Union(g, h)
151 if err != nil {
152 t.Fatalf("%s: unexpected union error: %s", name, err.Error())
153 }
154
155 unionAdjacencyMap, err := union.AdjacencyMap()
156 if err != nil {
157 t.Fatalf("%s: unexpected adjaceny map error: %s", name, err.Error())
158 }
159
160 edgesAreEqual := g.(*directed[int, int]).edgesAreEqual
161
162 if !adjacencyMapsAreEqual(test.expectedAdjacencyMap, unionAdjacencyMap, edgesAreEqual) {
163 t.Fatalf("expected adjacency map %v, got %v", test.expectedAdjacencyMap, unionAdjacencyMap)
164 }
165 }
166 }
167
168 func TestUnionFind_add(t *testing.T) {
169 tests := map[string]struct {
170 vertex int
171 expectedParent int
172 }{
173 "add vertex 1": {
174 vertex: 1,
175 expectedParent: 1,
176 },
177 }
178
179 for name, test := range tests {
180 t.Run(name, func(t *testing.T) {
181 u := newUnionFind[int]()
182 u.add(test.vertex)
183
184 if u.parents[test.vertex] != test.expectedParent {
185 t.Errorf("expected parent %v, got %v", test.expectedParent, u.parents[test.vertex])
186 }
187 })
188 }
189 }
190
191 func TestUnionFind_union(t *testing.T) {
192 tests := map[string]struct {
193 vertices []int
194 args [2]int
195 expectedParents map[int]int
196 }{
197 "merge 1 and 2": {
198 vertices: []int{1, 2, 3, 4},
199 args: [2]int{1, 2},
200 expectedParents: map[int]int{
201 1: 1,
202 2: 1,
203 3: 3,
204 4: 4,
205 },
206 },
207 }
208
209 for name, test := range tests {
210 t.Run(name, func(t *testing.T) {
211 u := newUnionFind(test.vertices...)
212 u.union(test.args[0], test.args[1])
213
214 for expectedKey, expectedValue := range test.expectedParents {
215 actualValue, ok := u.parents[expectedKey]
216 if !ok {
217 t.Fatalf("expected key %v not found", expectedKey)
218 }
219 if actualValue != expectedValue {
220 t.Fatalf("expected value %v for %v, got %v", expectedValue, expectedKey, actualValue)
221 }
222 }
223 })
224 }
225 }
226
227 func TestUnionFind_find(t *testing.T) {
228 tests := map[string]struct {
229 parents map[int]int
230 arg int
231 expected int
232 }{
233 "find 1 in sets 1, 2, 3": {
234 parents: map[int]int{
235 1: 1,
236 2: 2,
237 3: 3,
238 },
239 arg: 1,
240 expected: 1,
241 },
242 "find 3 in sets 1-2, 3-4": {
243 parents: map[int]int{
244 1: 1,
245 2: 1,
246 3: 3,
247 4: 3,
248 },
249 arg: 3,
250 expected: 3,
251 },
252 "find 4 in sets 1-2, 3-4": {
253 parents: map[int]int{
254 1: 1,
255 2: 1,
256 3: 3,
257 4: 3,
258 },
259 arg: 4,
260 expected: 3,
261 },
262 "find 3 in set 1-2-3": {
263 parents: map[int]int{
264 1: 1,
265 2: 1,
266 3: 2,
267 },
268 arg: 3,
269 expected: 1,
270 },
271 }
272
273 for name, test := range tests {
274 t.Run(name, func(t *testing.T) {
275 u := unionFind[int]{
276 parents: test.parents,
277 }
278
279 actual := u.find(test.arg)
280
281 if actual != test.expected {
282 t.Errorf("expected %v, got %v", test.expected, actual)
283 }
284 })
285 }
286 }
287
288 func adjacencyMapsAreEqual[K comparable](a, b map[K]map[K]Edge[K], edgesAreEqual func(a, b Edge[K]) bool) bool {
289 for aHash, aAdjacencies := range a {
290 bAdjacencies, ok := b[aHash]
291 if !ok {
292 return false
293 }
294
295 for aAdjacency, aEdge := range aAdjacencies {
296 bEdge, ok := bAdjacencies[aAdjacency]
297 if !ok {
298 return false
299 }
300
301 if !edgesAreEqual(aEdge, bEdge) {
302 return false
303 }
304
305 for aKey, aValue := range aEdge.Properties.Attributes {
306 bValue, ok := bEdge.Properties.Attributes[aKey]
307 if !ok {
308 return false
309 }
310 if bValue != aValue {
311 return false
312 }
313 }
314
315 if bEdge.Properties.Weight != aEdge.Properties.Weight {
316 return false
317 }
318 }
319 }
320
321 for aHash := range a {
322 if _, ok := b[aHash]; !ok {
323 return false
324 }
325 }
326
327 return true
328 }
329
330 func mapsAreEqual[K comparable](a, b map[K]K) bool {
331 for aHash, aValue := range a {
332 bValue, ok := b[aHash]
333 if !ok {
334 return false
335 }
336
337 if aValue != bValue {
338 return false
339 }
340 }
341
342 for aHash := range a {
343 if _, ok := b[aHash]; !ok {
344 return false
345 }
346 }
347
348 return true
349 }