Debian Codegraph @

store_test.go

raw symbols SCIP
1 package graph
2
3 import (
4 "errors"
5 "testing"
6 )
7
8 func TestRemoveEdge(t *testing.T) {
9 noerr := func(err error) {
10 if err != nil {
11 panic(err)
12 }
13 }
14
15 build := func(nodes []string, edges [][]string) Store[string, string] {
16 store := newMemoryStore[string, string]()
17 for _, n := range nodes {
18 noerr(store.AddVertex(n, n, VertexProperties{}))
19 }
20 for _, e := range edges {
21 noerr(store.AddEdge(e[0], e[1], Edge[string]{
22 Source: e[0],
23 Target: e[1],
24 }))
25 }
26 return store
27 }
28
29 t.Run("normal remove", func(t *testing.T) {
30 g := build([]string{
31 "a", "b", "c",
32 }, [][]string{
33 {"a", "b"},
34 {"a", "c"},
35 })
36 noerr(g.RemoveEdge("a", "b"))
37 noerr(g.RemoveEdge("a", "c"))
38 noerr(g.RemoveVertex("a"))
39 })
40 t.Run("remove edge has in-edges", func(t *testing.T) {
41 g := build([]string{
42 "a", "b", "c",
43 }, [][]string{
44 {"a", "b"},
45 {"a", "c"},
46 })
47 if err := g.RemoveVertex("b"); !errors.Is(err, ErrVertexHasEdges) {
48 t.Fail()
49 }
50 })
51 t.Run("remove edge has out-edges", func(t *testing.T) {
52 g := build([]string{
53 "a", "b", "c",
54 }, [][]string{
55 {"a", "b"},
56 {"a", "c"},
57 })
58 if err := g.RemoveVertex("a"); !errors.Is(err, ErrVertexHasEdges) {
59 t.Fail()
60 }
61 })
62 }