Debian Codegraph @

directed_test.go

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