Repository navigation
Expand file tree
/
Copy pathtest_rbtree.py
More file actions
96 lines (77 loc) · 2.93 KB
/
Copy pathtest_rbtree.py
File metadata and controls
96 lines (77 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
import unittest
import gdb
import crash.infra.callback
from crash.types.rbtree import rbtree_postorder_for_each, rbtree_postorder_for_each_entry
def get_symbol(name):
return gdb.lookup_symbol(name, None)[0].value()
class TestRbtree(unittest.TestCase):
def setUp(self):
gdb.execute("file tests/test-rbtree", to_string=True)
crash.infra.callback.target_ready()
try:
print()
print("--- Unsuppressable gdb output ---", end='')
gdb.execute("run", to_string=False)
self.number_node_type = gdb.lookup_type("struct number_node")
except gdb.error as e:
# If we don't tear it down, the rest of the tests in
# other files will fail due to it using the wrong core file
self.tearDown()
raise(e)
def tearDown(self):
try:
gdb.execute("detach", to_string=True)
gdb.execute("file")
except gdb.error:
print()
pass
print("--- End gdb output ---")
def test_none_root(self):
count = 0
with self.assertRaises(TypeError):
for node in rbtree_postorder_for_each(None):
count += 1
def test_invalid_value(self):
count = 0
gdbtype = gdb.lookup_type('unsigned int')
with self.assertRaises(TypeError):
for node in rbtree_postorder_for_each(gdbtype):
count += 1
def test_invalid_value_pointer(self):
count = 0
gdbtype = gdb.lookup_type('unsigned int').pointer()
with self.assertRaises(TypeError):
for node in rbtree_postorder_for_each(gdbtype):
count += 1
def test_nonroot_value(self):
count = 0
nn = get_symbol('naked_node')
with self.assertRaises(TypeError):
for node in rbtree_postorder_for_each(nn):
count += 1
def test_empty_tree(self):
count = 0
root = get_symbol('empty_tree_root')
for node in rbtree_postorder_for_each(root):
count += 1
self.assertEqual(count, 0)
def test_singular_tree(self):
count = 0
root = get_symbol('singular_tree_root')
for node in rbtree_postorder_for_each(root):
count += 1
self.assertEqual(count, 1)
def test_linear_binary_tree(self):
vals = []
root = get_symbol('linear_binary_tree_root')
for node in rbtree_postorder_for_each_entry(root, self.number_node_type, 'rb'):
vals.append(int(node['v']))
self.assertEqual(vals, [2, 1, 0])
def test_full_binary_tree(self):
vals = []
root = get_symbol('full_binary_tree_root')
for node in rbtree_postorder_for_each_entry(root, self.number_node_type, 'rb'):
vals.append(int(node['v']))
self.assertEqual(vals, [3, 4, 1, 5, 6, 2, 0])