|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Copyright (c) 2025 Microsoft Corporation. All rights reserved. |
| 4 | +// |
| 5 | +// This code is licensed under the MIT License (MIT). |
| 6 | +// |
| 7 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 13 | +// THE SOFTWARE. |
| 14 | +// |
| 15 | +/////////////////////////////////////////////////////////////////////////////// |
| 16 | + |
| 17 | +#include <gsl/pointers> // for not_null |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +#include <type_traits> // for declval |
| 21 | + |
| 22 | +using namespace gsl; |
| 23 | + |
| 24 | +namespace |
| 25 | +{ |
| 26 | +constexpr bool comparison_test(const int* ptr1, const int* ptr2) |
| 27 | +{ |
| 28 | + const not_null<const int*> p1(ptr1); |
| 29 | + const not_null<const int*> p1_same(ptr1); |
| 30 | + const not_null<const int*> p2(ptr2); |
| 31 | + |
| 32 | + // Testing operator== |
| 33 | + const bool eq_result = (p1 == p1_same); // Should be true |
| 34 | + const bool neq_result = (p1 != p2); // Should be true |
| 35 | + |
| 36 | + // Testing operator<= and operator>= |
| 37 | + const bool le_result = (p1 <= p1_same); // Should be true |
| 38 | + const bool ge_result = (p1 >= p1_same); // Should be true |
| 39 | + |
| 40 | + // The exact comparison results will depend on pointer ordering, |
| 41 | + // but we can verify that the basic equality checks work as expected |
| 42 | + return eq_result && neq_result && le_result && ge_result; |
| 43 | +} |
| 44 | + |
| 45 | +constexpr bool workaround_test(const int* ptr1, const int* ptr2) |
| 46 | +{ |
| 47 | + const not_null<const int*> p1(ptr1); |
| 48 | + const not_null<const int*> p1_same(ptr1); |
| 49 | + const not_null<const int*> p2(ptr2); |
| 50 | + |
| 51 | + // Using .get() to compare |
| 52 | + const bool eq_result = (p1.get() == p1_same.get()); // Should be true |
| 53 | + const bool neq_result = (p1.get() != p2.get()); // Should be true |
| 54 | + |
| 55 | + return eq_result && neq_result; |
| 56 | +} |
| 57 | +} // namespace |
| 58 | + |
| 59 | +constexpr int test_value1 = 1; |
| 60 | +constexpr int test_value2 = 2; |
| 61 | + |
| 62 | +static_assert(comparison_test(&test_value1, &test_value2), "not_null comparison operators should be constexpr"); |
| 63 | +static_assert(workaround_test(&test_value1, &test_value2), "not_null .get() comparison workaround should work"); |
| 64 | + |
| 65 | +TEST(notnull_constexpr_tests, TestNotNullConstexprComparison) |
| 66 | +{ |
| 67 | + // This test simply verifies that the constexpr functions compile and run |
| 68 | + // If we got here, it means the constexpr comparison operators are working |
| 69 | + static const int value1 = 1; |
| 70 | + static const int value2 = 2; |
| 71 | + EXPECT_TRUE(comparison_test(&value1, &value2)); |
| 72 | + EXPECT_TRUE(workaround_test(&value1, &value2)); |
| 73 | +} |
| 74 | + |
0 commit comments