3 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
13 # include "complete_digits.h"
14 # include "cppunit/cppunit_proxy.h"
16 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
23 class NumPutGetTest
: public CPPUNIT_NS::TestCase
25 CPPUNIT_TEST_SUITE(NumPutGetTest
);
26 # if defined (__BORLANDC__)
27 /* Ignore FPU exceptions, set FPU precision to 64 bits */
28 unsigned int _float_control_word
= _control87(0, 0);
29 _control87(PC_64
|MCW_EM
|IC_AFFINE
, MCW_PC
|MCW_EM
|MCW_IC
);
31 CPPUNIT_TEST(num_put_float
);
32 CPPUNIT_TEST(num_put_integer
);
33 CPPUNIT_TEST(num_get_float
);
34 CPPUNIT_TEST(num_get_integer
);
36 CPPUNIT_TEST(pointer
);
37 CPPUNIT_TEST(fix_float_long
);
38 CPPUNIT_TEST(custom_numpunct
);
39 # if defined (__BORLANDC__)
40 /* Reset floating point control word */
42 _control87(_float_control_word
, MCW_PC
|MCW_EM
|MCW_IC
);
44 CPPUNIT_TEST_SUITE_END();
48 void num_put_integer();
50 void num_get_integer();
53 void fix_float_long();
54 void custom_numpunct();
56 static bool check_float(float val
, float ref
)
58 float epsilon
= numeric_limits
<float>::epsilon();
59 return val
<= ref
+ epsilon
&& val
>= ref
- epsilon
;
62 static bool check_double(double val
, double ref
)
64 double epsilon
= numeric_limits
<double>::epsilon();
65 return val
<= ref
+ epsilon
&& val
>= ref
- epsilon
;
68 static string
reset_stream(ostringstream
&ostr
)
70 string tmp
= ostr
.str();
75 #if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES)
77 void check_get_float( F v
)
80 typedef numeric_limits
<F
> limits
;
84 str
<< "1E+" << limits::max_exponent10
;
87 CPPUNIT_ASSERT(!str
.fail());
88 CPPUNIT_ASSERT(str
.eof());
89 CPPUNIT_CHECK( in_val_d
== in_val_d
);
90 CPPUNIT_CHECK( in_val_d
!= limits::infinity() );
95 str
<< "-1E+" << limits::max_exponent10
;
98 CPPUNIT_ASSERT(!str
.fail());
99 CPPUNIT_ASSERT(str
.eof());
100 CPPUNIT_CHECK( in_val_d
== in_val_d
);
101 CPPUNIT_CHECK( in_val_d
!= -limits::infinity() );
106 str
<< "1E" << limits::min_exponent10
;
109 CPPUNIT_ASSERT(!str
.fail());
110 CPPUNIT_ASSERT(str
.eof());
111 CPPUNIT_CHECK( in_val_d
== in_val_d
);
112 CPPUNIT_CHECK( in_val_d
!= F(0.0) );
117 str
<< "1E+" << (limits::max_exponent10
+ 1);
120 CPPUNIT_ASSERT(!str
.fail());
121 CPPUNIT_ASSERT(str
.eof());
122 CPPUNIT_CHECK( in_val_d
== in_val_d
);
123 CPPUNIT_CHECK( in_val_d
== limits::infinity() );
128 str
<< "-1E+" << (limits::max_exponent10
+ 1);
131 CPPUNIT_ASSERT(!str
.fail());
132 CPPUNIT_ASSERT(str
.eof());
133 CPPUNIT_CHECK( in_val_d
== in_val_d
);
134 CPPUNIT_CHECK( in_val_d
== -limits::infinity() );
139 str
<< "1E" << (limits::min_exponent10
- 1);
142 CPPUNIT_ASSERT(!str
.fail());
143 CPPUNIT_ASSERT(str
.eof());
144 CPPUNIT_CHECK( in_val_d
== in_val_d
);
145 CPPUNIT_CHECK( in_val_d
>= F(0.0) && in_val_d
<= limits::min() );
147 #if !defined (__MINGW32__)
151 str
<< limits::max();
153 CPPUNIT_ASSERT(!str
.fail());
154 CPPUNIT_CHECK( str
.str() != "inf" && str
.str() != "Inf" );
155 CPPUNIT_CHECK( str
.str() != "-inf" && str
.str() != "-Inf" );
156 CPPUNIT_CHECK( str
.str() != "nan" && str
.str() != "NaN" );
157 CPPUNIT_CHECK( str
.str() != "-nan" && str
.str() != "-NaN" );
158 //CPPUNIT_MESSAGE( str.str().c_str() );
161 //str << limits::max_exponent10;
162 //CPPUNIT_MESSAGE( str.str().c_str() );
166 CPPUNIT_ASSERT(!str
.fail());
167 CPPUNIT_ASSERT(str
.eof());
168 CPPUNIT_CHECK( in_val_d
== in_val_d
);
169 CPPUNIT_CHECK( in_val_d
!= limits::infinity() );
174 str
<< fixed
<< limits::max();
176 CPPUNIT_ASSERT(!str
.fail());
177 CPPUNIT_CHECK( str
.str() != "inf" && str
.str() != "Inf" );
178 CPPUNIT_CHECK( str
.str() != "-inf" && str
.str() != "-Inf" );
179 CPPUNIT_CHECK( str
.str() != "nan" && str
.str() != "NaN" );
180 CPPUNIT_CHECK( str
.str() != "-nan" && str
.str() != "-NaN" );
181 //CPPUNIT_MESSAGE( str.str().c_str() );
184 //str << limits::max_exponent10;
185 //CPPUNIT_MESSAGE( str.str().c_str() );
189 CPPUNIT_ASSERT(!str
.fail());
190 CPPUNIT_ASSERT(str
.eof());
191 CPPUNIT_CHECK( in_val_d
== in_val_d
);
192 CPPUNIT_CHECK( in_val_d
!= limits::infinity() );
197 str
<< scientific
<< setprecision(50) << limits::max();
199 CPPUNIT_ASSERT(!str
.fail());
200 CPPUNIT_CHECK( str
.str() != "inf" && str
.str() != "Inf" );
201 CPPUNIT_CHECK( str
.str() != "-inf" && str
.str() != "-Inf" );
202 CPPUNIT_CHECK( str
.str() != "nan" && str
.str() != "NaN" );
203 CPPUNIT_CHECK( str
.str() != "-nan" && str
.str() != "-NaN" );
204 //CPPUNIT_MESSAGE( str.str().c_str() );
207 //str << limits::max_exponent10;
208 //CPPUNIT_MESSAGE( str.str().c_str() );
212 CPPUNIT_ASSERT(!str
.fail());
213 CPPUNIT_ASSERT(str
.eof());
214 CPPUNIT_CHECK( in_val_d
== in_val_d
);
215 CPPUNIT_CHECK( in_val_d
!= limits::infinity() );
221 str
<< limits::infinity();
223 CPPUNIT_ASSERT( !str
.fail() );
224 CPPUNIT_ASSERT( !limits::has_infinity
|| str
.str() == "inf" || str
.str() == "Inf" );
229 str
<< -limits::infinity();
231 CPPUNIT_ASSERT( !str
.fail() );
232 CPPUNIT_ASSERT( !limits::has_infinity
|| str
.str() == "-inf" || str
.str() == "-Inf" );
237 str
<< limits::quiet_NaN();
239 CPPUNIT_ASSERT( !str
.fail() );
240 CPPUNIT_ASSERT( !limits::has_quiet_NaN
|| str
.str() == "nan" || str
.str() == "NaN" );
245 str
<< "0." << string(limits::max_exponent10
, '0') << "1e" << (limits::max_exponent10
+ 1);
246 CPPUNIT_ASSERT( !str
.fail() );
249 CPPUNIT_ASSERT( !str
.fail() );
250 CPPUNIT_ASSERT( str
.eof() );
251 CPPUNIT_CHECK( in_val_d
== 1 );
256 str
<< "1" << string(-(limits::min_exponent10
- 1), '0') << "e" << (limits::min_exponent10
- 1);
257 CPPUNIT_ASSERT( !str
.fail() );
260 CPPUNIT_ASSERT( !str
.fail() );
261 CPPUNIT_ASSERT( str
.eof() );
262 CPPUNIT_CHECK( in_val_d
== 1 );
264 # if defined (_STLPORT_VERSION) && (_STLPORT_VERSION >= 0x530)
265 // The following tests are showing that simply changing stream
266 // precision lead to different result. Do not seems to be a real
267 // problem, simply rounding approximation but additional study should
268 // be done after 5.2 release.
271 str
<< setprecision(limits::digits10
+ 2) << limits::max();
273 CPPUNIT_MESSAGE(str
.str().c_str());
274 CPPUNIT_ASSERT( !str
.fail() );
279 CPPUNIT_ASSERT( !str
.fail() );
280 CPPUNIT_ASSERT( limits::infinity() > val
);
284 str
<< setprecision(limits::digits10
+ 1) << limits::max();
286 CPPUNIT_MESSAGE(str
.str().c_str());
287 CPPUNIT_ASSERT( !str
.fail() );
292 CPPUNIT_ASSERT( !str
.fail() );
293 CPPUNIT_ASSERT( limits::infinity() > val
);
298 # define __check_get_float( F ) \
299 void check_get_float( F v ) \
305 str << "1E+" << numeric_limits<F>::max_exponent10; \
308 CPPUNIT_ASSERT(!str.fail()); \
309 CPPUNIT_ASSERT(str.eof()); \
310 CPPUNIT_CHECK( in_val_d == in_val_d ); \
311 CPPUNIT_CHECK( in_val_d != numeric_limits<F>::infinity() ); \
316 str << "-1E+" << numeric_limits<F>::max_exponent10; \
319 CPPUNIT_ASSERT(!str.fail()); \
320 CPPUNIT_ASSERT(str.eof()); \
321 CPPUNIT_CHECK( in_val_d == in_val_d ); \
322 CPPUNIT_CHECK( in_val_d != -numeric_limits<F>::infinity() ); \
327 str << "1E" << numeric_limits<F>::min_exponent10; \
330 CPPUNIT_ASSERT(!str.fail()); \
331 CPPUNIT_ASSERT(str.eof()); \
332 CPPUNIT_CHECK( in_val_d == in_val_d ); \
333 CPPUNIT_CHECK( in_val_d != F(0.0) ); \
338 str << "1E+" << (numeric_limits<F>::max_exponent10 + 1); \
341 CPPUNIT_ASSERT(!str.fail()); \
342 CPPUNIT_ASSERT(str.eof()); \
343 CPPUNIT_CHECK( in_val_d == in_val_d ); \
344 CPPUNIT_CHECK( in_val_d == numeric_limits<F>::infinity() ); \
349 str << "-1E+" << (numeric_limits<F>::max_exponent10 + 1); \
352 CPPUNIT_ASSERT(!str.fail()); \
353 CPPUNIT_ASSERT(str.eof()); \
354 CPPUNIT_CHECK( in_val_d == in_val_d ); \
355 CPPUNIT_CHECK( in_val_d == -numeric_limits<F>::infinity() ); \
360 str << "1E" << (numeric_limits<F>::min_exponent10 - 1); \
363 CPPUNIT_ASSERT(!str.fail()); \
364 CPPUNIT_ASSERT(str.eof()); \
365 CPPUNIT_CHECK( in_val_d == in_val_d ); \
366 CPPUNIT_CHECK( in_val_d >= F(0.0) && in_val_d <= numeric_limits<F>::min() ); \
370 __check_get_float( float )
371 __check_get_float( double )
372 # if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE)
373 __check_get_float( long double )
375 # undef __check_get_float
376 #endif // _STLP_NO_MEMBER_TEMPLATES
379 CPPUNIT_TEST_SUITE_REGISTRATION(NumPutGetTest
);
381 #if defined (_MSC_VER)
382 # pragma warning (disable : 4056)
383 # pragma warning (disable : 4756)
387 // tests implementation
389 void NumPutGetTest::num_put_float()
392 string output
, digits
;
396 ostr
<< 1.23457e+17f
;
397 CPPUNIT_ASSERT(ostr
.good());
398 output
= reset_stream(ostr
);
400 complete_digits(digits
);
401 CPPUNIT_CHECK(output
== string("1.23457e+") + digits
);
406 ostr
<< setprecision(200) << 1.23457e+17f
;
407 CPPUNIT_ASSERT(ostr
.good());
408 output
= reset_stream(ostr
);
409 CPPUNIT_CHECK( output
.size() < 200 );
414 ostr
<< setprecision(200) << numeric_limits
<float>::min();
415 CPPUNIT_ASSERT(ostr
.good());
416 output
= reset_stream(ostr
);
417 CPPUNIT_CHECK( output
.size() < 200 );
422 ostr
<< fixed
<< 1.23457e+17f
;
423 CPPUNIT_ASSERT(ostr
.good());
424 output
= reset_stream(ostr
);
425 CPPUNIT_CHECK(output
.size() == 25);
426 CPPUNIT_CHECK(output
.substr(0, 5) == "12345");
427 CPPUNIT_CHECK(output
.substr(18) == ".000000");
432 ostr
<< fixed
<< showpos
<< 1.23457e+17f
;
433 CPPUNIT_ASSERT(ostr
.good());
434 output
= reset_stream(ostr
);
435 CPPUNIT_CHECK(output
.size() == 26);
436 CPPUNIT_CHECK(output
.substr(0, 6) == "+12345");
437 CPPUNIT_CHECK(output
.substr(19) == ".000000");
442 ostr
<< fixed
<< showpos
<< setprecision(100) << 1.23457e+17f
;
443 CPPUNIT_ASSERT(ostr
.good());
444 output
= reset_stream(ostr
);
445 CPPUNIT_CHECK(output
.size() == 120);
446 CPPUNIT_CHECK(output
.substr(0, 6) == "+12345");
447 CPPUNIT_CHECK(output
.substr(19) == ".0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" );
452 ostr
<< scientific
<< setprecision(8) << 0.12345678; // float doesn't have enough precision, 0.12345678f ended up 0.1234567836..
453 CPPUNIT_ASSERT(ostr
.good());
454 output
= reset_stream(ostr
);
456 complete_digits(digits
);
457 CPPUNIT_CHECK(output
== string("1.23456780e-") + digits
);
462 ostr
<< fixed
<< setprecision(8) << setw(30) << setfill('0') << 0.12345678f
;
463 CPPUNIT_ASSERT(ostr
.good());
464 output
= reset_stream(ostr
);
465 CPPUNIT_CHECK(output
== "000000000000000000000.12345678");
470 ostr
<< fixed
<< showpos
<< setprecision(8) << setw(30) << setfill('0') << 0.12345678f
;
471 CPPUNIT_ASSERT(ostr
.good());
472 output
= reset_stream(ostr
);
473 CPPUNIT_CHECK(output
== "0000000000000000000+0.12345678");
478 ostr
<< fixed
<< showpos
<< setprecision(8) << setw(30) << left
<< setfill('0') << 0.12345678f
;
479 CPPUNIT_ASSERT(ostr
.good());
480 output
= reset_stream(ostr
);
481 CPPUNIT_CHECK(output
== "+0.123456780000000000000000000");
486 ostr
<< fixed
<< showpos
<< setprecision(8) << setw(30) << internal
<< setfill('0') << 0.12345678f
;
487 CPPUNIT_ASSERT(ostr
.good());
488 output
= reset_stream(ostr
);
489 CPPUNIT_CHECK(output
== "+00000000000000000000.12345678");
494 ostr
<< fixed
<< showpos
<< setprecision(100) << 1.234567e+17;
495 CPPUNIT_ASSERT(ostr
.good());
496 output
= reset_stream(ostr
);
497 CPPUNIT_CHECK(output
.size() == 120);
498 CPPUNIT_CHECK(output
.substr(0, 6) == "+12345");
499 CPPUNIT_CHECK(output
.substr(19) == ".0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" );
502 #if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE)
505 ostr
<< fixed
<< showpos
<< setprecision(100) << 1.234567e+17l;
506 CPPUNIT_ASSERT(ostr
.good());
507 output
= reset_stream(ostr
);
508 CPPUNIT_CHECK(output
.size() == 120);
509 CPPUNIT_CHECK(output
.substr(0, 6) == "+12345");
510 CPPUNIT_CHECK(output
.substr(19) == ".0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" );
516 ostr
<< scientific
<< setprecision(50) << 0.0;
517 CPPUNIT_ASSERT(ostr
.good());
518 output
= reset_stream(ostr
);
519 CPPUNIT_CHECK( output
== "0.00000000000000000000000000000000000000000000000000e+00" );
523 ostr
<< fixed
<< setprecision(100) << numeric_limits
<float>::max();
524 CPPUNIT_ASSERT(ostr
.good());
525 output
= reset_stream(ostr
);
526 //CPPUNIT_MESSAGE( output.c_str() );
531 ostr
<< setprecision(100) << numeric_limits
<double>::max();
532 CPPUNIT_ASSERT(ostr
.good());
533 output
= reset_stream(ostr
);
534 //CPPUNIT_MESSAGE( output.c_str() );
537 #if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE)
540 ostr
<< setprecision(100) << numeric_limits
<long double>::max();
541 CPPUNIT_ASSERT(ostr
.good());
542 output
= reset_stream(ostr
);
543 //CPPUNIT_MESSAGE( output.c_str() );
548 // ostringstream ostr;
549 // ostr << setprecision(-numeric_limits<float>::min_exponent10 + numeric_limits<float>::digits10 + 9) << numeric_limits<float>::min();
550 // CPPUNIT_ASSERT(ostr.good());
551 // output = reset_stream(ostr);
552 // //CPPUNIT_MESSAGE( output.c_str() );
556 // ostringstream ostr;
557 // ostr << setprecision(-numeric_limits<double>::min_exponent10 + numeric_limits<double>::digits10) << numeric_limits<double>::min();
558 // CPPUNIT_ASSERT(ostr.good());
559 // output = reset_stream(ostr);
560 // //CPPUNIT_MESSAGE( output.c_str() );
563 //#if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE)
565 // ostringstream ostr;
566 // ostr << setprecision(-numeric_limits<long double>::min_exponent10 + numeric_limits<long double>::digits10) << numeric_limits<long double>::min();
567 // CPPUNIT_ASSERT(ostr.good());
568 // output = reset_stream(ostr);
569 // CPPUNIT_MESSAGE( output.c_str() );
577 str
.setf(ios::fixed
, ios::floatfield
);
579 // cerr << str.str() << endl;
580 CPPUNIT_CHECK( str
.str() == "100000.000000" );
585 CPPUNIT_CHECK( str
.str() == "100000" );
590 CPPUNIT_CHECK( str
.str() == "100000.0000" );
595 CPPUNIT_CHECK( str
.str().size() == 84 );
596 //printf("\nC result: %.0f\n", 1.0e+83);
597 //CPPUNIT_MESSAGE( str.str().c_str() );
598 //CPPUNIT_CHECK( str.str() == "100000000000000000000000000000000000000000000000000000000000000000000000000000000000" );
600 // cerr.setf(ios::fixed, ios::floatfield);
601 // cerr << DBL_MAX << endl;
602 // cerr << 1.0e+37 << endl;
606 #define CHECK_COMPLETE(type, val, base, showbase, showpos, casing, width, adjust, expected) \
609 ostringstream ostr; \
610 ostr << base << showbase << showpos << casing << setw(width) << adjust << tmp; \
611 CPPUNIT_CHECK( ostr.str() == expected ); \
614 #define CHECK(type, val, base, expected) \
615 CHECK_COMPLETE(type, val, base, noshowbase, noshowpos, nouppercase, 0, right, expected)
617 void NumPutGetTest::num_put_integer()
621 CHECK(short, 0, oct
, "0")
622 CHECK(short, 1, oct
, "1")
623 CHECK(short, 12345, oct
, "30071")
624 if (sizeof(short) == 2) {
625 CHECK(short, -1, oct
, "177777")
626 CHECK(short, -12345, oct
, "147707")
629 CHECK(unsigned short, 0, oct
, "0")
630 CHECK(unsigned short, 12345, oct
, "30071")
632 CHECK(int, 0, oct
, "0")
633 CHECK(int, 12345678, oct
, "57060516")
634 if (sizeof(int) == 4) {
635 CHECK(int, -1, oct
, "37777777777")
636 CHECK(int, -12345678, oct
, "37720717262")
639 CHECK(unsigned int, 0, oct
, "0")
640 CHECK(unsigned int, 12345678, oct
, "57060516")
642 CHECK(long, 0, oct
, "0")
643 CHECK(long, 12345678, oct
, "57060516")
644 if (sizeof(long) == 4) {
645 CHECK(long, -1, oct
, "37777777777")
646 CHECK(long, -12345678, oct
, "37720717262")
649 CHECK(unsigned long, 0, oct
, "0")
650 CHECK(unsigned long, 12345678, oct
, "57060516")
652 #if defined (STLPORT) && defined (_STLP_LONG_LONG)
653 CHECK(_STLP_LONG_LONG
, 0, oct
, "0")
654 CHECK(_STLP_LONG_LONG
, 12345678, oct
, "57060516")
655 if (sizeof(_STLP_LONG_LONG
) == 8) {
656 CHECK(_STLP_LONG_LONG
, -1, oct
, "1777777777777777777777")
657 CHECK(_STLP_LONG_LONG
, -12345678, oct
, "1777777777777720717262")
660 CHECK(unsigned _STLP_LONG_LONG
, 0, oct
, "0")
661 CHECK(unsigned _STLP_LONG_LONG
, 12345678, oct
, "57060516")
664 //Even with showbase, 0 value gives "0" (see printf documentation)
665 CHECK_COMPLETE(short, 0, oct
, showbase
, noshowpos
, nouppercase
, 0, right
, "0")
666 CHECK_COMPLETE(short, 0, oct
, showbase
, showpos
, nouppercase
, 6, right
, " 0")
668 CHECK_COMPLETE(short, 1, oct
, showbase
, noshowpos
, nouppercase
, 6, right
, " 01")
669 CHECK_COMPLETE(short, 1, oct
, showbase
, noshowpos
, nouppercase
, 6, left
, "01 ")
670 CHECK_COMPLETE(short, 1, oct
, showbase
, showpos
, nouppercase
, 6, internal
, " 01")
675 CHECK(short, 0, dec
, "0")
676 CHECK(short, -1, dec
, "-1")
677 CHECK(short, 12345, dec
, "12345")
678 CHECK(short, -12345, dec
, "-12345")
680 CHECK(unsigned short, 0, dec
, "0")
681 CHECK(unsigned short, 12345, dec
, "12345")
683 CHECK(int, 0, dec
, "0")
684 CHECK(int, -1, dec
, "-1")
685 CHECK(int, 12345678, dec
, "12345678")
686 CHECK(int, -12345678, dec
, "-12345678")
688 CHECK(unsigned int, 0, dec
, "0")
689 CHECK(unsigned int, 12345678, dec
, "12345678")
691 CHECK(long, 0, dec
, "0")
692 CHECK(long, -1, dec
, "-1")
693 CHECK(long, 12345678, dec
, "12345678")
694 CHECK(long, -12345678, dec
, "-12345678")
696 CHECK(unsigned long, 0, dec
, "0")
697 CHECK(unsigned long, 12345678, dec
, "12345678")
698 #if defined (STLPORT) && defined (_STLP_LONG_LONG)
699 CHECK(_STLP_LONG_LONG
, 0, dec
, "0")
700 CHECK(_STLP_LONG_LONG
, -1, dec
, "-1")
701 CHECK(_STLP_LONG_LONG
, 12345678, dec
, "12345678")
702 CHECK(_STLP_LONG_LONG
, -12345678, dec
, "-12345678")
704 CHECK(unsigned _STLP_LONG_LONG
, 0, dec
, "0")
705 CHECK(unsigned _STLP_LONG_LONG
, 12345678, dec
, "12345678")
708 CHECK_COMPLETE(short, 0, dec
, showbase
, showpos
, nouppercase
, 0, right
, "+0")
709 CHECK_COMPLETE(short, 0, dec
, showbase
, showpos
, nouppercase
, 6, right
, " +0")
710 CHECK_COMPLETE(short, 1, dec
, showbase
, showpos
, nouppercase
, 6, right
, " +1")
711 CHECK_COMPLETE(short, 1, dec
, showbase
, showpos
, nouppercase
, 6, left
, "+1 ")
712 CHECK_COMPLETE(short, 1, dec
, showbase
, showpos
, nouppercase
, 6, internal
, "+ 1")
715 //hexadecimal outputs
717 CHECK(short, 0, hex
, "0")
718 CHECK(short, 12345, hex
, "3039")
719 if (sizeof(short) == 2) {
720 CHECK(short, -1, hex
, "ffff")
721 CHECK(short, -12345, hex
, "cfc7")
724 CHECK(unsigned short, 0, hex
, "0")
725 CHECK(unsigned short, 12345, hex
, "3039")
727 CHECK(int, 0, hex
, "0")
728 CHECK(int, 12345678, hex
, "bc614e")
729 if (sizeof(int) == 4) {
730 CHECK(int, -1, hex
, "ffffffff")
731 CHECK(int, -12345678, hex
, "ff439eb2")
734 CHECK(unsigned int, 0, hex
, "0")
735 CHECK(unsigned int, 12345678, hex
, "bc614e")
737 CHECK(long, 0, hex
, "0")
738 CHECK(long, 12345678, hex
, "bc614e")
739 if (sizeof(long) == 4) {
740 CHECK(long, -1, hex
, "ffffffff")
741 CHECK(long, -12345678, hex
, "ff439eb2")
744 CHECK(unsigned long, 0, hex
, "0")
745 CHECK(unsigned long, 12345678, hex
, "bc614e")
746 #if defined (STLPORT) && defined (_STLP_LONG_LONG)
747 CHECK(_STLP_LONG_LONG
, 0, hex
, "0")
748 CHECK(_STLP_LONG_LONG
, 12345678, hex
, "bc614e")
749 if (sizeof(_STLP_LONG_LONG
) == 8) {
750 CHECK(_STLP_LONG_LONG
, -1, hex
, "ffffffffffffffff")
751 CHECK(_STLP_LONG_LONG
, -12345678, hex
, "ffffffffff439eb2")
754 CHECK(unsigned _STLP_LONG_LONG
, 0, hex
, "0")
755 CHECK(unsigned _STLP_LONG_LONG
, 12345678, hex
, "bc614e")
758 //Even with showbase, 0 value gives "0" output (see printf documentation)
759 CHECK_COMPLETE(short, 0, hex
, showbase
, showpos
, nouppercase
, 0, right
, "0")
760 CHECK_COMPLETE(short, 0, hex
, showbase
, noshowpos
, nouppercase
, 6, right
, " 0")
761 CHECK_COMPLETE(short, 0, hex
, showbase
, noshowpos
, nouppercase
, 6, internal
, " 0")
763 CHECK_COMPLETE(short, 1, hex
, showbase
, noshowpos
, nouppercase
, 6, right
, " 0x1")
764 CHECK_COMPLETE(short, 1, hex
, showbase
, noshowpos
, nouppercase
, 6, left
, "0x1 ")
765 CHECK_COMPLETE(short, 1, hex
, showbase
, noshowpos
, nouppercase
, 6, internal
, "0x 1")
766 CHECK_COMPLETE(short, 1, hex
, showbase
, noshowpos
, uppercase
, 6, left
, "0X1 ")
767 CHECK_COMPLETE(short, 1, hex
, showbase
, showpos
, uppercase
, 6, internal
, "0X 1")
771 void NumPutGetTest::num_get_float()
779 CPPUNIT_ASSERT(!istr
.fail());
780 CPPUNIT_ASSERT(istr
.eof());
781 CPPUNIT_ASSERT(check_float(in_val
, 1.2345f
));
786 CPPUNIT_ASSERT(!istr
.fail());
787 CPPUNIT_ASSERT(istr
.eof());
788 CPPUNIT_ASSERT(check_float(in_val
, -1.2345f
));
793 CPPUNIT_ASSERT(!istr
.fail());
794 CPPUNIT_ASSERT(check_float(in_val
, 1.2345f
));
797 istr
.str("000000000000001.234500000000");
799 CPPUNIT_ASSERT(!istr
.fail());
800 CPPUNIT_ASSERT(istr
.eof());
801 CPPUNIT_ASSERT(check_float(in_val
, 1.2345f
));
804 istr
.str("1.2345e+04");
806 CPPUNIT_ASSERT(!istr
.fail());
807 CPPUNIT_ASSERT(istr
.eof());
808 CPPUNIT_ASSERT(check_float(in_val
, 12345.0f
));
811 CPPUNIT_MESSAGE( "float" );
812 check_get_float( 0.0F
);
813 CPPUNIT_MESSAGE( "double" );
814 check_get_float( 0.0 );
815 #if (!defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE)) && !defined(__ANDROID__)
816 CPPUNIT_MESSAGE( "long double" );
817 check_get_float( 0.0L );
822 str
<< "1e" << numeric_limits
<double>::max_exponent10
;
823 CPPUNIT_ASSERT(!str
.fail());
827 CPPUNIT_ASSERT(!str
.fail());
828 CPPUNIT_ASSERT(str
.eof());
829 CPPUNIT_ASSERT( numeric_limits
<double>::max_exponent10
<= numeric_limits
<float>::max_exponent10
||
830 val
== numeric_limits
<float>::infinity() );
835 str
<< "1e" << numeric_limits
<double>::min_exponent10
;
836 CPPUNIT_ASSERT(!str
.fail());
840 CPPUNIT_ASSERT(!str
.fail());
841 CPPUNIT_ASSERT(str
.eof());
842 CPPUNIT_ASSERT( numeric_limits
<double>::min_exponent10
>= numeric_limits
<float>::min_exponent10
||
845 #if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE)
849 str
<< "1e" << numeric_limits
<long double>::max_exponent10
;
850 CPPUNIT_ASSERT(!str
.fail());
854 CPPUNIT_ASSERT(!str
.fail());
855 CPPUNIT_ASSERT(str
.eof());
856 CPPUNIT_ASSERT( numeric_limits
<long double>::max_exponent10
<= numeric_limits
<double>::max_exponent10
||
857 val
== numeric_limits
<double>::infinity() );
862 str
<< "1e" << numeric_limits
<long double>::min_exponent10
;
863 CPPUNIT_ASSERT(!str
.fail());
867 CPPUNIT_ASSERT(!str
.fail());
868 CPPUNIT_ASSERT(str
.eof());
869 CPPUNIT_ASSERT( numeric_limits
<long double>::min_exponent10
>= numeric_limits
<double>::min_exponent10
||
872 #if !defined(__ANDROID__) // "long double" in Android is still a distinct type but size is the same as "double"
874 const char* p
= "2.718281828459045235360287471352662497757247093e0";
879 CPPUNIT_ASSERT( x
> 2.70l && x
< 2.72l );
885 void NumPutGetTest::num_get_integer()
893 CPPUNIT_ASSERT( !istr
.fail() );
894 CPPUNIT_ASSERT( istr
.eof() );
895 CPPUNIT_ASSERT( val
== 12345 );
898 if (sizeof(short) == 2) {
901 CPPUNIT_ASSERT( !istr
.fail() );
902 CPPUNIT_ASSERT( istr
.eof() );
903 CPPUNIT_ASSERT( val
== -1 );
914 CPPUNIT_ASSERT( !istr
.fail() );
915 CPPUNIT_ASSERT( istr
.eof() );
916 CPPUNIT_ASSERT( val
== 10000 );
922 CPPUNIT_ASSERT( !istr
.fail() );
923 CPPUNIT_ASSERT( istr
.eof() );
924 CPPUNIT_ASSERT( val
== 10000 );
927 if (sizeof(short) == 2) {
929 istr
.str("10000000");
931 CPPUNIT_ASSERT( istr
.fail() );
932 CPPUNIT_ASSERT( istr
.eof() );
933 CPPUNIT_ASSERT( val
== -1 );
940 CPPUNIT_ASSERT( !istr
.fail() );
941 CPPUNIT_ASSERT( !istr
.eof() );
942 CPPUNIT_ASSERT( val
== 0 );
948 CPPUNIT_ASSERT( !istr
.fail() );
949 CPPUNIT_ASSERT( istr
.eof() );
950 CPPUNIT_ASSERT( val
== 1 );
960 CPPUNIT_ASSERT( !istr
.fail() );
961 CPPUNIT_ASSERT( istr
.eof() );
962 CPPUNIT_ASSERT( val
== 12345 );
968 CPPUNIT_ASSERT( istr
.fail() );
969 CPPUNIT_ASSERT( !istr
.eof() );
970 CPPUNIT_ASSERT( val
== -1 );
976 CPPUNIT_ASSERT( !istr
.fail() );
977 CPPUNIT_ASSERT( istr
.eof() );
978 CPPUNIT_ASSERT( val
== 12345 );
983 CPPUNIT_ASSERT( !istr
.fail() );
984 CPPUNIT_ASSERT( istr
.eof() );
985 CPPUNIT_ASSERT( val
== 12345 );
988 if (sizeof(short) == 2) {
992 CPPUNIT_ASSERT( !istr
.fail() );
993 CPPUNIT_ASSERT( istr
.eof() );
994 CPPUNIT_ASSERT( val
== -12345 );
1000 void NumPutGetTest::inhex()
1005 CPPUNIT_CHECK( s
.str() == "0" );
1010 CPPUNIT_CHECK( s
.str() == "ff" );
1014 s
<< hex
<< setw( 4 ) << 0xff;
1015 CPPUNIT_CHECK( s
.str() == " ff" );
1019 s
<< hex
<< setw( 4 ) << 0;
1020 CPPUNIT_CHECK( s
.str() == " 0" );
1024 s
<< hex
<< showbase
<< 0;
1025 CPPUNIT_CHECK( s
.str() == "0" );
1029 s
<< hex
<< showbase
<< 0xff;
1030 CPPUNIT_CHECK( s
.str() == "0xff" );
1034 s
<< hex
<< showbase
<< setw( 4 ) << 0xff;
1035 CPPUNIT_CHECK( s
.str() == "0xff" );
1037 { // special case for regression (partially duplicate CHECK_COMPLETE above):
1039 s
.setf( ios_base::internal
, ios_base::adjustfield
);
1040 s
<< hex
<< showbase
<< setw(8+2) << 0;
1041 CPPUNIT_CHECK( s
.str() == " 0" );
1045 void NumPutGetTest::pointer()
1047 // Problem with printing pointer to null
1050 * Really C's formatting not help here, due to:
1052 * p The argument shall be a pointer to void. The value of
1053 * the pointer is converted to a sequence of printable characters,
1054 * in an implementation-defined manner.
1059 void *p = (void *)0xff00;
1060 sprintf( buf, "%p", p );
1061 // cerr << buf << endl;
1062 // Hmmm, I see 0xff00 on box with 32-bits address; pointer like 'unsigned hex'?
1063 if ( sizeof( p ) == 2 ) {
1064 CPPUNIT_ASSERT( strcmp( buf, "0xff00" ) == 0 );
1065 } else if ( sizeof( p ) == 4 ) {
1066 CPPUNIT_ASSERT( strcmp( buf, "0x0000ff00" ) == 0 );
1067 } else if ( sizeof( p ) == 8 ) {
1068 CPPUNIT_ASSERT( strcmp( buf, "0x000000000000ff00" ) == 0 );
1070 CPPUNIT_CHECK( sizeof( p ) == 2 || sizeof( p ) == 4 || sizeof( p ) == 8 );
1079 // sprintf( buf, "%p", p );
1080 /* Cool. "%p" print '(nil)'; "%#x" print '0' */
1081 // sprintf( buf, "%#x", (unsigned)p );
1082 // cerr << buf << endl;
1086 void *p
= (void *)0xff00;
1088 CPPUNIT_ASSERT( s
.good() );
1089 if ( sizeof( p
) == 2 ) {
1090 CPPUNIT_ASSERT( s
.str() == "0xff00" );
1091 } else if ( sizeof( p
) == 4 ) {
1092 CPPUNIT_ASSERT( s
.str() == "0x0000ff00" ); // this pass
1093 } else if ( sizeof( p
) == 8 ) {
1094 CPPUNIT_ASSERT( s
.str() == "0x000000000000ff00" );
1096 CPPUNIT_CHECK( sizeof( p
) == 2 || sizeof( p
) == 4 || sizeof( p
) == 8 );
1103 CPPUNIT_ASSERT( s
.good() );
1104 if ( sizeof( p
) == 2 ) {
1105 CPPUNIT_ASSERT( s
.str() == "0x0000" );
1106 } else if ( sizeof( p
) == 4 ) {
1107 CPPUNIT_ASSERT( s
.str() == "0x00000000" ); // but this will fail, if follow %p
1108 } else if ( sizeof( p
) == 8 ) {
1109 CPPUNIT_ASSERT( s
.str() == "0x0000000000000000" );
1111 CPPUNIT_CHECK( sizeof( p
) == 2 || sizeof( p
) == 4 || sizeof( p
) == 8 );
1116 void NumPutGetTest::fix_float_long()
1120 str
.setf(ios::fixed
, ios::floatfield
);
1122 CPPUNIT_CHECK( str
.str() == "100000.000000" );
1127 CPPUNIT_CHECK( str
.str() == "100000" );
1132 CPPUNIT_CHECK( str
.str() == "100000.0000" );
1138 istringstream
istr( str
.str() );
1141 CPPUNIT_CHECK( !istr
.fail() );
1142 if ( int(numeric_limits
<double>::digits10
) < 83 ) {
1144 for ( int ee
= 83 - int(numeric_limits
<double>::digits10
); ee
> 0; --ee
) {
1147 // we may loss some digits here, but not more than mantissa:
1148 CPPUNIT_CHECK( (f
> (1.0e+83 - delta
)) && (f
< (1.0e+83 + delta
)) );
1150 CPPUNIT_CHECK( check_double(f
, 1.0e+83) );
1154 #if 0 // #ifndef _STLP_NO_LONG_DOUBLE
1159 istringstream
istr( str
.str() );
1162 CPPUNIT_CHECK( !istr
.fail() );
1163 if ( int(numeric_limits
<long double>::digits10
) < 83 ) {
1164 long double delta
= 1.0l;
1165 for ( int ee
= 83 - int(numeric_limits
<long double>::digits10
); ee
> 0; --ee
) {
1168 // we may loss some digits here, but not more than mantissa:
1169 cerr
<< "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" << endl
;
1170 cerr
<< str
.str() << endl
;
1171 cerr
<< delta
<< endl
;
1173 CPPUNIT_CHECK( (f
> (1.0e+83l - delta
)) && (f
< (1.0e+83l + delta
)) );
1175 CPPUNIT_CHECK( check_double(f
, 1.0e+83l) );
1182 str
<< numeric_limits
<double>::max();
1184 istringstream
istr( str
.str() );
1187 CPPUNIT_CHECK( !istr
.fail() );
1188 if ( int(numeric_limits
<double>::digits10
) < int(numeric_limits
<double>::max_exponent10
) ) {
1190 for ( int ee
= int(numeric_limits
<double>::max_exponent10
) - int(numeric_limits
<double>::digits10
); ee
> 0; --ee
) {
1193 // we may loss some digits here, but not more than mantissa:
1194 CPPUNIT_CHECK( (f
> (numeric_limits
<double>::max() - delta
)) );
1198 #if 0 // #ifndef _STLP_NO_LONG_DOUBLE
1201 str
<< numeric_limits
<long double>::max();
1203 istringstream
istr( str
.str() );
1206 CPPUNIT_CHECK( !istr
.fail() );
1207 if ( int(numeric_limits
<long double>::digits10
) < int(numeric_limits
<long double>::max_exponent10
) ) {
1208 long double delta
= 1.0l;
1209 for ( int ee
= int(numeric_limits
<long double>::max_exponent10
) - int(numeric_limits
<long double>::digits10
); ee
> 0; --ee
) {
1212 // we may loss some digits here, but not more than mantissa:
1213 CPPUNIT_CHECK( (f
> (numeric_limits
<long double>::max() - delta
)) );
1219 class CommaSepNumPunct
: public numpunct
<char> {
1220 char do_thousands_sep() const { return ','; }
1221 string
do_grouping() const { return string("\1\2\3") + (char)CHAR_MAX
; }
1224 #define CHECK2(val, expected) \
1225 os.str(""); os << fixed << setprecision(3) << showpos << val; \
1226 CPPUNIT_ASSERT( os.str() == expected )
1228 // Use unadulterated os2 when expecting inf
1229 #define CHECKINF(val, expected, expected2) \
1230 os2.str(""); os2 << fixed << setprecision(4) << showpos << val; \
1231 CPPUNIT_ASSERT( os2.str() == expected || os2.str() == expected2 )
1233 void NumPutGetTest::custom_numpunct()
1235 ostringstream os
, os2
;
1236 locale
loc(os
.getloc(), new CommaSepNumPunct());
1241 CHECK2(100, "+10,0");
1242 CHECK2(1000, "+1,00,0");
1244 CHECK2(1.234, "+1.234");
1245 CHECK2(123.456, "+12,3.456");
1246 CHECK2(1234.567, "+1,23,4.567");
1247 CHECK2(12345.678, "+12,34,5.678");
1248 CHECK2(123456.789, "+123,45,6.789");
1249 CHECK2(1234567.891, "+1,234,56,7.891");
1250 CHECK2(123456789.123, "+123,456,78,9.123");
1251 //CHECK2(100000000000000000000000000000.0, "+100000000000000000000000,000,00,0.000");
1252 CHECKINF(numeric_limits
<double>::infinity(), "+inf", "+Inf");
1254 CHECK2(-1.234, "-1.234");
1255 CHECK2(-123.456, "-12,3.456");
1256 CHECK2(-1234.567, "-1,23,4.567");
1257 CHECK2(-12345.678, "-12,34,5.678");
1258 CHECK2(-123456.789, "-123,45,6.789");
1259 CHECK2(-1234567.891, "-1,234,56,7.891");
1260 CHECK2(-123456789.123, "-123,456,78,9.123");
1261 //CHECK2(-100000000000000000000000000000.0, "-100000000000000000000000,000,00,0.000");
1262 CHECKINF(-numeric_limits
<double>::infinity(), "-inf", "-Inf");