|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include <__utility/is_valid_range.h> |
| 10 | +#include <cassert> |
| 11 | + |
| 12 | +#include "test_macros.h" |
| 13 | + |
| 14 | +template <class T, class TQualified> |
| 15 | +TEST_CONSTEXPR_CXX14 void check_type() { |
| 16 | + { |
| 17 | + // We need to ensure that the addresses of i and j are ordered as &i < &j for |
| 18 | + // the test below to work portably, so we define them in a struct. |
| 19 | + struct { |
| 20 | + T i = 0; |
| 21 | + T j = 0; |
| 22 | + } storage; |
| 23 | + assert(std::__is_valid_range(static_cast<TQualified*>(&storage.i), static_cast<TQualified*>(&storage.i))); |
| 24 | + assert(std::__is_valid_range(static_cast<TQualified*>(&storage.i), static_cast<TQualified*>(&storage.i + 1))); |
| 25 | + |
| 26 | + assert(!std::__is_valid_range(static_cast<TQualified*>(&storage.j), static_cast<TQualified*>(&storage.i))); |
| 27 | + assert(!std::__is_valid_range(static_cast<TQualified*>(&storage.i + 1), static_cast<TQualified*>(&storage.i))); |
| 28 | + |
| 29 | + // We detect this as being a valid range even though it is not really valid. |
| 30 | + assert(std::__is_valid_range(static_cast<TQualified*>(&storage.i), static_cast<TQualified*>(&storage.j))); |
| 31 | + } |
| 32 | + |
| 33 | + { |
| 34 | + T arr[3] = {1, 2, 3}; |
| 35 | + assert(std::__is_valid_range(static_cast<TQualified*>(&arr[0]), static_cast<TQualified*>(&arr[0]))); |
| 36 | + assert(std::__is_valid_range(static_cast<TQualified*>(&arr[0]), static_cast<TQualified*>(&arr[1]))); |
| 37 | + assert(std::__is_valid_range(static_cast<TQualified*>(&arr[0]), static_cast<TQualified*>(&arr[2]))); |
| 38 | + |
| 39 | + assert(!std::__is_valid_range(static_cast<TQualified*>(&arr[1]), static_cast<TQualified*>(&arr[0]))); |
| 40 | + assert(!std::__is_valid_range(static_cast<TQualified*>(&arr[2]), static_cast<TQualified*>(&arr[0]))); |
| 41 | + } |
| 42 | + |
| 43 | +#if TEST_STD_VER >= 20 |
| 44 | + { |
| 45 | + T* arr = new int[4]{1, 2, 3, 4}; |
| 46 | + assert(std::__is_valid_range(static_cast<TQualified*>(arr), static_cast<TQualified*>(arr + 4))); |
| 47 | + delete[] arr; |
| 48 | + } |
| 49 | +#endif |
| 50 | +} |
| 51 | + |
| 52 | +TEST_CONSTEXPR_CXX14 bool test() { |
| 53 | + check_type<int, int>(); |
| 54 | + check_type<int, int const>(); |
| 55 | + check_type<int, int volatile>(); |
| 56 | + check_type<int, int const volatile>(); |
| 57 | + |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +int main(int, char**) { |
| 62 | + test(); |
| 63 | +#if TEST_STD_VER >= 14 |
| 64 | + static_assert(test(), ""); |
| 65 | +#endif |
| 66 | + |
| 67 | + return 0; |
| 68 | +} |
0 commit comments