brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · ef645d4 Raw
58 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include <__cxx03/__utility/is_valid_range.h>10#include <cassert>11 12#include "test_macros.h"13 14template <class T, class TQualified>15TEST_CONSTEXPR_CXX14 void check_type() {16  {17    // We need to ensure that the addresses of i and j are ordered as &i < &j for18    // 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 44TEST_CONSTEXPR_CXX14 bool test() {45  check_type<int, int>();46  check_type<int, int const>();47  check_type<int, int volatile>();48  check_type<int, int const volatile>();49 50  return true;51}52 53int main(int, char**) {54  test();55 56  return 0;57}58