|
| 1 | +from ctypes_dlpack import DLDataTypeCode, DLDeviceType |
| 2 | + |
| 3 | + |
| 4 | +def test_enum_eq(): |
| 5 | + assert DLDeviceType.kDLCPU == 1 |
| 6 | + assert 1 == DLDeviceType.kDLCPU |
| 7 | + assert DLDeviceType.kDLCUDA == 2 |
| 8 | + assert DLDeviceType.kDLCPU != 2 |
| 9 | + |
| 10 | + assert DLDeviceType.kDLCPU == DLDeviceType.kDLCPU |
| 11 | + assert DLDeviceType.kDLCPU != DLDeviceType.kDLCUDA |
| 12 | + |
| 13 | + # Both have value 1, but different enum types — must not compare equal. |
| 14 | + assert DLDeviceType.kDLCPU != DLDataTypeCode.kDLInt |
| 15 | + assert DLDataTypeCode.kDLInt != DLDeviceType.kDLCPU |
| 16 | + |
| 17 | + |
| 18 | +def test_enum_hashable(): |
| 19 | + table = {DLDeviceType.kDLCPU: "cpu", DLDeviceType.kDLCUDA: "cuda"} |
| 20 | + assert table[DLDeviceType.kDLCPU] == "cpu" |
| 21 | + assert table[1] == "cpu" |
| 22 | + assert table[DLDeviceType.kDLCUDA] == "cuda" |
| 23 | + |
| 24 | + s = {DLDeviceType.kDLCPU, DLDeviceType.kDLCUDA, DLDeviceType.kDLCPU} |
| 25 | + assert len(s) == 2 |
| 26 | + |
| 27 | + |
| 28 | +def test_enum_repr(): |
| 29 | + # ``DLDeviceType.kDLCPU`` is a plain ``int`` (class-body assignment); the |
| 30 | + # custom ``__repr__`` only applies to instances, as produced by ctypes |
| 31 | + # when reading a structure field typed as ``DLDeviceType``. |
| 32 | + assert repr(DLDeviceType(1)) == "DLDeviceType.kDLCPU" |
| 33 | + assert repr(DLDataTypeCode(2)) == "DLDataTypeCode.kDLFloat" |
| 34 | + |
| 35 | + unknown = DLDeviceType(99) |
| 36 | + assert repr(unknown) == "DLDeviceType.99" |
| 37 | + |
| 38 | + assert repr(DLDeviceType) == "<Enum DLDeviceType>" |
| 39 | + assert repr(DLDataTypeCode) == "<Enum DLDataTypeCode>" |
0 commit comments