def test_simple_ratio(self): f23 = Fraction(2, 3) assert to_fraction(0.66666666) == f23 for i in f23, '2/3', 2 / 3, '2 / 3', 0.66666666, 0.6666666: assert to_fraction(i) == f23 assert to_fraction(0.666666) == Fraction(333333, 500000) assert to_fraction(0.66666) == Fraction(33333, 50000)
def test_fractions(self): MAX = 10000 # Set to 0 for a big test! for pp, p in more_itertools.windowed(gen_primes(), 2): if MAX and p >= MAX: break floating = to_fraction(p / pp) fractional = Fraction(p, pp) if pp > 1000000: assert floating == Fraction(766692, 766669) assert fractional == Fraction(1000033, 1000003) break else: assert floating == fractional pp = p
def test_to_samples(self): assert to_samples('2', 44100) == 88200 assert to_samples('2.3', 44100) == to_fraction(2.3) * 44100 assert to_samples('300s', 44100) == 300 * 44100 assert to_samples('300ms', 44100) == 13230 assert to_samples('300 samples', 44100) == 300
def test_type_error(self): with self.assertRaises(TypeError) as m: to_fraction(set()) assert m.exception.args == ( "Do not understand set() of type <class 'set'>", )
def test_empty(self): for x in 0, 0.0: assert to_fraction(x) == Fraction()
def test_value_error(self): with self.assertRaises(ValueError) as m: to_fraction('junk') assert m.exception.args == ( "could not convert string to float: 'junk'", )