Home | History | Annotate | Download | only in ext.manip
      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 #include <iomanip>
     15 #include <cassert>
     16 
     17 #include "platform_support.h" // locale name macros
     18 
     19 template <class CharT>
     20 class testbuf
     21     : public std::basic_streambuf<CharT>
     22 {
     23     typedef std::basic_streambuf<CharT> base;
     24     std::basic_string<CharT> str_;
     25 public:
     26     testbuf()
     27     {
     28     }
     29 
     30     std::basic_string<CharT> str() const
     31         {return std::basic_string<CharT>(base::pbase(), base::pptr());}
     32 
     33 protected:
     34 
     35     virtual typename base::int_type
     36         overflow(typename base::int_type __c = base::traits_type::eof())
     37         {
     38             if (__c != base::traits_type::eof())
     39             {
     40                 int n = str_.size();
     41                 str_.push_back(__c);
     42                 str_.resize(str_.capacity());
     43                 base::setp(const_cast<CharT*>(str_.data()),
     44                            const_cast<CharT*>(str_.data() + str_.size()));
     45                 base::pbump(n+1);
     46             }
     47             return __c;
     48         }
     49 };
     50 
     51 int main()
     52 {
     53 #if !defined(__ANDROID__)
     54     // Remove tests setlocale() to other than "", "C", and "POSIX"
     55     // for Android
     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 #endif // __ANDROID__
     93 }
     94