def test_clear(self): dll = dllist() for i in range(10): dll.insert(Node(i)) self.assertEqual(len(dll), 10) dll.clear() self.assertEqual(len(dll), 0)
def test_iter(self): dll = dllist() for i in range(10): dll.insert(Node(10+i)) check_dllist(dll) value = 10 for node in dll: self.assertIsInstance(node, Node) self.assertEqual(node.callback, value) value += 1 check_dllist(dll)
def test_iter(self): dll = dllist() for i in range(10): dll.insert(Node(10 + i)) check_dllist(dll) value = 10 for node in dll: self.assertIsInstance(node, Node) self.assertEqual(node.callback, value) value += 1 check_dllist(dll)
def test_basic(self): dll = dllist() check_dllist(dll) self.assertIsNone(dll.first) self.assertIsNone(dll.last) self.assertEqual(len(dll), 0) # insert first element n1 = Node('foo') dll.insert(n1) self.assertIs(dll.first, n1) self.assertIs(dll.last, n1) self.assertEqual(len(dll), 1) self.assertIn(n1, dll) check_dllist(dll) # insert second at end n2 = Node('bar') dll.insert(n2) self.assertIs(dll.first, n1) self.assertIs(dll.last, n2) self.assertEqual(len(dll), 2) self.assertIn(n2, dll) check_dllist(dll) # insert in middle n3 = Node('baz') dll.insert(n3, before=n2) self.assertIs(dll.first, n1) self.assertIs(dll.last, n2) self.assertEqual(len(dll), 3) self.assertIn(n3, dll) check_dllist(dll) # remove middle dll.remove(n3) self.assertIs(dll.first, n1) self.assertIs(dll.last, n2) self.assertEqual(len(dll), 2) self.assertNotIn(n3, dll) check_dllist(dll) # remove first dll.remove(n1) self.assertIs(dll.first, n2) self.assertIs(dll.last, n2) self.assertEqual(len(dll), 1) self.assertNotIn(n1, dll) check_dllist(dll) # remove remaining element dll.remove(n2) self.assertIsNone(dll.first) self.assertIsNone(dll.last) self.assertEqual(len(dll), 0) self.assertNotIn(n2, dll) check_dllist(dll)
def perf_insert_throughput(self): # Measure the number inserts we can do per second t0 = t1 = time.time() count = 0 batch = 1000 dll = dllist() value = 'foo' while t1 - t0 < 0.2: for i in range(batch): dll.insert(Node(value)) count += batch t1 = time.time() speed = count / (t1 - t0) self.add_result(speed)
def test_args(self): dll = dllist() def callback(): pass n1 = Node(callback) self.assertEqual(n1.callback, callback) self.assertIsNone(n1.args) dll.insert(n1) n2 = Node(callback, 'foo') self.assertIs(n2.callback, callback) self.assertEqual(n2.args, 'foo') dll.insert(n2) nodes = list(dll) self.assertEqual(nodes[0].callback, callback) self.assertIsNone(nodes[0].args) self.assertEqual(nodes[1].callback, callback) self.assertEqual(nodes[1].args, 'foo')
def test_basic(self): dll = dllist() check_dllist(dll) self.assertIsNone(dll.first) self.assertIsNone(dll.last) self.assertEqual(len(dll), 0) # insert first element n1 = Node('foo') dll.insert(n1) self.assertIs(dll.first, n1) self.assertIs(dll.last, n1) self.assertEqual(len(dll), 1) check_dllist(dll) # insert second at end n2 = Node('bar') dll.insert(n2) self.assertIs(dll.first, n1) self.assertIs(dll.last, n2) self.assertEqual(len(dll), 2) check_dllist(dll) # insert in middle n3 = Node('baz') dll.insert(n3, before=n2) self.assertIs(dll.first, n1) self.assertIs(dll.last, n2) self.assertEqual(len(dll), 3) check_dllist(dll) # remove middle dll.remove(n3) self.assertIs(dll.first, n1) self.assertIs(dll.last, n2) self.assertEqual(len(dll), 2) check_dllist(dll) # remove first dll.remove(n1) self.assertIs(dll.first, n2) self.assertIs(dll.last, n2) self.assertEqual(len(dll), 1) check_dllist(dll) # remove remaining element dll.remove(n2) self.assertIsNone(dll.first) self.assertIsNone(dll.last) self.assertEqual(len(dll), 0) check_dllist(dll)
def test_many_nodes(self): nodes = [] dll = dllist() count = 10000 for i in range(count): before = random.choice(nodes) if nodes else None node = Node(i) dll.insert(node, before) nodes.append(node) self.assertEqual(len(dll), i+1) if i % 100 == 0: check_dllist(dll) check_dllist(dll) for i in range(count): r = random.randint(0, len(nodes)-1) node = nodes[r]; del nodes[r] dll.remove(node) self.assertEqual(len(dll), count-i-1) if i % 100 == 0: check_dllist(dll) check_dllist(dll)
def test_many_nodes(self): nodes = [] dll = dllist() count = 10000 for i in range(count): before = random.choice(nodes) if nodes else None node = Node(i) dll.insert(node, before) nodes.append(node) self.assertEqual(len(dll), i + 1) if i % 100 == 0: check_dllist(dll) check_dllist(dll) for i in range(count): r = random.randint(0, len(nodes) - 1) node = nodes[r] del nodes[r] dll.remove(node) self.assertEqual(len(dll), count - i - 1) if i % 100 == 0: check_dllist(dll) check_dllist(dll)
def mem_dllist(self): self.add_result(sizeof(callbacks.dllist()))