Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Kaido Kert | 788710a | 2023-06-05 07:50:22 -0700 | [diff] [blame] | 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 |
Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Kaido Kert | 788710a | 2023-06-05 07:50:22 -0700 | [diff] [blame] | 9 | // size_t count() const; // constexpr since C++23 |
Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 10 | |
| 11 | #include <bitset> |
Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 12 | #include <cassert> |
Kaido Kert | 788710a | 2023-06-05 07:50:22 -0700 | [diff] [blame] | 13 | #include <cstddef> |
| 14 | #include <vector> |
Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 15 | |
Kaido Kert | 788710a | 2023-06-05 07:50:22 -0700 | [diff] [blame] | 16 | #include "../bitset_test_cases.h" |
Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 17 | #include "test_macros.h" |
| 18 | |
Kaido Kert | 788710a | 2023-06-05 07:50:22 -0700 | [diff] [blame] | 19 | template <std::size_t N> |
| 20 | TEST_CONSTEXPR_CXX23 void test_count() { |
| 21 | std::vector<std::bitset<N> > const cases = get_test_cases<N>(); |
| 22 | for (std::size_t c = 0; c != cases.size(); ++c) { |
| 23 | const std::bitset<N> v = cases[c]; |
| 24 | std::size_t c1 = v.count(); |
| 25 | std::size_t c2 = 0; |
| 26 | for (std::size_t i = 0; i < v.size(); ++i) |
| 27 | if (v[i]) |
| 28 | ++c2; |
| 29 | assert(c1 == c2); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | TEST_CONSTEXPR_CXX23 bool test() { |
| 34 | test_count<0>(); |
| 35 | test_count<1>(); |
| 36 | test_count<31>(); |
| 37 | test_count<32>(); |
| 38 | test_count<33>(); |
| 39 | test_count<63>(); |
| 40 | test_count<64>(); |
| 41 | test_count<65>(); |
| 42 | |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | int main(int, char**) { |
| 47 | test(); |
| 48 | test_count<1000>(); // not in constexpr because of constexpr evaluation step limits |
| 49 | #if TEST_STD_VER > 20 |
| 50 | static_assert(test()); |
Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 51 | #endif |
| 52 | |
Kaido Kert | 788710a | 2023-06-05 07:50:22 -0700 | [diff] [blame] | 53 | return 0; |
Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 54 | } |