2012-11-13 22:55:37 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import nilmdb
|
2012-12-31 15:52:28 -05:00
|
|
|
from nilmdb.utils.printf import *
|
2012-11-13 22:55:37 -05:00
|
|
|
|
|
|
|
from nose.tools import *
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
|
|
|
|
from nilmdb.rbtree import RBTree, RBNode
|
|
|
|
|
2013-01-05 15:00:34 -05:00
|
|
|
from testutil.helpers import *
|
2012-11-13 22:55:37 -05:00
|
|
|
import unittest
|
|
|
|
|
2012-11-28 20:57:23 -05:00
|
|
|
# set to False to skip live renders
|
|
|
|
do_live_renders = False
|
|
|
|
def render(tree, description = "", live = True):
|
2013-01-05 15:00:34 -05:00
|
|
|
import testutil.renderdot as renderdot
|
2012-11-29 00:42:50 -05:00
|
|
|
r = renderdot.RBTreeRenderer(tree)
|
|
|
|
return r.render(description, live and do_live_renders)
|
2012-11-15 13:55:56 -05:00
|
|
|
|
2012-11-13 22:55:37 -05:00
|
|
|
class TestRBTree:
|
|
|
|
def test_rbtree(self):
|
|
|
|
rb = RBTree()
|
2012-11-28 20:57:23 -05:00
|
|
|
rb.insert(RBNode(10000, 10001))
|
|
|
|
rb.insert(RBNode(10004, 10007))
|
|
|
|
rb.insert(RBNode(10001, 10002))
|
2012-11-13 23:12:15 -05:00
|
|
|
# There was a typo that gave the RBTree a loop in this case.
|
|
|
|
# Verify that the dot isn't too big.
|
2012-11-28 20:57:23 -05:00
|
|
|
s = render(rb, live = False)
|
2012-11-13 23:12:15 -05:00
|
|
|
assert(len(s.splitlines()) < 30)
|
2012-11-13 23:23:53 -05:00
|
|
|
|
|
|
|
def test_rbtree_big(self):
|
|
|
|
import random
|
|
|
|
random.seed(1234)
|
2012-11-14 15:10:43 -05:00
|
|
|
|
2012-11-29 00:42:50 -05:00
|
|
|
# make a set of 100 intervals, inserted in order
|
2012-11-14 15:10:43 -05:00
|
|
|
rb = RBTree()
|
2012-11-29 00:42:50 -05:00
|
|
|
j = 100
|
2012-11-14 15:10:43 -05:00
|
|
|
for i in xrange(j):
|
2012-11-28 20:57:23 -05:00
|
|
|
rb.insert(RBNode(i, i+1))
|
|
|
|
render(rb, "in-order insert")
|
2012-11-14 15:10:43 -05:00
|
|
|
|
|
|
|
# remove about half of them
|
|
|
|
for i in random.sample(xrange(j),j):
|
|
|
|
if random.randint(0,1):
|
|
|
|
rb.delete(rb.find(i, i+1))
|
2012-11-28 20:57:23 -05:00
|
|
|
render(rb, "in-order insert, random delete")
|
2012-11-14 15:10:43 -05:00
|
|
|
|
2012-11-29 00:42:50 -05:00
|
|
|
# make a set of 100 intervals, inserted at random
|
2012-11-13 23:23:53 -05:00
|
|
|
rb = RBTree()
|
2012-11-29 00:42:50 -05:00
|
|
|
j = 100
|
2012-11-13 23:23:53 -05:00
|
|
|
for i in random.sample(xrange(j),j):
|
2012-11-28 20:57:23 -05:00
|
|
|
rb.insert(RBNode(i, i+1))
|
|
|
|
render(rb, "random insert")
|
2012-11-13 23:23:53 -05:00
|
|
|
|
|
|
|
# remove about half of them
|
|
|
|
for i in random.sample(xrange(j),j):
|
|
|
|
if random.randint(0,1):
|
|
|
|
rb.delete(rb.find(i, i+1))
|
2012-11-28 20:57:23 -05:00
|
|
|
render(rb, "random insert, random delete")
|
2012-11-14 15:10:43 -05:00
|
|
|
|
2012-11-29 00:42:50 -05:00
|
|
|
# in-order insert of 50 more
|
|
|
|
for i in xrange(50):
|
2012-11-28 20:57:23 -05:00
|
|
|
rb.insert(RBNode(i+500, i+501))
|
|
|
|
render(rb, "random insert, random delete, in-order insert")
|
2012-11-14 15:10:43 -05:00
|
|
|
|
2012-11-28 20:57:23 -05:00
|
|
|
def test_rbtree_basics(self):
|
|
|
|
rb = RBTree()
|
|
|
|
vals = [ 7, 14, 1, 2, 8, 11, 5, 15, 4]
|
|
|
|
for n in vals:
|
|
|
|
rb.insert(RBNode(n, n))
|
|
|
|
|
|
|
|
# stringify
|
|
|
|
s = ""
|
|
|
|
for node in rb:
|
|
|
|
s += str(node)
|
2012-11-29 01:25:51 -05:00
|
|
|
in_("[node (None) 1", s)
|
|
|
|
eq_(str(rb.nil), "[node nil]")
|
2012-11-28 20:57:23 -05:00
|
|
|
|
|
|
|
# inorder traversal, successor and predecessor
|
|
|
|
last = 0
|
|
|
|
for node in rb:
|
|
|
|
assert(node.start > last)
|
|
|
|
last = node.start
|
|
|
|
successor = rb.successor(node)
|
|
|
|
if successor:
|
|
|
|
assert(rb.predecessor(successor) is node)
|
|
|
|
predecessor = rb.predecessor(node)
|
|
|
|
if predecessor:
|
|
|
|
assert(rb.successor(predecessor) is node)
|
|
|
|
|
|
|
|
# Delete node not in the tree
|
|
|
|
with assert_raises(AttributeError):
|
|
|
|
rb.delete(RBNode(1,2))
|
|
|
|
|
|
|
|
# Delete all nodes!
|
|
|
|
for node in rb:
|
|
|
|
rb.delete(node)
|
|
|
|
|
|
|
|
# Build it up again, make sure it matches
|
|
|
|
for n in vals:
|
|
|
|
rb.insert(RBNode(n, n))
|
|
|
|
s2 = ""
|
|
|
|
for node in rb:
|
|
|
|
s2 += str(node)
|
|
|
|
assert(s == s2)
|
|
|
|
|
|
|
|
def test_rbtree_find(self):
|
|
|
|
# Get a little bit of coverage for some overlapping cases,
|
|
|
|
# even though the class doesn't fully support it.
|
|
|
|
rb = RBTree()
|
|
|
|
nodes = [ RBNode(1, 5), RBNode(1, 10), RBNode(1, 15) ]
|
|
|
|
for n in nodes:
|
|
|
|
rb.insert(n)
|
|
|
|
assert(rb.find(1, 5) is nodes[0])
|
|
|
|
assert(rb.find(1, 10) is nodes[1])
|
|
|
|
assert(rb.find(1, 15) is nodes[2])
|
|
|
|
|
|
|
|
def test_rbtree_find_leftright(self):
|
|
|
|
# Now let's get some ranges in there
|
|
|
|
rb = RBTree()
|
|
|
|
vals = [ 7, 14, 1, 2, 8, 11, 5, 15, 4]
|
|
|
|
for n in vals:
|
|
|
|
rb.insert(RBNode(n*10, n*10+5))
|
|
|
|
|
|
|
|
# Check find_end_left, find_right_start
|
|
|
|
for i in range(160):
|
|
|
|
left = rb.find_left_end(i)
|
|
|
|
right = rb.find_right_start(i)
|
|
|
|
if left:
|
|
|
|
# endpoint should be more than i
|
|
|
|
assert(left.end >= i)
|
|
|
|
# all earlier nodes should have a lower endpoint
|
|
|
|
for node in rb:
|
|
|
|
if node is left:
|
|
|
|
break
|
|
|
|
assert(node.end < i)
|
|
|
|
if right:
|
|
|
|
# startpoint should be less than i
|
|
|
|
assert(right.start <= i)
|
|
|
|
# all later nodes should have a higher startpoint
|
|
|
|
for node in reversed(list(rb)):
|
|
|
|
if node is right:
|
|
|
|
break
|
|
|
|
assert(node.start > i)
|
|
|
|
|
|
|
|
def test_rbtree_intersect(self):
|
2012-11-29 00:07:49 -05:00
|
|
|
# Fill with some ranges
|
2012-11-28 20:57:23 -05:00
|
|
|
rb = RBTree()
|
2012-11-29 00:07:49 -05:00
|
|
|
rb.insert(RBNode(10,20))
|
|
|
|
rb.insert(RBNode(20,25))
|
|
|
|
rb.insert(RBNode(30,40))
|
|
|
|
# Just a quick test; test_interval will do better.
|
|
|
|
eq_(len(list(rb.intersect(1,100))), 3)
|
|
|
|
eq_(len(list(rb.intersect(10,20))), 1)
|
|
|
|
eq_(len(list(rb.intersect(5,15))), 1)
|
|
|
|
eq_(len(list(rb.intersect(15,15))), 1)
|
|
|
|
eq_(len(list(rb.intersect(20,21))), 1)
|
|
|
|
eq_(len(list(rb.intersect(19,21))), 2)
|