Debian Codegraph @

undirected_test.go

raw symbols SCIP
1 package graph
2
3 import (
4 "errors"
5 "testing"
6 )
7
8 func TestUndirected_Traits(t *testing.T) {
9 tests := map[string]struct {
10 traits *Traits
11 expected *Traits
12 }{
13 "default traits": {
14 traits: &Traits{},
15 expected: &Traits{},
16 },
17 "directed": {
18 traits: &Traits{IsDirected: true},
19 expected: &Traits{IsDirected: true},
20 },
21 }
22
23 for name, test := range tests {
24 g := newUndirected(IntHash, test.traits, newMemoryStore[int, int]())
25 traits := g.Traits()
26
27 if !traitsAreEqual(traits, test.expected) {
28 t.Errorf("%s: traits expectancy doesn't match: expected %v, got %v", name, test.expected, traits)
29 }
30 }
31 }
32
33 func TestUndirected_AddVertex(t *testing.T) {
34 tests := map[string]struct {
35 vertices []int
36 properties *VertexProperties
37 expectedVertices []int
38 expectedProperties *VertexProperties
39 // Even though some AddVertex calls might work, at least one of them
40 // could fail, e.g. if the last call would add an existing vertex.
41 finallyExpectedError error
42 }{
43 "graph with 3 vertices": {
44 vertices: []int{1, 2, 3},
45 properties: &VertexProperties{
46 Attributes: map[string]string{"color": "red"},
47 Weight: 10,
48 },
49 expectedVertices: []int{1, 2, 3},
50 expectedProperties: &VertexProperties{
51 Attributes: map[string]string{"color": "red"},
52 Weight: 10,
53 },
54 },
55 "graph with duplicated vertex": {
56 vertices: []int{1, 2, 2},
57 expectedVertices: []int{1, 2},
58 finallyExpectedError: ErrVertexAlreadyExists,
59 },
60 }
61
62 for name, test := range tests {
63 graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]())
64
65 var err error
66
67 for _, vertex := range test.vertices {
68 if test.properties == nil {
69 err = graph.AddVertex(vertex)
70 continue
71 }
72 // If there are vertex attributes, iterate over them and call the
73 // VertexAttribute functional option for each entry. A vertex should
74 // only have one attribute so that AddVertex is invoked once.
75 for key, value := range test.properties.Attributes {
76 err = graph.AddVertex(vertex, VertexWeight(test.properties.Weight), VertexAttribute(key, value))
77 }
78 }
79
80 if err != test.finallyExpectedError {
81 t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.finallyExpectedError, err)
82 }
83
84 graphStore := graph.store.(*memoryStore[int, int])
85
86 for _, vertex := range test.vertices {
87 if len(graphStore.vertices) != len(test.expectedVertices) {
88 t.Errorf("%s: vertex count doesn't match: expected %v, got %v", name, len(test.expectedVertices), len(graphStore.vertices))
89 }
90
91 hash := graph.hash(vertex)
92 vertices := graph.store.(*memoryStore[int, int]).vertices
93 if _, ok := vertices[hash]; !ok {
94 t.Errorf("%s: vertex %v not found in graph: %v", name, vertex, vertices)
95 }
96
97 if test.properties == nil {
98 continue
99 }
100
101 if graphStore.vertexProperties[hash].Weight != test.expectedProperties.Weight {
102 t.Errorf("%s: edge weights don't match: expected weight %v, got %v", name, test.expectedProperties.Weight, graphStore.vertexProperties[hash].Weight)
103 }
104
105 if len(graphStore.vertexProperties[hash].Attributes) != len(test.expectedProperties.Attributes) {
106 t.Fatalf("%s: attributes lengths don't match: expcted %v, got %v", name, len(test.expectedProperties.Attributes), len(graphStore.vertexProperties[hash].Attributes))
107 }
108
109 for expectedKey, expectedValue := range test.expectedProperties.Attributes {
110 value, ok := graphStore.vertexProperties[hash].Attributes[expectedKey]
111 if !ok {
112 t.Errorf("%s: attribute keys don't match: expected key %v not found", name, expectedKey)
113 }
114 if value != expectedValue {
115 t.Errorf("%s: attribute values don't match: expected value %v for key %v, got %v", name, expectedValue, expectedKey, value)
116 }
117 }
118 }
119 }
120 }
121
122 func TestUndirected_AddVerticesFrom(t *testing.T) {
123 tests := map[string]struct {
124 vertices []int
125 properties map[int]VertexProperties
126 existingVertices []int
127 expectedVertices []int
128 expectedProperties map[int]VertexProperties
129 expectedError error
130 }{
131 "graph with 3 vertices": {
132 vertices: []int{1, 2, 3},
133 properties: map[int]VertexProperties{
134 1: {
135 Attributes: map[string]string{"color": "red"},
136 Weight: 10,
137 },
138 2: {
139 Attributes: map[string]string{"color": "green"},
140 Weight: 20,
141 },
142 3: {
143 Attributes: map[string]string{"color": "blue"},
144 Weight: 30,
145 },
146 },
147 existingVertices: []int{},
148 expectedVertices: []int{1, 2, 3},
149 expectedProperties: map[int]VertexProperties{
150 1: {
151 Attributes: map[string]string{"color": "red"},
152 Weight: 10,
153 },
154 2: {
155 Attributes: map[string]string{"color": "green"},
156 Weight: 20,
157 },
158 3: {
159 Attributes: map[string]string{"color": "blue"},
160 Weight: 30,
161 },
162 },
163 },
164 "graph with duplicated vertex": {
165 vertices: []int{1, 2, 3},
166 properties: map[int]VertexProperties{},
167 existingVertices: []int{2},
168 expectedVertices: []int{1},
169 expectedProperties: map[int]VertexProperties{},
170 expectedError: ErrVertexAlreadyExists,
171 },
172 }
173
174 for name, test := range tests {
175 t.Run(name, func(t *testing.T) {
176 source := New(IntHash)
177
178 for _, vertex := range test.vertices {
179 _ = source.AddVertex(vertex, copyVertexProperties(test.properties[vertex]))
180 }
181
182 g := New(IntHash)
183
184 for _, vertex := range test.existingVertices {
185 _ = g.AddVertex(vertex)
186 }
187
188 err := g.AddVerticesFrom(source)
189
190 if !errors.Is(err, test.expectedError) {
191 t.Errorf("expected error %v, got %v", test.expectedError, err)
192 }
193
194 if err != nil {
195 return
196 }
197
198 for _, vertex := range test.expectedVertices {
199 _, actualProperties, err := g.VertexWithProperties(vertex)
200 if err != nil {
201 t.Errorf("failed to get vertex %v with properties: %v", vertex, err.Error())
202 return
203 }
204
205 if expectedProperties, ok := test.expectedProperties[vertex]; ok {
206 if !vertexPropertiesAreEqual(expectedProperties, actualProperties) {
207 t.Errorf("expected properties %v for %v, got %v", expectedProperties, vertex, actualProperties)
208 }
209 }
210 }
211 })
212 }
213 }
214
215 func TestUndirected_Vertex(t *testing.T) {
216 tests := map[string]struct {
217 vertices []int
218 vertex int
219 expectedError error
220 }{
221 "existing vertex": {
222 vertices: []int{1, 2, 3},
223 vertex: 2,
224 },
225 "non-existent vertex": {
226 vertices: []int{1, 2, 3},
227 vertex: 4,
228 expectedError: ErrVertexNotFound,
229 },
230 }
231
232 for name, test := range tests {
233 graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]())
234
235 for _, vertex := range test.vertices {
236 _ = graph.AddVertex(vertex)
237 }
238
239 vertex, err := graph.Vertex(test.vertex)
240
241 if err != test.expectedError {
242 t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err)
243 }
244
245 if test.expectedError != nil {
246 continue
247 }
248
249 if vertex != test.vertex {
250 t.Errorf("%s: vertex expectancy doesn't match: expected %v, got %v", name, test.vertex, vertex)
251 }
252 }
253 }
254
255 func TestUndirected_AddEdge(t *testing.T) {
256 tests := map[string]struct {
257 vertices []int
258 edges []Edge[int]
259 traits *Traits
260 expectedEdges []Edge[int]
261 // Even though some AddVertex calls might work, at least one of them
262 // could fail, e.g. if the last call would introduce a cycle.
263 finallyExpectedError error
264 }{
265 "graph with 2 edges": {
266 vertices: []int{1, 2, 3},
267 edges: []Edge[int]{
268 {Source: 1, Target: 2, Properties: EdgeProperties{Weight: 10}},
269 {Source: 1, Target: 3, Properties: EdgeProperties{Weight: 20}},
270 },
271 traits: &Traits{},
272 expectedEdges: []Edge[int]{
273 {Source: 1, Target: 2, Properties: EdgeProperties{Weight: 10}},
274 {Source: 1, Target: 3, Properties: EdgeProperties{Weight: 20}},
275 },
276 },
277 "hashes for non-existent vertices": {
278 vertices: []int{1, 2},
279 edges: []Edge[int]{
280 {Source: 1, Target: 3, Properties: EdgeProperties{Weight: 20}},
281 },
282 traits: &Traits{},
283 finallyExpectedError: ErrVertexNotFound,
284 },
285 "edge introducing a cycle in an acyclic graph": {
286 vertices: []int{1, 2, 3},
287 edges: []Edge[int]{
288 {Source: 1, Target: 2},
289 {Source: 2, Target: 3},
290 {Source: 3, Target: 1},
291 },
292 traits: &Traits{
293 PreventCycles: true,
294 },
295 finallyExpectedError: ErrEdgeCreatesCycle,
296 },
297 "edge already exists": {
298 vertices: []int{1, 2, 3},
299 edges: []Edge[int]{
300 {Source: 1, Target: 2},
301 {Source: 2, Target: 3},
302 {Source: 3, Target: 1},
303 {Source: 3, Target: 1},
304 },
305 traits: &Traits{},
306 finallyExpectedError: ErrEdgeAlreadyExists,
307 },
308 "edge with attributes": {
309 vertices: []int{1, 2},
310 edges: []Edge[int]{
311 {
312 Source: 1,
313 Target: 2,
314 Properties: EdgeProperties{
315 Attributes: map[string]string{
316 "color": "red",
317 },
318 },
319 },
320 },
321 expectedEdges: []Edge[int]{
322 {
323 Source: 1,
324 Target: 2,
325 Properties: EdgeProperties{
326 Attributes: map[string]string{
327 "color": "red",
328 },
329 },
330 },
331 },
332 traits: &Traits{},
333 },
334 "edge with data": {
335 vertices: []int{1, 2},
336 edges: []Edge[int]{
337 {
338 Source: 1,
339 Target: 2,
340 Properties: EdgeProperties{
341 Data: "foo",
342 },
343 },
344 },
345 expectedEdges: []Edge[int]{
346 {
347 Source: 1,
348 Target: 2,
349 Properties: EdgeProperties{
350 Data: "foo",
351 },
352 },
353 },
354 traits: &Traits{},
355 },
356 }
357
358 for name, test := range tests {
359 graph := newUndirected(IntHash, test.traits, newMemoryStore[int, int]())
360
361 for _, vertex := range test.vertices {
362 _ = graph.AddVertex(vertex)
363 }
364
365 var err error
366
367 for _, edge := range test.edges {
368 if len(edge.Properties.Attributes) == 0 {
369 err = graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight), EdgeData(edge.Properties.Data))
370 }
371 // If there are vertex attributes, iterate over them and call the
372 // VertexAttribute functional option for each entry. A vertex should
373 // only have one attribute so that AddVertex is invoked once.
374 for key, value := range edge.Properties.Attributes {
375 err = graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight), EdgeData(edge.Properties.Data), EdgeAttribute(key, value))
376 }
377 if err != nil {
378 break
379 }
380 }
381
382 if !errors.Is(err, test.finallyExpectedError) {
383 t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, test.finallyExpectedError, err)
384 }
385
386 for _, expectedEdge := range test.expectedEdges {
387 sourceHash := graph.hash(expectedEdge.Source)
388 targetHash := graph.hash(expectedEdge.Target)
389
390 edge, ok := graph.store.(*memoryStore[int, int]).outEdges[sourceHash][targetHash]
391 if !ok {
392 t.Fatalf("%s: edge with source %v and target %v not found", name, expectedEdge.Source, expectedEdge.Target)
393 }
394
395 if !edgesAreEqual(expectedEdge, edge, false) {
396 t.Errorf("%s: expected edge %v, got %v", name, expectedEdge, edge)
397 }
398 }
399 }
400 }
401
402 func TestUndirected_AddEdgesFrom(t *testing.T) {
403 tests := map[string]struct {
404 vertices []int
405 edges []Edge[int]
406 existingVertices []int
407 existingEdges []Edge[int]
408 expectedEdges []Edge[int]
409 expectedError error
410 }{
411 "graph with 3 edges": {
412 vertices: []int{1, 2, 3},
413 edges: []Edge[int]{
414 {
415 Source: 1,
416 Target: 2,
417 Properties: EdgeProperties{
418 Weight: 10,
419 Attributes: map[string]string{
420 "color": "red",
421 },
422 },
423 },
424 {
425 Source: 2,
426 Target: 3,
427 Properties: EdgeProperties{
428 Weight: 20,
429 Attributes: map[string]string{
430 "color": "green",
431 },
432 },
433 },
434 {
435 Source: 3,
436 Target: 1,
437 Properties: EdgeProperties{
438 Weight: 30,
439 Attributes: map[string]string{
440 "color": "blue",
441 },
442 },
443 },
444 },
445 existingVertices: []int{1, 2, 3},
446 existingEdges: []Edge[int]{},
447 expectedEdges: []Edge[int]{
448 {
449 Source: 1,
450 Target: 2,
451 Properties: EdgeProperties{
452 Weight: 10,
453 Attributes: map[string]string{
454 "color": "red",
455 },
456 },
457 },
458 {
459 Source: 2,
460 Target: 3,
461 Properties: EdgeProperties{
462 Weight: 20,
463 Attributes: map[string]string{
464 "color": "green",
465 },
466 },
467 },
468 {
469 Source: 3,
470 Target: 1,
471 Properties: EdgeProperties{
472 Weight: 30,
473 Attributes: map[string]string{
474 "color": "blue",
475 },
476 },
477 },
478 },
479 expectedError: nil,
480 },
481 "edge with non-existing vertex": {
482 vertices: []int{1, 2, 3},
483 edges: []Edge[int]{
484 {Source: 1, Target: 3},
485 },
486 existingVertices: []int{1, 2},
487 existingEdges: []Edge[int]{},
488 expectedEdges: []Edge[int]{},
489 expectedError: ErrVertexNotFound,
490 },
491 "graph with duplicated edge": {
492 vertices: []int{1, 2},
493 edges: []Edge[int]{
494 {Source: 1, Target: 2},
495 },
496 existingVertices: []int{1, 2},
497 existingEdges: []Edge[int]{
498 {Source: 1, Target: 2},
499 },
500 expectedEdges: []Edge[int]{},
501 expectedError: ErrEdgeAlreadyExists,
502 },
503 }
504
505 for name, test := range tests {
506 t.Run(name, func(t *testing.T) {
507 source := New(IntHash)
508
509 for _, vertex := range test.vertices {
510 _ = source.AddVertex(vertex)
511 }
512
513 for _, edge := range test.edges {
514 _ = source.AddEdge(copyEdge(edge))
515 }
516
517 g := New(IntHash)
518
519 for _, vertex := range test.existingVertices {
520 _ = g.AddVertex(vertex)
521 }
522
523 for _, edge := range test.existingEdges {
524 _ = g.AddEdge(copyEdge(edge))
525 }
526
527 err := g.AddEdgesFrom(source)
528
529 if !errors.Is(err, test.expectedError) {
530 t.Fatalf("expected error %v, got %v", test.expectedError, err)
531 }
532
533 for _, edge := range test.expectedEdges {
534 actualEdge, err := g.Edge(edge.Source, edge.Target)
535 if err != nil {
536 t.Fatalf("failed to get edge (%v, %v): %v", edge.Source, edge.Target, err.Error())
537 }
538
539 if !edgesAreEqual(edge, actualEdge, false) {
540 t.Errorf("%s: expected edge %v, got %v", name, edge, actualEdge)
541 }
542 }
543 })
544 }
545 }
546
547 func TestUndirected_RemoveVertex(t *testing.T) {
548 tests := map[string]struct {
549 vertices []int
550 edges []Edge[int]
551 vertex int
552 expectedError error
553 }{
554 "existing disconnected vertex": {
555 vertices: []int{1, 2, 3},
556 edges: []Edge[int]{
557 {Source: 2, Target: 3},
558 },
559 vertex: 1,
560 },
561 "existing connected vertex": {
562 vertices: []int{1, 2, 3},
563 edges: []Edge[int]{
564 {Source: 1, Target: 2},
565 {Source: 2, Target: 3},
566 },
567 vertex: 1,
568 expectedError: ErrVertexHasEdges,
569 },
570 "non-existent vertex": {
571 vertices: []int{1, 2, 3},
572 edges: []Edge[int]{},
573 vertex: 4,
574 expectedError: ErrVertexNotFound,
575 },
576 }
577
578 for name, test := range tests {
579 graph := New(IntHash)
580
581 for _, vertex := range test.vertices {
582 _ = graph.AddVertex(vertex)
583 }
584
585 for _, edge := range test.edges {
586 _ = graph.AddEdge(edge.Source, edge.Target)
587 }
588
589 err := graph.RemoveVertex(test.vertex)
590
591 if !errors.Is(err, test.expectedError) {
592 t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err)
593 }
594 }
595 }
596
597 func TestUndirected_Edge(t *testing.T) {
598 tests := map[string]struct {
599 vertices []int
600 edge Edge[int]
601 args [2]int
602 expectedError error
603 }{
604 "get edge of undirected graph": {
605 vertices: []int{1, 2, 3},
606 edge: Edge[int]{Source: 1, Target: 2},
607 args: [2]int{1, 2},
608 },
609 "get edge of undirected graph with swapped source and target": {
610 vertices: []int{1, 2, 3},
611 edge: Edge[int]{Source: 1, Target: 2},
612 args: [2]int{2, 1},
613 },
614 "get non-existent edge of undirected graph": {
615 vertices: []int{1, 2, 3},
616 edge: Edge[int]{Source: 1, Target: 2},
617 args: [2]int{2, 3},
618 expectedError: ErrEdgeNotFound,
619 },
620 "get edge with properties": {
621 vertices: []int{1, 2, 3},
622 edge: Edge[int]{
623 Source: 1,
624 Target: 2,
625 Properties: EdgeProperties{
626 // Attributes can't be tested at the moment, because there
627 // is no way to add multiple attributes at once (using a
628 // functional option like EdgeAttributes).
629 // ToDo: Add Attributes once EdgeAttributes exists.
630 Attributes: map[string]string{},
631 Weight: 10,
632 Data: "this is an edge",
633 },
634 },
635 args: [2]int{1, 2},
636 },
637 }
638
639 for name, test := range tests {
640 graph := New(IntHash)
641
642 for _, vertex := range test.vertices {
643 _ = graph.AddVertex(vertex)
644 }
645
646 if err := graph.AddEdge(test.edge.Source, test.edge.Target, EdgeWeight(test.edge.Properties.Weight), EdgeData(test.edge.Properties.Data)); err != nil {
647 t.Fatalf("%s: failed to add edge: %s", name, err.Error())
648 }
649
650 edge, err := graph.Edge(test.args[0], test.args[1])
651
652 if !errors.Is(err, test.expectedError) {
653 t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err)
654 }
655
656 if test.expectedError != nil {
657 continue
658 }
659
660 if edge.Source != test.args[0] {
661 t.Errorf("%s: source expectancy doesn't match: expected %v, got %v", name, test.args[0], edge.Source)
662 }
663
664 if edge.Target != test.args[1] {
665 t.Errorf("%s: target expectancy doesn't match: expected %v, got %v", name, test.args[1], edge.Target)
666 }
667
668 if !edgesAreEqual(test.edge, edge, false) {
669 t.Errorf("%s: expected edge %v, got %v", name, test.edge, edge)
670 }
671 }
672 }
673
674 func TestUndirected_Edges(t *testing.T) {
675 tests := map[string]struct {
676 vertices []int
677 edges []Edge[int]
678 expectedEdges []Edge[int]
679 }{
680 "graph with 3 edges": {
681 vertices: []int{1, 2, 3},
682 edges: []Edge[int]{
683 {
684 Source: 1,
685 Target: 2,
686 Properties: EdgeProperties{
687 Weight: 10,
688 Attributes: map[string]string{
689 "color": "red",
690 },
691 },
692 },
693 {
694 Source: 2,
695 Target: 3,
696 Properties: EdgeProperties{
697 Weight: 20,
698 Attributes: map[string]string{
699 "color": "green",
700 },
701 },
702 },
703 {
704 Source: 3,
705 Target: 1,
706 Properties: EdgeProperties{
707 Weight: 30,
708 Attributes: map[string]string{
709 "color": "blue",
710 },
711 },
712 },
713 },
714 expectedEdges: []Edge[int]{
715 {
716 Source: 1,
717 Target: 2,
718 Properties: EdgeProperties{
719 Weight: 10,
720 Attributes: map[string]string{
721 "color": "red",
722 },
723 },
724 },
725 {
726 Source: 2,
727 Target: 3,
728 Properties: EdgeProperties{
729 Weight: 20,
730 Attributes: map[string]string{
731 "color": "green",
732 },
733 },
734 },
735 {
736 Source: 3,
737 Target: 1,
738 Properties: EdgeProperties{
739 Weight: 30,
740 Attributes: map[string]string{
741 "color": "blue",
742 },
743 },
744 },
745 },
746 },
747 }
748
749 for name, test := range tests {
750 t.Run(name, func(t *testing.T) {
751 g := New(IntHash)
752
753 for _, vertex := range test.vertices {
754 _ = g.AddVertex(vertex)
755 }
756
757 for _, edge := range test.edges {
758 _ = g.AddEdge(copyEdge(edge))
759 }
760
761 edges, err := g.Edges()
762 if err != nil {
763 t.Fatalf("unexpected error: %v", err.Error())
764 }
765
766 for _, expectedEdge := range test.expectedEdges {
767 for _, actualEdge := range edges {
768 if actualEdge.Source != expectedEdge.Source || actualEdge.Target != expectedEdge.Target {
769 continue
770 }
771 if !edgesAreEqual(expectedEdge, actualEdge, false) {
772 t.Errorf("%s: expected edge %v, got %v", name, expectedEdge, actualEdge)
773 }
774 }
775 }
776 })
777 }
778 }
779
780 func TestUndirected_UpdateEdge(t *testing.T) {
781 tests := map[string]struct {
782 vertices []int
783 edges []Edge[int]
784 updateEdge Edge[int]
785 expectedErr error
786 }{
787 "update an edge": {
788 vertices: []int{1, 2},
789 edges: []Edge[int]{
790 {
791 Source: 1,
792 Target: 2,
793 Properties: EdgeProperties{
794 Weight: 10,
795 Attributes: map[string]string{
796 "color": "red",
797 },
798 Data: "my-edge",
799 },
800 },
801 },
802 updateEdge: Edge[int]{
803 Source: 1,
804 Target: 2,
805 Properties: EdgeProperties{
806 Weight: 20,
807 Attributes: map[string]string{
808 "color": "blue",
809 "label": "a blue edge",
810 },
811 Data: "my-updated-edge",
812 },
813 },
814 },
815 }
816
817 for name, test := range tests {
818 t.Run(name, func(t *testing.T) {
819 g := New(IntHash)
820
821 for _, vertex := range test.vertices {
822 _ = g.AddVertex(vertex)
823 }
824
825 for _, edge := range test.edges {
826 _ = g.AddEdge(copyEdge(edge))
827 }
828
829 err := g.UpdateEdge(copyEdge(test.updateEdge))
830
831 if !errors.Is(err, test.expectedErr) {
832 t.Fatalf("expected error %v, got %v", test.expectedErr, err)
833 }
834
835 actualEdge, err := g.Edge(test.updateEdge.Source, test.updateEdge.Target)
836 if err != nil {
837 t.Fatalf("unexpected error: %v", err.Error())
838 }
839
840 if !edgesAreEqual(test.updateEdge, actualEdge, false) {
841 t.Errorf("expected edge %v, got %v", test.updateEdge, actualEdge)
842 }
843 })
844 }
845 }
846
847 func TestUndirected_RemoveEdge(t *testing.T) {
848 tests := map[string]struct {
849 vertices []int
850 edges []Edge[int]
851 removeEdges []Edge[int]
852 expectedError error
853 }{
854 "two-vertices graph": {
855 vertices: []int{1, 2},
856 edges: []Edge[int]{
857 {Source: 1, Target: 2},
858 },
859 removeEdges: []Edge[int]{
860 {Source: 1, Target: 2},
861 },
862 },
863 "remove 2 edges from triangle": {
864 vertices: []int{1, 2, 3},
865 edges: []Edge[int]{
866 {Source: 1, Target: 2},
867 {Source: 1, Target: 3},
868 {Source: 2, Target: 3},
869 },
870 removeEdges: []Edge[int]{
871 {Source: 1, Target: 3},
872 {Source: 2, Target: 3},
873 },
874 },
875 "remove non-existent edge": {
876 vertices: []int{1, 2, 3},
877 edges: []Edge[int]{
878 {Source: 1, Target: 2},
879 },
880 removeEdges: []Edge[int]{
881 {Source: 2, Target: 3},
882 },
883 expectedError: ErrEdgeNotFound,
884 },
885 }
886
887 for name, test := range tests {
888 graph := New(IntHash)
889
890 for _, vertex := range test.vertices {
891 _ = graph.AddVertex(vertex)
892 }
893
894 for _, edge := range test.edges {
895 if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil {
896 t.Fatalf("%s: failed to add edge: %s", name, err.Error())
897 }
898 }
899
900 for _, removeEdge := range test.removeEdges {
901 if err := graph.RemoveEdge(removeEdge.Source, removeEdge.Target); !errors.Is(err, test.expectedError) {
902 t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err)
903 }
904 // After removing the edge, verify that it can't be retrieved using
905 // Edge anymore.
906 if _, err := graph.Edge(removeEdge.Source, removeEdge.Target); err != ErrEdgeNotFound {
907 t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, ErrEdgeNotFound, err)
908 }
909 }
910 }
911 }
912
913 func TestUndirected_Adjacencies(t *testing.T) {
914 tests := map[string]struct {
915 vertices []int
916 edges []Edge[int]
917 expected map[int]map[int]Edge[int]
918 }{
919 "Y-shaped graph": {
920 vertices: []int{1, 2, 3, 4},
921 edges: []Edge[int]{
922 {Source: 1, Target: 3},
923 {Source: 2, Target: 3},
924 {Source: 3, Target: 4},
925 },
926 expected: map[int]map[int]Edge[int]{
927 1: {
928 3: {Source: 1, Target: 3},
929 },
930 2: {
931 3: {Source: 2, Target: 3},
932 },
933 3: {
934 1: {Source: 3, Target: 1},
935 2: {Source: 3, Target: 2},
936 4: {Source: 3, Target: 4},
937 },
938 4: {
939 3: {Source: 4, Target: 3},
940 },
941 },
942 },
943 "diamond-shaped graph": {
944 vertices: []int{1, 2, 3, 4},
945 edges: []Edge[int]{
946 {Source: 1, Target: 2},
947 {Source: 1, Target: 3},
948 {Source: 2, Target: 4},
949 {Source: 3, Target: 4},
950 },
951 expected: map[int]map[int]Edge[int]{
952 1: {
953 2: {Source: 1, Target: 2},
954 3: {Source: 1, Target: 3},
955 },
956 2: {
957 1: {Source: 2, Target: 1},
958 4: {Source: 2, Target: 4},
959 },
960 3: {
961 1: {Source: 3, Target: 1},
962 4: {Source: 3, Target: 4},
963 },
964 4: {
965 2: {Source: 4, Target: 2},
966 3: {Source: 4, Target: 3},
967 },
968 },
969 },
970 }
971
972 for name, test := range tests {
973 graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]())
974
975 for _, vertex := range test.vertices {
976 _ = graph.AddVertex(vertex)
977 }
978
979 for _, edge := range test.edges {
980 if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil {
981 t.Fatalf("%s: failed to add edge: %s", name, err.Error())
982 }
983 }
984
985 adjacencyMap, _ := graph.AdjacencyMap()
986
987 for expectedVertex, expectedAdjacencies := range test.expected {
988 adjacencies, ok := adjacencyMap[expectedVertex]
989 if !ok {
990 t.Errorf("%s: expected vertex %v does not exist in adjacency map", name, expectedVertex)
991 }
992
993 for expectedAdjacency, expectedEdge := range expectedAdjacencies {
994 edge, ok := adjacencies[expectedAdjacency]
995 if !ok {
996 t.Errorf("%s: expected adjacency %v does not exist in map of %v", name, expectedAdjacency, expectedVertex)
997 }
998 if edge.Source != expectedEdge.Source || edge.Target != expectedEdge.Target {
999 t.Errorf("%s: edge expectancy doesn't match: expected %v, got %v", name, expectedEdge, edge)
1000 }
1001 }
1002 }
1003 }
1004 }
1005
1006 func TestUndirected_PredecessorMap(t *testing.T) {
1007 tests := map[string]struct {
1008 vertices []int
1009 edges []Edge[int]
1010 expected map[int]map[int]Edge[int]
1011 }{
1012 "Y-shaped graph": {
1013 vertices: []int{1, 2, 3, 4},
1014 edges: []Edge[int]{
1015 {Source: 1, Target: 3},
1016 {Source: 2, Target: 3},
1017 {Source: 3, Target: 4},
1018 },
1019 expected: map[int]map[int]Edge[int]{
1020 1: {
1021 3: {Source: 1, Target: 3},
1022 },
1023 2: {
1024 3: {Source: 2, Target: 3},
1025 },
1026 3: {
1027 1: {Source: 3, Target: 1},
1028 2: {Source: 3, Target: 2},
1029 4: {Source: 3, Target: 4},
1030 },
1031 4: {
1032 3: {Source: 4, Target: 3},
1033 },
1034 },
1035 },
1036 "diamond-shaped graph": {
1037 vertices: []int{1, 2, 3, 4},
1038 edges: []Edge[int]{
1039 {Source: 1, Target: 2},
1040 {Source: 1, Target: 3},
1041 {Source: 2, Target: 4},
1042 {Source: 3, Target: 4},
1043 },
1044 expected: map[int]map[int]Edge[int]{
1045 1: {
1046 2: {Source: 1, Target: 2},
1047 3: {Source: 1, Target: 3},
1048 },
1049 2: {
1050 1: {Source: 2, Target: 1},
1051 4: {Source: 2, Target: 4},
1052 },
1053 3: {
1054 1: {Source: 3, Target: 1},
1055 4: {Source: 3, Target: 4},
1056 },
1057 4: {
1058 2: {Source: 4, Target: 2},
1059 3: {Source: 4, Target: 3},
1060 },
1061 },
1062 },
1063 }
1064
1065 for name, test := range tests {
1066 graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]())
1067
1068 for _, vertex := range test.vertices {
1069 _ = graph.AddVertex(vertex)
1070 }
1071
1072 for _, edge := range test.edges {
1073 if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil {
1074 t.Fatalf("%s: failed to add edge: %s", name, err.Error())
1075 }
1076 }
1077
1078 predecessors, _ := graph.PredecessorMap()
1079
1080 for expectedVertex, expectedPredecessors := range test.expected {
1081 predecessors, ok := predecessors[expectedVertex]
1082 if !ok {
1083 t.Errorf("%s: expected vertex %v does not exist in adjacency map", name, expectedVertex)
1084 }
1085
1086 for expectedPredecessor, expectedEdge := range expectedPredecessors {
1087 edge, ok := predecessors[expectedPredecessor]
1088 if !ok {
1089 t.Errorf("%s: expected adjacency %v does not exist in map of %v", name, expectedPredecessor, expectedVertex)
1090 }
1091 if edge.Source != expectedEdge.Source || edge.Target != expectedEdge.Target {
1092 t.Errorf("%s: edge expectancy doesn't match: expected %v, got %v", name, expectedEdge, edge)
1093 }
1094 }
1095 }
1096 }
1097 }
1098
1099 func TestUndirected_Clone(t *testing.T) {
1100 tests := map[string]struct {
1101 vertices []int
1102 edges []Edge[int]
1103 }{
1104 "Y-shaped graph": {
1105 vertices: []int{1, 2, 3, 4},
1106 edges: []Edge[int]{
1107 {Source: 1, Target: 3},
1108 {Source: 2, Target: 3},
1109 {Source: 3, Target: 4},
1110 },
1111 },
1112 "diamond-shaped graph": {
1113 vertices: []int{1, 2, 3, 4},
1114 edges: []Edge[int]{
1115 {Source: 1, Target: 2},
1116 {Source: 1, Target: 3},
1117 {Source: 2, Target: 4},
1118 {Source: 3, Target: 4},
1119 },
1120 },
1121 }
1122
1123 for name, test := range tests {
1124 graph := New(IntHash)
1125
1126 for _, vertex := range test.vertices {
1127 _ = graph.AddVertex(vertex, VertexWeight(vertex), VertexAttribute("color", "red"))
1128 }
1129
1130 for _, edge := range test.edges {
1131 if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil {
1132 t.Fatalf("%s: failed to add edge: %s", name, err.Error())
1133 }
1134 }
1135
1136 clonedGraph, err := graph.Clone()
1137 if err != nil {
1138 t.Fatalf("%s: failed to clone graph: %s", name, err.Error())
1139 }
1140
1141 expected := graph.(*undirected[int, int])
1142 actual := clonedGraph.(*undirected[int, int])
1143
1144 if actual.hash(5) != expected.hash(5) {
1145 t.Errorf("%s: hash expectancy doesn't match: expected %v, got %v", name, expected.hash, actual.hash)
1146 }
1147
1148 if !traitsAreEqual(actual.traits, expected.traits) {
1149 t.Errorf("%s: traits expectancy doesn't match: expected %v, got %v", name, expected.traits, actual.traits)
1150 }
1151
1152 expectedAdjacencyMap, _ := graph.AdjacencyMap()
1153 actualAdjacencyMap, _ := actual.AdjacencyMap()
1154
1155 if !adjacencyMapsAreEqual(expectedAdjacencyMap, actualAdjacencyMap, expected.edgesAreEqual) {
1156 t.Errorf("%s: expected adjacency map %v, got %v", name, expectedAdjacencyMap, actualAdjacencyMap)
1157 }
1158
1159 _ = clonedGraph.AddVertex(10)
1160
1161 if _, err := graph.Vertex(10); err == nil {
1162 t.Errorf("%s: vertex 10 shouldn't exist in original graph", name)
1163 }
1164 }
1165 }
1166
1167 func TestUndirected_OrderAndSize(t *testing.T) {
1168 tests := map[string]struct {
1169 vertices []int
1170 edges []Edge[int]
1171 expectedOrder int
1172 expectedSize int
1173 }{
1174 "Y-shaped graph": {
1175 vertices: []int{1, 2, 3, 4},
1176 edges: []Edge[int]{
1177 {Source: 1, Target: 3},
1178 {Source: 2, Target: 3},
1179 {Source: 3, Target: 4},
1180 },
1181 expectedOrder: 4,
1182 expectedSize: 3,
1183 },
1184 "diamond-shaped graph": {
1185 vertices: []int{1, 2, 3, 4},
1186 edges: []Edge[int]{
1187 {Source: 1, Target: 2},
1188 {Source: 1, Target: 3},
1189 {Source: 2, Target: 4},
1190 {Source: 3, Target: 4},
1191 },
1192 expectedOrder: 4,
1193 expectedSize: 4,
1194 },
1195 "two-vertices graph": {
1196 vertices: []int{1, 2},
1197 edges: []Edge[int]{
1198 {Source: 1, Target: 2},
1199 },
1200 expectedOrder: 2,
1201 expectedSize: 1,
1202 },
1203 "edgeless graph": {
1204 vertices: []int{1, 2},
1205 edges: []Edge[int]{},
1206 expectedOrder: 2,
1207 expectedSize: 0,
1208 },
1209 }
1210
1211 for name, test := range tests {
1212 graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]())
1213
1214 for _, vertex := range test.vertices {
1215 _ = graph.AddVertex(vertex)
1216 }
1217
1218 for _, edge := range test.edges {
1219 if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil {
1220 t.Fatalf("%s: failed to add edge: %s", name, err.Error())
1221 }
1222 }
1223
1224 order, _ := graph.Order()
1225 size, _ := graph.Size()
1226
1227 if order != test.expectedOrder {
1228 t.Errorf("%s: order expectancy doesn't match: expected %d, got %d", name, test.expectedOrder, order)
1229 }
1230
1231 if size != test.expectedSize {
1232 t.Errorf("%s: size expectancy doesn't match: expected %d, got %d", name, test.expectedSize, size)
1233 }
1234
1235 }
1236 }
1237
1238 func TestUndirected_edgesAreEqual(t *testing.T) {
1239 tests := map[string]struct {
1240 a Edge[int]
1241 b Edge[int]
1242 edgesAreEqual bool
1243 }{
1244 "equal edges in undirected graph": {
1245 a: Edge[int]{Source: 1, Target: 2},
1246 b: Edge[int]{Source: 1, Target: 2},
1247 edgesAreEqual: true,
1248 },
1249 "swapped equal edges in undirected graph": {
1250 a: Edge[int]{Source: 1, Target: 2},
1251 b: Edge[int]{Source: 2, Target: 1},
1252 edgesAreEqual: true,
1253 },
1254 "unequal edges in undirected graph": {
1255 a: Edge[int]{Source: 1, Target: 2},
1256 b: Edge[int]{Source: 1, Target: 3},
1257 },
1258 }
1259
1260 for name, test := range tests {
1261 graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]())
1262 actual := graph.edgesAreEqual(test.a, test.b)
1263
1264 if actual != test.edgesAreEqual {
1265 t.Errorf("%s: equality expectations don't match: expected %v, got %v", name, test.edgesAreEqual, actual)
1266 }
1267 }
1268 }
1269
1270 func TestUndirected_addEdge(t *testing.T) {
1271 tests := map[string]struct {
1272 edges []Edge[int]
1273 }{
1274 "add 3 edges": {
1275 edges: []Edge[int]{
1276 {Source: 1, Target: 2, Properties: EdgeProperties{Weight: 1}},
1277 {Source: 2, Target: 3, Properties: EdgeProperties{Weight: 2}},
1278 {Source: 3, Target: 1, Properties: EdgeProperties{Weight: 3}},
1279 },
1280 },
1281 }
1282
1283 for name, test := range tests {
1284 graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]())
1285
1286 for _, edge := range test.edges {
1287 sourceHash := graph.hash(edge.Source)
1288 TargetHash := graph.hash(edge.Target)
1289 err := graph.addEdge(sourceHash, TargetHash, edge)
1290 if err != nil {
1291 t.Fatalf("%s: failed to add edge: %s", name, err.Error())
1292 }
1293 }
1294
1295 outEdges := graph.store.(*memoryStore[int, int]).outEdges
1296 if len(outEdges) != len(test.edges) {
1297 t.Errorf("%s: number of outgoing edges doesn't match: expected %v, got %v", name, len(test.edges), len(outEdges))
1298 }
1299 inEdges := graph.store.(*memoryStore[int, int]).inEdges
1300 if len(inEdges) != len(test.edges) {
1301 t.Errorf("%s: number of ingoing edges doesn't match: expected %v, got %v", name, len(test.edges), len(inEdges))
1302 }
1303 }
1304 }
1305
1306 func TestUndirected_adjacencies(t *testing.T) {
1307 tests := map[string]struct {
1308 vertices []int
1309 edges []Edge[int]
1310 vertex int
1311 expectedAdjancencies []int
1312 }{
1313 "graph with 3 vertices": {
1314 vertices: []int{1, 2, 3},
1315 edges: []Edge[int]{
1316 {Source: 1, Target: 2},
1317 {Source: 1, Target: 3},
1318 },
1319 vertex: 2,
1320 expectedAdjancencies: []int{1},
1321 },
1322 "graph with 6 vertices": {
1323 vertices: []int{1, 2, 3, 4, 5, 6},
1324 edges: []Edge[int]{
1325 {Source: 1, Target: 2},
1326 {Source: 1, Target: 3},
1327 {Source: 2, Target: 4},
1328 {Source: 2, Target: 5},
1329 {Source: 3, Target: 6},
1330 },
1331 vertex: 2,
1332 expectedAdjancencies: []int{1, 4, 5},
1333 },
1334 "graph with 7 vertices and a diamond cycle (#1)": {
1335 vertices: []int{1, 2, 3, 4, 5, 6, 7},
1336 edges: []Edge[int]{
1337 {Source: 1, Target: 2},
1338 {Source: 1, Target: 3},
1339 {Source: 2, Target: 4},
1340 {Source: 2, Target: 5},
1341 {Source: 3, Target: 6},
1342 {Source: 4, Target: 7},
1343 {Source: 5, Target: 7},
1344 },
1345 vertex: 5,
1346 expectedAdjancencies: []int{2, 7},
1347 },
1348 "graph with 7 vertices and a diamond cycle (#2)": {
1349 vertices: []int{1, 2, 3, 4, 5, 6, 7},
1350 edges: []Edge[int]{
1351 {Source: 1, Target: 2},
1352 {Source: 1, Target: 3},
1353 {Source: 2, Target: 4},
1354 {Source: 2, Target: 5},
1355 {Source: 3, Target: 6},
1356 {Source: 4, Target: 7},
1357 {Source: 5, Target: 7},
1358 },
1359 vertex: 7,
1360 expectedAdjancencies: []int{4, 5},
1361 },
1362 "graph with 7 vertices and a diamond cycle (#3)": {
1363 vertices: []int{1, 2, 3, 4, 5, 6, 7},
1364 edges: []Edge[int]{
1365 {Source: 1, Target: 2},
1366 {Source: 1, Target: 3},
1367 {Source: 2, Target: 4},
1368 {Source: 2, Target: 5},
1369 {Source: 3, Target: 6},
1370 {Source: 4, Target: 7},
1371 {Source: 5, Target: 7},
1372 },
1373 vertex: 2,
1374 expectedAdjancencies: []int{1, 4, 5},
1375 },
1376 }
1377
1378 for name, test := range tests {
1379 graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]())
1380
1381 for _, vertex := range test.vertices {
1382 _ = graph.AddVertex(vertex)
1383 }
1384
1385 for _, edge := range test.edges {
1386 if err := graph.AddEdge(edge.Source, edge.Target); err != nil {
1387 t.Fatalf("%s: failed to add edge: %s", name, err.Error())
1388 }
1389 }
1390
1391 adjacencyList := adjacencyList(graph.store, graph.hash(test.vertex))
1392
1393 if !slicesAreEqual(adjacencyList, test.expectedAdjancencies) {
1394 t.Errorf("%s: adjacencies don't match: expected %v, got %v", name, test.expectedAdjancencies, adjacencyList)
1395 }
1396 }
1397 }
1398
1399 func adjacencyList[K comparable, T any](store Store[K, T], vertexHash K) []K {
1400 var adjacencyHashes []K
1401
1402 // An undirected graph creates an undirected edge as two directed edges in
1403 // the opposite direction, so both the in-edges and the out-edges work here.
1404 inEdges, ok := store.(*memoryStore[K, T]).inEdges[vertexHash]
1405 if !ok {
1406 return adjacencyHashes
1407 }
1408
1409 for hash := range inEdges {
1410 adjacencyHashes = append(adjacencyHashes, hash)
1411 }
1412
1413 return adjacencyHashes
1414 }