File: begin-end.array.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (96 lines) | stat: -rw-r--r-- 2,845 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03

// <iterator>
//
// template <class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
// template <class T, size_t N> constexpr T* end(T (&array)[N]) noexcept;
//
// template <class T, size_t N> constexpr reverse_iterator<T*> rbegin(T (&array)[N]);      // C++14, constexpr since C++17
// template <class T, size_t N> constexpr reverse_iterator<T*> rend(T (&array)[N]);        // C++14, constexpr since C++17

#include <cassert>
#include <iterator>

#include "test_macros.h"

TEST_CONSTEXPR_CXX14 bool test() {
  int a[]        = {1, 2, 3};
  const auto& ca = a;

  // std::begin(T (&)[N]) / std::end(T (&)[N])
  {
    ASSERT_NOEXCEPT(std::begin(a));
    ASSERT_SAME_TYPE(decltype(std::begin(a)), int*);
    assert(std::begin(a) == a);

    ASSERT_NOEXCEPT(std::end(a));
    ASSERT_SAME_TYPE(decltype(std::end(a)), int*);
    assert(std::end(a) == a + 3);

    // kind of overkill since it follows from the definition, but worth testing
    ASSERT_NOEXCEPT(std::begin(ca));
    ASSERT_SAME_TYPE(decltype(std::begin(ca)), const int*);
    assert(std::begin(ca) == ca);

    ASSERT_NOEXCEPT(std::end(ca));
    ASSERT_SAME_TYPE(decltype(std::end(ca)), const int*);
    assert(std::end(ca) == ca + 3);
  }

  return true;
}

TEST_CONSTEXPR_CXX17 bool test_r() {
#if TEST_STD_VER >= 14
  int a[]        = {1, 2, 3};
  const auto& ca = a;

  // std::rbegin(T (&)[N]) / std::rend(T (&)[N])
  {
    ASSERT_SAME_TYPE(decltype(std::rbegin(a)), std::reverse_iterator<int*>);
    assert(std::rbegin(a).base() == a + 3);

    ASSERT_SAME_TYPE(decltype(std::rend(a)), std::reverse_iterator<int*>);
    assert(std::rend(a).base() == a);

    // kind of overkill since it follows from the definition, but worth testing
    ASSERT_SAME_TYPE(decltype(std::rbegin(ca)), std::reverse_iterator<const int*>);
    assert(std::rbegin(ca).base() == ca + 3);

    ASSERT_SAME_TYPE(decltype(std::rend(ca)), std::reverse_iterator<const int*>);
    assert(std::rend(ca).base() == ca);
  }
#endif

  return true;
}

int main(int, char**) {
  test();
  test_r();
#if TEST_STD_VER >= 14
  static_assert(test(), "");
#endif
#if TEST_STD_VER >= 17
  static_assert(test_r(), "");
#endif

  // Make sure std::begin(T (&)[N]) and std::end(T (&)[N]) are constexpr in C++11 too (see LWG2280).
  {
    static constexpr int a[] = {1, 2, 3};
    constexpr auto b         = std::begin(a);
    assert(b == a);
    constexpr auto e = std::end(a);
    assert(e == a + 3);
  }

  return 0;
}