def test_type_casting_rules(self): tm = TypeManager() tcr = TypeCastingRules(tm) i32 = types.int32 i64 = types.int64 f64 = types.float64 f32 = types.float32 made_up = types.Dummy("made_up") tcr.promote_unsafe(i32, i64) tcr.safe_unsafe(i32, f64) tcr.promote_unsafe(f32, f64) def base_test(): # As declared self.assertEqual(tm.check_compatible(i32, i64), Conversion.promote) self.assertEqual(tm.check_compatible(i32, f64), Conversion.safe) self.assertEqual(tm.check_compatible(f32, f64), Conversion.promote) self.assertEqual(tm.check_compatible(i64, i32), Conversion.unsafe) self.assertEqual(tm.check_compatible(f64, i32), Conversion.unsafe) self.assertEqual(tm.check_compatible(f64, f32), Conversion.unsafe) # Propagated self.assertEqual(tm.check_compatible(i64, f64), Conversion.unsafe) self.assertEqual(tm.check_compatible(f64, i64), Conversion.unsafe) self.assertEqual(tm.check_compatible(i64, f32), Conversion.unsafe) self.assertEqual(tm.check_compatible(i32, f32), Conversion.unsafe) self.assertEqual(tm.check_compatible(f32, i32), Conversion.unsafe) # Test base graph base_test() self.assertIsNone(tm.check_compatible(i64, made_up)) self.assertIsNone(tm.check_compatible(i32, made_up)) self.assertIsNone(tm.check_compatible(f32, made_up)) self.assertIsNone(tm.check_compatible(made_up, f64)) self.assertIsNone(tm.check_compatible(made_up, i64)) # Add new test tcr.promote(f64, made_up) tcr.unsafe(made_up, i32) # Ensure the graph did not change by adding the new type base_test() # To "made up" type self.assertEqual(tm.check_compatible(i64, made_up), Conversion.unsafe) self.assertEqual(tm.check_compatible(i32, made_up), Conversion.safe) self.assertEqual(tm.check_compatible(f32, made_up), Conversion.promote) self.assertEqual(tm.check_compatible(made_up, f64), Conversion.unsafe) self.assertEqual(tm.check_compatible(made_up, i64), Conversion.unsafe)
def test_typeconv(self): tm = TypeManager() i32 = types.int32 i64 = types.int64 f32 = types.float32 tm.set_promote(i32, i64) tm.set_unsafe_convert(i32, f32) sig = (i32, f32) ovs = [ (i32, i32), (f32, f32), ] sel = tm.select_overload(sig, ovs) self.assertEqual(sel, 1)
def test_typeconv(self): tm = TypeManager() i32 = types.int32 i64 = types.int64 f32 = types.float32 tm.set_promote(i32, i64) tm.set_unsafe_convert(i32, f32) sig = (i32, f32) ovs = [ (i32, i32), (f32, f32), (i64, i64), ] # allow_unsafe = True => a conversion from i32 to f32 is chosen sel = tm.select_overload(sig, ovs, True) self.assertEqual(sel, 1) # allow_unsafe = False => no overload available with self.assertRaises(TypeError): sel = tm.select_overload(sig, ovs, False)