Beispiel #1
0
 def test_list_functions(self):
     # __init__
     b1 = branchedElement("one", "two")
     b1dupe = branchedElement(b1, b1)
     #self.assertTrue(b1dupe[0] != b1dupe[1])
     
     b1 = branchedElement()
     self.assertEqual(len(b1), 2)
     self.assertEqual(b1.rank(), 0)
     
     # __getitem__
     b2 = branchedElement("hi", "there")
     self.assertEqual(b2[0], "hi")
     self.assertEqual(b2[1], "there")
     
     # __setitem__
     b2[0] = "hello"
     b2[1] = "world!"
     self.assertEqual(b2[0] + " " + b2[1], "hello world!")
     
     # __len__, __delitem__
     self.assertEqual(len(b2), 2)
     del b2[1]
     self.assertEqual(len(b2), 1)
     del b2[0]
     self.assertEqual(len(b2), 0)
     
     # __contains__
     b3 = branchedElement(rankedElement("four", 4), rankedElement("4", 4))
     b3first = b3[0]
     self.assertTrue(b3first in b3)
     self.assertFalse(rankedElement("bloop", 4) in b3)
Beispiel #2
0
 def test_iter(self):
     composers = ["Ludwig", "Copland", "Bernstein", "Britten", "Schubert", "Chopin"]
 
     b1 = rankedElement(composers[0])
     b2 = rankedElement(composers[1])
     b3 = rankedElement(composers[2])
     b4 = rankedElement(composers[3])
     b5 = rankedElement(composers[4])
     b6 = rankedElement(composers[5])
     b7 = branchedElement(b1, b2)
     b8 = branchedElement(b3, b4)
     
     b9 = branchedElement(b5, b7)
     b10 = branchedElement(b6, b8)
     
     top = branchedElement(b9, b10)
     
     br = bracket.bracket(top)
     
     elements = [b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, top]
     
     for e in br:
         if e in elements:
             elements.remove(e)
         else:
             raise ValueError("Found element " + str(e) + " in this tree for some reason!")
             
     self.assertEqual(len(elements), 0)
Beispiel #3
0
    def test_swap(self):
        b1 = rankedElement("Charles", 1)
        b2 = rankedElement("Anakin", 2)
        b3 = rankedElement("George", 3)
        
        b4 = branchedElement(b1, b2)
        b5 = branchedElement(b3, b4)
                
        self.assertEqual(b5[0].rank(), 3) 
        self.assertEqual(b5[0].name, "George") 
        
        b1.swap(b3)
        
        self.assertEqual(b5[0].rank(), 1) 
        self.assertEqual(b5[0].name, "Charles") 
                
        self.assertEqual(b4[0].rank(), 3) 
        self.assertEqual(b4[0].name, "George")
        
        b5[0].swap(b4[0])
        
        self.assertEqual(b5[0].rank(), 3) 
        self.assertEqual(b5[0].name, "George")

        self.assertEqual(b4[0].rank(), 1) 
        self.assertEqual(b4[0].name, "Charles")
        
        with self.assertRaises(ValueError):
            b5.swap(b4)
        
        b6 = rankedElement("Errol", 4)
        b7 = rankedElement("Hedwig", 5)
        
        b8 = branchedElement(b6, b7)
        
        b8.swap(b4)
        
        self.assertEqual(b4.rank(), 4)
        self.assertEqual(b8.rank(), 1)
        
        b9 = branchedElement(b3, branchedElement(b1, b2))
        b9.swap(b4)
        
        self.assertEqual(b9.count(), 3)
        self.assertEqual(b4.count(), 5)
Beispiel #4
0
 def test_rank(self):
     # simple, ranked element
     b1 = rankedElement("ten", 10)
     self.assertEqual(b1.rank(), 10)
     
     # two ranked members
     b2 = branchedElement(rankedElement("four", 4), rankedElement("eight", 8))
     self.assertEqual(b2[0].rank(), 4)
     self.assertEqual(b2[1].rank(), 8)
     self.assertEqual(b2.rank(), 4)
     
     # unranked memebers
     b3 = branchedElement(rankedElement(), rankedElement())
     self.assertEqual(b3.rank(), 0)
     
     b3[0].set_rank(5)
     b3[1].set_rank(4)
     
     self.assertEqual(b3.rank(), 4)
Beispiel #5
0
 def test_residual(self):
     b1 = rankedElement(rank=1)
     b2 = rankedElement(rank=2)
     b3 = rankedElement(rank=7)
     b4 = rankedElement(rank=16)
     
     b5 = branchedElement(b1, b2)
     b6 = branchedElement(b3, b4)
     
     r1 = bracketPhase(participants=4).shifted_to_top()
     b7 = branchedElement(b5, b6, r1)
     
     self.assertEqual(b1.residual(), 0)
     self.assertEqual(b2.residual(), 0)
     self.assertEqual(b3.residual(), 3)
     self.assertEqual(b4.residual(), 12)
     self.assertEqual(b5.residual(), -2)
     self.assertEqual(b6.residual(), 18)
     self.assertEqual(b7.residual(), 5)
     
     b8 = rankedElement(rank=6)
     b9 = rankedElement(rank=7)
     
     b10 = branchedElement(b8, b9)
     
     r2 = bracketPhase(participants=6).shifted_to_top()
     b11 = branchedElement(b7, b10, r2)
     
     self.assertEqual(b1.residual(), -2)
     self.assertEqual(b2.residual(), -1)
     self.assertEqual(b3.residual(), 0)
     self.assertEqual(b4.residual(), 8)
     self.assertEqual(b5.residual(), -6)
     self.assertEqual(b6.residual(), 14)
     self.assertEqual(b7.residual(), 3)
     self.assertEqual(b8.residual(), 4)
     self.assertEqual(b9.residual(), 5)
     self.assertEqual(b10.residual(), 8)
     self.assertEqual(b11.residual(), 4)
Beispiel #6
0
 def test_phase(self):
     b1 = rankedElement(rank=1)
     b2 = rankedElement(rank=2)
     
     self.assertEqual(b1.phase.size(), 1)
     self.assertEqual(b2.phase.size(), 1)
     
     b3 = branchedElement(b1, b2)
     
     self.assertEqual(b1.phase.size(), 1)
     self.assertEqual(b2.phase.size(), 1)
     
     self.assertEqual(b3.phase, None)
     
     b3.phase = bracketPhase(2).shifted_to_top()
     
     self.assertEqual(b1.phase.size(), 2)
     self.assertEqual(b2.phase.size(), 2)
     self.assertEqual(b3.phase.size(), 1)
     self.assertEqual(b1.phase, b2.phase)
Beispiel #7
0
 def test_phase(self):
     b1 = rankedElement()
     b2 = rankedElement()
     b3 = rankedElement()
     b4 = branchedElement(b1, b2)
     b5 = branchedElement(b3, b4)
Beispiel #8
0
 def test_count(self):
     b1 = branchedElement(branchedElement(rankedElement(), rankedElement()), rankedElement())
     self.assertEqual(b1.count(), 5)
Beispiel #9
0
 def test_sum_members(self):
     b1 = branchedElement(rankedElement("eight", 8), rankedElement("two", 2))
     self.assertEqual(b1.sum_members(), 10)