| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | // 2010-10-11 Paolo Carlini <[email protected]> |
| 5 | |
| 6 | // Copyright (C) 2010-2025 Free Software Foundation, Inc. |
| 7 | // |
| 8 | // This file is part of the GNU ISO C++ Library. This library is free |
| 9 | // software; you can redistribute it and/or modify it under the |
| 10 | // terms of the GNU General Public License as published by the |
| 11 | // Free Software Foundation; either version 3, or (at your option) |
| 12 | // any later version. |
| 13 | |
| 14 | // This library is distributed in the hope that it will be useful, |
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | // GNU General Public License for more details. |
| 18 | |
| 19 | // You should have received a copy of the GNU General Public License along |
| 20 | // with this library; see the file COPYING3. If not see |
| 21 | // <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | #include <bitset> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | template<std::size_t Nb, typename CharT> |
| 27 | std::bitset<Nb> |
| 28 | test01_ref(const CharT* str, |
| 29 | typename std::basic_string<CharT>::size_type n |
| 30 | = std::basic_string<CharT>::npos, |
| 31 | CharT zero = CharT('0'), CharT one = CharT('1')) |
| 32 | { |
| 33 | return std::bitset<Nb>(n == std::basic_string<CharT>::npos |
| 34 | ? std::basic_string<CharT>(str) |
| 35 | : std::basic_string<CharT>(str, n), |
| 36 | 0, n, zero, one); |
| 37 | } |
| 38 | |
| 39 | // DR 1325. |
| 40 | void test01() |
| 41 | { |
| 42 | using namespace std; |
| 43 | using std::bitset; // Work around struct ::bitset on rtems. |
| 44 | |
| 45 | const char s1[4] = { '0', '1', '0', '1' }; |
| 46 | VERIFY( bitset<4>(s1, 4) == test01_ref<4>(s1, 4) ); |
| 47 | |
| 48 | const char s2[3] = { '1', '1', '0' }; |
| 49 | VERIFY( bitset<6>(s2, 3) == test01_ref<6>(s2, 3) ); |
| 50 | |
| 51 | const char* s3 = "1110110"; |
| 52 | VERIFY( bitset<7>(s3) == test01_ref<7>(s3) ); |
| 53 | |
| 54 | const char* s4 = "0011"; |
| 55 | VERIFY( bitset<10>(s4) == test01_ref<10>(s4) ); |
| 56 | |
| 57 | const char* s5 = "011110000111001"; |
| 58 | VERIFY( bitset<5>(s5) == test01_ref<5>(s5) ); |
| 59 | |
| 60 | const char* s6 = "1cc1c1"; |
| 61 | VERIFY( bitset<6>(s6, basic_string<char>::npos, 'c') |
| 62 | == test01_ref<6>(s6, basic_string<char>::npos, 'c') ); |
| 63 | |
| 64 | const char* s7 = "001011101"; |
| 65 | VERIFY( bitset<9>(s7, basic_string<char>::npos, '0', '1') |
| 66 | == test01_ref<9>(s7, basic_string<char>::npos, '0', '1') ); |
| 67 | |
| 68 | const char* s8 = "babb"; |
| 69 | VERIFY( bitset<4>(s8, basic_string<char>::npos, 'a', 'b') |
| 70 | == test01_ref<4>(s8, basic_string<char>::npos, 'a', 'b') ); |
| 71 | |
| 72 | const char* s9 = "bbabbbaaa"; |
| 73 | VERIFY( bitset<100>(s9, basic_string<char>::npos, 'a', 'b') |
| 74 | == test01_ref<100>(s9, basic_string<char>::npos, 'a', 'b') ); |
| 75 | } |
| 76 | |
| 77 | int main() |
| 78 | { |
| 79 | test01(); |
| 80 | return 0; |
| 81 | } |