put_money.pass.cpp revision f5256e16dfc425c1d466f6308d4026d529ce9e0b
1//===----------------------------------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. 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 17template <class CharT> 18class testbuf 19 : public std::basic_streambuf<CharT> 20{ 21 typedef std::basic_streambuf<CharT> base; 22 std::basic_string<CharT> str_; 23public: 24 testbuf() 25 { 26 } 27 28 std::basic_string<CharT> str() const 29 {return std::basic_string<CharT>(base::pbase(), base::pptr());} 30 31protected: 32 33 virtual typename base::int_type 34 overflow(typename base::int_type __c = base::traits_type::eof()) 35 { 36 if (__c != base::traits_type::eof()) 37 { 38 int n = str_.size(); 39 str_.push_back(__c); 40 str_.resize(str_.capacity()); 41 base::setp(const_cast<CharT*>(str_.data()), 42 const_cast<CharT*>(str_.data() + str_.size())); 43 base::pbump(n+1); 44 } 45 return __c; 46 } 47}; 48 49int main() 50{ 51 { 52 testbuf<char> sb; 53 std::ostream os(&sb); 54 os.imbue(std::locale("en_US")); 55 showbase(os); 56 long double x = -123456789; 57 os << std::put_money(x, false); 58 assert(sb.str() == "-$1,234,567.89"); 59 } 60 { 61 testbuf<char> sb; 62 std::ostream os(&sb); 63 os.imbue(std::locale("en_US")); 64 showbase(os); 65 long double x = -123456789; 66 os << std::put_money(x, true); 67 assert(sb.str() == "-USD 1,234,567.89"); 68 } 69 { 70 testbuf<wchar_t> sb; 71 std::wostream os(&sb); 72 os.imbue(std::locale("en_US")); 73 showbase(os); 74 long double x = -123456789; 75 os << std::put_money(x, false); 76 assert(sb.str() == L"-$1,234,567.89"); 77 } 78 { 79 testbuf<wchar_t> sb; 80 std::wostream os(&sb); 81 os.imbue(std::locale("en_US")); 82 showbase(os); 83 long double x = -123456789; 84 os << std::put_money(x, true); 85 assert(sb.str() == L"-USD 1,234,567.89"); 86 } 87} 88