1 // 1999-11-15 Kevin Ediger <kediger@licor.com>
2 // test the floating point inserters (facet num_put)
4 // Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 #include <cstdio> // for sprintf
23 #include <cmath> // for abs
24 #include <cfloat> // for DBL_EPSILON
29 #include <testsuite_hooks.h>
33 #ifndef _GLIBCXX_ASSERT
34 # define TEST_NUMPUT_VERBOSE 1
61 static _TestCase testcases
[] =
63 // standard output (no formatting applied)
64 { 1.2, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "1.2" },
65 { 54, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "54" },
66 { -.012, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "-0.012" },
67 { -.00000012, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "-1.2e-07" },
70 { 10.2345, 0,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10" },
71 { 10.2345, 0,0,'.',' ', T
,F
,F
,T
,F
,F
,F
,F
, "10." },
72 { 10.2345, 1,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2" },
73 { 10.2345, 4,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345" },
74 { 10.2345, 6,0,'.',' ', T
,F
,T
,F
,F
,F
,F
,F
, "+10.234500" },
75 { -10.2345, 6,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "-10.234500" },
76 { -10.2345, 6,0,',',' ', T
,F
,F
,F
,F
,F
,F
,F
, "-10,234500" },
78 // fixed formatting with width
79 { 10.2345, 4,5,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345" },
80 { 10.2345, 4,6,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345" },
81 { 10.2345, 4,7,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345" },
82 { 10.2345, 4,8,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, " 10.2345" },
83 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, " 10.2345" },
84 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,F
,T
,F
, "10.2345 " },
85 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,F
,F
,T
, " 10.2345" },
86 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,T
,F
,F
, " 10.2345" },
87 { -10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,T
,F
,F
, "- 10.2345" },
88 { -10.2345, 4,10,'.','A', T
,F
,F
,F
,F
,T
,F
,F
, "-AA10.2345" },
89 { 10.2345, 4,10,'.','#', T
,F
,T
,F
,F
,T
,F
,F
, "+##10.2345" },
91 // scientific formatting
92 { 1.23e+12, 1,0,'.',' ', F
,T
,F
,F
,F
,F
,F
,F
, "1.2e+12" },
93 { 1.23e+12, 1,0,'.',' ', F
,T
,F
,F
,T
,F
,F
,F
, "1.2E+12" },
94 { 1.23e+12, 2,0,'.',' ', F
,T
,F
,F
,F
,F
,F
,F
, "1.23e+12" },
95 { 1.23e+12, 3,0,'.',' ', F
,T
,F
,F
,F
,F
,F
,F
, "1.230e+12" },
96 { 1.23e+12, 3,0,'.',' ', F
,T
,T
,F
,F
,F
,F
,F
, "+1.230e+12" },
97 { -1.23e-12, 3,0,'.',' ', F
,T
,F
,F
,F
,F
,F
,F
, "-1.230e-12" },
98 { 1.23e+12, 3,0,',',' ', F
,T
,F
,F
,F
,F
,F
,F
, "1,230e+12" },
101 template<typename _CharT
>
102 class testpunct
: public numpunct
<_CharT
>
105 typedef _CharT char_type
;
106 const char_type dchar
;
109 testpunct(char_type decimal_char
) : numpunct
<_CharT
>(), dchar(decimal_char
)
114 do_decimal_point() const
118 do_thousands_sep() const
126 template<typename _CharT
>
127 void apply_formatting(const _TestCase
& tc
, basic_ostream
<_CharT
> & os
)
129 os
.precision(tc
.precision
);
131 os
.fill(static_cast<_CharT
>(tc
.fill
));
135 os
.setf(ios::scientific
);
137 os
.setf(ios::showpos
);
139 os
.setf(ios::showpoint
);
141 os
.setf(ios::uppercase
);
143 os
.setf(ios::internal
);
153 bool test
__attribute__((unused
)) = true;
154 for (std::size_t j
= 0; j
<sizeof(testcases
)/sizeof(testcases
[0]); j
++)
156 _TestCase
& tc
= testcases
[j
];
157 #ifdef TEST_NUMPUT_VERBOSE
158 cout
<< "expect: " << tc
.result
<< endl
;
160 // test double with char type
162 testpunct
<char>* __tp
= new testpunct
<char>(tc
.decimal
);
164 locale
__loc(os
.getloc(), __tp
);
166 apply_formatting(tc
, os
);
168 #ifdef TEST_NUMPUT_VERBOSE
169 cout
<< j
<< "result 1: " << os
.str() << endl
;
171 VERIFY( os
&& os
.str() == tc
.result
);
173 // test long double with char type
175 testpunct
<char>* __tp
= new testpunct
<char>(tc
.decimal
);
177 locale
__loc(os
.getloc(), __tp
);
179 apply_formatting(tc
, os
);
180 os
<< (long double)tc
.val
;
181 #ifdef TEST_NUMPUT_VERBOSE
182 cout
<< j
<< "result 2: " << os
.str() << endl
;
184 VERIFY( os
&& os
.str() == tc
.result
);