def test_powers_of_two(self): """Test iterator for all powers of two less than a given number.""" pow2 = powers_of_two(70) self.assertEqual(next(pow2), 1) self.assertEqual(next(pow2), 2) self.assertEqual(next(pow2), 4) self.assertEqual(next(pow2), 8) self.assertEqual(next(pow2), 16) self.assertEqual(next(pow2), 32) self.assertEqual(next(pow2), 64) pow2 = powers_of_two(2) self.assertEqual(next(pow2), 1) self.assertEqual(next(pow2), 2)
def test_powers_of_two_produces_an_actual_generator(self): g = powers_of_two(3) self.assertEqual(g.next(), 1) self.assertEqual(g.next(), 2) self.assertRaises(StopIteration, g.next)
def test_powers_of_two_produces_correct_sequences(self): self.assertEqual(list(powers_of_two(60)), [1, 2, 4, 8, 16, 32]) self.assertEqual(list(powers_of_two(63)), [1, 2, 4, 8, 16, 32]) self.assertEqual(list(powers_of_two(64)), [1, 2, 4, 8, 16, 32, 64]) self.assertEqual(list(powers_of_two(0)), []) self.assertEqual(list(powers_of_two(1)), [1])