| 1 |
d. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package interval |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"math/rand" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
"unsafe" |
| 14 |
|
| 15 |
"gopkg.in/check.v1" |
| 16 |
|
| 17 |
"github.com/biogo/store/llrb" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
func (t *IntTree) isBST() bool { |
| 24 |
if t == nil { |
| 25 |
return true |
| 26 |
} |
| 27 |
return t.Root.isBST(t.Min(), t.Max()) |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
func (n *IntNode) isBST(min, max IntInterface) bool { |
| 33 |
if n == nil { |
| 34 |
return true |
| 35 |
} |
| 36 |
if n.Elem.Range().Start < min.Range().Start || n.Elem.Range().Start > max.Range().Start { |
| 37 |
return false |
| 38 |
} |
| 39 |
return n.Left.isBST(min, n.Elem) || n.Right.isBST(n.Elem, max) |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func (t *IntTree) is23_234() bool { |
| 44 |
if t == nil { |
| 45 |
return true |
| 46 |
} |
| 47 |
return t.Root.is23_234() |
| 48 |
} |
| 49 |
func (n *IntNode) is23_234() bool { |
| 50 |
if n == nil { |
| 51 |
return true |
| 52 |
} |
| 53 |
if Mode == BU23 { |
| 54 |
|
| 55 |
|
| 56 |
if (n.Left != nil) && (n.Right != nil) { |
| 57 |
if n.Left.color() == llrb.Red && n.Right.color() == llrb.Red { |
| 58 |
return false |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if n.Right.color() == llrb.Red { |
| 63 |
return false |
| 64 |
} |
| 65 |
} else if Mode == TD234 { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
if n.Right.color() == llrb.Red && n.Left.color() == llrb.Black { |
| 70 |
return false |
| 71 |
} |
| 72 |
} else { |
| 73 |
panic("cannot reach") |
| 74 |
} |
| 75 |
if n.color() == llrb.Red && n.Left.color() == llrb.Red { |
| 76 |
return false |
| 77 |
} |
| 78 |
return n.Left.is23_234() && n.Right.is23_234() |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
func (t *IntTree) isBalanced() bool { |
| 83 |
if t == nil { |
| 84 |
return true |
| 85 |
} |
| 86 |
var black int |
| 87 |
for x := t.Root; x != nil; x = x.Left { |
| 88 |
if x.color() == llrb.Black { |
| 89 |
black++ |
| 90 |
} |
| 91 |
} |
| 92 |
return t.Root.isBalanced(black) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
func (n *IntNode) isBalanced(black int) bool { |
| 98 |
if n == nil && black == 0 { |
| 99 |
return true |
| 100 |
} else if n == nil && black != 0 { |
| 101 |
return false |
| 102 |
} |
| 103 |
if n.color() == llrb.Black { |
| 104 |
black-- |
| 105 |
} |
| 106 |
return n.Left.isBalanced(black) && n.Right.isBalanced(black) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
func (t *IntTree) isRanged() bool { |
| 111 |
if t == nil { |
| 112 |
return true |
| 113 |
} |
| 114 |
return t.Root.isRanged() |
| 115 |
} |
| 116 |
func (n *IntNode) isRanged() bool { |
| 117 |
if n == nil { |
| 118 |
return true |
| 119 |
} |
| 120 |
e, r := n.Elem, n.Range |
| 121 |
m := n.bounding(e.Range()) |
| 122 |
return m.Start == r.Start && m.End == r.End && |
| 123 |
n.Left.isRanged() && |
| 124 |
n.Right.isRanged() |
| 125 |
} |
| 126 |
func (n *IntNode) bounding(m IntRange) IntRange { |
| 127 |
m.Start = intMin(n.Elem.Range().Start, m.Start) |
| 128 |
m.End = intMax(n.Elem.Range().End, m.End) |
| 129 |
if n.Left != nil { |
| 130 |
m = n.Left.bounding(m) |
| 131 |
} |
| 132 |
if n.Right != nil { |
| 133 |
m = n.Right.bounding(m) |
| 134 |
} |
| 135 |
return m |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
type intOverlap struct { |
| 141 |
start, end int |
| 142 |
id uintptr |
| 143 |
} |
| 144 |
|
| 145 |
func (o *intOverlap) Overlap(r IntRange) bool { |
| 146 |
return o.end > r.Start && o.start < r.End |
| 147 |
} |
| 148 |
func (o *intOverlap) ID() uintptr { return o.id } |
| 149 |
func (o *intOverlap) Range() IntRange { return IntRange{o.start, o.end} } |
| 150 |
func (o *intOverlap) String() string { return fmt.Sprintf("[%d,%d)", o.start, o.end) } |
| 151 |
|
| 152 |
|
| 153 |
func (n *IntNode) describeTree(char, color bool) string { |
| 154 |
s := []rune(nil) |
| 155 |
|
| 156 |
var follow func(*IntNode) |
| 157 |
follow = func(n *IntNode) { |
| 158 |
children := n.Left != nil || n.Right != nil |
| 159 |
if children { |
| 160 |
s = append(s, '(') |
| 161 |
} |
| 162 |
if n.Left != nil { |
| 163 |
follow(n.Left) |
| 164 |
} |
| 165 |
if children { |
| 166 |
s = append(s, ',') |
| 167 |
} |
| 168 |
if n.Right != nil { |
| 169 |
follow(n.Right) |
| 170 |
} |
| 171 |
if children { |
| 172 |
s = append(s, ')') |
| 173 |
} |
| 174 |
if n.Elem != nil { |
| 175 |
s = append(s, []rune(fmt.Sprintf("%d", n.Elem))...) |
| 176 |
if color { |
| 177 |
s = append(s, []rune(fmt.Sprintf(" %v", n.color()))...) |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
if n == nil { |
| 182 |
s = []rune("()") |
| 183 |
} else { |
| 184 |
follow(n) |
| 185 |
} |
| 186 |
s = append(s, ';') |
| 187 |
|
| 188 |
return string(s) |
| 189 |
} |
| 190 |
|
| 191 |
func intMin(a, b int) int { |
| 192 |
if a < b { |
| 193 |
return a |
| 194 |
} |
| 195 |
return b |
| 196 |
} |
| 197 |
|
| 198 |
func intMax(a, b int) int { |
| 199 |
if a > b { |
| 200 |
return a |
| 201 |
} |
| 202 |
return b |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
func (s *S) TestIntNilOperations(c *check.C) { |
| 208 |
t := &IntTree{} |
| 209 |
c.Check(t.Min(), check.Equals, nil) |
| 210 |
c.Check(t.Max(), check.Equals, nil) |
| 211 |
if Mode == TD234 { |
| 212 |
return |
| 213 |
} |
| 214 |
t.DeleteMin(false) |
| 215 |
c.Check(*t, check.Equals, IntTree{}) |
| 216 |
t.DeleteMax(false) |
| 217 |
c.Check(*t, check.Equals, IntTree{}) |
| 218 |
} |
| 219 |
|
| 220 |
func (s *S) TestIntRange(c *check.C) { |
| 221 |
t := &IntTree{} |
| 222 |
for i, iv := range []*intOverlap{ |
| 223 |
{0, 2, 0}, |
| 224 |
{2, 4, 0}, |
| 225 |
{1, 6, 0}, |
| 226 |
{3, 4, 0}, |
| 227 |
{1, 3, 0}, |
| 228 |
{4, 6, 0}, |
| 229 |
{5, 8, 0}, |
| 230 |
{6, 8, 0}, |
| 231 |
{5, 9, 0}, |
| 232 |
} { |
| 233 |
t.Insert(iv, false) |
| 234 |
ok := c.Check(t.isRanged(), check.Equals, true, check.Commentf("insertion %d: %v", i, iv)) |
| 235 |
if !ok && *genDot && t.Len() <= *dotLimit { |
| 236 |
err := t.dotFile(fmt.Sprintf("TestRange_%d", i), "") |
| 237 |
if err != nil { |
| 238 |
c.Errorf("Dot file write failed: %v", err) |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func (s *S) TestIntInsertion(c *check.C) { |
| 245 |
var ( |
| 246 |
min, max = 0, 1000 |
| 247 |
t = &IntTree{} |
| 248 |
length = 100 |
| 249 |
) |
| 250 |
for i := min; i <= max; i++ { |
| 251 |
t.Insert(&intOverlap{start: i, end: i + length}, false) |
| 252 |
c.Check(t.Len(), check.Equals, int(i+1)) |
| 253 |
failed := false |
| 254 |
failed = failed || !c.Check(t.isBST(), check.Equals, true) |
| 255 |
failed = failed || !c.Check(t.is23_234(), check.Equals, true) |
| 256 |
failed = failed || !c.Check(t.isBalanced(), check.Equals, true) |
| 257 |
failed = failed || !c.Check(t.isRanged(), check.Equals, true) |
| 258 |
if failed { |
| 259 |
if *printTree { |
| 260 |
c.Logf("Failing tree: %s\n\n", t.Root.describeTree(false, true)) |
| 261 |
} |
| 262 |
if *genDot && t.Len() <= *dotLimit { |
| 263 |
err := t.dotFile(fmt.Sprintf("TestInsertion_after_ins_%d", i), "") |
| 264 |
if err != nil { |
| 265 |
c.Errorf("Dot file write failed: %v", err) |
| 266 |
} |
| 267 |
} |
| 268 |
c.Fatal("Cannot continue test: invariant contradiction") |
| 269 |
} |
| 270 |
} |
| 271 |
c.Check(t.Min().Range().Start, check.Equals, min) |
| 272 |
c.Check(t.Max().Range().Start, check.Equals, max) |
| 273 |
} |
| 274 |
|
| 275 |
func (s *S) TestIntFastInsertion(c *check.C) { |
| 276 |
var ( |
| 277 |
min, max = 0, 1000 |
| 278 |
t = &IntTree{} |
| 279 |
length = 100 |
| 280 |
) |
| 281 |
for i := min; i <= max; i++ { |
| 282 |
t.Insert(&intOverlap{start: i, end: i + length}, true) |
| 283 |
c.Check(t.Len(), check.Equals, int(i+1)) |
| 284 |
c.Check(t.isBST(), check.Equals, true) |
| 285 |
c.Check(t.is23_234(), check.Equals, true) |
| 286 |
c.Check(t.isBalanced(), check.Equals, true) |
| 287 |
} |
| 288 |
t.AdjustRanges() |
| 289 |
c.Check(t.isRanged(), check.Equals, true) |
| 290 |
c.Check(t.Min().Range().Start, check.Equals, min) |
| 291 |
c.Check(t.Max().Range().Start, check.Equals, max) |
| 292 |
} |
| 293 |
|
| 294 |
func (s *S) TestIntDeletion(c *check.C) { |
| 295 |
var ( |
| 296 |
min, max = 0, 1000 |
| 297 |
e = int(max-min) + 1 |
| 298 |
t = &IntTree{} |
| 299 |
length = 1 |
| 300 |
) |
| 301 |
for i := min; i <= max; i++ { |
| 302 |
t.Insert(&intOverlap{start: i, end: i + length, id: uintptr(i)}, false) |
| 303 |
} |
| 304 |
for i := min; i <= max; i++ { |
| 305 |
var dotString string |
| 306 |
if o := t.Get(&intOverlap{start: i, end: i + length}); o != nil { |
| 307 |
e-- |
| 308 |
} |
| 309 |
if *genDot && t.Len() <= *dotLimit { |
| 310 |
dotString = t.dot(fmt.Sprintf("TestDeletion_before_del_%d", i)) |
| 311 |
} |
| 312 |
t.Delete(&intOverlap{start: i, end: i + length, id: uintptr(i)}, false) |
| 313 |
c.Check(t.Len(), check.Equals, e) |
| 314 |
if i < max { |
| 315 |
failed := false |
| 316 |
failed = failed || !c.Check(t.isBST(), check.Equals, true) |
| 317 |
failed = failed || !c.Check(t.is23_234(), check.Equals, true) |
| 318 |
failed = failed || !c.Check(t.isBalanced(), check.Equals, true) |
| 319 |
failed = failed || !c.Check(t.isRanged(), check.Equals, true) |
| 320 |
if failed { |
| 321 |
if *printTree { |
| 322 |
c.Logf("Failing tree: %s\n\n", t.Root.describeTree(false, true)) |
| 323 |
} |
| 324 |
if *genDot && t.Len() < *dotLimit { |
| 325 |
var err error |
| 326 |
err = (*IntTree)(nil).dotFile(fmt.Sprintf("TestDeletion_before_del_%d", i), dotString) |
| 327 |
if err != nil { |
| 328 |
c.Errorf("Dot file write failed: %v", err) |
| 329 |
} |
| 330 |
err = t.dotFile(fmt.Sprintf("TestDeletion_after_del_%d", i), "") |
| 331 |
if err != nil { |
| 332 |
c.Errorf("Dot file write failed: %v", err) |
| 333 |
} |
| 334 |
} |
| 335 |
c.Fatal("Cannot continue test: invariant contradiction") |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
c.Check(*t, check.Equals, IntTree{}) |
| 340 |
} |
| 341 |
|
| 342 |
func (s *S) TestIntFastDeletion(c *check.C) { |
| 343 |
var ( |
| 344 |
min, max = 0, 1000 |
| 345 |
t = &IntTree{} |
| 346 |
length = 1 |
| 347 |
) |
| 348 |
for i := min; i <= max; i++ { |
| 349 |
t.Insert(&intOverlap{start: i, end: i + length, id: uintptr(i)}, false) |
| 350 |
} |
| 351 |
for i := min; i <= max; i++ { |
| 352 |
t.Delete(&intOverlap{start: i, end: i + length, id: uintptr(i)}, true) |
| 353 |
c.Check(t.isBST(), check.Equals, true) |
| 354 |
c.Check(t.is23_234(), check.Equals, true) |
| 355 |
c.Check(t.isBalanced(), check.Equals, true) |
| 356 |
if i == max/2 { |
| 357 |
t.AdjustRanges() |
| 358 |
c.Check(t.isRanged(), check.Equals, true) |
| 359 |
} |
| 360 |
} |
| 361 |
c.Check(*t, check.Equals, IntTree{}) |
| 362 |
} |
| 363 |
|
| 364 |
func (s *S) TestIntGet(c *check.C) { |
| 365 |
var ( |
| 366 |
min, max = 0, 1000 |
| 367 |
t = &IntTree{} |
| 368 |
) |
| 369 |
for i := min; i <= max; i++ { |
| 370 |
if i&1 == 0 { |
| 371 |
t.Insert(&intOverlap{start: i, end: i + 1}, false) |
| 372 |
} |
| 373 |
} |
| 374 |
for i := min; i <= max; i++ { |
| 375 |
if i&1 == 0 { |
| 376 |
o := t.Get(&intOverlap{start: i, end: i + 1}) |
| 377 |
c.Check(len(o), check.Equals, 1) |
| 378 |
c.Check(o[0], check.DeepEquals, &intOverlap{start: i, end: i + 1}) |
| 379 |
} else { |
| 380 |
o := t.Get(&intOverlap{start: i, end: i + 1}) |
| 381 |
c.Check(o, check.DeepEquals, []IntInterface(nil)) |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
func (s *S) TestIntFloor(c *check.C) { |
| 387 |
min, max := 0, 1000 |
| 388 |
t := &IntTree{} |
| 389 |
for i := min; i <= max; i++ { |
| 390 |
if i&1 == 0 { |
| 391 |
t.Insert(&intOverlap{start: i, end: i + 1}, false) |
| 392 |
} |
| 393 |
} |
| 394 |
for i := min; i <= max; i++ { |
| 395 |
l, _ := t.Floor(&intOverlap{start: i, end: i + 1}) |
| 396 |
if i&1 == 0 { |
| 397 |
c.Check(l, check.DeepEquals, &intOverlap{start: i, end: i + 1}) |
| 398 |
} else { |
| 399 |
c.Check(l, check.DeepEquals, &intOverlap{start: i - 1, end: i}) |
| 400 |
} |
| 401 |
} |
| 402 |
l, _ := t.Floor(&intOverlap{start: min - 1, end: min}) |
| 403 |
c.Check(l, check.DeepEquals, IntInterface(nil)) |
| 404 |
} |
| 405 |
|
| 406 |
func (s *S) TestIntCeil(c *check.C) { |
| 407 |
min, max := 0, 1000 |
| 408 |
t := &IntTree{} |
| 409 |
for i := min; i <= max; i++ { |
| 410 |
if i&1 == 1 { |
| 411 |
t.Insert(&intOverlap{start: i, end: i + 1}, false) |
| 412 |
} |
| 413 |
} |
| 414 |
for i := min; i < max; i++ { |
| 415 |
u, _ := t.Ceil(&intOverlap{start: i, end: i + 1}) |
| 416 |
if i&1 == 1 { |
| 417 |
c.Check(u, check.DeepEquals, &intOverlap{start: i, end: i + 1}) |
| 418 |
} else { |
| 419 |
c.Check(u, check.DeepEquals, &intOverlap{start: i + 1, end: i + 2}) |
| 420 |
} |
| 421 |
} |
| 422 |
u, _ := t.Ceil(&intOverlap{start: max, end: max + 2}) |
| 423 |
c.Check(u, check.DeepEquals, IntInterface(nil)) |
| 424 |
} |
| 425 |
|
| 426 |
func (s *S) TestIntRandomlyInsertedGet(c *check.C) { |
| 427 |
var ( |
| 428 |
count, max = 1000, 1000 |
| 429 |
t = &IntTree{} |
| 430 |
length = 100 |
| 431 |
verify = map[intOverlap]struct{}{} |
| 432 |
verified = map[intOverlap]struct{}{} |
| 433 |
) |
| 434 |
for i := 0; i < count; i++ { |
| 435 |
s := (rand.Intn(max)) |
| 436 |
v := intOverlap{start: s, end: s + length} |
| 437 |
t.Insert(&v, false) |
| 438 |
verify[v] = struct{}{} |
| 439 |
} |
| 440 |
|
| 441 |
for v := range verify { |
| 442 |
o := t.Get(&v) |
| 443 |
c.Check(len(o), check.Not(check.Equals), 0) |
| 444 |
for _, iv := range o { |
| 445 |
vr := *iv.(*intOverlap) |
| 446 |
_, ok := verify[vr] |
| 447 |
c.Check(ok, check.Equals, true, check.Commentf("%v should exist", vr)) |
| 448 |
if ok { |
| 449 |
verified[vr] = struct{}{} |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
c.Check(len(verify), check.Equals, len(verified)) |
| 454 |
for v := range verify { |
| 455 |
_, ok := verified[v] |
| 456 |
c.Check(ok, check.Equals, true, check.Commentf("%v should exist", v)) |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
for s := 0; s <= max; s++ { |
| 461 |
v := intOverlap{start: s, end: s + length} |
| 462 |
o := t.Get(&v) |
| 463 |
if _, ok := verify[v]; ok { |
| 464 |
c.Check(len(o), check.Not(check.Equals), 0) |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
func (s *S) TestIntRandomInsertion(c *check.C) { |
| 470 |
var ( |
| 471 |
count, max = 1000, 1000 |
| 472 |
t = &IntTree{} |
| 473 |
length = 100 |
| 474 |
) |
| 475 |
for i := 0; i < count; i++ { |
| 476 |
s := (rand.Intn(max)) |
| 477 |
v := intOverlap{start: s, end: s + length} |
| 478 |
t.Insert(&v, false) |
| 479 |
failed := false |
| 480 |
failed = failed || !c.Check(t.isBST(), check.Equals, true) |
| 481 |
failed = failed || !c.Check(t.is23_234(), check.Equals, true) |
| 482 |
failed = failed || !c.Check(t.isBalanced(), check.Equals, true) |
| 483 |
failed = failed || !c.Check(t.isRanged(), check.Equals, true) |
| 484 |
if failed { |
| 485 |
if *printTree { |
| 486 |
c.Logf("Failing tree: %s\n\n", t.Root.describeTree(false, true)) |
| 487 |
} |
| 488 |
if *genDot && t.Len() <= *dotLimit { |
| 489 |
err := t.dotFile(fmt.Sprintf("TestRandomInsertion_after_ins_%d_%d", v.start, v.end), "") |
| 490 |
if err != nil { |
| 491 |
c.Errorf("Dot file write failed: %v", err) |
| 492 |
} |
| 493 |
} |
| 494 |
c.Fatal("Cannot continue test: invariant contradiction") |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
func (s *S) TestIntRandomDeletion(c *check.C) { |
| 500 |
var ( |
| 501 |
count, max = 14, 3 |
| 502 |
r = make([]intOverlap, count) |
| 503 |
t = &IntTree{} |
| 504 |
length = 1 |
| 505 |
) |
| 506 |
for i := range r { |
| 507 |
s := (rand.Intn(max)) |
| 508 |
r[i] = intOverlap{start: s, end: s + length, id: uintptr(i)} |
| 509 |
t.Insert(&r[i], false) |
| 510 |
} |
| 511 |
for i, v := range r { |
| 512 |
var dotString string |
| 513 |
if *genDot && t.Len() <= *dotLimit { |
| 514 |
dotString = t.dot(fmt.Sprintf("TestRandomDeletion_before_del_%d_%d_%d", i, v.start, v.end)) |
| 515 |
} |
| 516 |
t.Delete(&v, false) |
| 517 |
if t != nil { |
| 518 |
failed := false |
| 519 |
failed = failed || !c.Check(t.isBST(), check.Equals, true) |
| 520 |
failed = failed || !c.Check(t.is23_234(), check.Equals, true) |
| 521 |
failed = failed || !c.Check(t.isBalanced(), check.Equals, true) |
| 522 |
failed = failed || !c.Check(t.isRanged(), check.Equals, true) |
| 523 |
if failed { |
| 524 |
if *printTree { |
| 525 |
c.Logf("Failing tree: %s\n\n", t.Root.describeTree(false, true)) |
| 526 |
} |
| 527 |
if *genDot && t.Len() <= *dotLimit { |
| 528 |
var err error |
| 529 |
err = (*IntTree)(nil).dotFile(fmt.Sprintf("TestRandomDeletion_before_del_%d_%d_%d", i, v.start, v.end), dotString) |
| 530 |
if err != nil { |
| 531 |
c.Errorf("Dot file write failed: %v", err) |
| 532 |
} |
| 533 |
err = t.dotFile(fmt.Sprintf("TestRandomDeletion_after_del_%d_%d_%d", i, v.start, v.end), "") |
| 534 |
if err != nil { |
| 535 |
c.Errorf("Dot file write failed: %v", err) |
| 536 |
} |
| 537 |
} |
| 538 |
c.Fatal("Cannot continue test: invariant contradiction") |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
c.Check(*t, check.DeepEquals, IntTree{}) |
| 543 |
} |
| 544 |
|
| 545 |
func (s *S) TestIntDeleteMinMax(c *check.C) { |
| 546 |
var ( |
| 547 |
min, max = 0, 10 |
| 548 |
t = &IntTree{} |
| 549 |
length = 1 |
| 550 |
dI int |
| 551 |
) |
| 552 |
for i := min; i <= max; i++ { |
| 553 |
v := intOverlap{start: i, end: i + length} |
| 554 |
t.Insert(&v, false) |
| 555 |
dI = t.Len() |
| 556 |
} |
| 557 |
c.Check(dI, check.Equals, int(max-min+1)) |
| 558 |
for i, m := 0, int(max); i < m/2; i++ { |
| 559 |
var failed bool |
| 560 |
t.DeleteMin(false) |
| 561 |
dI-- |
| 562 |
c.Check(t.Len(), check.Equals, dI) |
| 563 |
min++ |
| 564 |
failed = !c.Check(t.Min(), check.DeepEquals, &intOverlap{start: min, end: min + length}) |
| 565 |
failed = failed || !c.Check(t.isBST(), check.Equals, true) |
| 566 |
failed = failed || !c.Check(t.is23_234(), check.Equals, true) |
| 567 |
failed = failed || !c.Check(t.isBalanced(), check.Equals, true) |
| 568 |
failed = failed || !c.Check(t.isRanged(), check.Equals, true) |
| 569 |
if failed { |
| 570 |
if *printTree { |
| 571 |
c.Logf("Failing tree: %s\n\n", t.Root.describeTree(false, true)) |
| 572 |
} |
| 573 |
if *genDot && t.Len() <= *dotLimit { |
| 574 |
err := t.dotFile(fmt.Sprintf("TestDeleteMinMax_after_delmin_%d", i), "") |
| 575 |
if err != nil { |
| 576 |
c.Errorf("Dot file write failed: %v", err) |
| 577 |
} |
| 578 |
} |
| 579 |
c.Fatal("Cannot continue test: invariant contradiction") |
| 580 |
} |
| 581 |
t.DeleteMax(false) |
| 582 |
dI-- |
| 583 |
c.Check(t.Len(), check.Equals, dI) |
| 584 |
max-- |
| 585 |
failed = !c.Check(t.Max(), check.DeepEquals, &intOverlap{start: max, end: max + length}) |
| 586 |
failed = failed || !c.Check(t.isBST(), check.Equals, true) |
| 587 |
failed = failed || !c.Check(t.is23_234(), check.Equals, true) |
| 588 |
failed = failed || !c.Check(t.isBalanced(), check.Equals, true) |
| 589 |
failed = failed || !c.Check(t.isRanged(), check.Equals, true) |
| 590 |
if failed { |
| 591 |
if *printTree { |
| 592 |
c.Logf("Failing tree: %s\n\n", t.Root.describeTree(false, true)) |
| 593 |
} |
| 594 |
if *genDot && t.Len() <= *dotLimit { |
| 595 |
err := t.dotFile(fmt.Sprintf("TestDeleteMinMax_after_delmax_%d", i), "") |
| 596 |
if err != nil { |
| 597 |
c.Errorf("Dot file write failed: %v", err) |
| 598 |
} |
| 599 |
} |
| 600 |
c.Fatal("Cannot continue test: invariant contradiction") |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
func (s *S) TestIntRangeBug(c *check.C) { |
| 608 |
var t IntTree |
| 609 |
for i, e := range []*intOverlap{ |
| 610 |
{start: 0, end: 10}, |
| 611 |
{start: 1, end: 5}, |
| 612 |
{start: 1, end: 5}, |
| 613 |
} { |
| 614 |
e.id = uintptr(i) |
| 615 |
err := t.Insert(e, false) |
| 616 |
c.Assert(err, check.Equals, nil) |
| 617 |
} |
| 618 |
c.Check(t.isRanged(), check.Equals, true) |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
func (s *S) TestIssue15Int(c *check.C) { |
| 624 |
ranges := []*intOverlap{ |
| 625 |
{start: 5, end: 6}, |
| 626 |
{start: 7, end: 8}, |
| 627 |
{start: 9, end: 10}, |
| 628 |
{start: 0, end: 4}, |
| 629 |
{start: 0, end: 1}, |
| 630 |
{start: 0, end: 1}, |
| 631 |
{start: 0, end: 1}, |
| 632 |
} |
| 633 |
|
| 634 |
var t IntTree |
| 635 |
for i, iv := range ranges { |
| 636 |
iv.id = uintptr(i) |
| 637 |
err := t.Insert(iv, false) |
| 638 |
c.Assert(err, check.Equals, nil) |
| 639 |
|
| 640 |
failed := !c.Check(t.isRanged(), check.Equals, true) |
| 641 |
if failed { |
| 642 |
if *printTree { |
| 643 |
c.Logf("Failing tree: %s\n\n", t.Root.describeTree(false, true)) |
| 644 |
} |
| 645 |
if *genDot && t.Len() <= *dotLimit { |
| 646 |
err := t.dotFile(fmt.Sprintf("Issue15IntTest_%02d", i), "") |
| 647 |
if err != nil { |
| 648 |
c.Errorf("Dot file write failed: %v", err) |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
got := t.Get(&intOverlap{start: 2, end: 3}) |
| 655 |
c.Check(len(got), check.Equals, 1, check.Commentf("Expected one overlap, got %d", len(got))) |
| 656 |
} |
| 657 |
|
| 658 |
func (s *S) TestIntInsertBug(c *check.C) { |
| 659 |
var t IntTree |
| 660 |
for i := 10; i > 0; i-- { |
| 661 |
t.Insert(&intOverlap{start: 0, end: 1, id: uintptr(i)}, false) |
| 662 |
} |
| 663 |
c.Check(t.Len(), check.Equals, 10, check.Commentf("Expected 10 entries, got %d", t.Len())) |
| 664 |
for i := 1; i <= 10; i++ { |
| 665 |
t.Delete(&intOverlap{start: 0, end: 1, id: uintptr(i)}, false) |
| 666 |
} |
| 667 |
c.Check(t.Len(), check.Equals, 0, check.Commentf("Expected 0 entries, got %d", t.Len())) |
| 668 |
} |
| 669 |
|
| 670 |
func (t *IntTree) dot(label string) string { |
| 671 |
if t == nil { |
| 672 |
return "" |
| 673 |
} |
| 674 |
var ( |
| 675 |
s []string |
| 676 |
follow func(*IntNode) |
| 677 |
arrows = map[llrb.Color]string{llrb.Red: "none", llrb.Black: "normal"} |
| 678 |
) |
| 679 |
follow = func(n *IntNode) { |
| 680 |
id := uintptr(unsafe.Pointer(n)) |
| 681 |
c := fmt.Sprintf("%d[label = \"<Left> |<Elem> interval:%v\\nrange:[%d,%d)\\nid:%d|<Right>\"];", |
| 682 |
id, n.Elem, n.Range.Start, n.Range.End, n.Elem.ID()) |
| 683 |
if n.Left != nil { |
| 684 |
c += fmt.Sprintf("\n\t\tedge [color=%v,arrowhead=%s]; \"%d\":Left -> \"%d\":Elem;", |
| 685 |
n.Left.color(), arrows[n.Left.color()], id, uintptr(unsafe.Pointer(n.Left))) |
| 686 |
follow(n.Left) |
| 687 |
} |
| 688 |
if n.Right != nil { |
| 689 |
c += fmt.Sprintf("\n\t\tedge [color=%v,arrowhead=%s]; \"%d\":Right -> \"%d\":Elem;", |
| 690 |
n.Right.color(), arrows[n.Right.color()], id, uintptr(unsafe.Pointer(n.Right))) |
| 691 |
follow(n.Right) |
| 692 |
} |
| 693 |
s = append(s, c) |
| 694 |
} |
| 695 |
if t.Root != nil { |
| 696 |
follow(t.Root) |
| 697 |
} |
| 698 |
return fmt.Sprintf("digraph %s {\n\tnode [shape=record,height=0.1];\n\t%s\n}\n", |
| 699 |
label, |
| 700 |
strings.Join(s, "\n\t"), |
| 701 |
) |
| 702 |
} |
| 703 |
|
| 704 |
func (t *IntTree) dotFile(label, dotString string) (err error) { |
| 705 |
if t == nil && dotString == "" { |
| 706 |
return |
| 707 |
} |
| 708 |
f, err := os.Create(label + ".dot") |
| 709 |
if err != nil { |
| 710 |
return |
| 711 |
} |
| 712 |
defer f.Close() |
| 713 |
if dotString == "" { |
| 714 |
fmt.Fprintf(f, t.dot(label)) |
| 715 |
} else { |
| 716 |
fmt.Fprintf(f, dotString) |
| 717 |
} |
| 718 |
return |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
func BenchmarkIntInsert(b *testing.B) { |
| 724 |
var ( |
| 725 |
t = &IntTree{} |
| 726 |
length = 10 |
| 727 |
) |
| 728 |
for i := 0; i < b.N; i++ { |
| 729 |
s := b.N - i |
| 730 |
t.Insert(&intOverlap{start: s, end: s + length, id: uintptr(s)}, false) |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
func BenchmarkIntFastInsert(b *testing.B) { |
| 735 |
var ( |
| 736 |
t = &IntTree{} |
| 737 |
length = 10 |
| 738 |
) |
| 739 |
for i := 0; i < b.N; i++ { |
| 740 |
s := b.N - i |
| 741 |
t.Insert(&intOverlap{start: s, end: s + length, id: uintptr(s)}, true) |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
func BenchmarkIntGet(b *testing.B) { |
| 746 |
b.StopTimer() |
| 747 |
var ( |
| 748 |
t = &IntTree{} |
| 749 |
length = 10 |
| 750 |
) |
| 751 |
for i := 0; i < b.N; i++ { |
| 752 |
s := b.N - i |
| 753 |
t.Insert(&intOverlap{start: s, end: s + length, id: uintptr(s)}, false) |
| 754 |
} |
| 755 |
b.StartTimer() |
| 756 |
for i := 0; i < b.N; i++ { |
| 757 |
s := b.N - i |
| 758 |
t.Get(&intOverlap{start: s, end: s + length}) |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
func BenchmarkIntMin(b *testing.B) { |
| 763 |
b.StopTimer() |
| 764 |
var ( |
| 765 |
t = &IntTree{} |
| 766 |
length = 10 |
| 767 |
) |
| 768 |
for i := 0; i < 1e5; i++ { |
| 769 |
s := b.N - i |
| 770 |
t.Insert(&intOverlap{start: s, end: s + length, id: uintptr(s)}, false) |
| 771 |
} |
| 772 |
b.StartTimer() |
| 773 |
var m IntInterface |
| 774 |
for i := 0; i < b.N; i++ { |
| 775 |
m = t.Min() |
| 776 |
} |
| 777 |
_ = m |
| 778 |
} |
| 779 |
|
| 780 |
func BenchmarkIntMax(b *testing.B) { |
| 781 |
b.StopTimer() |
| 782 |
var ( |
| 783 |
t = &IntTree{} |
| 784 |
length = 10 |
| 785 |
) |
| 786 |
for i := 0; i < 1e5; i++ { |
| 787 |
s := b.N - i |
| 788 |
t.Insert(&intOverlap{start: s, end: s + length, id: uintptr(s)}, false) |
| 789 |
} |
| 790 |
b.StartTimer() |
| 791 |
var m IntInterface |
| 792 |
for i := 0; i < b.N; i++ { |
| 793 |
m = t.Max() |
| 794 |
} |
| 795 |
_ = m |
| 796 |
} |
| 797 |
|
| 798 |
func BenchmarkIntDelete(b *testing.B) { |
| 799 |
b.StopTimer() |
| 800 |
var ( |
| 801 |
t = &IntTree{} |
| 802 |
length = 1 |
| 803 |
) |
| 804 |
for i := 0; i < b.N; i++ { |
| 805 |
s := b.N - i |
| 806 |
t.Insert(&intOverlap{start: s, end: s + length, id: uintptr(s)}, false) |
| 807 |
} |
| 808 |
b.StartTimer() |
| 809 |
for i := 0; i < b.N; i++ { |
| 810 |
s := b.N - i |
| 811 |
t.Delete(&intOverlap{start: s, end: s + length, id: uintptr(s)}, false) |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
func BenchmarkIntFastDelete(b *testing.B) { |
| 816 |
b.StopTimer() |
| 817 |
var ( |
| 818 |
t = &IntTree{} |
| 819 |
length = 1 |
| 820 |
) |
| 821 |
for i := 0; i < b.N; i++ { |
| 822 |
s := b.N - i |
| 823 |
t.Insert(&intOverlap{start: s, end: s + length, id: uintptr(s)}, false) |
| 824 |
} |
| 825 |
b.StartTimer() |
| 826 |
for i := 0; i < b.N; i++ { |
| 827 |
s := b.N - i |
| 828 |
t.Delete(&intOverlap{start: s, end: s + length, id: uintptr(s)}, true) |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
func BenchmarkIntDeleteMin(b *testing.B) { |
| 833 |
b.StopTimer() |
| 834 |
var ( |
| 835 |
t = &IntTree{} |
| 836 |
length = 10 |
| 837 |
) |
| 838 |
for i := 0; i < b.N; i++ { |
| 839 |
s := b.N - i |
| 840 |
t.Insert(&intOverlap{start: s, end: s + length, id: uintptr(s)}, false) |
| 841 |
} |
| 842 |
b.StartTimer() |
| 843 |
for i := 0; i < b.N; i++ { |
| 844 |
t.DeleteMin(false) |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
func BenchmarkIntFastDeleteMin(b *testing.B) { |
| 849 |
b.StopTimer() |
| 850 |
var ( |
| 851 |
t = &IntTree{} |
| 852 |
length = 10 |
| 853 |
) |
| 854 |
for i := 0; i < b.N; i++ { |
| 855 |
s := b.N - i |
| 856 |
t.Insert(&intOverlap{start: s, end: s + length, id: uintptr(s)}, false) |
| 857 |
} |
| 858 |
b.StartTimer() |
| 859 |
for i := 0; i < b.N; i++ { |
| 860 |
t.DeleteMin(true) |
| 861 |
} |
| 862 |
} |