put_money.pass.cpp revision c0d0cbad9ed434267a7af9531bdeeae52eb6d706
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 19template <class CharT> 20class testbuf 21 : public std::basic_streambuf<CharT> 22{ 23 typedef std::basic_streambuf<CharT> base; 24 std::basic_string<CharT> str_; 25public: 26 testbuf() 27 { 28 } 29 30 std::basic_string<CharT> str() const 31 {return std::basic_string<CharT>(base::pbase(), base::pptr());} 32 33protected: 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 51int main() 52{ 53 { 54 testbuf<char> sb; 55 std::ostream os(&sb); 56 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 57 showbase(os); 58 long double x = -123456789; 59 os << std::put_money(x, false); 60 assert(sb.str() == "-$1,234,567.89"); 61 } 62 { 63 testbuf<char> sb; 64 std::ostream os(&sb); 65 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 66 showbase(os); 67 long double x = -123456789; 68 os << std::put_money(x, true); 69 assert(sb.str() == "-USD 1,234,567.89"); 70 } 71 { 72 testbuf<wchar_t> sb; 73 std::wostream os(&sb); 74 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 75 showbase(os); 76 long double x = -123456789; 77 os << std::put_money(x, false); 78 assert(sb.str() == L"-$1,234,567.89"); 79 } 80 { 81 testbuf<wchar_t> sb; 82 std::wostream os(&sb); 83 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 84 showbase(os); 85 long double x = -123456789; 86 os << std::put_money(x, true); 87 assert(sb.str() == L"-USD 1,234,567.89"); 88 } 89} 90