| 1 | package build |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestConnect(t *testing.T) { |
| 9 | cost := func(v, w int) int64 { return int64(10*v + w) } |
| 10 | res := Grid(1, 2).AddCostFunc(cost).Connect(1, Grid(1, 2).AddCostFunc(cost)) |
| 11 | exp := "3 [(0 1):1 (1 0):10 (1 2):1 (2 1):10]" |
| 12 | if mess, diff := diff(res.String(), exp); diff { |
| 13 | t.Errorf("Connect %s", mess) |
| 14 | } |
| 15 | Consistent("Connect1", t, res) |
| 16 | |
| 17 | res = Grid(1, 2).AddCostFunc(cost).Connect(0, Grid(1, 2).AddCostFunc(cost)) |
| 18 | exp = "3 [(0 1):1 (0 2):1 (1 0):10 (2 0):10]" |
| 19 | if mess, diff := diff(res.String(), exp); diff { |
| 20 | t.Errorf("Connect %s", mess) |
| 21 | } |
| 22 | Consistent("Connect2", t, res) |
| 23 | |
| 24 | res = Kn(1).Connect(0, Grid(1, 2).AddCostFunc(cost)) |
| 25 | exp = "2 [(0 1):1 (1 0):10]" |
| 26 | if mess, diff := diff(res.String(), exp); diff { |
| 27 | t.Errorf("Connect %s", mess) |
| 28 | } |
| 29 | Consistent("Connect3", t, res) |
| 30 | |
| 31 | res = Grid(1, 2).AddCostFunc(cost).Connect(1, Kn(1)) |
| 32 | exp = "2 [(0 1):1 (1 0):10]" |
| 33 | if mess, diff := diff(res.String(), exp); diff { |
| 34 | t.Errorf("Connect %s", mess) |
| 35 | } |
| 36 | Consistent("Connect4", t, res) |
| 37 | |
| 38 | res = Grid(1, 2).AddCostFunc(cost).Connect(0, Kn(1)) |
| 39 | exp = "2 [(0 1):1 (1 0):10]" |
| 40 | if mess, diff := diff(res.String(), exp); diff { |
| 41 | t.Errorf("Connect %s", mess) |
| 42 | } |
| 43 | Consistent("Connect5", t, res) |
| 44 | |
| 45 | res = Kn(0).Connect(0, Kn(1)) |
| 46 | if res != nil { |
| 47 | t.Errorf("Connect should produce nil connecting a null graph.") |
| 48 | } |
| 49 | Consistent("Connect6", t, res) |
| 50 | |
| 51 | res = Kn(1).Connect(0, Kn(0)) |
| 52 | if res != nil { |
| 53 | t.Errorf("Connect should produce nil connecting a null graph.") |
| 54 | } |
| 55 | Consistent("Connect7", t, res) |
| 56 | |
| 57 | res = Cycle(3).Connect(0, Cycle(3)).Connect(0, Cycle(3)) |
| 58 | exp = "7 [{0 1} {0 2} {0 3} {0 4} {0 5} {0 6} {1 2} {3 4} {5 6}]" |
| 59 | if mess, diff := diff(res.String(), exp); diff { |
| 60 | t.Errorf("Connect %s", mess) |
| 61 | } |
| 62 | Consistent("Connect8", t, res) |
| 63 | |
| 64 | for m := 0; m < 5; m++ { |
| 65 | for n := 0; n < 5; n++ { |
| 66 | res = Kn(m).Connect(m-1, Kn(n)) |
| 67 | mess := fmt.Sprintf("Kn(%d).Connect(Kn(%d))", m, n) |
| 68 | Consistent(mess, t, res) |
| 69 | |
| 70 | res = Grid(m, n).AddCostFunc(cost).Connect(m-1, Kn(n)) |
| 71 | mess = fmt.Sprintf("Grid(%d,%d).Connect(Kn(%d))", m, n, n) |
| 72 | Consistent(mess, t, res) |
| 73 | res = Kn(n).Connect(n-1, Grid(m, n).AddCostFunc(cost)) |
| 74 | mess = fmt.Sprintf("Kn(%d).Connect(Grid(%d,%d))", n, m, n) |
| 75 | Consistent(mess, t, res) |
| 76 | |
| 77 | km := Kn(m).Keep(func(v, w int) bool { |
| 78 | return v != m-1 && w != m-1 |
| 79 | }).AddCostFunc(cost) |
| 80 | res = km.Connect(0, Kn(n).AddCostFunc(cost)) |
| 81 | Consistent(mess, t, res) |
| 82 | } |
| 83 | } |
| 84 | } |