def test_int(self): ''' Int. ''' self.assertTupleEqual(util.closest_factor(24, 5), (4, 6)) self.assertTupleEqual(util.closest_factor(24, 10), (8, 12)) self.assertTupleEqual(util.closest_factor(25, 3), (1, 5)) self.assertTupleEqual(util.closest_factor(25, 20), (5, 25))
def test_missing(self): ''' Missing one or both. ''' fs = util.closest_factor(4, 5) self.assertTupleEqual(fs, (4, )) fs = util.closest_factor(4, 0.2) self.assertTupleEqual(fs, (1, ))
def test_negative(self): ''' Negative. ''' with self.assertRaisesRegexp(ValueError, '.*negative.*'): _ = util.closest_factor(24, -5) with self.assertRaisesRegexp(ValueError, '.*negative.*'): _ = util.closest_factor(-24, -5) with self.assertRaisesRegexp(ValueError, '.*negative.*'): _ = util.closest_factor(-24, 5)
def test_random(self): ''' Random test. ''' for val in range(1, 11): for f in range(1, 11): fs = util.closest_factor(val, f) string = 'closest_factor: {} {} {}'.format(val, f, fs) if len(fs) == 2: self.assertEqual(val % fs[0], 0, string) self.assertGreaterEqual(f, fs[0], string) self.assertEqual(val % fs[1], 0, string) self.assertLessEqual(f, fs[1], string) elif len(fs) == 1: self.assertEqual(val % fs[0], 0, string)
def test_zero(self): ''' Zero. ''' self.assertTupleEqual(util.closest_factor(0, 3), (3, )) self.assertTupleEqual(util.closest_factor(24, 0), (1, ))
def test_factor_float(self): ''' Factor is float. ''' self.assertTupleEqual(util.closest_factor(24, 5.3), (4, 6)) self.assertTupleEqual(util.closest_factor(24, 10.2), (8, 12))
def test_value_float(self): ''' Value is float. ''' with self.assertRaisesRegexp(TypeError, '.*integers.*'): _ = util.closest_factor(24.3, 5) with self.assertRaisesRegexp(TypeError, '.*integers.*'): _ = util.closest_factor(24., 10)
def test_exact(self): ''' Exact factor. ''' self.assertTupleEqual(util.closest_factor(24, 6), (6, 6)) self.assertTupleEqual(util.closest_factor(24, 2), (2, 2)) self.assertTupleEqual(util.closest_factor(3, 1), (1, 1))