| 1 |
package graph |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func diff(res, exp interface{}) (message string, diff bool) { |
| 10 |
if !reflect.DeepEqual(res, exp) { |
| 11 |
message = fmt.Sprintf("%v; want %v", res, exp) |
| 12 |
diff = true |
| 13 |
} |
| 14 |
return |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
func Consistent(mess string, t *testing.T, g Iterator) { |
| 19 |
if g == nil { |
| 20 |
return |
| 21 |
} |
| 22 |
gm, mutable := g.(*Mutable) |
| 23 |
gi, immutable := g.(*Immutable) |
| 24 |
n := g.Order() |
| 25 |
|
| 26 |
for v := 0; v < n; v++ { |
| 27 |
deg := 0 |
| 28 |
visited := make([]bool, n) |
| 29 |
g.Visit(v, func(w int, c int64) (skip bool) { |
| 30 |
visited[w] = true |
| 31 |
deg++ |
| 32 |
if mutable && !gm.Edge(v, w) || immutable && !gi.Edge(v, w) { |
| 33 |
t.Errorf("%s: visit(%d): (%d,%d); edge(%d,%d): false\n", mess, v, w, c, v, w) |
| 34 |
} |
| 35 |
if mutable && gm.Cost(v, w) != c { |
| 36 |
t.Errorf("%s: visit(%d): (%d,%d); cost(%d,%d): %v\n", mess, v, w, c, v, w, gm.Cost(v, w)) |
| 37 |
} |
| 38 |
return |
| 39 |
}) |
| 40 |
CheckAbort(mess, t, g, v, deg) |
| 41 |
switch { |
| 42 |
case mutable: |
| 43 |
if deg != gm.Degree(v) { |
| 44 |
t.Errorf("%s: visit(%d) found %v neighbors; degree(%d): %v\n", mess, v, deg, v, gm.Degree(v)) |
| 45 |
} |
| 46 |
case immutable: |
| 47 |
if deg != gi.Degree(v) { |
| 48 |
t.Errorf("%s: visit(%d) found %v neighbors; degree(%d): %v\n", mess, v, deg, v, gi.Degree(v)) |
| 49 |
} |
| 50 |
} |
| 51 |
for w, ok := range visited { |
| 52 |
if ok { |
| 53 |
continue |
| 54 |
} |
| 55 |
if mutable && gm.Edge(v, w) || immutable && gi.Edge(v, w) { |
| 56 |
t.Errorf("%s: visit(%d,%d): --; edge(%d,%d): true\n", mess, v, w, v, w) |
| 57 |
} |
| 58 |
} |
| 59 |
if immutable { |
| 60 |
CheckStart(mess, t, gi, v, visited) |
| 61 |
} |
| 62 |
} |
| 63 |
return |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func CheckStart(mess string, t *testing.T, g *Immutable, v int, visited []bool) { |
| 68 |
n := g.Order() |
| 69 |
check := make([]bool, len(visited)) |
| 70 |
for a := 0; a <= n+1; a++ { |
| 71 |
prev := -1 |
| 72 |
for v := a; v < n; v++ { |
| 73 |
check[v] = visited[v] |
| 74 |
} |
| 75 |
g.VisitFrom(v, a, func(w int, _ int64) (skip bool) { |
| 76 |
if w < 0 { |
| 77 |
t.Errorf("%s: visit(%d, %d): returned negative w=%d", mess, v, a, w) |
| 78 |
} |
| 79 |
if w >= n { |
| 80 |
t.Errorf("%s: visit(%d, %d): returned w=%d bigger than n=%d", mess, v, a, w, n) |
| 81 |
} |
| 82 |
if w < a { |
| 83 |
t.Errorf("%s: visit(%d, %d): returned small w=%d", mess, v, a, w) |
| 84 |
} else if 0 <= w && w < n && !check[w] { |
| 85 |
t.Errorf("%s, visit(%d, %d) didn't return %d when a=0.\n", mess, v, a, w) |
| 86 |
} |
| 87 |
if prev > w { |
| 88 |
t.Errorf("%s, visit(%d, %d) returned %d before %d.\n", mess, v, a, prev, w) |
| 89 |
} |
| 90 |
check[w] = false |
| 91 |
prev = w |
| 92 |
return |
| 93 |
}) |
| 94 |
for w := a; w < n; w++ { |
| 95 |
if check[w] { |
| 96 |
t.Errorf("%s, visit(%d, %d) did return %d when a=0.\n", mess, v, a, w) |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
func CheckAbort(mess string, t *testing.T, g Iterator, v int, deg int) { |
| 104 |
for n := 1; n <= deg; n++ { |
| 105 |
count := 0 |
| 106 |
aborted := g.Visit(v, func(_ int, _ int64) bool { |
| 107 |
count++ |
| 108 |
return count == n |
| 109 |
}) |
| 110 |
if count != n { |
| 111 |
t.Errorf("%s: visit(%d) didnt't abort after %d iteration(s).\n", mess, v, n) |
| 112 |
} |
| 113 |
if !aborted { |
| 114 |
t.Errorf("%s: visit(%d) didnt't return true after %d iteration(s).\n", mess, v, n) |
| 115 |
} |
| 116 |
aborted = g.Visit(v, func(_ int, _ int64) bool { |
| 117 |
return false |
| 118 |
}) |
| 119 |
if aborted { |
| 120 |
t.Errorf("%s: visit(%d) returned true after completed iteration.\n", mess, v) |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
type Multi struct{} |
| 127 |
|
| 128 |
func (m Multi) Order() int { |
| 129 |
return 3 |
| 130 |
} |
| 131 |
|
| 132 |
func (m Multi) Visit(v int, action func(w int, c int64) (skip bool)) (aborted bool) { |
| 133 |
if v == 1 { |
| 134 |
for i := 0; i < 4; i++ { |
| 135 |
if action(0, 5) { |
| 136 |
return true |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
if v == 0 { |
| 141 |
for i := 0; i < 3; i++ { |
| 142 |
if action(0, 0) { |
| 143 |
return true |
| 144 |
} |
| 145 |
} |
| 146 |
for i := 0; i < 2; i++ { |
| 147 |
if action(0, 5) { |
| 148 |
return true |
| 149 |
} |
| 150 |
} |
| 151 |
for i := 0; i < 5; i++ { |
| 152 |
if action(1, 5) { |
| 153 |
return true |
| 154 |
} |
| 155 |
} |
| 156 |
for i := 0; i < 1; i++ { |
| 157 |
if action(1, 7) { |
| 158 |
return true |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
return |
| 163 |
} |
| 164 |
|
| 165 |
func TestString(t *testing.T) { |
| 166 |
res := String(Multi{}) |
| 167 |
exp := "3 [3×(0 0) 2×(0 0):5 4×{0 1}:5 (0 1):5 (0 1):7]" |
| 168 |
if mess, diff := diff(res, exp); diff { |
| 169 |
t.Errorf("String: %s", mess) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func TestCheck(t *testing.T) { |
| 174 |
g := New(0) |
| 175 |
res := Check(g) |
| 176 |
exp := Stats{0, 0, 0, 0, 0} |
| 177 |
if mess, diff := diff(res, exp); diff { |
| 178 |
t.Errorf("Check: %s", mess) |
| 179 |
} |
| 180 |
res = Check(Sort(g)) |
| 181 |
if mess, diff := diff(res, exp); diff { |
| 182 |
t.Errorf("Check immutable: %s", mess) |
| 183 |
} |
| 184 |
|
| 185 |
g = New(1) |
| 186 |
res = Check(g) |
| 187 |
exp = Stats{0, 0, 0, 0, 1} |
| 188 |
if mess, diff := diff(res, exp); diff { |
| 189 |
t.Errorf("Check: %s", mess) |
| 190 |
} |
| 191 |
res = Check(Sort(g)) |
| 192 |
if mess, diff := diff(res, exp); diff { |
| 193 |
t.Errorf("Check immutable: %s", mess) |
| 194 |
} |
| 195 |
|
| 196 |
g = New(1) |
| 197 |
g.AddCost(0, 0, 1) |
| 198 |
res = Check(g) |
| 199 |
exp = Stats{1, 0, 1, 1, 0} |
| 200 |
if mess, diff := diff(res, exp); diff { |
| 201 |
t.Errorf("Check %s", mess) |
| 202 |
} |
| 203 |
res = Check(Sort(g)) |
| 204 |
if mess, diff := diff(res, exp); diff { |
| 205 |
t.Errorf("Check immutable: %s", mess) |
| 206 |
} |
| 207 |
|
| 208 |
g = New(3) |
| 209 |
g.AddCost(0, 1, 1) |
| 210 |
res = Check(g) |
| 211 |
exp = Stats{1, 0, 1, 0, 2} |
| 212 |
if mess, diff := diff(res, exp); diff { |
| 213 |
t.Errorf("Check: %s", mess) |
| 214 |
} |
| 215 |
res = Check(Sort(g)) |
| 216 |
if mess, diff := diff(res, exp); diff { |
| 217 |
t.Errorf("Check immutable: %s", mess) |
| 218 |
} |
| 219 |
|
| 220 |
g.AddCost(1, 0, 2) |
| 221 |
res = Check(g) |
| 222 |
exp = Stats{2, 0, 2, 0, 1} |
| 223 |
if mess, diff := diff(res, exp); diff { |
| 224 |
t.Errorf("Check: %s", mess) |
| 225 |
} |
| 226 |
res = Check(Sort(g)) |
| 227 |
if mess, diff := diff(res, exp); diff { |
| 228 |
t.Errorf("Check immutable: %s", mess) |
| 229 |
} |
| 230 |
|
| 231 |
g.AddCost(1, 0, 1) |
| 232 |
res = Check(g) |
| 233 |
exp = Stats{2, 0, 2, 0, 1} |
| 234 |
if mess, diff := diff(res, exp); diff { |
| 235 |
t.Errorf("Check: %s", mess) |
| 236 |
} |
| 237 |
res = Check(Sort(g)) |
| 238 |
if mess, diff := diff(res, exp); diff { |
| 239 |
t.Errorf("Check immutable: %s", mess) |
| 240 |
} |
| 241 |
|
| 242 |
g.Add(2, 2) |
| 243 |
res = Check(g) |
| 244 |
exp = Stats{3, 0, 2, 1, 0} |
| 245 |
if mess, diff := diff(res, exp); diff { |
| 246 |
t.Errorf("Check: %s", mess) |
| 247 |
} |
| 248 |
res = Check(Sort(g)) |
| 249 |
if mess, diff := diff(res, exp); diff { |
| 250 |
t.Errorf("Check immutable: %s", mess) |
| 251 |
} |
| 252 |
|
| 253 |
g.Add(1, 2) |
| 254 |
res = Check(g) |
| 255 |
exp = Stats{4, 0, 2, 1, 0} |
| 256 |
if mess, diff := diff(res, exp); diff { |
| 257 |
t.Errorf("Check: %s", mess) |
| 258 |
} |
| 259 |
res = Check(Sort(g)) |
| 260 |
if mess, diff := diff(res, exp); diff { |
| 261 |
t.Errorf("Check immutable: %s", mess) |
| 262 |
} |
| 263 |
|
| 264 |
g.AddBoth(0, 1) |
| 265 |
res = Check(g) |
| 266 |
exp = Stats{4, 0, 0, 1, 0} |
| 267 |
if mess, diff := diff(res, exp); diff { |
| 268 |
t.Errorf("Check: %s", mess) |
| 269 |
} |
| 270 |
res = Check(Sort(g)) |
| 271 |
if mess, diff := diff(res, exp); diff { |
| 272 |
t.Errorf("Check immutable: %s", mess) |
| 273 |
} |
| 274 |
|
| 275 |
res = Check(Multi{}) |
| 276 |
exp = Stats{3, 12, 12, 5, 1} |
| 277 |
if mess, diff := diff(res, exp); diff { |
| 278 |
t.Errorf("Check: %s", mess) |
| 279 |
} |
| 280 |
res = Check(Sort(Multi{})) |
| 281 |
if mess, diff := diff(res, exp); diff { |
| 282 |
t.Errorf("Check immutable: %s", mess) |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
func TestEqual(t *testing.T) { |
| 287 |
g := New(0) |
| 288 |
if mess, diff := diff(Equal(g, g), true); diff { |
| 289 |
t.Errorf("Equal: %s", mess) |
| 290 |
} |
| 291 |
|
| 292 |
h := New(1) |
| 293 |
if mess, diff := diff(Equal(h, h), true); diff { |
| 294 |
t.Errorf("Equal: %s", mess) |
| 295 |
} |
| 296 |
if mess, diff := diff(Equal(g, h), false); diff { |
| 297 |
t.Errorf("Equal: %s", mess) |
| 298 |
} |
| 299 |
if mess, diff := diff(Equal(h, g), false); diff { |
| 300 |
t.Errorf("Equal: %s", mess) |
| 301 |
} |
| 302 |
|
| 303 |
g = New(1) |
| 304 |
if mess, diff := diff(Equal(g, h), true); diff { |
| 305 |
t.Errorf("Equal: %s", mess) |
| 306 |
} |
| 307 |
if mess, diff := diff(Equal(h, g), true); diff { |
| 308 |
t.Errorf("Equal: %s", mess) |
| 309 |
} |
| 310 |
|
| 311 |
g.Add(0, 0) |
| 312 |
if mess, diff := diff(Equal(g, h), false); diff { |
| 313 |
t.Errorf("Equal: %s", mess) |
| 314 |
} |
| 315 |
if mess, diff := diff(Equal(h, g), false); diff { |
| 316 |
t.Errorf("Equal: %s", mess) |
| 317 |
} |
| 318 |
|
| 319 |
h.Add(0, 0) |
| 320 |
if mess, diff := diff(Equal(g, h), true); diff { |
| 321 |
t.Errorf("Equal: %s", mess) |
| 322 |
} |
| 323 |
if mess, diff := diff(Equal(h, g), true); diff { |
| 324 |
t.Errorf("Equal: %s", mess) |
| 325 |
} |
| 326 |
|
| 327 |
g.AddCost(0, 0, 1) |
| 328 |
if mess, diff := diff(Equal(g, h), false); diff { |
| 329 |
t.Errorf("Equal: %s", mess) |
| 330 |
} |
| 331 |
if mess, diff := diff(Equal(h, g), false); diff { |
| 332 |
t.Errorf("Equal: %s", mess) |
| 333 |
} |
| 334 |
|
| 335 |
g = New(3) |
| 336 |
h = New(3) |
| 337 |
g.AddBoth(0, 1) |
| 338 |
g.AddCost(0, 1, 1) |
| 339 |
g.AddBoth(1, 2) |
| 340 |
g.Add(0, 2) |
| 341 |
h.AddBoth(0, 1) |
| 342 |
h.AddCost(0, 1, 1) |
| 343 |
h.AddBoth(1, 2) |
| 344 |
h.Add(0, 2) |
| 345 |
if mess, diff := diff(Equal(g, h), true); diff { |
| 346 |
t.Errorf("Equal: %s", mess) |
| 347 |
} |
| 348 |
if mess, diff := diff(Equal(h, g), true); diff { |
| 349 |
t.Errorf("Equal: %s", mess) |
| 350 |
} |
| 351 |
|
| 352 |
g.Add(0, 0) |
| 353 |
if mess, diff := diff(Equal(g, h), false); diff { |
| 354 |
t.Errorf("Equal: %s", mess) |
| 355 |
} |
| 356 |
if mess, diff := diff(Equal(h, g), false); diff { |
| 357 |
t.Errorf("Equal: %s", mess) |
| 358 |
} |
| 359 |
|
| 360 |
g.Delete(0, 0) |
| 361 |
g.Delete(0, 2) |
| 362 |
if mess, diff := diff(Equal(g, h), false); diff { |
| 363 |
t.Errorf("Equal: %s", mess) |
| 364 |
} |
| 365 |
if mess, diff := diff(Equal(h, g), false); diff { |
| 366 |
t.Errorf("Equal: %s", mess) |
| 367 |
} |
| 368 |
} |