def test_treeChildParent_raises(self): """TestDuplexAdjacencyList: Tree with a cycle raises.""" t = Tree.DuplexAdjacencyList() t.add('a', 'b') t.add('b', 'c') t.add('c', 'a') self.assertRaises(ValueError, t.treeChildParent, 'B')
def test_02(self): """TestDuplexAdjacencyList: single parent, two children.""" t = Tree.DuplexAdjacencyList() t.add('parent', 'child_00') t.add('parent', 'child_01') self.assertEqual([ 'child_00', 'child_01', ], t.children('parent')) self.assertEqual([ 'parent', ], t.parents('child_00')) self.assertEqual([ 'parent', ], t.parents('child_01')) self.assertEqual(['parent'], list(t.allParents)) self.assertEqual(['child_00', 'child_01'], sorted(t.allChildren)) # print(t.treeParentChild('parent').branches()) self.assertEqual( [ ['parent'], ['parent', 'child_00'], ['parent', 'child_01'], ], t.treeParentChild('parent').branches(), )
def test_00(self): """TestDuplexAdjacencyList: ctor, empty list.""" t = Tree.DuplexAdjacencyList() self.assertRaises(KeyError, t.children, '') self.assertRaises(KeyError, t.parents, '') self.assertEqual([], list(t.allParents)) self.assertEqual([], list(t.allChildren))
def test_str(self): t = Tree.DuplexAdjacencyList() t.add('parent', 'child_00') t.add('parent', 'child_01') exp = """Parent -> Children: parent -> ['child_00', 'child_01']""" # print() # print(str(t)) self.assertEqual(exp, str(t))
def test_20(self): """TestDuplexAdjacencyList: __str__ on a Tree.""" t = Tree.DuplexAdjacencyList() t.add('A', 'AA') t.add('A', 'AB') t.add('AA', 'AAA') t.add('AA', 'AAB') t.add('AB', 'ABA') t.add('AB', 'ABB')
def test_06(self): """TestDuplexAdjacencyList: test hasParent(), hasChildren().""" t = Tree.DuplexAdjacencyList() t.add('parent', 'child_00') t.add('parent', 'child_01') self.assertTrue(t.hasParent('parent')) self.assertTrue(t.hasChild('child_00')) self.assertTrue(t.hasChild('child_01')) self.assertFalse(t.hasParent('foo')) self.assertFalse(t.hasChild('bar'))
def test_treeParentChild_raises(self): """TestDuplexAdjacencyList: Tree with no cycles.""" t = Tree.DuplexAdjacencyList() t.add('A', 'AA') t.add('A', 'AB') t.add('AA', 'AAA') t.add('AA', 'AAB') t.add('AB', 'ABA') t.add('AB', 'ABB') # pprint.pprint(t.treeParentChild('A').branches()) self.assertRaises(ValueError, t.treeParentChild, 'B')
def test_01(self): """TestDuplexAdjacencyList: single add.""" t = Tree.DuplexAdjacencyList() t.add('parent', 'child') self.assertEqual([ 'child', ], t.children('parent')) self.assertEqual([ 'parent', ], t.parents('child')) self.assertEqual(['parent'], list(t.allParents)) self.assertEqual(['child'], list(t.allChildren))
def test_treeChildParent(self): """TestDuplexAdjacencyList: Tree with a cycle.""" t = Tree.DuplexAdjacencyList() t.add('a', 'b') t.add('b', 'c') t.add('c', 'a') exp = { 'a': [['a'], ['a', 'c'], ['a', 'c', 'b']], 'b': [['b'], ['b', 'a'], ['b', 'a', 'c']], 'c': [['c'], ['c', 'b'], ['c', 'b', 'a']], } # print() # pprint.pprint(t.treeChildParent('a').branches()) # pprint.pprint(t.treeChildParent('b').branches()) # pprint.pprint(t.treeChildParent('c').branches()) for k in exp: self.assertEqual(exp[k], t.treeChildParent(k).branches())
def test_03(self): """TestDuplexAdjacencyList: simple cycle.""" t = Tree.DuplexAdjacencyList() t.add('A', 'A') self.assertEqual([ 'A', ], t.children('A')) self.assertEqual([ 'A', ], t.parents('A')) # print(t.treeParentChild('A').branches()) self.assertEqual( [ ['A'], ], t.treeParentChild('A').branches(), )
def test_10(self): """TestDuplexAdjacencyList: Tree with no cycles.""" t = Tree.DuplexAdjacencyList() t.add('A', 'AA') t.add('A', 'AB') t.add('AA', 'AAA') t.add('AA', 'AAB') t.add('AB', 'ABA') t.add('AB', 'ABB') # pprint.pprint(t.treeParentChild('A').branches()) self.assertEqual( [ ['A'], ['A', 'AA'], ['A', 'AA', 'AAA'], ['A', 'AA', 'AAB'], ['A', 'AB'], ['A', 'AB', 'ABA'], ['A', 'AB', 'ABB'], ], t.treeParentChild('A').branches(), )
def test_04(self): """TestDuplexAdjacencyList: cycle A -> B -> A.""" t = Tree.DuplexAdjacencyList() t.add('A', 'B') t.add('B', 'A') self.assertEqual([ 'B', ], t.children('A')) self.assertEqual([ 'A', ], t.parents('B')) # print(t.treeParentChild('A').branches()) self.assertEqual( [ [ 'A', ], [ 'A', 'B', ], ], t.treeParentChild('A').branches(), ) self.assertEqual( [ [ 'B', ], [ 'B', 'A', ], ], t.treeParentChild('B').branches(), )