def test_total_infection_from_student_to_parent(self): c = Coach() for i in range(5): c.add_student() s = c.students[0] total_infection(s) self.assertEqual(c.count_infected(), c.count())
def test_total_infection(self): coaches = [] for i in range(10): c = Coach() coaches.append(c) import random for i in range(len(coaches)): num_students = int(1 + random.random() * 10) for _ in range(num_students): coaches[i].add_student() s1 = int(random.random() * 10) # here we add a coach-coach relationship s2 = 10 - s1 coaches[s1].add_coach() for coach in coaches: total_infection(coach) # infect them all! """ Since not every coach coaches other coaches (wow), we need to loop through all of the coaches to look at their children. Essentially right now we have a forest which consists of coaches at the roots, where a child may be a coach or a student. So we do a depth-first traversal of each tree in the forest. """ for coach in coaches: self.assertTrue(coach.infected) s = [coach] while s: v = s.pop() self.assertTrue(v.infected) if isinstance(v, Coach): for student in v.students: s.append(student)
def testFour(self): """ Test #4: Complex Multiple Graph Total Infection. """ a = User('a') b = User('b') c = User('c') d = User('d') e = User('e') f = User('f') g = User('g') h = User('h') i = User('i') j = User('j') coach(a, b) coach(a, c) coach(b, a) coach(d, c) coach(c, b) coach(e, f) coach(g, b) coach(b, d) coach(c, h) coach(h, i) coach(b, j) coach(d, j) total_infection([a,b,c,d,e,f,g,h,i,j], a, 1) correct = True for user in [a,b,c,d,g,h,i,j]: if user.get_version() != 1: correct = False self.failUnless(correct)
def testOne(self): """ Test #1: Simple Link Total Infection. """ a = User('a') b = User('b') coach(a, b) total_infection([a,b], a, 1) self.failUnless(a.get_version() == 1 and b.get_version() == 1)
def testThree(self): """ Test #3: Tree Total Infection from leaf. """ a = User('a') b = User('b') c = User('c') d = User('d') e = User('e') f = User('f') g = User('g') coach(a, b) coach(a, c) coach(b, d) coach(b, e) coach(c, f) coach(c, g) users = [a, b, c, d, e, f, g] total_infection(users, g, 1) correct = True for user in users: if user.get_version() != 1: correct = False self.failUnless(correct)
def test_count_infected(self): total_infection(self.coach) self.assertEqual(self.coach.count_infected(), self.coach.count())