xref: /llvm-project/libcxx/test/std/input.output/iostream.format/ext.manip/get_money.pass.cpp (revision f4c1258d5633fcf06385ff3fd1f4bf57ab971964)
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 moneyT> T7 get_money(moneyT& mon, bool intl = false);
12 
13 // REQUIRES: locale.en_US.UTF-8
14 
15 // XFAIL: LIBCXX-WINDOWS-FIXME
16 
17 #include <iomanip>
18 #include <istream>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "platform_support.h" // locale name macros
23 
24 template <class CharT>
25 struct testbuf
26     : public std::basic_streambuf<CharT>
27 {
28     typedef std::basic_string<CharT> string_type;
29     typedef std::basic_streambuf<CharT> base;
30 private:
31     string_type str_;
32 public:
33 
34     testbuf() {}
35     testbuf(const string_type& str)
36         : str_(str)
37     {
38         base::setg(const_cast<CharT*>(str_.data()),
39                    const_cast<CharT*>(str_.data()),
40                    const_cast<CharT*>(str_.data()) + str_.size());
41     }
42 };
43 
44 int main(int, char**)
45 {
46     {
47         testbuf<char> sb("  -$1,234,567.89");
48         std::istream is(&sb);
49         is.imbue(std::locale(LOCALE_en_US_UTF_8));
50         long double x = 0;
51         is >> std::get_money(x, false);
52         assert(x == -123456789);
53     }
54     {
55         testbuf<char> sb("  -USD 1,234,567.89");
56         std::istream is(&sb);
57         is.imbue(std::locale(LOCALE_en_US_UTF_8));
58         long double x = 0;
59         is >> std::get_money(x, true);
60         assert(x == -123456789);
61     }
62 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
63     {
64         testbuf<wchar_t> sb(L"  -$1,234,567.89");
65         std::wistream is(&sb);
66         is.imbue(std::locale(LOCALE_en_US_UTF_8));
67         long double x = 0;
68         is >> std::get_money(x, false);
69         assert(x == -123456789);
70     }
71     {
72         testbuf<wchar_t> sb(L"  -USD 1,234,567.89");
73         std::wistream is(&sb);
74         is.imbue(std::locale(LOCALE_en_US_UTF_8));
75         long double x = 0;
76         is >> std::get_money(x, true);
77         assert(x == -123456789);
78     }
79 #endif
80 
81   return 0;
82 }
83