test initialization with a valid name policy dtypepolicymixedfloat16 self assertequalpolicy computedtype float16 self assertequalpolicy variabledtype float32 def testinitializationinvalidnameself test initialization with a nonstring name with self assertraisesregextypeerror name must be a string dtypepolicy123 def testpropertiesmixedfloat16self test properties for mixedbfloat16 policy dtypepolicymixedbfloat16 self assertequalpolicy computedtype bfloat16 self assertequalpolicy variabledtype float32 def testinitializationwithinvalidnamebehaviourself test variabledtype computedtype and name properties policy dtypepolicymixedfloat16 self assertequalpolicy variabledtype float32 self assertequalpolicy computedtype float16 self assertequalpolicy name mixedfloat16 def testreprself test getconfig and fromconfig methods policy dtypepolicymixedfloat16 config policy getconfig self assertequalconfig name mixedfloat16 newpolicy dtypepolicy fromconfigconfig self assertequalnewpolicy name mixedfloat16 class dtypepolicyglobalfunctionstesttestcase testcase def setupself test setdtypepolicy with a valid string setdtypepolicymixedfloat16 policy dtypepolicy self assertequalpolicy name mixedfloat16 def testsetdtypepolicyvalidpolicyself test setdtypepolicy with an invalid input with self assertraisesregexvalueerror invalid policy argument setdtypepolicy12345 def testdtypepolicydefaultself test initialization with an empty name with self assertraisesregexvalueerror cannot convert dtypepolicy def testspecialcharacternameself test initialization with a very long name with self assertraisesregexvalueerror cannot convert dtypepolicymixedfloat16 100 def testalmostvalidnameself reset the global dtype policy before each test setdtypepolicyfloat32 def testsetpolicymultipletimesself test setting the policy to none with self assertraisesregexvalueerror invalid policy argument setdtypepolicynone test initialization with a valid name test initialization with an invalid name test initialization with a non string name test properties for mixed_float16 test properties for mixed_bfloat16 test initialization behavior with an invalid name test variable_dtype compute_dtype and name properties test __repr__ method test get_config and from_config methods reset the global dtype policy before each test test set_dtype_policy with a valid string test set_dtype_policy with a valid dtypepolicy object test set_dtype_policy with an invalid input test dtype_policy default value test initialization with an empty name test initialization with special characters in the name test initialization with a very long name test initialization with a name close to a valid one reset the global dtype policy before each test test setting the policy multiple times in a row test setting the policy to none
from keras.mixed_precision import DTypePolicy
from keras.mixed_precision import dtype_policy
from keras.mixed_precision import set_dtype_policy
from keras.testing import test_case


class DTypePolicyTest(test_case.TestCase):
    def test_initialization_valid_name(self):
        
        policy = DTypePolicy("mixed_float16")
        self.assertEqual(policy.compute_dtype, "float16")
        self.assertEqual(policy.variable_dtype, "float32")

    def test_initialization_invalid_name(self):
        
        with self.assertRaisesRegex(ValueError, "Cannot convert"):
            DTypePolicy("invalid_name")

    def test_initialization_non_string_name(self):
        
        with self.assertRaisesRegex(TypeError, "'name' must be a string"):
            DTypePolicy(123)

    def test_properties_mixed_float16(self):
        
        policy = DTypePolicy("mixed_float16")
        self.assertEqual(policy.compute_dtype, "float16")
        self.assertEqual(policy.variable_dtype, "float32")

    def test_properties_mixed_bfloat16(self):
        
        policy = DTypePolicy("mixed_bfloat16")
        self.assertEqual(policy.compute_dtype, "bfloat16")
        self.assertEqual(policy.variable_dtype, "float32")

    def test_initialization_with_invalid_name_behaviour(self):
        
        with self.assertRaisesRegex(ValueError, "Cannot convert"):
            DTypePolicy("invalid_name")

    def test_properties(self):
        
        policy = DTypePolicy("mixed_float16")
        self.assertEqual(policy.variable_dtype, "float32")
        self.assertEqual(policy.compute_dtype, "float16")
        self.assertEqual(policy.name, "mixed_float16")

    def test_repr(self):
        
        policy = DTypePolicy("mixed_float16")
        self.assertEqual(repr(policy), '<DTypePolicy "mixed_float16">')

    def test_get_config_from_config(self):
        
        policy = DTypePolicy("mixed_float16")
        config = policy.get_config()
        self.assertEqual(config, {"name": "mixed_float16"})

        new_policy = DTypePolicy.from_config(config)
        self.assertEqual(new_policy.name, "mixed_float16")


class DTypePolicyGlobalFunctionsTest(test_case.TestCase):
    def setUp(self):
        
        set_dtype_policy("float32")

    def test_set_dtype_policy_valid_string(self):
        
        set_dtype_policy("mixed_float16")
        policy = dtype_policy()
        self.assertEqual(policy.name, "mixed_float16")

    def test_set_dtype_policy_valid_policy(self):
        
        policy_obj = DTypePolicy("mixed_float16")
        set_dtype_policy(policy_obj)
        policy = dtype_policy()
        self.assertEqual(policy.name, "mixed_float16")

    def test_set_dtype_policy_invalid(self):
        
        with self.assertRaisesRegex(ValueError, "Invalid `policy` argument"):
            set_dtype_policy(12345)

    def test_dtype_policy_default(self):
        
        policy = dtype_policy()
        self.assertEqual(policy.name, "float32")


class DTypePolicyEdgeCasesTest(test_case.TestCase):
    def test_empty_name(self):
        
        with self.assertRaisesRegex(ValueError, "Cannot convert"):
            DTypePolicy("")

    def test_special_character_name(self):
        
        with self.assertRaisesRegex(ValueError, "Cannot convert"):
            DTypePolicy("@mixed_float16!")

    def test_very_long_name(self):
        
        with self.assertRaisesRegex(ValueError, "Cannot convert"):
            DTypePolicy("mixed_float16" * 100)

    def test_almost_valid_name(self):
        
        with self.assertRaisesRegex(ValueError, "Cannot convert"):
            DTypePolicy("mixed_float15")


class DTypePolicyGlobalFunctionsEdgeCasesTest(test_case.TestCase):
    def setUp(self):
        
        set_dtype_policy("float32")

    def test_set_policy_multiple_times(self):
        
        set_dtype_policy("mixed_float16")
        policy = dtype_policy()
        self.assertEqual(policy.name, "mixed_float16")

        set_dtype_policy("float32")
        policy = dtype_policy()
        self.assertEqual(policy.name, "float32")

    def test_set_policy_none(self):
        
        with self.assertRaisesRegex(ValueError, "Invalid `policy` argument"):
            set_dtype_policy(None)