Home | History | Annotate | Download | only in bitset.members
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // test:
     11 
     12 // template <class charT, class traits, class Allocator>
     13 // basic_string<charT, traits, Allocator>
     14 // to_string(charT zero = charT('0'), charT one = charT('1')) const;
     15 //
     16 // template <class charT, class traits>
     17 // basic_string<charT, traits, allocator<charT> > to_string() const;
     18 //
     19 // template <class charT>
     20 // basic_string<charT, char_traits<charT>, allocator<charT> > to_string() const;
     21 //
     22 // basic_string<char, char_traits<char>, allocator<char> > to_string() const;
     23 
     24 #include <bitset>
     25 #include <string>
     26 #include <cstdlib>
     27 #include <cassert>
     28 
     29 #include "test_macros.h"
     30 
     31 #if defined(TEST_COMPILER_CLANG)
     32 #pragma clang diagnostic ignored "-Wtautological-compare"
     33 #elif defined(TEST_COMPILER_C1XX)
     34 #pragma warning(disable: 6294) // Ill-defined for-loop:  initial condition does not satisfy test.  Loop body not executed.
     35 #endif
     36 
     37 template <std::size_t N>
     38 std::bitset<N>
     39 make_bitset()
     40 {
     41     std::bitset<N> v;
     42     for (std::size_t i = 0; i < N; ++i)
     43         v[i] = static_cast<bool>(std::rand() & 1);
     44     return v;
     45 }
     46 
     47 template <std::size_t N>
     48 void test_to_string()
     49 {
     50 {
     51     std::bitset<N> v = make_bitset<N>();
     52     {
     53     std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >();
     54     for (std::size_t i = 0; i < N; ++i)
     55         if (v[i])
     56             assert(s[N - 1 - i] == '1');
     57         else
     58             assert(s[N - 1 - i] == '0');
     59     }
     60     {
     61     std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >();
     62     for (std::size_t i = 0; i < N; ++i)
     63         if (v[i])
     64             assert(s[N - 1 - i] == '1');
     65         else
     66             assert(s[N - 1 - i] == '0');
     67     }
     68     {
     69     std::string s = v.template to_string<char>();
     70     for (std::size_t i = 0; i < N; ++i)
     71         if (v[i])
     72             assert(s[N - 1 - i] == '1');
     73         else
     74             assert(s[N - 1 - i] == '0');
     75     }
     76     {
     77     std::string s = v.to_string();
     78     for (std::size_t i = 0; i < N; ++i)
     79         if (v[i])
     80             assert(s[N - 1 - i] == '1');
     81         else
     82             assert(s[N - 1 - i] == '0');
     83     }
     84 }
     85 {
     86     std::bitset<N> v = make_bitset<N>();
     87     {
     88     std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0');
     89     for (std::size_t i = 0; i < N; ++i)
     90         if (v[i])
     91             assert(s[N - 1 - i] == '1');
     92         else
     93             assert(s[N - 1 - i] == '0');
     94     }
     95     {
     96     std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0');
     97     for (std::size_t i = 0; i < N; ++i)
     98         if (v[i])
     99             assert(s[N - 1 - i] == '1');
    100         else
    101             assert(s[N - 1 - i] == '0');
    102     }
    103     {
    104     std::string s = v.template to_string<char>('0');
    105     for (std::size_t i = 0; i < N; ++i)
    106         if (v[i])
    107             assert(s[N - 1 - i] == '1');
    108         else
    109             assert(s[N - 1 - i] == '0');
    110     }
    111     {
    112     std::string s = v.to_string('0');
    113     for (std::size_t i = 0; i < N; ++i)
    114         if (v[i])
    115             assert(s[N - 1 - i] == '1');
    116         else
    117             assert(s[N - 1 - i] == '0');
    118     }
    119 }
    120 {
    121     std::bitset<N> v = make_bitset<N>();
    122     {
    123     std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0', '1');
    124     for (std::size_t i = 0; i < N; ++i)
    125         if (v[i])
    126             assert(s[N - 1 - i] == '1');
    127         else
    128             assert(s[N - 1 - i] == '0');
    129     }
    130     {
    131     std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0', '1');
    132     for (std::size_t i = 0; i < N; ++i)
    133         if (v[i])
    134             assert(s[N - 1 - i] == '1');
    135         else
    136             assert(s[N - 1 - i] == '0');
    137     }
    138     {
    139     std::string s = v.template to_string<char>('0', '1');
    140     for (std::size_t i = 0; i < N; ++i)
    141         if (v[i])
    142             assert(s[N - 1 - i] == '1');
    143         else
    144             assert(s[N - 1 - i] == '0');
    145     }
    146     {
    147     std::string s = v.to_string('0', '1');
    148     for (std::size_t i = 0; i < N; ++i)
    149         if (v[i])
    150             assert(s[N - 1 - i] == '1');
    151         else
    152             assert(s[N - 1 - i] == '0');
    153     }
    154 }
    155 }
    156 
    157 int main()
    158 {
    159     test_to_string<0>();
    160     test_to_string<1>();
    161     test_to_string<31>();
    162     test_to_string<32>();
    163     test_to_string<33>();
    164     test_to_string<63>();
    165     test_to_string<64>();
    166     test_to_string<65>();
    167     test_to_string<1000>();
    168 }
    169