@@ -783,6 +783,178 @@ func TestDirected_Edges(t *testing.T) {
783
783
}
784
784
}
785
785
786
+ func TestDirected_OutEdges (t * testing.T ) {
787
+ tests := map [string ]struct {
788
+ vertices []int
789
+ edges []Edge [int ]
790
+ expectedEdges []Edge [int ]
791
+ }{
792
+ "graph with 3 edges" : {
793
+ vertices : []int {1 , 2 , 3 },
794
+ edges : []Edge [int ]{
795
+ {
796
+ Source : 1 ,
797
+ Target : 2 ,
798
+ Properties : EdgeProperties {
799
+ Weight : 10 ,
800
+ Attributes : map [string ]string {
801
+ "color" : "red" ,
802
+ },
803
+ },
804
+ },
805
+ {
806
+ Source : 2 ,
807
+ Target : 3 ,
808
+ Properties : EdgeProperties {
809
+ Weight : 20 ,
810
+ Attributes : map [string ]string {
811
+ "color" : "green" ,
812
+ },
813
+ },
814
+ },
815
+ {
816
+ Source : 3 ,
817
+ Target : 1 ,
818
+ Properties : EdgeProperties {
819
+ Weight : 30 ,
820
+ Attributes : map [string ]string {
821
+ "color" : "blue" ,
822
+ },
823
+ },
824
+ },
825
+ },
826
+ expectedEdges : []Edge [int ]{
827
+ {
828
+ Source : 1 ,
829
+ Target : 2 ,
830
+ Properties : EdgeProperties {
831
+ Weight : 10 ,
832
+ Attributes : map [string ]string {
833
+ "color" : "red" ,
834
+ },
835
+ },
836
+ },
837
+ },
838
+ },
839
+ }
840
+
841
+ for name , test := range tests {
842
+ t .Run (name , func (t * testing.T ) {
843
+ g := New (IntHash , Directed ())
844
+
845
+ for _ , vertex := range test .vertices {
846
+ _ = g .AddVertex (vertex )
847
+ }
848
+
849
+ for _ , edge := range test .edges {
850
+ _ = g .AddEdge (copyEdge (edge ))
851
+ }
852
+
853
+ edges , err := g .OutEdges (test .vertices [0 ])
854
+ if err != nil {
855
+ t .Fatalf ("unexpected error: %v" , err .Error ())
856
+ }
857
+
858
+ for _ , expectedEdge := range test .expectedEdges {
859
+ for _ , actualEdge := range edges {
860
+ if actualEdge .Source != expectedEdge .Source || actualEdge .Target != expectedEdge .Target {
861
+ continue
862
+ }
863
+ if ! edgesAreEqual (expectedEdge , actualEdge , true ) {
864
+ t .Errorf ("%s: expected edge %v, got %v" , name , expectedEdge , actualEdge )
865
+ }
866
+ }
867
+ }
868
+ })
869
+ }
870
+ }
871
+
872
+ func TestDirected_InEdges (t * testing.T ) {
873
+ tests := map [string ]struct {
874
+ vertices []int
875
+ edges []Edge [int ]
876
+ expectedEdges []Edge [int ]
877
+ }{
878
+ "graph with 3 edges" : {
879
+ vertices : []int {1 , 2 , 3 },
880
+ edges : []Edge [int ]{
881
+ {
882
+ Source : 1 ,
883
+ Target : 2 ,
884
+ Properties : EdgeProperties {
885
+ Weight : 10 ,
886
+ Attributes : map [string ]string {
887
+ "color" : "red" ,
888
+ },
889
+ },
890
+ },
891
+ {
892
+ Source : 2 ,
893
+ Target : 3 ,
894
+ Properties : EdgeProperties {
895
+ Weight : 20 ,
896
+ Attributes : map [string ]string {
897
+ "color" : "green" ,
898
+ },
899
+ },
900
+ },
901
+ {
902
+ Source : 3 ,
903
+ Target : 1 ,
904
+ Properties : EdgeProperties {
905
+ Weight : 30 ,
906
+ Attributes : map [string ]string {
907
+ "color" : "blue" ,
908
+ },
909
+ },
910
+ },
911
+ },
912
+ expectedEdges : []Edge [int ]{
913
+ {
914
+ Source : 3 ,
915
+ Target : 1 ,
916
+ Properties : EdgeProperties {
917
+ Weight : 30 ,
918
+ Attributes : map [string ]string {
919
+ "color" : "blue" ,
920
+ },
921
+ },
922
+ },
923
+ },
924
+ },
925
+ }
926
+
927
+ for name , test := range tests {
928
+ t .Run (name , func (t * testing.T ) {
929
+ g := New (IntHash , Directed ())
930
+
931
+ for _ , vertex := range test .vertices {
932
+ _ = g .AddVertex (vertex )
933
+ }
934
+
935
+ for _ , edge := range test .edges {
936
+ _ = g .AddEdge (copyEdge (edge ))
937
+ }
938
+
939
+ edges , err := g .InEdges (test .vertices [0 ])
940
+ if err != nil {
941
+ t .Fatalf ("unexpected error: %v" , err .Error ())
942
+ }
943
+
944
+ for _ , expectedEdge := range test .expectedEdges {
945
+ for _ , actualEdge := range edges {
946
+ if actualEdge .Source != expectedEdge .Source || actualEdge .Target != expectedEdge .Target {
947
+ continue
948
+ }
949
+ if ! edgesAreEqual (expectedEdge , actualEdge , true ) {
950
+ t .Errorf ("%s: expected edge %v, got %v" , name , expectedEdge , actualEdge )
951
+ }
952
+ }
953
+ }
954
+ })
955
+ }
956
+ }
957
+
786
958
func TestDirected_UpdateEdge (t * testing.T ) {
787
959
tests := map [string ]struct {
788
960
vertices []int
0 commit comments