brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · 3ed8cd7 Raw
73 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// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++2310 11// <mdspan>12 13// constexpr typename offset_policy::data_handle_type offset(data_handle_type p, size_t i) const noexcept;14//15// Effects: Equivalent to: return assume_aligned<byte_alignment>(p) + i;16 17#include <mdspan>18#include <cassert>19#include <cstddef>20#include <concepts>21#include <type_traits>22 23#include "test_macros.h"24 25// We are not using MinimalElementType.h because MinimalElementType is not26// default constructible and uninitialized storage does not work in constexpr.27 28// Same as MinimalElementType but with a defaulted default constructor29struct MyMinimalElementType {30  int val;31  constexpr MyMinimalElementType()                            = default;32  constexpr MyMinimalElementType(const MyMinimalElementType&) = delete;33  constexpr explicit MyMinimalElementType(int v) noexcept : val(v) {}34  constexpr MyMinimalElementType& operator=(const MyMinimalElementType&) = delete;35};36 37template <class T, std::size_t N>38constexpr void test_offset() {39  constexpr std::size_t Sz = 10;40  alignas(N) T data[Sz]{};41  T* ptr = &data[0];42  std::aligned_accessor<T, N> acc;43  for (std::size_t i = 0; i < Sz; ++i) {44    std::same_as<typename std::default_accessor<T>::data_handle_type> decltype(auto) x = acc.offset(ptr, i);45    ASSERT_NOEXCEPT(acc.offset(ptr, i));46    assert(x == ptr + i);47  }48}49 50template <class T>51constexpr void test_it() {52  constexpr std::size_t N = alignof(T);53  test_offset<T, N>();54  test_offset<T, 2 * N>();55  test_offset<T, 4 * N>();56  test_offset<T, 8 * N>();57  test_offset<T, 16 * N>();58}59 60constexpr bool test() {61  test_it<int>();62  test_it<const int>();63  test_it<MyMinimalElementType>();64  test_it<const MyMinimalElementType>();65  return true;66}67 68int main(int, char**) {69  test();70  static_assert(test());71  return 0;72}73