1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // <iomanip> 10 11 // template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false); 12 13 // REQUIRES: locale.en_US.UTF-8 14 15 #include <iomanip> 16 #include <ostream> 17 #include <cassert> 18 19 #include "test_macros.h" 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(int, char**) 55 { 56 { 57 testbuf<char> sb; 58 std::ostream os(&sb); 59 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 60 std::showbase(os); 61 long double x = -123456789; 62 os << std::put_money(x, false); 63 #if defined(_WIN32) 64 assert(sb.str() == "($1,234,567.89)"); 65 #else 66 assert(sb.str() == "-$1,234,567.89"); 67 #endif 68 } 69 { 70 testbuf<char> sb; 71 std::ostream os(&sb); 72 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 73 std::showbase(os); 74 long double x = -123456789; 75 os << std::put_money(x, true); 76 #if defined(_WIN32) 77 assert(sb.str() == "(USD1,234,567.89)"); 78 #else 79 assert(sb.str() == "-USD 1,234,567.89"); 80 #endif 81 } 82 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 83 { 84 testbuf<wchar_t> sb; 85 std::wostream os(&sb); 86 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 87 std::showbase(os); 88 long double x = -123456789; 89 os << std::put_money(x, false); 90 #if defined(_WIN32) 91 assert(sb.str() == L"($1,234,567.89)"); 92 #else 93 assert(sb.str() == L"-$1,234,567.89"); 94 #endif 95 } 96 { 97 testbuf<wchar_t> sb; 98 std::wostream os(&sb); 99 os.imbue(std::locale(LOCALE_en_US_UTF_8)); 100 std::showbase(os); 101 long double x = -123456789; 102 os << std::put_money(x, true); 103 #if defined(_WIN32) 104 assert(sb.str() == L"(USD1,234,567.89)"); 105 #else 106 assert(sb.str() == L"-USD 1,234,567.89"); 107 #endif 108 } 109 #endif 110 111 return 0; 112 } 113