123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- //===----------------------------------------------------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is dual licensed under the MIT and the University of Illinois Open
- // Source Licenses. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- // test:
- // template <class charT, class traits, class Allocator>
- // basic_string<charT, traits, Allocator>
- // to_string(charT zero = charT('0'), charT one = charT('1')) const;
- //
- // template <class charT, class traits>
- // basic_string<charT, traits, allocator<charT> > to_string() const;
- //
- // template <class charT>
- // basic_string<charT, char_traits<charT>, allocator<charT> > to_string() const;
- //
- // basic_string<char, char_traits<char>, allocator<char> > to_string() const;
- #include <bitset>
- #include <string>
- #include <cstdlib>
- #include <cassert>
- #pragma clang diagnostic ignored "-Wtautological-compare"
- template <std::size_t N>
- std::bitset<N>
- make_bitset()
- {
- std::bitset<N> v;
- for (std::size_t i = 0; i < N; ++i)
- v[i] = static_cast<bool>(std::rand() & 1);
- return v;
- }
- template <std::size_t N>
- void test_to_string()
- {
- {
- std::bitset<N> v = make_bitset<N>();
- {
- std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >();
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- {
- std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >();
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- {
- std::string s = v.template to_string<char>();
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- {
- std::string s = v.to_string();
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- }
- {
- std::bitset<N> v = make_bitset<N>();
- {
- std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0');
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- {
- std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0');
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- {
- std::string s = v.template to_string<char>('0');
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- {
- std::string s = v.to_string('0');
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- }
- {
- std::bitset<N> v = make_bitset<N>();
- {
- std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0', '1');
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- {
- std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0', '1');
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- {
- std::string s = v.template to_string<char>('0', '1');
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- {
- std::string s = v.to_string('0', '1');
- for (std::size_t i = 0; i < N; ++i)
- if (v[i])
- assert(s[N - 1 - i] == '1');
- else
- assert(s[N - 1 - i] == '0');
- }
- }
- }
- int main()
- {
- test_to_string<0>();
- test_to_string<1>();
- test_to_string<31>();
- test_to_string<32>();
- test_to_string<33>();
- test_to_string<63>();
- test_to_string<64>();
- test_to_string<65>();
- test_to_string<1000>();
- }
|