| 1 | package build |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strconv" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestAdd(t *testing.T) { |
| 10 | res := Empty(0).Add(AllEdges()) |
| 11 | exp := "0 []" |
| 12 | if mess, diff := diff(res.String(), exp); diff { |
| 13 | t.Errorf("Add %s", mess) |
| 14 | } |
| 15 | Consistent("Add", t, res) |
| 16 | |
| 17 | res = Grid(2, 2).Add(EdgeSet{ |
| 18 | Cost: Cost(4), |
| 19 | }) |
| 20 | exp = "4 [{0 1} {0 2} {0 3}:4 {1 2}:4 {1 3} {2 3}]" |
| 21 | if mess, diff := diff(res.String(), exp); diff { |
| 22 | t.Errorf("Add %s", mess) |
| 23 | } |
| 24 | Consistent("Add", t, res) |
| 25 | |
| 26 | for m := 0; m < 5; m++ { |
| 27 | for n := 0; n < 5; n++ { |
| 28 | res = Kn(m).Add(AllEdges()) |
| 29 | mess := fmt.Sprintf("Kn(%d).Add(AllEdges())", m) |
| 30 | Consistent(mess, t, res) |
| 31 | |
| 32 | res = Kn(m).Add(NoEdges()) |
| 33 | mess = fmt.Sprintf("Kn(%d).Add(NoEdges())", m) |
| 34 | Consistent(mess, t, res) |
| 35 | |
| 36 | res = Grid(m, n).Add(EdgeSet{ |
| 37 | From: Vertex(m), |
| 38 | To: Vertex(n), |
| 39 | Cost: Cost(3), |
| 40 | }) |
| 41 | mess = fmt.Sprintf("Grid(%d,%d).Add(Edge(%d, %d, 3))", m, n, m, n) |
| 42 | Consistent(mess, t, res) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestDelete(t *testing.T) { |
| 48 | res := Kn(4).Delete(EdgeSet{From: Vertex(1)}) |
| 49 | exp := "4 [{0 2} {0 3} {2 3}]" |
| 50 | if mess, diff := diff(res.String(), exp); diff { |
| 51 | t.Errorf("Delete %s", mess) |
| 52 | } |
| 53 | Consistent("Delete1", t, res) |
| 54 | |
| 55 | res = Kn(0).Delete(EdgeSet{From: Vertex(1)}) |
| 56 | exp = "0 []" |
| 57 | if mess, diff := diff(res.String(), exp); diff { |
| 58 | t.Errorf("Delete %s", mess) |
| 59 | } |
| 60 | Consistent("Delete2", t, res) |
| 61 | |
| 62 | res = Kn(4).AddCost(8).Delete(EdgeSet{From: Vertex(1)}) |
| 63 | exp = "4 [{0 2}:8 {0 3}:8 {2 3}:8]" |
| 64 | if mess, diff := diff(res.String(), exp); diff { |
| 65 | t.Errorf("Delete %s", mess) |
| 66 | } |
| 67 | Consistent("Delete3", t, res) |
| 68 | |
| 69 | res = Kn(4).Delete(AllEdges()) |
| 70 | exp = "4 []" |
| 71 | if mess, diff := diff(res.String(), exp); diff { |
| 72 | t.Errorf("Delete %s", mess) |
| 73 | } |
| 74 | Consistent("Delete4", t, res) |
| 75 | |
| 76 | res = Grid(2, 2).Delete(Edge(2, 3)) |
| 77 | exp = "4 [{0 1} {0 2} {1 3}]" |
| 78 | if mess, diff := diff(res.String(), exp); diff { |
| 79 | t.Errorf("Delete %s", mess) |
| 80 | } |
| 81 | Consistent("Delete5", t, res) |
| 82 | |
| 83 | for m := 0; m < 5; m++ { |
| 84 | for n := 0; n < 5; n++ { |
| 85 | res = Kn(m).Delete(AllEdges()) |
| 86 | mess := fmt.Sprintf("Kn(%d).Delete(AllEdges())", m) |
| 87 | Consistent(mess, t, res) |
| 88 | |
| 89 | res = Kn(m).Delete(NoEdges()) |
| 90 | mess = fmt.Sprintf("Kn(%d).Delete(NoEdges())", m) |
| 91 | Consistent(mess, t, res) |
| 92 | |
| 93 | res = Grid(m, n).Delete(EdgeSet{ |
| 94 | From: Vertex(m), |
| 95 | To: Vertex(n), |
| 96 | Cost: Cost(3), |
| 97 | }) |
| 98 | mess = fmt.Sprintf("Grid(%d,%d).Delete(Edge(%d, %d, 3))", m, n, m, n) |
| 99 | Consistent(mess, t, res) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestNewEdges(t *testing.T) { |
| 105 | res := newEdges(0, AllEdges()) |
| 106 | exp := "0 []" |
| 107 | if mess, diff := diff(res.String(), exp); diff { |
| 108 | t.Errorf("newEdges %s", mess) |
| 109 | } |
| 110 | Consistent("newEdges1", t, res) |
| 111 | |
| 112 | res = newEdges(3, Edge(1, 2)) |
| 113 | exp = "3 [{1 2}]" |
| 114 | if mess, diff := diff(res.String(), exp); diff { |
| 115 | t.Errorf("newEdges %s", mess) |
| 116 | } |
| 117 | Consistent("newEdges2", t, res) |
| 118 | |
| 119 | res = newEdges(3, Edge(1, 1)) |
| 120 | exp = "3 []" |
| 121 | if mess, diff := diff(res.String(), exp); diff { |
| 122 | t.Errorf("newEdges %s", mess) |
| 123 | } |
| 124 | Consistent("newEdges3", t, res) |
| 125 | |
| 126 | res = newEdges(4, EdgeSet{ |
| 127 | From: Vertex(1), |
| 128 | To: Vertex(2), |
| 129 | Cost: Cost(3), |
| 130 | }) |
| 131 | exp = "4 [{1 2}:3]" |
| 132 | if mess, diff := diff(res.String(), exp); diff { |
| 133 | t.Errorf("newEdges %s", mess) |
| 134 | } |
| 135 | Consistent("newEdges4", t, res) |
| 136 | |
| 137 | res = newEdges(3, EdgeSet{}) |
| 138 | exp = "3 [{0 1} {0 2} {1 2}]" |
| 139 | if mess, diff := diff(res.String(), exp); diff { |
| 140 | t.Errorf("newEdges %s", mess) |
| 141 | } |
| 142 | Consistent("newEdges5", t, res) |
| 143 | |
| 144 | res = newEdges(4, EdgeSet{ |
| 145 | From: Range(0, 3), |
| 146 | To: Range(1, 4), |
| 147 | Keep: func(v, w int) bool { return v <= w }, |
| 148 | }) |
| 149 | exp = "4 [(0 1) (0 2) (0 3) (1 2) (1 3) (2 3)]" |
| 150 | if mess, diff := diff(res.String(), exp); diff { |
| 151 | t.Errorf("newEdges %s", mess) |
| 152 | } |
| 153 | Consistent("newEdges6", t, res) |
| 154 | |
| 155 | for m := 0; m < 8; m++ { |
| 156 | for n := 0; n < 8; n++ { |
| 157 | res = newEdges(m+n, EdgeSet{ |
| 158 | From: Range(m-n, m+n-3), |
| 159 | To: Range(n-m+4, m+n+2), |
| 160 | Keep: func(v, w int) bool { return true }, |
| 161 | Cost: func(v, w int) int64 { return int64(10*v + w) }, |
| 162 | }) |
| 163 | mess := "newEdges(" + strconv.Itoa(m) + "," + strconv.Itoa(n) + ")" |
| 164 | Consistent(mess, t, res) |
| 165 | } |
| 166 | } |
| 167 | } |