|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +""" |
| 5 | +Tests for __eq__ and __ne__ implementations in cuda.core classes. |
| 6 | +
|
| 7 | +These tests verify multi-type equality behavior and subclassing equality behavior |
| 8 | +across Device, Stream, Event, and Context objects. |
| 9 | +""" |
| 10 | + |
| 11 | +from cuda.core.experimental import Device, Stream |
| 12 | +from cuda.core.experimental._context import Context |
| 13 | +from cuda.core.experimental._event import Event, EventOptions |
| 14 | +from cuda.core.experimental._stream import StreamOptions |
| 15 | + |
| 16 | +# ============================================================================ |
| 17 | +# Equality Contract Tests |
| 18 | +# ============================================================================ |
| 19 | + |
| 20 | + |
| 21 | +def test_equality_is_not_identity(): |
| 22 | + """Test that equality (==) is different from identity (is).""" |
| 23 | + device = Device(0) |
| 24 | + device.set_current() |
| 25 | + |
| 26 | + # Streams: Different objects can be equal |
| 27 | + s1 = device.create_stream() |
| 28 | + s2 = Stream.from_handle(int(s1.handle)) |
| 29 | + |
| 30 | + assert s1 == s2, "Streams with same handle are equal" |
| 31 | + assert s1 is not s2, "But they are not the same object" |
| 32 | + |
| 33 | + # Device: Same object due to singleton (special case) |
| 34 | + d1 = Device(0) |
| 35 | + d2 = Device(0) |
| 36 | + |
| 37 | + assert d1 == d2, "Devices with same ID are equal" |
| 38 | + assert d1 is d2, "And they ARE the same object (singleton)" |
| 39 | + |
| 40 | + |
| 41 | +# ============================================================================ |
| 42 | +# Subclassing Equality Tests |
| 43 | +# ============================================================================ |
| 44 | + |
| 45 | + |
| 46 | +def test_device_subclass_equality(init_cuda): |
| 47 | + """Test Device subclass equality behavior. |
| 48 | +
|
| 49 | + Device uses a singleton pattern where Device(0) always returns the same |
| 50 | + cached instance. This means subclassing Device doesn't create new instances; |
| 51 | + MyDevice(0) returns the original Device(0) instance from the cache. |
| 52 | + """ |
| 53 | + |
| 54 | + class MyDevice(Device): |
| 55 | + pass |
| 56 | + |
| 57 | + device = Device(0) |
| 58 | + device.set_current() |
| 59 | + my_device = MyDevice(0) |
| 60 | + |
| 61 | + # Due to singleton pattern, both return the exact same instance |
| 62 | + assert device is my_device, "Device singleton returns same instance for same device_id" |
| 63 | + assert type(device) is Device, "Singleton returns original Device type, not subclass" |
| 64 | + assert type(my_device) is Device, "Even MyDevice(0) returns Device instance due to singleton" |
| 65 | + |
| 66 | + # Since they're the same object, they're equal |
| 67 | + assert device == my_device |
| 68 | + |
| 69 | + |
| 70 | +def test_stream_subclass_equality(init_cuda): |
| 71 | + """Test Stream subclass equality behavior. |
| 72 | +
|
| 73 | + Stream uses isinstance() for equality checking, which means a Stream instance |
| 74 | + and a MyStream subclass instance wrapping the same handle will compare equal. |
| 75 | + """ |
| 76 | + |
| 77 | + class MyStream(Stream): |
| 78 | + pass |
| 79 | + |
| 80 | + device = Device(0) |
| 81 | + device.set_current() |
| 82 | + |
| 83 | + # Create base Stream instance |
| 84 | + stream = Stream._init(options=StreamOptions(), device_id=device.device_id) |
| 85 | + |
| 86 | + # Create another Stream wrapping same handle |
| 87 | + stream2 = Stream.from_handle(int(stream.handle)) |
| 88 | + assert stream == stream2, "Streams wrapping same handle are equal" |
| 89 | + |
| 90 | + # Create subclass instance with different handle |
| 91 | + my_stream = MyStream._init(options=StreamOptions(), device_id=device.device_id) |
| 92 | + |
| 93 | + # Different handles -> not equal |
| 94 | + assert stream != my_stream, "Streams with different handles are not equal" |
| 95 | + assert stream.handle != my_stream.handle |
| 96 | + |
| 97 | + # sanity check: base and subclass compare equal (and hash equal) |
| 98 | + stream_from_handle = MyStream.from_handle(int(my_stream.handle)) |
| 99 | + assert my_stream == stream_from_handle, "MyStream and Stream wrapping same handle compare equal" |
| 100 | + assert hash(my_stream) == hash(stream_from_handle) |
| 101 | + |
| 102 | + |
| 103 | +def test_event_subclass_equality(init_cuda): |
| 104 | + """Test Event subclass equality behavior. |
| 105 | +
|
| 106 | + Event uses isinstance() for equality checking, similar to Stream. |
| 107 | + """ |
| 108 | + |
| 109 | + class MyEvent(Event): |
| 110 | + pass |
| 111 | + |
| 112 | + device = Device(0) |
| 113 | + device.set_current() |
| 114 | + |
| 115 | + # Create two different events |
| 116 | + event = Event._init(device.device_id, device.context, options=EventOptions()) |
| 117 | + my_event = MyEvent._init(device.device_id, device.context, options=EventOptions()) |
| 118 | + |
| 119 | + # Different events should not be equal (different handles) |
| 120 | + assert event != my_event, "Different Event instances are not equal" |
| 121 | + |
| 122 | + # Same subclass type with different handles |
| 123 | + my_event2 = MyEvent._init(device.device_id, device.context, options=EventOptions()) |
| 124 | + assert my_event != my_event2, "Different MyEvent instances are not equal" |
| 125 | + |
| 126 | + |
| 127 | +def test_context_subclass_equality(init_cuda): |
| 128 | + """Test Context subclass equality behavior.""" |
| 129 | + |
| 130 | + class MyContext(Context): |
| 131 | + pass |
| 132 | + |
| 133 | + device = Device(0) |
| 134 | + device.set_current() |
| 135 | + stream = device.create_stream() |
| 136 | + context = stream.context |
| 137 | + |
| 138 | + # MyContext._from_ctx() returns a Context instance, not MyContext |
| 139 | + my_context = MyContext._from_ctx(context._handle, device.device_id) |
| 140 | + assert type(my_context) is Context, "_from_ctx returns Context, not subclass" |
| 141 | + assert type(my_context) is not MyContext |
| 142 | + |
| 143 | + # Since both are Context instances with same handle, they're equal |
| 144 | + assert context == my_context, "Context instances with same handle are equal" |
| 145 | + |
| 146 | + # Create another context from different stream |
| 147 | + stream2 = device.create_stream() |
| 148 | + context2 = stream2.context |
| 149 | + |
| 150 | + # Same device, same primary context, should be equal |
| 151 | + assert context == context2, "Contexts from same device are equal" |
| 152 | + |
| 153 | + |
| 154 | +def test_subclass_type_safety(init_cuda): |
| 155 | + """Test that equality checks with incompatible types return False or NotImplemented.""" |
| 156 | + device = Device(0) |
| 157 | + device.set_current() |
| 158 | + |
| 159 | + stream = device.create_stream() |
| 160 | + event = stream.record() |
| 161 | + context = stream.context |
| 162 | + |
| 163 | + # None of these should be equal to each other |
| 164 | + assert device != stream |
| 165 | + assert device != event |
| 166 | + assert device != context |
| 167 | + assert stream != event |
| 168 | + assert stream != context |
| 169 | + assert event != context |
| 170 | + |
| 171 | + # None should be equal to arbitrary types |
| 172 | + assert device != "device" |
| 173 | + assert stream != 123 |
| 174 | + assert event != [] |
| 175 | + assert context != {"key": "value"} |
0 commit comments