| 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 | // bitset<N>::reference operator[](size_t pos); // constexpr since C++23 |
| 10 | |
| 11 | #include <bitset> |
| 12 | #include <cassert> |
| 13 | #include <cstddef> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "../bitset_test_cases.h" |
| 17 | #include "test_macros.h" |
| 18 | |
| 19 | template <std::size_t N> |
| 20 | TEST_CONSTEXPR_CXX23 void test_index() { |
| 21 | std::vector<std::bitset<N> > const cases = get_test_cases<N>(); |
| 22 | for (std::size_t c = 0; c != cases.size(); ++c) { |
| 23 | std::bitset<N> v1 = cases[c]; |
| 24 | if (v1.size() > 0) { |
| 25 | assert(v1[N/2] == v1.test(N/2)); |
| 26 | typename std::bitset<N>::reference r = v1[N/2]; |
| 27 | assert(r == v1.test(N/2)); |
| 28 | typename std::bitset<N>::reference r2 = v1[N/2]; |
| 29 | r = r2; |
| 30 | assert(r == v1.test(N/2)); |
| 31 | r = false; |
| 32 | assert(r == false); |
| 33 | assert(v1.test(N/2) == false); |
| 34 | r = true; |
| 35 | assert(r == true); |
| 36 | assert(v1.test(N/2) == true); |
| 37 | bool b = ~r; |
| 38 | assert(r == true); |
| 39 | assert(v1.test(N/2) == true); |
| 40 | assert(b == false); |
| 41 | r.flip(); |
| 42 | assert(r == false); |
| 43 | assert(v1.test(N/2) == false); |
| 44 | } |
| 45 | ASSERT_SAME_TYPE(decltype(v1[0]), typename std::bitset<N>::reference); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | TEST_CONSTEXPR_CXX23 bool test() { |
| 50 | test_index<0>(); |
| 51 | test_index<1>(); |
| 52 | test_index<31>(); |
| 53 | test_index<32>(); |
| 54 | test_index<33>(); |
| 55 | test_index<63>(); |
| 56 | test_index<64>(); |
| 57 | test_index<65>(); |
| 58 | |
| 59 | std::bitset<1> set; |
| 60 | set[0] = false; |
| 61 | auto b = set[0]; |
| 62 | set[0] = true; |
| 63 | assert(b); |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | int main(int, char**) { |
| 69 | test(); |
| 70 | test_index<1000>(); // not in constexpr because of constexpr evaluation step limits |
| 71 | #if TEST_STD_VER > 20 |
| 72 | static_assert(test()); |
| 73 | #endif |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |