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// <iterator>
10
11// reverse_iterator
12
13// requires RandomAccessIterator<Iter>
14// unspecified operator[](difference_type n) const; // constexpr since C++17
15
16#include <iterator>
17#include <cassert>
18
19#include "test_macros.h"
20#include "test_iterators.h"
21
22template <class It>
23TEST_CONSTEXPR_CXX17 void test(It i,
24 typename std::iterator_traits<It>::difference_type n,
25 typename std::iterator_traits<It>::value_type x) {
26 typedef typename std::iterator_traits<It>::value_type value_type;
27 const std::reverse_iterator<It> r(i);
28 value_type rr = r[n];
29 assert(rr == x);
30}
31
32TEST_CONSTEXPR_CXX17 bool tests() {
33 const char* s = "1234567890";
34 test(random_access_iterator<const char*>(s+5), 4, '1');
35 test(random_access_iterator<const char*>(s+5), 0, '5');
36#if TEST_STD_VER >= 20
37 test(cpp20_random_access_iterator<const char*>(s + 5), 4, '1');
38 test(cpp20_random_access_iterator<const char*>(s + 5), 0, '5');
39#endif
40 test(s+5, 4, '1');
41 test(s+5, 0, '5');
42 return true;
43}
44
45int main(int, char**) {
46 tests();
47#if TEST_STD_VER > 14
48 static_assert(tests(), "");
49#endif
50 return 0;
51}
52

source code of libcxx/test/std/iterators/predef.iterators/reverse.iterators/reverse.iter.elem/bracket.pass.cpp