diff --git a/collection_test.go b/collection_test.go index 76b2f9a..e5a79e9 100644 --- a/collection_test.go +++ b/collection_test.go @@ -25,31 +25,33 @@ func TestPriorityQueue_Push(t *testing.T) { } for name, test := range tests { - queue := newPriorityQueue[int]() + t.Run(name, func(t *testing.T) { + queue := newPriorityQueue[int]() - for i, item := range test.items { - queue.Push(item, test.priorities[i]) - } + for i, item := range test.items { + queue.Push(item, test.priorities[i]) + } - if queue.Len() != len(test.expectedPriorityItems) { - t.Fatalf("%s: item length expectancy doesn't match: expected %v, got %v", name, len(test.expectedPriorityItems), queue.Len()) - } + if queue.Len() != len(test.expectedPriorityItems) { + t.Fatalf("item length expectancy doesn't match: expected %v, got %v", len(test.expectedPriorityItems), queue.Len()) + } - popped := make([]int, queue.Len()) + popped := make([]int, queue.Len()) - for queue.Len() > 0 { - item, _ := queue.Pop() - popped = append(popped, item) - } + for queue.Len() > 0 { + item, _ := queue.Pop() + popped = append(popped, item) + } - n := len(popped) + n := len(popped) - for i, item := range test.expectedPriorityItems { - poppedItem := popped[n-1-i] - if item.value != poppedItem { - t.Errorf("%s: item doesn't match: expected %v at index %d, got %v", name, item.value, i, poppedItem) + for i, item := range test.expectedPriorityItems { + poppedItem := popped[n-1-i] + if item.value != poppedItem { + t.Errorf("item doesn't match: expected %v at index %d, got %v", item.value, i, poppedItem) + } } - } + }) } } @@ -80,21 +82,23 @@ func TestPriorityQueue_Pop(t *testing.T) { } for name, test := range tests { - queue := newPriorityQueue[int]() + t.Run(name, func(t *testing.T) { + queue := newPriorityQueue[int]() - for i, item := range test.items { - queue.Push(item, test.priorities[i]) - } + for i, item := range test.items { + queue.Push(item, test.priorities[i]) + } - item, err := queue.Pop() + item, err := queue.Pop() - if test.shouldFail != (err != nil) { - t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, (err != nil), err) - } + if test.shouldFail != (err != nil) { + t.Fatalf("error expectancy doesn't match: expected %v, got %v (error: %v)", test.shouldFail, (err != nil), err) + } - if item != test.expectedItem { - t.Errorf("%s: item expectancy doesn't match: expected %v, got %v", name, test.expectedItem, item) - } + if item != test.expectedItem { + t.Errorf("item expectancy doesn't match: expected %v, got %v", test.expectedItem, item) + } + }) } } @@ -156,33 +160,35 @@ func TestPriorityQueue_UpdatePriority(t *testing.T) { } for name, test := range tests { - queue := newPriorityQueue[int]() + t.Run(name, func(t *testing.T) { + queue := newPriorityQueue[int]() - for _, item := range test.items { - queue.Push(item.value, item.priority) - } + for _, item := range test.items { + queue.Push(item.value, item.priority) + } - queue.UpdatePriority(test.decreaseItem, test.decreasePriority) + queue.UpdatePriority(test.decreaseItem, test.decreasePriority) - if queue.Len() != len(test.expectedPriorityItems) { - t.Fatalf("%s: item length expectancy doesn't match: expected %v, got %v", name, len(test.expectedPriorityItems), queue.Len()) - } + if queue.Len() != len(test.expectedPriorityItems) { + t.Fatalf("item length expectancy doesn't match: expected %v, got %v", len(test.expectedPriorityItems), queue.Len()) + } - popped := make([]int, queue.Len()) + popped := make([]int, queue.Len()) - for queue.Len() > 0 { - item, _ := queue.Pop() - popped = append(popped, item) - } + for queue.Len() > 0 { + item, _ := queue.Pop() + popped = append(popped, item) + } - n := len(popped) + n := len(popped) - for i, item := range test.expectedPriorityItems { - poppedItem := popped[n-1-i] - if item.value != poppedItem { - t.Errorf("%s: item doesn't match: expected %v at index %d, got %v", name, item.value, i, poppedItem) + for i, item := range test.expectedPriorityItems { + poppedItem := popped[n-1-i] + if item.value != poppedItem { + t.Errorf("item doesn't match: expected %v at index %d, got %v", item.value, i, poppedItem) + } } - } + }) } } @@ -210,17 +216,19 @@ func TestPriorityQueue_Len(t *testing.T) { } for name, test := range tests { - queue := newPriorityQueue[int]() + t.Run(name, func(t *testing.T) { + queue := newPriorityQueue[int]() - for i, item := range test.items { - queue.Push(item, test.priorities[i]) - } + for i, item := range test.items { + queue.Push(item, test.priorities[i]) + } - n := queue.Len() + n := queue.Len() - if n != test.expectedLen { - t.Errorf("%s: length expectancy doesn't match: expected %v, got %v", name, test.expectedLen, n) - } + if n != test.expectedLen { + t.Errorf("length expectancy doesn't match: expected %v, got %v", test.expectedLen, n) + } + }) } } @@ -229,21 +237,19 @@ func TestStack_push(t *testing.T) { t T } type testCase[T comparable] struct { - name string elements []int args args[T] } - tests := []testCase[int]{ - { - "push 1", + tests := map[string]testCase[int]{ + "push 1": { []int{1, 2, 3, 4, 5, 6}, args[int]{ t: 1, }, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + for name, tt := range tests { + t.Run(name, func(t *testing.T) { s := newStack[int]() for _, element := range tt.elements { @@ -257,27 +263,24 @@ func TestStack_push(t *testing.T) { func TestStack_pop(t *testing.T) { type testCase[T comparable] struct { - name string elements []int want T wantErr bool } - tests := []testCase[int]{ - { - "pop element", + tests := map[string]testCase[int]{ + "pop element": { []int{1, 2, 3, 4, 5, 6}, 6, false, }, - { - "pop element from empty stack", + "pop element from empty stack": { []int{}, 0, true, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + for name, tt := range tests { + t.Run(name, func(t *testing.T) { s := newStack[int]() for _, element := range tt.elements { @@ -298,27 +301,24 @@ func TestStack_pop(t *testing.T) { func TestStack_top(t *testing.T) { type testCase[T comparable] struct { - name string elements []int want T wantErr bool } - tests := []testCase[int]{ - { - "top element", + tests := map[string]testCase[int]{ + "top element": { []int{1, 2, 3, 4, 5, 6}, 6, false, }, - { - "top element of empty stack", + "top element of empty stack": { []int{}, 0, true, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + for name, tt := range tests { + t.Run(name, func(t *testing.T) { s := newStack[int]() for _, element := range tt.elements { @@ -339,24 +339,21 @@ func TestStack_top(t *testing.T) { func TestStack_isEmpty(t *testing.T) { type testCase[T comparable] struct { - name string elements []int want bool } - tests := []testCase[int]{ - { - "empty", + tests := map[string]testCase[int]{ + "empty": { []int{}, true, }, - { - "not empty", + "not empty": { []int{1, 2, 3, 4, 5, 6}, false, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + for name, tt := range tests { + t.Run(name, func(t *testing.T) { s := newStack[int]() for _, element := range tt.elements { @@ -375,13 +372,11 @@ func TestStack_forEach(t *testing.T) { f func(T) } type testCase[T comparable] struct { - name string elements []int args args[T] } - tests := []testCase[int]{ - { - name: "forEach", + tests := map[string]testCase[int]{ + "forEach": { elements: []int{1, 2, 3, 4, 5, 6}, args: args[int]{ f: func(i int) { @@ -389,8 +384,8 @@ func TestStack_forEach(t *testing.T) { }, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + for name, tt := range tests { + t.Run(name, func(t *testing.T) { s := newStack[int]() for _, element := range tt.elements { @@ -404,28 +399,26 @@ func TestStack_forEach(t *testing.T) { func TestStack_contains(t *testing.T) { type testCase[T comparable] struct { - name string elements []int arg T expected bool } - tests := []testCase[int]{ - { - name: "contains 6", + tests := map[string]testCase[int]{ + + "contains 6": { elements: []int{1, 2, 3, 4, 5, 6}, arg: 6, expected: true, }, - { - name: "contains 7", + "contains 7": { elements: []int{1, 2, 3, 4, 5, 6}, arg: 7, expected: false, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + for name, tt := range tests { + t.Run(name, func(t *testing.T) { s := newStack[int]() for _, element := range tt.elements { diff --git a/dag_test.go b/dag_test.go index 04a448f..bdd099d 100644 --- a/dag_test.go +++ b/dag_test.go @@ -50,41 +50,43 @@ func TestDirectedTopologicalSort(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New(IntHash, Directed()) - err := buildGraph(&graph, test.vertices, test.edges) - if err != nil { - t.Fatalf("%s: failed to construct graph: %s", name, err.Error()) - } + err := buildGraph(&graph, test.vertices, test.edges) + if err != nil { + t.Fatalf("failed to construct graph: %s", err.Error()) + } - order, err := TopologicalSort(graph) + order, err := TopologicalSort(graph) - if test.shouldFail != (err != nil) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, err != nil, err) - } + if test.shouldFail != (err != nil) { + t.Errorf("error expectancy doesn't match: expected %v, got %v (error: %v)", test.shouldFail, err != nil, err) + } - if test.shouldFail { - continue - } + if test.shouldFail { + t.SkipNow() + } - if len(order) != len(test.vertices) { - t.Errorf("%s: order length expectancy doesn't match: expected %v, got %v", name, len(test.vertices), len(order)) - } + if len(order) != len(test.vertices) { + t.Errorf("order length expectancy doesn't match: expected %v, got %v", len(test.vertices), len(order)) + } - if len(test.expectedOrder) <= 0 { + if len(test.expectedOrder) <= 0 { - fmt.Println("topological sort", order) + fmt.Println("topological sort", order) - if err := verifyTopologicalSort(graph, order); err != nil { - t.Errorf("%s: invalid topological sort - %v", name, err) + if err := verifyTopologicalSort(graph, order); err != nil { + t.Errorf("invalid topological sort - %v", err) + } } - } - for i, expectedVertex := range test.expectedOrder { - if expectedVertex != order[i] { - t.Errorf("%s: order expectancy doesn't match: expected %v at %d, got %v", name, expectedVertex, i, order[i]) + for i, expectedVertex := range test.expectedOrder { + if expectedVertex != order[i] { + t.Errorf("order expectancy doesn't match: expected %v at %d, got %v", expectedVertex, i, order[i]) + } } - } + }) } } @@ -100,17 +102,19 @@ func TestUndirectedTopologicalSort(t *testing.T) { } for name, test := range tests { - graph := New(IntHash) + t.Run(name, func(t *testing.T) { + graph := New(IntHash) - order, err := TopologicalSort(graph) + order, err := TopologicalSort(graph) - if test.shouldFail != (err != nil) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, err != nil, err) - } + if test.shouldFail != (err != nil) { + t.Errorf("error expectancy doesn't match: expected %v, got %v (error: %v)", test.shouldFail, err != nil, err) + } - if test.expectedOrder == nil && order != nil { - t.Errorf("%s: order expectancy doesn't match: expcted %v, got %v", name, test.expectedOrder, order) - } + if test.expectedOrder == nil && order != nil { + t.Errorf("order expectancy doesn't match: expcted %v, got %v", test.expectedOrder, order) + } + }) } } @@ -158,37 +162,39 @@ func TestDirectedStableTopologicalSort(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New(IntHash, Directed()) - err := buildGraph(&graph, test.vertices, test.edges) - if err != nil { - t.Fatalf("%s: failed to construct graph: %s", name, err.Error()) - } + err := buildGraph(&graph, test.vertices, test.edges) + if err != nil { + t.Fatalf("%s: failed to construct graph: %s", name, err.Error()) + } - order, err := StableTopologicalSort(graph, func(a, b int) bool { - return a < b - }) + order, err := StableTopologicalSort(graph, func(a, b int) bool { + return a < b + }) - if test.shouldFail != (err != nil) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, err != nil, err) - } + if test.shouldFail != (err != nil) { + t.Errorf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, err != nil, err) + } - if test.shouldFail { - continue - } + if test.shouldFail { + t.SkipNow() + } - if len(order) != len(test.expectedOrder) { - t.Errorf("%s: order length expectancy doesn't match: expected %v, got %v", name, len(test.expectedOrder), len(order)) - } + if len(order) != len(test.expectedOrder) { + t.Errorf("%s: order length expectancy doesn't match: expected %v, got %v", name, len(test.expectedOrder), len(order)) + } - fmt.Println("expected", test.expectedOrder) - fmt.Println("actual", order) + fmt.Println("expected", test.expectedOrder) + fmt.Println("actual", order) - for i, expectedVertex := range test.expectedOrder { - if expectedVertex != order[i] { - t.Errorf("%s: order expectancy doesn't match: expected %v at %d, got %v", name, expectedVertex, i, order[i]) + for i, expectedVertex := range test.expectedOrder { + if expectedVertex != order[i] { + t.Errorf("%s: order expectancy doesn't match: expected %v at %d, got %v", name, expectedVertex, i, order[i]) + } } - } + }) } } @@ -256,37 +262,39 @@ func TestDirectedTransitiveReduction(t *testing.T) { } for name, test := range tests { - graph := New(StringHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New(StringHash, Directed()) - err := buildGraph(&graph, test.vertices, test.edges) - if err != nil { - t.Fatalf("%s: failed to construct graph: %s", name, err.Error()) - } + err := buildGraph(&graph, test.vertices, test.edges) + if err != nil { + t.Fatalf("failed to construct graph: %s", err.Error()) + } - reduction, err := TransitiveReduction(graph) + reduction, err := TransitiveReduction(graph) - if test.shouldFail != (err != nil) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, err != nil, err) - } + if test.shouldFail != (err != nil) { + t.Errorf("error expectancy doesn't match: expected %v, got %v (error: %v)", test.shouldFail, err != nil, err) + } - if test.shouldFail { - continue - } + if test.shouldFail { + t.SkipNow() + } - actualEdges := make([]Edge[string], 0) - adjacencyMap, _ := reduction.AdjacencyMap() + actualEdges := make([]Edge[string], 0) + adjacencyMap, _ := reduction.AdjacencyMap() - for _, adjacencies := range adjacencyMap { - for _, edge := range adjacencies { - actualEdges = append(actualEdges, edge) + for _, adjacencies := range adjacencyMap { + for _, edge := range adjacencies { + actualEdges = append(actualEdges, edge) + } } - } - equalsFunc := reduction.(*directed[string, string]).edgesAreEqual + equalsFunc := reduction.(*directed[string, string]).edgesAreEqual - if !slicesAreEqualWithFunc(actualEdges, test.expectedEdges, equalsFunc) { - t.Errorf("%s: edge expectancy doesn't match: expected %v, got %v", name, test.expectedEdges, actualEdges) - } + if !slicesAreEqualWithFunc(actualEdges, test.expectedEdges, equalsFunc) { + t.Errorf("edge expectancy doesn't match: expected %v, got %v", test.expectedEdges, actualEdges) + } + }) } } @@ -300,20 +308,22 @@ func TestUndirectedTransitiveReduction(t *testing.T) { } for name, test := range tests { - graph := New(StringHash) + t.Run(name, func(t *testing.T) { + graph := New(StringHash) - _, err := TransitiveReduction(graph) + _, err := TransitiveReduction(graph) - if test.shouldFail != (err != nil) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, err != nil, err) - } + if test.shouldFail != (err != nil) { + t.Errorf("error expectancy doesn't match: expected %v, got %v (error: %v)", test.shouldFail, err != nil, err) + } + }) } } func TestVerifyTopologicalSort(t *testing.T) { tests := map[string]struct { - vertices []int - edges []Edge[int] + vertices []int + edges []Edge[int] invalidOrder []int }{ "graph with 2 vertices": { @@ -379,31 +389,33 @@ func TestVerifyTopologicalSort(t *testing.T) { } for name, test := range tests { - graph := New[int, int](IntHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New[int, int](IntHash, Directed()) - err := buildGraph(&graph, test.vertices, test.edges) - if err != nil { - t.Fatalf("%s: failed to construct graph: %s", name, err.Error()) - } + err := buildGraph(&graph, test.vertices, test.edges) + if err != nil { + t.Fatalf("failed to construct graph: %s", err.Error()) + } - var order[] int + var order []int - if len(test.invalidOrder) > 0 { - order = test.invalidOrder - } else { - order, err = TopologicalSort(graph) - if err != nil { - t.Fatalf("%s: error failed to produce topological sort: %v)", name, err) + if len(test.invalidOrder) > 0 { + order = test.invalidOrder + } else { + order, err = TopologicalSort(graph) + if err != nil { + t.Fatalf("error failed to produce topological sort: %v)", err) + } } - } - err = verifyTopologicalSort(graph, order) + err = verifyTopologicalSort(graph, order) - shouldFail := len(test.invalidOrder) > 0 + shouldFail := len(test.invalidOrder) > 0 - if shouldFail != (err != nil) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, shouldFail, err != nil, err) - } + if shouldFail != (err != nil) { + t.Errorf("error expectancy doesn't match: expected %v, got %v (error: %v)", shouldFail, err != nil, err) + } + }) } } diff --git a/directed_test.go b/directed_test.go index 34a9c8d..42e6869 100644 --- a/directed_test.go +++ b/directed_test.go @@ -21,12 +21,14 @@ func TestDirected_Traits(t *testing.T) { } for name, test := range tests { - g := newDirected(IntHash, test.traits, newMemoryStore[int, int]()) - traits := g.Traits() + t.Run(name, func(t *testing.T) { + g := newDirected(IntHash, test.traits, newMemoryStore[int, int]()) + traits := g.Traits() - if !traitsAreEqual(traits, test.expected) { - t.Errorf("%s: traits expectancy doesn't match: expected %v, got %v", name, test.expected, traits) - } + if !traitsAreEqual(traits, test.expected) { + t.Errorf("traits expectancy doesn't match: expected %v, got %v", test.expected, traits) + } + }) } } @@ -60,61 +62,63 @@ func TestDirected_AddVertex(t *testing.T) { } for name, test := range tests { - graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - var err error + var err error - for _, vertex := range test.vertices { - if test.properties == nil { - err = graph.AddVertex(vertex) - continue - } - // If there are vertex attributes, iterate over them and call the - // VertexAttribute functional option for each entry. A vertex should - // only have one attribute so that AddVertex is invoked once. - for key, value := range test.properties.Attributes { - err = graph.AddVertex(vertex, VertexWeight(test.properties.Weight), VertexAttribute(key, value)) + for _, vertex := range test.vertices { + if test.properties == nil { + err = graph.AddVertex(vertex) + continue + } + // If there are vertex attributes, iterate over them and call the + // VertexAttribute functional option for each entry. A vertex should + // only have one attribute so that AddVertex is invoked once. + for key, value := range test.properties.Attributes { + err = graph.AddVertex(vertex, VertexWeight(test.properties.Weight), VertexAttribute(key, value)) + } } - } - - if !errors.Is(err, test.finallyExpectedError) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.finallyExpectedError, err) - } - graphStore := graph.store.(*memoryStore[int, int]) - for _, vertex := range test.vertices { - if len(graphStore.vertices) != len(test.expectedVertices) { - t.Errorf("%s: vertex count doesn't match: expected %v, got %v", name, len(test.expectedVertices), len(graphStore.vertices)) + if !errors.Is(err, test.finallyExpectedError) { + t.Errorf("error expectancy doesn't match: expected %v, got %v", test.finallyExpectedError, err) } - hash := graph.hash(vertex) - if _, _, err := graph.store.Vertex(hash); err != nil { - vertices := graphStore.vertices - t.Errorf("%s: vertex %v not found in graph: %v", name, vertex, vertices) - } + graphStore := graph.store.(*memoryStore[int, int]) + for _, vertex := range test.vertices { + if len(graphStore.vertices) != len(test.expectedVertices) { + t.Errorf("vertex count doesn't match: expected %v, got %v", len(test.expectedVertices), len(graphStore.vertices)) + } - if test.properties == nil { - continue - } + hash := graph.hash(vertex) + if _, _, err := graph.store.Vertex(hash); err != nil { + vertices := graphStore.vertices + t.Errorf("vertex %v not found in graph: %v", vertex, vertices) + } - if graphStore.vertexProperties[hash].Weight != test.expectedProperties.Weight { - t.Errorf("%s: edge weights don't match: expected weight %v, got %v", name, test.expectedProperties.Weight, graphStore.vertexProperties[hash].Weight) - } + if test.properties == nil { + continue + } - if len(graphStore.vertexProperties[hash].Attributes) != len(test.expectedProperties.Attributes) { - t.Fatalf("%s: attributes lengths don't match: expcted %v, got %v", name, len(test.expectedProperties.Attributes), len(graphStore.vertexProperties[hash].Attributes)) - } + if graphStore.vertexProperties[hash].Weight != test.expectedProperties.Weight { + t.Errorf("edge weights don't match: expected weight %v, got %v", test.expectedProperties.Weight, graphStore.vertexProperties[hash].Weight) + } - for expectedKey, expectedValue := range test.expectedProperties.Attributes { - value, ok := graphStore.vertexProperties[hash].Attributes[expectedKey] - if !ok { - t.Errorf("%s: attribute keys don't match: expected key %v not found", name, expectedKey) + if len(graphStore.vertexProperties[hash].Attributes) != len(test.expectedProperties.Attributes) { + t.Fatalf("attributes lengths don't match: expcted %v, got %v", len(test.expectedProperties.Attributes), len(graphStore.vertexProperties[hash].Attributes)) } - if value != expectedValue { - t.Errorf("%s: attribute values don't match: expected value %v for key %v, got %v", name, expectedValue, expectedKey, value) + + for expectedKey, expectedValue := range test.expectedProperties.Attributes { + value, ok := graphStore.vertexProperties[hash].Attributes[expectedKey] + if !ok { + t.Errorf("attribute keys don't match: expected key %v not found", expectedKey) + } + if value != expectedValue { + t.Errorf("attribute values don't match: expected value %v for key %v, got %v", expectedValue, expectedKey, value) + } } } - } + }) } } @@ -229,25 +233,27 @@ func TestDirected_Vertex(t *testing.T) { } for name, test := range tests { - graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - vertex, err := graph.Vertex(test.vertex) + vertex, err := graph.Vertex(test.vertex) - if !errors.Is(err, test.expectedError) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err) - } + if !errors.Is(err, test.expectedError) { + t.Errorf("error expectancy doesn't match: expected %v, got %v", test.expectedError, err) + } - if test.expectedError != nil { - continue - } + if test.expectedError != nil { + t.SkipNow() + } - if vertex != test.vertex { - t.Errorf("%s: vertex expectancy doesn't match: expected %v, got %v", name, test.vertex, vertex) - } + if vertex != test.vertex { + t.Errorf("vertex expectancy doesn't match: expected %v, got %v", test.vertex, vertex) + } + }) } } @@ -283,21 +289,23 @@ func TestDirected_RemoveVertex(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New(IntHash, Directed()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - _ = graph.AddEdge(edge.Source, edge.Target) - } + for _, edge := range test.edges { + _ = graph.AddEdge(edge.Source, edge.Target) + } - err := graph.RemoveVertex(test.vertex) + err := graph.RemoveVertex(test.vertex) - if !errors.Is(err, test.expectedError) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err) - } + if !errors.Is(err, test.expectedError) { + t.Errorf("error expectancy doesn't match: expected %v, got %v", test.expectedError, err) + } + }) } } @@ -405,52 +413,54 @@ func TestDirected_AddEdge(t *testing.T) { } for name, test := range tests { - graph := newDirected(IntHash, test.traits, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newDirected(IntHash, test.traits, newMemoryStore[int, int]()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - var err error + var err error - for _, edge := range test.edges { - if len(edge.Properties.Attributes) == 0 { - err = graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight), EdgeData(edge.Properties.Data)) - } - // If there are edge attributes, iterate over them and call the - // EdgeAttribute functional option for each entry. An edge should - // only have one attribute so that AddEdge is invoked once. - for key, value := range edge.Properties.Attributes { - err = graph.AddEdge( - edge.Source, - edge.Target, - EdgeWeight(edge.Properties.Weight), - EdgeData(edge.Properties.Data), - EdgeAttribute(key, value), - ) - } - if err != nil { - break + for _, edge := range test.edges { + if len(edge.Properties.Attributes) == 0 { + err = graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight), EdgeData(edge.Properties.Data)) + } + // If there are edge attributes, iterate over them and call the + // EdgeAttribute functional option for each entry. An edge should + // only have one attribute so that AddEdge is invoked once. + for key, value := range edge.Properties.Attributes { + err = graph.AddEdge( + edge.Source, + edge.Target, + EdgeWeight(edge.Properties.Weight), + EdgeData(edge.Properties.Data), + EdgeAttribute(key, value), + ) + } + if err != nil { + break + } } - } - if !errors.Is(err, test.finallyExpectedError) { - t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, test.finallyExpectedError, err) - } + if !errors.Is(err, test.finallyExpectedError) { + t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, test.finallyExpectedError, err) + } - for _, expectedEdge := range test.expectedEdges { - sourceHash := graph.hash(expectedEdge.Source) - targetHash := graph.hash(expectedEdge.Target) + for _, expectedEdge := range test.expectedEdges { + sourceHash := graph.hash(expectedEdge.Source) + targetHash := graph.hash(expectedEdge.Target) - edge, err := graph.Edge(sourceHash, targetHash) - if err != nil { - t.Fatalf("%s: edge with source %v and target %v not found", name, expectedEdge.Source, expectedEdge.Target) - } + edge, err := graph.Edge(sourceHash, targetHash) + if err != nil { + t.Fatalf("%s: edge with source %v and target %v not found", name, expectedEdge.Source, expectedEdge.Target) + } - if !edgesAreEqual(expectedEdge, edge, true) { - t.Errorf("%s: expected edge %v, got %v", name, expectedEdge, edge) + if !edgesAreEqual(expectedEdge, edge, true) { + t.Errorf("%s: expected edge %v, got %v", name, expectedEdge, edge) + } } - } + }) } } @@ -643,37 +653,39 @@ func TestDirected_Edge(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New(IntHash, Directed()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - if err := graph.AddEdge(test.edge.Source, test.edge.Target, EdgeWeight(test.edge.Properties.Weight), EdgeData(test.edge.Properties.Data)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) - } + if err := graph.AddEdge(test.edge.Source, test.edge.Target, EdgeWeight(test.edge.Properties.Weight), EdgeData(test.edge.Properties.Data)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } - edge, err := graph.Edge(test.args[0], test.args[1]) + edge, err := graph.Edge(test.args[0], test.args[1]) - if !errors.Is(err, test.expectedError) { - t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err) - } + if !errors.Is(err, test.expectedError) { + t.Fatalf("error expectancy doesn't match: expected %v, got %v", test.expectedError, err) + } - if test.expectedError != nil { - continue - } + if test.expectedError != nil { + t.SkipNow() + } - if edge.Source != test.args[0] { - t.Errorf("%s: source expectancy doesn't match: expected %v, got %v", name, test.args[0], edge.Source) - } + if edge.Source != test.args[0] { + t.Errorf("source expectancy doesn't match: expected %v, got %v", test.args[0], edge.Source) + } - if edge.Target != test.args[1] { - t.Errorf("%s: target expectancy doesn't match: expected %v, got %v", name, test.args[1], edge.Target) - } + if edge.Target != test.args[1] { + t.Errorf("target expectancy doesn't match: expected %v, got %v", test.args[1], edge.Target) + } - if !edgesAreEqual(test.edge, edge, true) { - t.Errorf("%s: expected edge %v, got %v", name, test.edge, edge) - } + if !edgesAreEqual(test.edge, edge, true) { + t.Errorf("expected edge %v, got %v", test.edge, edge) + } + }) } } @@ -775,7 +787,7 @@ func TestDirected_Edges(t *testing.T) { continue } if !edgesAreEqual(expectedEdge, actualEdge, true) { - t.Errorf("%s: expected edge %v, got %v", name, expectedEdge, actualEdge) + t.Errorf("expected edge %v, got %v", expectedEdge, actualEdge) } } } @@ -891,28 +903,30 @@ func TestDirected_RemoveEdge(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, Directed()) - - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + t.Run(name, func(t *testing.T) { + graph := New(IntHash, Directed()) - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) } - } - for _, removeEdge := range test.removeEdges { - if err := graph.RemoveEdge(removeEdge.Source, removeEdge.Target); !errors.Is(err, test.expectedError) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - // After removing the edge, verify that it can't be retrieved using - // Edge anymore. - if _, err := graph.Edge(removeEdge.Source, removeEdge.Target); err != ErrEdgeNotFound { - t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, ErrEdgeNotFound, err) + + for _, removeEdge := range test.removeEdges { + if err := graph.RemoveEdge(removeEdge.Source, removeEdge.Target); !errors.Is(err, test.expectedError) { + t.Errorf("error expectancy doesn't match: expected %v, got %v", test.expectedError, err) + } + // After removing the edge, verify that it can't be retrieved using + // Edge anymore. + if _, err := graph.Edge(removeEdge.Source, removeEdge.Target); err != ErrEdgeNotFound { + t.Fatalf("error expectancy doesn't match: expected %v, got %v", ErrEdgeNotFound, err) + } } - } + }) } } @@ -967,36 +981,38 @@ func TestDirected_AdjacencyList(t *testing.T) { } for name, test := range tests { - graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + t.Run(name, func(t *testing.T) { + graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) } - } - - adjacencyMap, _ := graph.AdjacencyMap() - for expectedVertex, expectedAdjacencies := range test.expected { - adjacencies, ok := adjacencyMap[expectedVertex] - if !ok { - t.Errorf("%s: expected vertex %v does not exist in adjacency map", name, expectedVertex) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - for expectedAdjacency, expectedEdge := range expectedAdjacencies { - edge, ok := adjacencies[expectedAdjacency] + adjacencyMap, _ := graph.AdjacencyMap() + + for expectedVertex, expectedAdjacencies := range test.expected { + adjacencies, ok := adjacencyMap[expectedVertex] if !ok { - t.Errorf("%s: expected adjacency %v does not exist in map of %v", name, expectedAdjacency, expectedVertex) + t.Errorf("expected vertex %v does not exist in adjacency map", expectedVertex) } - if edge.Source != expectedEdge.Source || edge.Target != expectedEdge.Target { - t.Errorf("%s: edge expectancy doesn't match: expected %v, got %v", name, expectedEdge, edge) + + for expectedAdjacency, expectedEdge := range expectedAdjacencies { + edge, ok := adjacencies[expectedAdjacency] + if !ok { + t.Errorf("expected adjacency %v does not exist in map of %v", expectedAdjacency, expectedVertex) + } + if edge.Source != expectedEdge.Source || edge.Target != expectedEdge.Target { + t.Errorf("edge expectancy doesn't match: expected %v, got %v", expectedEdge, edge) + } } } - } + }) } } @@ -1050,36 +1066,38 @@ func TestDirected_PredecessorMap(t *testing.T) { } for name, test := range tests { - graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + t.Run(name, func(t *testing.T) { + graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) } - } - - predecessors, _ := graph.PredecessorMap() - for expectedVertex, expectedPredecessors := range test.expected { - predecessors, ok := predecessors[expectedVertex] - if !ok { - t.Errorf("%s: expected vertex %v does not exist in adjacency map", name, expectedVertex) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - for expectedPredecessor, expectedEdge := range expectedPredecessors { - edge, ok := predecessors[expectedPredecessor] + predecessors, _ := graph.PredecessorMap() + + for expectedVertex, expectedPredecessors := range test.expected { + predecessors, ok := predecessors[expectedVertex] if !ok { - t.Errorf("%s: expected adjacency %v does not exist in map of %v", name, expectedPredecessor, expectedVertex) + t.Errorf("expected vertex %v does not exist in adjacency map", expectedVertex) } - if edge.Source != expectedEdge.Source || edge.Target != expectedEdge.Target { - t.Errorf("%s: edge expectancy doesn't match: expected %v, got %v", name, expectedEdge, edge) + + for expectedPredecessor, expectedEdge := range expectedPredecessors { + edge, ok := predecessors[expectedPredecessor] + if !ok { + t.Errorf("expected adjacency %v does not exist in map of %v", expectedPredecessor, expectedVertex) + } + if edge.Source != expectedEdge.Source || edge.Target != expectedEdge.Target { + t.Errorf("edge expectancy doesn't match: expected %v, got %v", expectedEdge, edge) + } } } - } + }) } } @@ -1108,46 +1126,48 @@ func TestDirected_Clone(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New(IntHash, Directed()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex, VertexWeight(vertex), VertexAttribute("color", "red")) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex, VertexWeight(vertex), VertexAttribute("color", "red")) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - clonedGraph, err := graph.Clone() - if err != nil { - t.Fatalf("%s: failed to clone graph: %s", name, err.Error()) - } + clonedGraph, err := graph.Clone() + if err != nil { + t.Fatalf("failed to clone graph: %s", err.Error()) + } - expected := graph.(*directed[int, int]) - actual := clonedGraph.(*directed[int, int]) + expected := graph.(*directed[int, int]) + actual := clonedGraph.(*directed[int, int]) - if actual.hash(5) != expected.hash(5) { - t.Errorf("%s: hash expectancy doesn't match: expected %v, got %v", name, expected.hash, actual.hash) - } + if actual.hash(5) != expected.hash(5) { + t.Errorf("hash expectancy doesn't match: expected %v, got %v", expected.hash, actual.hash) + } - if !traitsAreEqual(actual.traits, expected.traits) { - t.Errorf("%s: traits expectancy doesn't match: expected %v, got %v", name, expected.traits, actual.traits) - } + if !traitsAreEqual(actual.traits, expected.traits) { + t.Errorf("traits expectancy doesn't match: expected %v, got %v", expected.traits, actual.traits) + } - expectedAdjacencyMap, _ := graph.AdjacencyMap() - actualAdjacencyMap, _ := actual.AdjacencyMap() + expectedAdjacencyMap, _ := graph.AdjacencyMap() + actualAdjacencyMap, _ := actual.AdjacencyMap() - if !adjacencyMapsAreEqual(expectedAdjacencyMap, actualAdjacencyMap, expected.edgesAreEqual) { - t.Errorf("%s: expected adjacency map %v, got %v", name, expectedAdjacencyMap, actualAdjacencyMap) - } + if !adjacencyMapsAreEqual(expectedAdjacencyMap, actualAdjacencyMap, expected.edgesAreEqual) { + t.Errorf("expected adjacency map %v, got %v", expectedAdjacencyMap, actualAdjacencyMap) + } - _ = clonedGraph.AddVertex(10) + _ = clonedGraph.AddVertex(10) - if _, err := graph.Vertex(10); err == nil { - t.Errorf("%s: vertex 10 shouldn't exist in original graph", name) - } + if _, err := graph.Vertex(10); err == nil { + t.Errorf("vertex 10 shouldn't exist in original graph") + } + }) } } @@ -1197,28 +1217,30 @@ func TestDirected_OrderAndSize(t *testing.T) { } for name, test := range tests { - graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - order, _ := graph.Order() - size, _ := graph.Size() + order, _ := graph.Order() + size, _ := graph.Size() - if order != test.expectedOrder { - t.Errorf("%s: order expectancy doesn't match: expected %d, got %d", name, test.expectedOrder, order) - } + if order != test.expectedOrder { + t.Errorf("order expectancy doesn't match: expected %d, got %d", test.expectedOrder, order) + } - if size != test.expectedSize { - t.Errorf("%s: size expectancy doesn't match: expected %d, got %d", name, test.expectedSize, size) - } + if size != test.expectedSize { + t.Errorf("size expectancy doesn't match: expected %d, got %d", test.expectedSize, size) + } + }) } } @@ -1241,12 +1263,14 @@ func TestDirected_edgesAreEqual(t *testing.T) { } for name, test := range tests { - graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - actual := graph.edgesAreEqual(test.a, test.b) + t.Run(name, func(t *testing.T) { + graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + actual := graph.edgesAreEqual(test.a, test.b) - if actual != test.edgesAreEqual { - t.Errorf("%s: equality expectations don't match: expected %v, got %v", name, test.edgesAreEqual, actual) - } + if actual != test.edgesAreEqual { + t.Errorf("equality expectations don't match: expected %v, got %v", test.edgesAreEqual, actual) + } + }) } } @@ -1264,25 +1288,27 @@ func TestDirected_addEdge(t *testing.T) { } for name, test := range tests { - graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, edge := range test.edges { - sourceHash := graph.hash(edge.Source) - TargetHash := graph.hash(edge.Target) - err := graph.addEdge(sourceHash, TargetHash, edge) - if err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + sourceHash := graph.hash(edge.Source) + TargetHash := graph.hash(edge.Target) + err := graph.addEdge(sourceHash, TargetHash, edge) + if err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - outEdges := graph.store.(*memoryStore[int, int]).outEdges - if len(outEdges) != len(test.edges) { - t.Errorf("%s: number of outgoing edges doesn't match: expected %v, got %v", name, len(test.edges), len(outEdges)) - } - inEdges := graph.store.(*memoryStore[int, int]).inEdges - if len(inEdges) != len(test.edges) { - t.Errorf("%s: number of ingoing edges doesn't match: expected %v, got %v", name, len(test.edges), len(inEdges)) - } + outEdges := graph.store.(*memoryStore[int, int]).outEdges + if len(outEdges) != len(test.edges) { + t.Errorf("number of outgoing edges doesn't match: expected %v, got %v", len(test.edges), len(outEdges)) + } + inEdges := graph.store.(*memoryStore[int, int]).inEdges + if len(inEdges) != len(test.edges) { + t.Errorf("number of ingoing edges doesn't match: expected %v, got %v", len(test.edges), len(inEdges)) + } + }) } } @@ -1327,23 +1353,25 @@ func TestDirected_predecessors(t *testing.T) { } for name, test := range tests { - graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - predecessors, _ := predecessors(graph, graph.hash(test.vertex)) + predecessors, _ := predecessors(graph, graph.hash(test.vertex)) - if !slicesAreEqual(predecessors, test.expectedPredecessors) { - t.Errorf("%s: predecessors don't match: expected %v, got %v", name, test.expectedPredecessors, predecessors) - } + if !slicesAreEqual(predecessors, test.expectedPredecessors) { + t.Errorf("predecessors don't match: expected %v, got %v", test.expectedPredecessors, predecessors) + } + }) } } diff --git a/graph_test.go b/graph_test.go index 82ea96d..9e4adef 100644 --- a/graph_test.go +++ b/graph_test.go @@ -24,12 +24,14 @@ func TestNew(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, test.options...) - actualType := reflect.TypeOf(graph) + t.Run(name, func(t *testing.T) { + graph := New(IntHash, test.options...) + actualType := reflect.TypeOf(graph) - if actualType != test.expectedType { - t.Errorf("%s: graph type expectancy doesn't match: expected %v, got %v", name, test.expectedType, actualType) - } + if actualType != test.expectedType { + t.Errorf("graph type expectancy doesn't match: expected %v, got %v", test.expectedType, actualType) + } + }) } } @@ -76,7 +78,7 @@ func TestNewLike(t *testing.T) { } else { actual, ok := h.(*undirected[int, int]) if !ok { - t.Fatalf("type assertion to *directed failed") + t.Fatalf("type assertion to *undirected failed") } expected := test.g.(*undirected[int, int]) @@ -105,11 +107,13 @@ func TestStringHash(t *testing.T) { } for name, test := range tests { - hash := StringHash(test.value) + t.Run(name, func(t *testing.T) { + hash := StringHash(test.value) - if hash != test.expectedHash { - t.Errorf("%s: hash expectancy doesn't match: expected %v, got %v", name, test.expectedHash, hash) - } + if hash != test.expectedHash { + t.Errorf("hash expectancy doesn't match: expected %v, got %v", test.expectedHash, hash) + } + }) } } @@ -125,11 +129,13 @@ func TestIntHash(t *testing.T) { } for name, test := range tests { - hash := IntHash(test.value) + t.Run(name, func(t *testing.T) { + hash := IntHash(test.value) - if hash != test.expectedHash { - t.Errorf("%s: hash expectancy doesn't match: expected %v, got %v", name, test.expectedHash, hash) - } + if hash != test.expectedHash { + t.Errorf("hash expectancy doesn't match: expected %v, got %v", test.expectedHash, hash) + } + }) } } @@ -147,13 +153,15 @@ func TestEdgeWeight(t *testing.T) { } for name, test := range tests { - properties := EdgeProperties{} + t.Run(name, func(t *testing.T) { + properties := EdgeProperties{} - EdgeWeight(test.weight)(&properties) + EdgeWeight(test.weight)(&properties) - if properties.Weight != test.expected.Weight { - t.Errorf("%s: weight expectation doesn't match: expected %v, got %v", name, test.expected.Weight, properties.Weight) - } + if properties.Weight != test.expected.Weight { + t.Errorf("weight expectation doesn't match: expected %v, got %v", test.expected.Weight, properties.Weight) + } + }) } } diff --git a/paths_test.go b/paths_test.go index 3874629..0204033 100644 --- a/paths_test.go +++ b/paths_test.go @@ -60,26 +60,28 @@ func TestDirectedCreatesCycle(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New(IntHash, Directed()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - createsCycle, err := CreatesCycle(graph, test.sourceHash, test.targetHash) - if err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) - } + createsCycle, err := CreatesCycle(graph, test.sourceHash, test.targetHash) + if err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } - if createsCycle != test.createsCycle { - t.Errorf("%s: cycle expectancy doesn't match: expected %v, got %v", name, test.createsCycle, createsCycle) - } + if createsCycle != test.createsCycle { + t.Errorf("cycle expectancy doesn't match: expected %v, got %v", test.createsCycle, createsCycle) + } + }) } } @@ -135,26 +137,28 @@ func TestUndirectedCreatesCycle(t *testing.T) { } for name, test := range tests { - graph := New(IntHash) + t.Run(name, func(t *testing.T) { + graph := New(IntHash) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - createsCycle, err := CreatesCycle(graph, test.sourceHash, test.targetHash) - if err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) - } + createsCycle, err := CreatesCycle(graph, test.sourceHash, test.targetHash) + if err != nil { + t.Fatalf("failed to determine if edge would create cycle: %s", err.Error()) + } - if createsCycle != test.createsCycle { - t.Errorf("%s: cycle expectancy doesn't match: expected %v, got %v", name, test.createsCycle, createsCycle) - } + if createsCycle != test.createsCycle { + t.Errorf("cycle expectancy doesn't match: expected %v, got %v", test.createsCycle, createsCycle) + } + }) } } @@ -268,34 +272,36 @@ func TestDirectedShortestPath(t *testing.T) { } for name, test := range tests { - graph := New(StringHash, Directed()) - graph.(*directed[string, string]).traits.IsWeighted = test.isWeighted + t.Run(name, func(t *testing.T) { + graph := New(StringHash, Directed()) + graph.(*directed[string, string]).traits.IsWeighted = test.isWeighted - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - shortestPath, err := ShortestPath(graph, test.sourceHash, test.targetHash) + shortestPath, err := ShortestPath(graph, test.sourceHash, test.targetHash) - if test.shouldFail != (err != nil) { - t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, (err != nil), err) - } + if test.shouldFail != (err != nil) { + t.Fatalf("error expectancy doesn't match: expected %v, got %v (error: %v)", test.shouldFail, (err != nil), err) + } - if len(shortestPath) != len(test.expectedShortestPath) { - t.Fatalf("%s: path length expectancy doesn't match: expected %v, got %v", name, len(test.expectedShortestPath), len(shortestPath)) - } + if len(shortestPath) != len(test.expectedShortestPath) { + t.Fatalf("path length expectancy doesn't match: expected %v, got %v", len(test.expectedShortestPath), len(shortestPath)) + } - for i, expectedVertex := range test.expectedShortestPath { - if shortestPath[i] != expectedVertex { - t.Errorf("%s: path vertex expectancy doesn't match: expected %v at index %d, got %v", name, expectedVertex, i, shortestPath[i]) + for i, expectedVertex := range test.expectedShortestPath { + if shortestPath[i] != expectedVertex { + t.Errorf("path vertex expectancy doesn't match: expected %v at index %d, got %v", expectedVertex, i, shortestPath[i]) + } } - } + }) } } @@ -384,34 +390,36 @@ func TestUndirectedShortestPath(t *testing.T) { } for name, test := range tests { - graph := New(StringHash) - graph.(*undirected[string, string]).traits.IsWeighted = test.isWeighted + t.Run(name, func(t *testing.T) { + graph := New(StringHash) + graph.(*undirected[string, string]).traits.IsWeighted = test.isWeighted - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - shortestPath, err := ShortestPath(graph, test.sourceHash, test.targetHash) + shortestPath, err := ShortestPath(graph, test.sourceHash, test.targetHash) - if test.shouldFail != (err != nil) { - t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, (err != nil), err) - } + if test.shouldFail != (err != nil) { + t.Fatalf("error expectancy doesn't match: expected %v, got %v (error: %v)", test.shouldFail, (err != nil), err) + } - if len(shortestPath) != len(test.expectedShortestPath) { - t.Fatalf("%s: path length expectancy doesn't match: expected %v, got %v", name, len(test.expectedShortestPath), len(shortestPath)) - } + if len(shortestPath) != len(test.expectedShortestPath) { + t.Fatalf("path length expectancy doesn't match: expected %v, got %v", len(test.expectedShortestPath), len(shortestPath)) + } - for i, expectedVertex := range test.expectedShortestPath { - if shortestPath[i] != expectedVertex { - t.Errorf("%s: path vertex expectancy doesn't match: expected %v at index %d, got %v", name, expectedVertex, i, shortestPath[i]) + for i, expectedVertex := range test.expectedShortestPath { + if shortestPath[i] != expectedVertex { + t.Errorf("path vertex expectancy doesn't match: expected %v at index %d, got %v", expectedVertex, i, shortestPath[i]) + } } - } + }) } } @@ -444,32 +452,34 @@ func TestDirectedStronglyConnectedComponents(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New(IntHash, Directed()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - sccs, _ := StronglyConnectedComponents(graph) - matchedSCCs := 0 + sccs, _ := StronglyConnectedComponents(graph) + matchedSCCs := 0 - for _, scc := range sccs { - for _, expectedSCC := range test.expectedSCCs { - if slicesAreEqual(scc, expectedSCC) { - matchedSCCs++ + for _, scc := range sccs { + for _, expectedSCC := range test.expectedSCCs { + if slicesAreEqual(scc, expectedSCC) { + matchedSCCs++ + } } } - } - if matchedSCCs != len(test.expectedSCCs) { - t.Errorf("%s: expected SCCs don't match: expected %v, got %v", name, test.expectedSCCs, sccs) - } + if matchedSCCs != len(test.expectedSCCs) { + t.Errorf("expected SCCs don't match: expected %v, got %v", test.expectedSCCs, sccs) + } + }) } } @@ -485,17 +495,19 @@ func TestUndirectedStronglyConnectedComponents(t *testing.T) { } for name, test := range tests { - graph := New(IntHash) + t.Run(name, func(t *testing.T) { + graph := New(IntHash) - sccs, err := StronglyConnectedComponents(graph) + sccs, err := StronglyConnectedComponents(graph) - if test.shouldFail != (err != nil) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, (err != nil), err) - } + if test.shouldFail != (err != nil) { + t.Errorf("error expectancy doesn't match: expected %v, got %v (error: %v)", test.shouldFail, (err != nil), err) + } - if test.expectedSCCs == nil && sccs != nil { - t.Errorf("%s: SCC expectancy doesn't match: expcted %v, got %v", name, test.expectedSCCs, sccs) - } + if test.expectedSCCs == nil && sccs != nil { + t.Errorf("SCC expectancy doesn't match: expcted %v, got %v", test.expectedSCCs, sccs) + } + }) } } @@ -506,14 +518,12 @@ func TestAllPathsBetween(t *testing.T) { end K } type testCase[K comparable, T any] struct { - name string args args[K, T] want [][]K wantErr bool } - tests := []testCase[int, int]{ - { - name: "directed", + tests := map[string]testCase[int, int]{ + "directed": { args: args[int, int]{ g: func() Graph[int, int] { g := New(IntHash, Directed()) @@ -545,8 +555,7 @@ func TestAllPathsBetween(t *testing.T) { }, wantErr: false, }, - { - name: "undirected", + "undirected": { args: args[int, int]{ g: func() Graph[int, int] { g := New(IntHash) @@ -581,8 +590,7 @@ func TestAllPathsBetween(t *testing.T) { }, wantErr: false, }, - { - name: "undirected (complex)", + "undirected (complex)": { args: args[int, int]{ g: func() Graph[int, int] { g := New(IntHash) @@ -624,8 +632,8 @@ func TestAllPathsBetween(t *testing.T) { wantErr: false, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + for name, tt := range tests { + t.Run(name, func(t *testing.T) { got, err := AllPathsBetween(tt.args.g, tt.args.start, tt.args.end) if (err != nil) != tt.wantErr { t.Errorf("AllPathsBetween() error = %v, wantErr %v", err, tt.wantErr) diff --git a/sets_test.go b/sets_test.go index fa833d1..e1eb5dc 100644 --- a/sets_test.go +++ b/sets_test.go @@ -127,41 +127,43 @@ func TestDirectedUnion(t *testing.T) { } for name, test := range tests { - g := New(IntHash, Directed()) + t.Run(name, func(t *testing.T) { + g := New(IntHash, Directed()) - for _, vertex := range test.gVertices { - _ = g.AddVertex(vertex, copyVertexProperties(test.gVertexProperties[vertex])) - } + for _, vertex := range test.gVertices { + _ = g.AddVertex(vertex, copyVertexProperties(test.gVertexProperties[vertex])) + } - for _, edge := range test.gEdges { - _ = g.AddEdge(copyEdge(edge)) - } + for _, edge := range test.gEdges { + _ = g.AddEdge(copyEdge(edge)) + } - h := New(IntHash, Directed()) + h := New(IntHash, Directed()) - for _, vertex := range test.hVertices { - _ = h.AddVertex(vertex, copyVertexProperties(test.gVertexProperties[vertex])) - } + for _, vertex := range test.hVertices { + _ = h.AddVertex(vertex, copyVertexProperties(test.gVertexProperties[vertex])) + } - for _, edge := range test.hEdges { - _ = h.AddEdge(copyEdge(edge)) - } + for _, edge := range test.hEdges { + _ = h.AddEdge(copyEdge(edge)) + } - union, err := Union(g, h) - if err != nil { - t.Fatalf("%s: unexpected union error: %s", name, err.Error()) - } + union, err := Union(g, h) + if err != nil { + t.Fatalf("unexpected union error: %s", err.Error()) + } - unionAdjacencyMap, err := union.AdjacencyMap() - if err != nil { - t.Fatalf("%s: unexpected adjaceny map error: %s", name, err.Error()) - } + unionAdjacencyMap, err := union.AdjacencyMap() + if err != nil { + t.Fatalf("unexpected adjaceny map error: %s", err.Error()) + } - edgesAreEqual := g.(*directed[int, int]).edgesAreEqual + edgesAreEqual := g.(*directed[int, int]).edgesAreEqual - if !adjacencyMapsAreEqual(test.expectedAdjacencyMap, unionAdjacencyMap, edgesAreEqual) { - t.Fatalf("expected adjacency map %v, got %v", test.expectedAdjacencyMap, unionAdjacencyMap) - } + if !adjacencyMapsAreEqual(test.expectedAdjacencyMap, unionAdjacencyMap, edgesAreEqual) { + t.Fatalf("expected adjacency map %v, got %v", test.expectedAdjacencyMap, unionAdjacencyMap) + } + }) } } diff --git a/traits_test.go b/traits_test.go index 169d259..7484991 100644 --- a/traits_test.go +++ b/traits_test.go @@ -14,13 +14,15 @@ func TestDirected(t *testing.T) { } for name, test := range tests { - p := &Traits{} + t.Run(name, func(t *testing.T) { + p := &Traits{} - Directed()(p) + Directed()(p) - if !traitsAreEqual(test.expected, p) { - t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) - } + if !traitsAreEqual(test.expected, p) { + t.Errorf("trait expectation doesn't match: expected %v, got %v", test.expected, p) + } + }) } } @@ -36,13 +38,15 @@ func TestAcyclic(t *testing.T) { } for name, test := range tests { - p := &Traits{} + t.Run(name, func(t *testing.T) { + p := &Traits{} - Acyclic()(p) + Acyclic()(p) - if !traitsAreEqual(test.expected, p) { - t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) - } + if !traitsAreEqual(test.expected, p) { + t.Errorf("trait expectation doesn't match: expected %v, got %v", test.expected, p) + } + }) } } @@ -58,13 +62,15 @@ func TestWeighted(t *testing.T) { } for name, test := range tests { - p := &Traits{} + t.Run(name, func(t *testing.T) { + p := &Traits{} - Weighted()(p) + Weighted()(p) - if !traitsAreEqual(test.expected, p) { - t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) - } + if !traitsAreEqual(test.expected, p) { + t.Errorf("trait expectation doesn't match: expected %v, got %v", test.expected, p) + } + }) } } @@ -80,13 +86,15 @@ func TestRooted(t *testing.T) { } for name, test := range tests { - p := &Traits{} + t.Run(name, func(t *testing.T) { + p := &Traits{} - Rooted()(p) + Rooted()(p) - if !traitsAreEqual(test.expected, p) { - t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) - } + if !traitsAreEqual(test.expected, p) { + t.Errorf("trait expectation doesn't match: expected %v, got %v", test.expected, p) + } + }) } } @@ -103,13 +111,15 @@ func TestTree(t *testing.T) { } for name, test := range tests { - p := &Traits{} + t.Run(name, func(t *testing.T) { + p := &Traits{} - Tree()(p) + Tree()(p) - if !traitsAreEqual(test.expected, p) { - t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) - } + if !traitsAreEqual(test.expected, p) { + t.Errorf("trait expectation doesn't match: expected %v, got %v", test.expected, p) + } + }) } } @@ -126,13 +136,15 @@ func TestPreventCycles(t *testing.T) { } for name, test := range tests { - p := &Traits{} + t.Run(name, func(t *testing.T) { + p := &Traits{} - PreventCycles()(p) + PreventCycles()(p) - if !traitsAreEqual(test.expected, p) { - t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) - } + if !traitsAreEqual(test.expected, p) { + t.Errorf("trait expectation doesn't match: expected %v, got %v", test.expected, p) + } + }) } } diff --git a/traversal_test.go b/traversal_test.go index 1000e0b..84d1c6f 100644 --- a/traversal_test.go +++ b/traversal_test.go @@ -58,42 +58,44 @@ func TestDirectedDFS(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New(IntHash, Directed()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - visited := make(map[int]struct{}) + visited := make(map[int]struct{}) - visit := func(value int) bool { - visited[value] = struct{}{} + visit := func(value int) bool { + visited[value] = struct{}{} - if test.stopAtVertex != -1 { - if value == test.stopAtVertex { - return true + if test.stopAtVertex != -1 { + if value == test.stopAtVertex { + return true + } } + return false } - return false - } - _ = DFS(graph, test.startHash, visit) + _ = DFS(graph, test.startHash, visit) - if len(visited) != len(test.expectedVisits) { - t.Fatalf("%s: numbers of visited vertices don't match: expected %v, got %v", name, len(test.expectedVisits), len(visited)) - } + if len(visited) != len(test.expectedVisits) { + t.Fatalf("numbers of visited vertices don't match: expected %v, got %v", len(test.expectedVisits), len(visited)) + } - for _, expectedVisit := range test.expectedVisits { - if _, ok := visited[expectedVisit]; !ok { - t.Errorf("%s: expected vertex %v to be visited, but it isn't", name, expectedVisit) + for _, expectedVisit := range test.expectedVisits { + if _, ok := visited[expectedVisit]; !ok { + t.Errorf("expected vertex %v to be visited, but it isn't", expectedVisit) + } } - } + }) } } @@ -196,50 +198,52 @@ func TestUndirectedDFS(t *testing.T) { } for name, test := range tests { - graph := New(IntHash) + t.Run(name, func(t *testing.T) { + graph := New(IntHash) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - visited := make(map[int]struct{}) + visited := make(map[int]struct{}) - visit := func(value int) bool { - visited[value] = struct{}{} + visit := func(value int) bool { + visited[value] = struct{}{} - if test.stopAtVertex != -1 { - if value == test.stopAtVertex { - return true + if test.stopAtVertex != -1 { + if value == test.stopAtVertex { + return true + } } + return false } - return false - } - _ = DFS(graph, test.startHash, visit) + _ = DFS(graph, test.startHash, visit) - if len(visited) < len(test.expectedMinimumVisits) { - t.Fatalf("%s: expected number of minimum visits doesn't match: expected %v, got %v", name, len(test.expectedMinimumVisits), len(visited)) - } + if len(visited) < len(test.expectedMinimumVisits) { + t.Fatalf("expected number of minimum visits doesn't match: expected %v, got %v", len(test.expectedMinimumVisits), len(visited)) + } - if test.forbiddenVisits != nil { - for _, forbiddenVisit := range test.forbiddenVisits { - if _, ok := visited[forbiddenVisit]; ok { - t.Errorf("%s: expected vertex %v to not be visited, but it is", name, forbiddenVisit) + if test.forbiddenVisits != nil { + for _, forbiddenVisit := range test.forbiddenVisits { + if _, ok := visited[forbiddenVisit]; ok { + t.Errorf("expected vertex %v to not be visited, but it is", forbiddenVisit) + } } } - } - for _, expectedVisit := range test.expectedMinimumVisits { - if _, ok := visited[expectedVisit]; !ok { - t.Errorf("%s: expected vertex %v to be visited, but it isn't", name, expectedVisit) + for _, expectedVisit := range test.expectedMinimumVisits { + if _, ok := visited[expectedVisit]; !ok { + t.Errorf("expected vertex %v to be visited, but it isn't", expectedVisit) + } } - } + }) } } @@ -287,51 +291,53 @@ func TestDirectedBFS(t *testing.T) { } for name, test := range tests { - graph := New(IntHash, Directed()) + t.Run(name, func(t *testing.T) { + graph := New(IntHash, Directed()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - visited := make(map[int]struct{}) + visited := make(map[int]struct{}) - visit := func(value int) bool { - visited[value] = struct{}{} + visit := func(value int) bool { + visited[value] = struct{}{} - if test.stopAtVertex != -1 { - if value == test.stopAtVertex { - return true + if test.stopAtVertex != -1 { + if value == test.stopAtVertex { + return true + } } + return false } - return false - } - _ = BFS(graph, test.startHash, visit) + _ = BFS(graph, test.startHash, visit) - for _, expectedVisit := range test.expectedVisits { - if _, ok := visited[expectedVisit]; !ok { - t.Errorf("%s: expected vertex %v to be visited, but it isn't", name, expectedVisit) + for _, expectedVisit := range test.expectedVisits { + if _, ok := visited[expectedVisit]; !ok { + t.Errorf("expected vertex %v to be visited, but it isn't", expectedVisit) + } } - } - visitWithDepth := func(value int, depth int) bool { - visited[value] = struct{}{} - log.Printf("cur depth: %d", depth) + visitWithDepth := func(value int, depth int) bool { + visited[value] = struct{}{} + log.Printf("cur depth: %d", depth) - if test.stopAtVertex != -1 { - if value == test.stopAtVertex { - return true + if test.stopAtVertex != -1 { + if value == test.stopAtVertex { + return true + } } + return false } - return false - } - _ = BFSWithDepth(graph, test.startHash, visitWithDepth) + _ = BFSWithDepth(graph, test.startHash, visitWithDepth) + }) } } @@ -379,37 +385,39 @@ func TestUndirectedBFS(t *testing.T) { } for name, test := range tests { - graph := New(IntHash) + t.Run(name, func(t *testing.T) { + graph := New(IntHash) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - visited := make(map[int]struct{}) + visited := make(map[int]struct{}) - visit := func(value int) bool { - visited[value] = struct{}{} + visit := func(value int) bool { + visited[value] = struct{}{} - if test.stopAtVertex != -1 { - if value == test.stopAtVertex { - return true + if test.stopAtVertex != -1 { + if value == test.stopAtVertex { + return true + } } + return false } - return false - } - _ = BFS(graph, test.startHash, visit) + _ = BFS(graph, test.startHash, visit) - for _, expectedVisit := range test.expectedVisits { - if _, ok := visited[expectedVisit]; !ok { - t.Errorf("%s: expected vertex %v to be visited, but it isn't", name, expectedVisit) + for _, expectedVisit := range test.expectedVisits { + if _, ok := visited[expectedVisit]; !ok { + t.Errorf("expected vertex %v to be visited, but it isn't", expectedVisit) + } } - } + }) } } diff --git a/undirected_test.go b/undirected_test.go index c304eb3..093f867 100644 --- a/undirected_test.go +++ b/undirected_test.go @@ -21,12 +21,14 @@ func TestUndirected_Traits(t *testing.T) { } for name, test := range tests { - g := newUndirected(IntHash, test.traits, newMemoryStore[int, int]()) - traits := g.Traits() + t.Run(name, func(t *testing.T) { + g := newUndirected(IntHash, test.traits, newMemoryStore[int, int]()) + traits := g.Traits() - if !traitsAreEqual(traits, test.expected) { - t.Errorf("%s: traits expectancy doesn't match: expected %v, got %v", name, test.expected, traits) - } + if !traitsAreEqual(traits, test.expected) { + t.Errorf("traits expectancy doesn't match: expected %v, got %v", test.expected, traits) + } + }) } } @@ -60,62 +62,64 @@ func TestUndirected_AddVertex(t *testing.T) { } for name, test := range tests { - graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - var err error + var err error - for _, vertex := range test.vertices { - if test.properties == nil { - err = graph.AddVertex(vertex) - continue - } - // If there are vertex attributes, iterate over them and call the - // VertexAttribute functional option for each entry. A vertex should - // only have one attribute so that AddVertex is invoked once. - for key, value := range test.properties.Attributes { - err = graph.AddVertex(vertex, VertexWeight(test.properties.Weight), VertexAttribute(key, value)) + for _, vertex := range test.vertices { + if test.properties == nil { + err = graph.AddVertex(vertex) + continue + } + // If there are vertex attributes, iterate over them and call the + // VertexAttribute functional option for each entry. A vertex should + // only have one attribute so that AddVertex is invoked once. + for key, value := range test.properties.Attributes { + err = graph.AddVertex(vertex, VertexWeight(test.properties.Weight), VertexAttribute(key, value)) + } } - } - - if err != test.finallyExpectedError { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.finallyExpectedError, err) - } - graphStore := graph.store.(*memoryStore[int, int]) - - for _, vertex := range test.vertices { - if len(graphStore.vertices) != len(test.expectedVertices) { - t.Errorf("%s: vertex count doesn't match: expected %v, got %v", name, len(test.expectedVertices), len(graphStore.vertices)) + if err != test.finallyExpectedError { + t.Errorf("error expectancy doesn't match: expected %v, got %v", test.finallyExpectedError, err) } - hash := graph.hash(vertex) - vertices := graph.store.(*memoryStore[int, int]).vertices - if _, ok := vertices[hash]; !ok { - t.Errorf("%s: vertex %v not found in graph: %v", name, vertex, vertices) - } + graphStore := graph.store.(*memoryStore[int, int]) - if test.properties == nil { - continue - } + for _, vertex := range test.vertices { + if len(graphStore.vertices) != len(test.expectedVertices) { + t.Errorf("vertex count doesn't match: expected %v, got %v", len(test.expectedVertices), len(graphStore.vertices)) + } - if graphStore.vertexProperties[hash].Weight != test.expectedProperties.Weight { - t.Errorf("%s: edge weights don't match: expected weight %v, got %v", name, test.expectedProperties.Weight, graphStore.vertexProperties[hash].Weight) - } + hash := graph.hash(vertex) + vertices := graph.store.(*memoryStore[int, int]).vertices + if _, ok := vertices[hash]; !ok { + t.Errorf("vertex %v not found in graph: %v", vertex, vertices) + } - if len(graphStore.vertexProperties[hash].Attributes) != len(test.expectedProperties.Attributes) { - t.Fatalf("%s: attributes lengths don't match: expcted %v, got %v", name, len(test.expectedProperties.Attributes), len(graphStore.vertexProperties[hash].Attributes)) - } + if test.properties == nil { + continue + } - for expectedKey, expectedValue := range test.expectedProperties.Attributes { - value, ok := graphStore.vertexProperties[hash].Attributes[expectedKey] - if !ok { - t.Errorf("%s: attribute keys don't match: expected key %v not found", name, expectedKey) + if graphStore.vertexProperties[hash].Weight != test.expectedProperties.Weight { + t.Errorf("edge weights don't match: expected weight %v, got %v", test.expectedProperties.Weight, graphStore.vertexProperties[hash].Weight) } - if value != expectedValue { - t.Errorf("%s: attribute values don't match: expected value %v for key %v, got %v", name, expectedValue, expectedKey, value) + + if len(graphStore.vertexProperties[hash].Attributes) != len(test.expectedProperties.Attributes) { + t.Fatalf("attributes lengths don't match: expcted %v, got %v", len(test.expectedProperties.Attributes), len(graphStore.vertexProperties[hash].Attributes)) + } + + for expectedKey, expectedValue := range test.expectedProperties.Attributes { + value, ok := graphStore.vertexProperties[hash].Attributes[expectedKey] + if !ok { + t.Errorf("attribute keys don't match: expected key %v not found", expectedKey) + } + if value != expectedValue { + t.Errorf("attribute values don't match: expected value %v for key %v, got %v", expectedValue, expectedKey, value) + } } } - } + }) } } @@ -230,25 +234,27 @@ func TestUndirected_Vertex(t *testing.T) { } for name, test := range tests { - graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - vertex, err := graph.Vertex(test.vertex) + vertex, err := graph.Vertex(test.vertex) - if err != test.expectedError { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err) - } + if err != test.expectedError { + t.Errorf("error expectancy doesn't match: expected %v, got %v", test.expectedError, err) + } - if test.expectedError != nil { - continue - } + if test.expectedError != nil { + t.SkipNow() + } - if vertex != test.vertex { - t.Errorf("%s: vertex expectancy doesn't match: expected %v, got %v", name, test.vertex, vertex) - } + if vertex != test.vertex { + t.Errorf("vertex expectancy doesn't match: expected %v, got %v", test.vertex, vertex) + } + }) } } @@ -356,46 +362,48 @@ func TestUndirected_AddEdge(t *testing.T) { } for name, test := range tests { - graph := newUndirected(IntHash, test.traits, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newUndirected(IntHash, test.traits, newMemoryStore[int, int]()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - var err error + var err error - for _, edge := range test.edges { - if len(edge.Properties.Attributes) == 0 { - err = graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight), EdgeData(edge.Properties.Data)) - } - // If there are vertex attributes, iterate over them and call the - // VertexAttribute functional option for each entry. A vertex should - // only have one attribute so that AddVertex is invoked once. - for key, value := range edge.Properties.Attributes { - err = graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight), EdgeData(edge.Properties.Data), EdgeAttribute(key, value)) - } - if err != nil { - break + for _, edge := range test.edges { + if len(edge.Properties.Attributes) == 0 { + err = graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight), EdgeData(edge.Properties.Data)) + } + // If there are vertex attributes, iterate over them and call the + // VertexAttribute functional option for each entry. A vertex should + // only have one attribute so that AddVertex is invoked once. + for key, value := range edge.Properties.Attributes { + err = graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight), EdgeData(edge.Properties.Data), EdgeAttribute(key, value)) + } + if err != nil { + break + } } - } - if !errors.Is(err, test.finallyExpectedError) { - t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, test.finallyExpectedError, err) - } + if !errors.Is(err, test.finallyExpectedError) { + t.Fatalf("error expectancy doesn't match: expected %v, got %v", test.finallyExpectedError, err) + } - for _, expectedEdge := range test.expectedEdges { - sourceHash := graph.hash(expectedEdge.Source) - targetHash := graph.hash(expectedEdge.Target) + for _, expectedEdge := range test.expectedEdges { + sourceHash := graph.hash(expectedEdge.Source) + targetHash := graph.hash(expectedEdge.Target) - edge, ok := graph.store.(*memoryStore[int, int]).outEdges[sourceHash][targetHash] - if !ok { - t.Fatalf("%s: edge with source %v and target %v not found", name, expectedEdge.Source, expectedEdge.Target) - } + edge, ok := graph.store.(*memoryStore[int, int]).outEdges[sourceHash][targetHash] + if !ok { + t.Fatalf("edge with source %v and target %v not found", expectedEdge.Source, expectedEdge.Target) + } - if !edgesAreEqual(expectedEdge, edge, false) { - t.Errorf("%s: expected edge %v, got %v", name, expectedEdge, edge) + if !edgesAreEqual(expectedEdge, edge, false) { + t.Errorf("expected edge %v, got %v", expectedEdge, edge) + } } - } + }) } } @@ -537,7 +545,7 @@ func TestUndirected_AddEdgesFrom(t *testing.T) { } if !edgesAreEqual(edge, actualEdge, false) { - t.Errorf("%s: expected edge %v, got %v", name, edge, actualEdge) + t.Errorf("expected edge %v, got %v", edge, actualEdge) } } }) @@ -576,21 +584,23 @@ func TestUndirected_RemoveVertex(t *testing.T) { } for name, test := range tests { - graph := New(IntHash) + t.Run(name, func(t *testing.T) { + graph := New(IntHash) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - _ = graph.AddEdge(edge.Source, edge.Target) - } + for _, edge := range test.edges { + _ = graph.AddEdge(edge.Source, edge.Target) + } - err := graph.RemoveVertex(test.vertex) + err := graph.RemoveVertex(test.vertex) - if !errors.Is(err, test.expectedError) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err) - } + if !errors.Is(err, test.expectedError) { + t.Errorf("error expectancy doesn't match: expected %v, got %v", test.expectedError, err) + } + }) } } @@ -637,37 +647,39 @@ func TestUndirected_Edge(t *testing.T) { } for name, test := range tests { - graph := New(IntHash) + t.Run(name, func(t *testing.T) { + graph := New(IntHash) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - if err := graph.AddEdge(test.edge.Source, test.edge.Target, EdgeWeight(test.edge.Properties.Weight), EdgeData(test.edge.Properties.Data)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) - } + if err := graph.AddEdge(test.edge.Source, test.edge.Target, EdgeWeight(test.edge.Properties.Weight), EdgeData(test.edge.Properties.Data)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } - edge, err := graph.Edge(test.args[0], test.args[1]) + edge, err := graph.Edge(test.args[0], test.args[1]) - if !errors.Is(err, test.expectedError) { - t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err) - } + if !errors.Is(err, test.expectedError) { + t.Fatalf("error expectancy doesn't match: expected %v, got %v", test.expectedError, err) + } - if test.expectedError != nil { - continue - } + if test.expectedError != nil { + t.SkipNow() + } - if edge.Source != test.args[0] { - t.Errorf("%s: source expectancy doesn't match: expected %v, got %v", name, test.args[0], edge.Source) - } + if edge.Source != test.args[0] { + t.Errorf("source expectancy doesn't match: expected %v, got %v", test.args[0], edge.Source) + } - if edge.Target != test.args[1] { - t.Errorf("%s: target expectancy doesn't match: expected %v, got %v", name, test.args[1], edge.Target) - } + if edge.Target != test.args[1] { + t.Errorf("target expectancy doesn't match: expected %v, got %v", test.args[1], edge.Target) + } - if !edgesAreEqual(test.edge, edge, false) { - t.Errorf("%s: expected edge %v, got %v", name, test.edge, edge) - } + if !edgesAreEqual(test.edge, edge, false) { + t.Errorf("expected edge %v, got %v", test.edge, edge) + } + }) } } @@ -769,7 +781,7 @@ func TestUndirected_Edges(t *testing.T) { continue } if !edgesAreEqual(expectedEdge, actualEdge, false) { - t.Errorf("%s: expected edge %v, got %v", name, expectedEdge, actualEdge) + t.Errorf("expected edge %v, got %v", expectedEdge, actualEdge) } } } @@ -885,28 +897,30 @@ func TestUndirected_RemoveEdge(t *testing.T) { } for name, test := range tests { - graph := New(IntHash) - - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + t.Run(name, func(t *testing.T) { + graph := New(IntHash) - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) } - } - for _, removeEdge := range test.removeEdges { - if err := graph.RemoveEdge(removeEdge.Source, removeEdge.Target); !errors.Is(err, test.expectedError) { - t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.expectedError, err) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - // After removing the edge, verify that it can't be retrieved using - // Edge anymore. - if _, err := graph.Edge(removeEdge.Source, removeEdge.Target); err != ErrEdgeNotFound { - t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v", name, ErrEdgeNotFound, err) + + for _, removeEdge := range test.removeEdges { + if err := graph.RemoveEdge(removeEdge.Source, removeEdge.Target); !errors.Is(err, test.expectedError) { + t.Errorf("error expectancy doesn't match: expected %v, got %v", test.expectedError, err) + } + // After removing the edge, verify that it can't be retrieved using + // Edge anymore. + if _, err := graph.Edge(removeEdge.Source, removeEdge.Target); err != ErrEdgeNotFound { + t.Fatalf("error expectancy doesn't match: expected %v, got %v", ErrEdgeNotFound, err) + } } - } + }) } } @@ -970,36 +984,38 @@ func TestUndirected_Adjacencies(t *testing.T) { } for name, test := range tests { - graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + t.Run(name, func(t *testing.T) { + graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) } - } - - adjacencyMap, _ := graph.AdjacencyMap() - for expectedVertex, expectedAdjacencies := range test.expected { - adjacencies, ok := adjacencyMap[expectedVertex] - if !ok { - t.Errorf("%s: expected vertex %v does not exist in adjacency map", name, expectedVertex) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - for expectedAdjacency, expectedEdge := range expectedAdjacencies { - edge, ok := adjacencies[expectedAdjacency] + adjacencyMap, _ := graph.AdjacencyMap() + + for expectedVertex, expectedAdjacencies := range test.expected { + adjacencies, ok := adjacencyMap[expectedVertex] if !ok { - t.Errorf("%s: expected adjacency %v does not exist in map of %v", name, expectedAdjacency, expectedVertex) + t.Errorf("expected vertex %v does not exist in adjacency map", expectedVertex) } - if edge.Source != expectedEdge.Source || edge.Target != expectedEdge.Target { - t.Errorf("%s: edge expectancy doesn't match: expected %v, got %v", name, expectedEdge, edge) + + for expectedAdjacency, expectedEdge := range expectedAdjacencies { + edge, ok := adjacencies[expectedAdjacency] + if !ok { + t.Errorf("expected adjacency %v does not exist in map of %v", expectedAdjacency, expectedVertex) + } + if edge.Source != expectedEdge.Source || edge.Target != expectedEdge.Target { + t.Errorf("edge expectancy doesn't match: expected %v, got %v", expectedEdge, edge) + } } } - } + }) } } @@ -1063,36 +1079,38 @@ func TestUndirected_PredecessorMap(t *testing.T) { } for name, test := range tests { - graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + t.Run(name, func(t *testing.T) { + graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) } - } - predecessors, _ := graph.PredecessorMap() - - for expectedVertex, expectedPredecessors := range test.expected { - predecessors, ok := predecessors[expectedVertex] - if !ok { - t.Errorf("%s: expected vertex %v does not exist in adjacency map", name, expectedVertex) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - for expectedPredecessor, expectedEdge := range expectedPredecessors { - edge, ok := predecessors[expectedPredecessor] + predecessors, _ := graph.PredecessorMap() + + for expectedVertex, expectedPredecessors := range test.expected { + predecessors, ok := predecessors[expectedVertex] if !ok { - t.Errorf("%s: expected adjacency %v does not exist in map of %v", name, expectedPredecessor, expectedVertex) + t.Errorf("expected vertex %v does not exist in adjacency map", expectedVertex) } - if edge.Source != expectedEdge.Source || edge.Target != expectedEdge.Target { - t.Errorf("%s: edge expectancy doesn't match: expected %v, got %v", name, expectedEdge, edge) + + for expectedPredecessor, expectedEdge := range expectedPredecessors { + edge, ok := predecessors[expectedPredecessor] + if !ok { + t.Errorf("expected adjacency %v does not exist in map of %v", expectedPredecessor, expectedVertex) + } + if edge.Source != expectedEdge.Source || edge.Target != expectedEdge.Target { + t.Errorf("edge expectancy doesn't match: expected %v, got %v", expectedEdge, edge) + } } } - } + }) } } @@ -1121,46 +1139,48 @@ func TestUndirected_Clone(t *testing.T) { } for name, test := range tests { - graph := New(IntHash) + t.Run(name, func(t *testing.T) { + graph := New(IntHash) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex, VertexWeight(vertex), VertexAttribute("color", "red")) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex, VertexWeight(vertex), VertexAttribute("color", "red")) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - clonedGraph, err := graph.Clone() - if err != nil { - t.Fatalf("%s: failed to clone graph: %s", name, err.Error()) - } + clonedGraph, err := graph.Clone() + if err != nil { + t.Fatalf("failed to clone graph: %s", err.Error()) + } - expected := graph.(*undirected[int, int]) - actual := clonedGraph.(*undirected[int, int]) + expected := graph.(*undirected[int, int]) + actual := clonedGraph.(*undirected[int, int]) - if actual.hash(5) != expected.hash(5) { - t.Errorf("%s: hash expectancy doesn't match: expected %v, got %v", name, expected.hash, actual.hash) - } + if actual.hash(5) != expected.hash(5) { + t.Errorf("hash expectancy doesn't match: expected %v, got %v", expected.hash, actual.hash) + } - if !traitsAreEqual(actual.traits, expected.traits) { - t.Errorf("%s: traits expectancy doesn't match: expected %v, got %v", name, expected.traits, actual.traits) - } + if !traitsAreEqual(actual.traits, expected.traits) { + t.Errorf("traits expectancy doesn't match: expected %v, got %v", expected.traits, actual.traits) + } - expectedAdjacencyMap, _ := graph.AdjacencyMap() - actualAdjacencyMap, _ := actual.AdjacencyMap() + expectedAdjacencyMap, _ := graph.AdjacencyMap() + actualAdjacencyMap, _ := actual.AdjacencyMap() - if !adjacencyMapsAreEqual(expectedAdjacencyMap, actualAdjacencyMap, expected.edgesAreEqual) { - t.Errorf("%s: expected adjacency map %v, got %v", name, expectedAdjacencyMap, actualAdjacencyMap) - } + if !adjacencyMapsAreEqual(expectedAdjacencyMap, actualAdjacencyMap, expected.edgesAreEqual) { + t.Errorf("expected adjacency map %v, got %v", expectedAdjacencyMap, actualAdjacencyMap) + } - _ = clonedGraph.AddVertex(10) + _ = clonedGraph.AddVertex(10) - if _, err := graph.Vertex(10); err == nil { - t.Errorf("%s: vertex 10 shouldn't exist in original graph", name) - } + if _, err := graph.Vertex(10); err == nil { + t.Errorf("vertex 10 shouldn't exist in original graph") + } + }) } } @@ -1209,29 +1229,30 @@ func TestUndirected_OrderAndSize(t *testing.T) { } for name, test := range tests { - graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + t.Run(name, func(t *testing.T) { + graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) } - } - order, _ := graph.Order() - size, _ := graph.Size() + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target, EdgeWeight(edge.Properties.Weight)); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } + } - if order != test.expectedOrder { - t.Errorf("%s: order expectancy doesn't match: expected %d, got %d", name, test.expectedOrder, order) - } + order, _ := graph.Order() + size, _ := graph.Size() - if size != test.expectedSize { - t.Errorf("%s: size expectancy doesn't match: expected %d, got %d", name, test.expectedSize, size) - } + if order != test.expectedOrder { + t.Errorf("order expectancy doesn't match: expected %d, got %d", test.expectedOrder, order) + } + if size != test.expectedSize { + t.Errorf("size expectancy doesn't match: expected %d, got %d", test.expectedSize, size) + } + }) } } @@ -1258,12 +1279,14 @@ func TestUndirected_edgesAreEqual(t *testing.T) { } for name, test := range tests { - graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - actual := graph.edgesAreEqual(test.a, test.b) + t.Run(name, func(t *testing.T) { + graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + actual := graph.edgesAreEqual(test.a, test.b) - if actual != test.edgesAreEqual { - t.Errorf("%s: equality expectations don't match: expected %v, got %v", name, test.edgesAreEqual, actual) - } + if actual != test.edgesAreEqual { + t.Errorf("equality expectations don't match: expected %v, got %v", test.edgesAreEqual, actual) + } + }) } } @@ -1281,25 +1304,27 @@ func TestUndirected_addEdge(t *testing.T) { } for name, test := range tests { - graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, edge := range test.edges { - sourceHash := graph.hash(edge.Source) - TargetHash := graph.hash(edge.Target) - err := graph.addEdge(sourceHash, TargetHash, edge) - if err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) - } - } - - outEdges := graph.store.(*memoryStore[int, int]).outEdges - if len(outEdges) != len(test.edges) { - t.Errorf("%s: number of outgoing edges doesn't match: expected %v, got %v", name, len(test.edges), len(outEdges)) - } - inEdges := graph.store.(*memoryStore[int, int]).inEdges - if len(inEdges) != len(test.edges) { - t.Errorf("%s: number of ingoing edges doesn't match: expected %v, got %v", name, len(test.edges), len(inEdges)) - } + for _, edge := range test.edges { + sourceHash := graph.hash(edge.Source) + TargetHash := graph.hash(edge.Target) + err := graph.addEdge(sourceHash, TargetHash, edge) + if err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } + } + + outEdges := graph.store.(*memoryStore[int, int]).outEdges + if len(outEdges) != len(test.edges) { + t.Errorf("number of outgoing edges doesn't match: expected %v, got %v", len(test.edges), len(outEdges)) + } + inEdges := graph.store.(*memoryStore[int, int]).inEdges + if len(inEdges) != len(test.edges) { + t.Errorf("number of ingoing edges doesn't match: expected %v, got %v", len(test.edges), len(inEdges)) + } + }) } } @@ -1376,23 +1401,25 @@ func TestUndirected_adjacencies(t *testing.T) { } for name, test := range tests { - graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) + t.Run(name, func(t *testing.T) { + graph := newUndirected(IntHash, &Traits{}, newMemoryStore[int, int]()) - for _, vertex := range test.vertices { - _ = graph.AddVertex(vertex) - } + for _, vertex := range test.vertices { + _ = graph.AddVertex(vertex) + } - for _, edge := range test.edges { - if err := graph.AddEdge(edge.Source, edge.Target); err != nil { - t.Fatalf("%s: failed to add edge: %s", name, err.Error()) + for _, edge := range test.edges { + if err := graph.AddEdge(edge.Source, edge.Target); err != nil { + t.Fatalf("failed to add edge: %s", err.Error()) + } } - } - adjacencyList := adjacencyList(graph.store, graph.hash(test.vertex)) + adjacencyList := adjacencyList(graph.store, graph.hash(test.vertex)) - if !slicesAreEqual(adjacencyList, test.expectedAdjancencies) { - t.Errorf("%s: adjacencies don't match: expected %v, got %v", name, test.expectedAdjancencies, adjacencyList) - } + if !slicesAreEqual(adjacencyList, test.expectedAdjancencies) { + t.Errorf("adjacencies don't match: expected %v, got %v", test.expectedAdjancencies, adjacencyList) + } + }) } }