libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

put_money.pass.cpp (2550B)


      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 // <iomanip>
     11 
     12 // template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false);
     13 
     14 // REQUIRES: locale.en_US.UTF-8
     15 
     16 #include <iomanip>
     17 #include <ostream>
     18 #include <cassert>
     19 
     20 #include "platform_support.h" // locale name macros
     21 
     22 template <class CharT>
     23 class testbuf
     24     : public std::basic_streambuf<CharT>
     25 {
     26     typedef std::basic_streambuf<CharT> base;
     27     std::basic_string<CharT> str_;
     28 public:
     29     testbuf()
     30     {
     31     }
     32 
     33     std::basic_string<CharT> str() const
     34         {return std::basic_string<CharT>(base::pbase(), base::pptr());}
     35 
     36 protected:
     37 
     38     virtual typename base::int_type
     39         overflow(typename base::int_type ch = base::traits_type::eof())
     40         {
     41             if (ch != base::traits_type::eof())
     42             {
     43                 int n = str_.size();
     44                 str_.push_back(static_cast<CharT>(ch));
     45                 str_.resize(str_.capacity());
     46                 base::setp(const_cast<CharT*>(str_.data()),
     47                            const_cast<CharT*>(str_.data() + str_.size()));
     48                 base::pbump(n+1);
     49             }
     50             return ch;
     51         }
     52 };
     53 
     54 int main()
     55 {
     56     {
     57         testbuf<char> sb;
     58         std::ostream os(&sb);
     59         os.imbue(std::locale(LOCALE_en_US_UTF_8));
     60         showbase(os);
     61         long double x = -123456789;
     62         os << std::put_money(x, false);
     63         assert(sb.str() == "-$1,234,567.89");
     64     }
     65     {
     66         testbuf<char> sb;
     67         std::ostream os(&sb);
     68         os.imbue(std::locale(LOCALE_en_US_UTF_8));
     69         showbase(os);
     70         long double x = -123456789;
     71         os << std::put_money(x, true);
     72         assert(sb.str() == "-USD 1,234,567.89");
     73     }
     74     {
     75         testbuf<wchar_t> sb;
     76         std::wostream os(&sb);
     77         os.imbue(std::locale(LOCALE_en_US_UTF_8));
     78         showbase(os);
     79         long double x = -123456789;
     80         os << std::put_money(x, false);
     81         assert(sb.str() == L"-$1,234,567.89");
     82     }
     83     {
     84         testbuf<wchar_t> sb;
     85         std::wostream os(&sb);
     86         os.imbue(std::locale(LOCALE_en_US_UTF_8));
     87         showbase(os);
     88         long double x = -123456789;
     89         os << std::put_money(x, true);
     90         assert(sb.str() == L"-USD 1,234,567.89");
     91     }
     92 }