def test_01(self): """TestTreeAdd: simple add and check .obj.""" t = Tree.Tree('A') t.addChild('B') t.addChild('B') t.addChild('C') self.assertEqual(['B', 'B', 'C'], [v.obj for v in t.children])
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_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_01(self): """TestTreeAdd: simple add and convert to a Dict Tree.""" t = Tree.Tree('A') t.addChild('AA') t.youngestChild.addChild('AAA') t.addChild('AB') t.youngestChild.addChild('ABA') # print(t.branches()) self.assertEquals([['A'], ['A', 'AA'], ['A', 'AA', 'AAA'], ['A', 'AB'], ['A', 'AB', 'ABA']], t.branches()) dt = DictTree.DictTree('list') for branch in t.branches(): dt.add(branch, None) # print(dt.indentedStr()) self.assertEqual( """A [None] AA [None] AAA [None] AB [None] ABA [None]""", dt.indentedStr())
def test_02(self): """TestTreeCtor: test_02(): youngestChild raises when no children.""" t = Tree.Tree('A') try: t.youngestChild self.fail('IndexError not raised.') except IndexError: pass
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_00(self): """TestTreeAdd: simple add.""" t = Tree.Tree('A') t.addChild('B') self.assertEquals(1, len(t)) t.addChild('B') self.assertEquals(2, len(t)) t.addChild('C') self.assertEquals(3, len(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_00(self): """TestTreeAdd: simple add.""" t = Tree.Tree('A') t.addChild('AA') t.youngestChild.addChild('AAA') t.addChild('AB') t.youngestChild.addChild('ABA') # print(t.branches()) self.assertEquals([['A'], ['A', 'AA'], ['A', 'AA', 'AAA'], ['A', 'AB'], ['A', 'AB', 'ABA']], t.branches())
def test_str(self): t = Tree.Tree('A') t.addChild('AA') t.youngestChild.addChild('AAA') t.addChild('AB') t.youngestChild.addChild('ABA') # print() # print(str(t)) exp = "[['A'], ['A', 'AA'], ['A', 'AA', 'AAA'], ['A', 'AB'], ['A', 'AB', 'ABA']]" self.assertEquals(exp, str(t))
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_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_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_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(), )
def test_02(self): """TestTreeCtor: test_00(): simple object has len == 0.""" t = Tree.Tree('A') self.assertEquals(0, len(t))
def test_00(self): """TestTreeCtor: test_00(): constructor.""" t = Tree.Tree('A') self.assertEquals('A', t.obj) self.assertEquals(0, len(t))