Revert "Roll NDK to r11c and extract it into its own repository."
[android_tools.git] / ndk / tests / device / test-stlport / unit / num_put_get_test.cpp
blob2c7d7944eae7a1ac626f65aa52fd75d3fc554f3a
1 #include <limits>
3 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
4 # include <iomanip>
5 # include <string>
6 # include <sstream>
7 # include <cstdio>
8 /*
9 # include <iostream>
10 # include <ieee754.h>
13 # include "complete_digits.h"
14 # include "cppunit/cppunit_proxy.h"
16 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
17 using namespace std;
18 # endif
21 // TestCase class
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);
30 # endif
31 CPPUNIT_TEST(num_put_float);
32 CPPUNIT_TEST(num_put_integer);
33 CPPUNIT_TEST(num_get_float);
34 CPPUNIT_TEST(num_get_integer);
35 CPPUNIT_TEST(inhex);
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 */
41 _clear87();
42 _control87(_float_control_word, MCW_PC|MCW_EM|MCW_IC);
43 # endif
44 CPPUNIT_TEST_SUITE_END();
46 private:
47 void num_put_float();
48 void num_put_integer();
49 void num_get_float();
50 void num_get_integer();
51 void inhex();
52 void pointer();
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();
71 ostr.str("");
72 return tmp;
75 #if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES)
76 template <class F>
77 void check_get_float( F v )
79 F in_val_d = v;
80 typedef numeric_limits<F> limits;
82 stringstream str;
84 str << "1E+" << limits::max_exponent10;
86 str >> in_val_d;
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() );
93 stringstream str;
95 str << "-1E+" << limits::max_exponent10;
97 str >> in_val_d;
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() );
104 stringstream str;
106 str << "1E" << limits::min_exponent10;
108 str >> in_val_d;
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) );
115 stringstream str;
117 str << "1E+" << (limits::max_exponent10 + 1);
119 str >> in_val_d;
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() );
126 stringstream str;
128 str << "-1E+" << (limits::max_exponent10 + 1);
130 str >> in_val_d;
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() );
137 stringstream str;
139 str << "1E" << (limits::min_exponent10 - 1);
141 str >> in_val_d;
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__)
149 stringstream str;
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() );
160 //str.str("");
161 //str << limits::max_exponent10;
162 //CPPUNIT_MESSAGE( str.str().c_str() );
164 str >> in_val_d;
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() );
172 stringstream str;
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() );
183 //str.str("");
184 //str << limits::max_exponent10;
185 //CPPUNIT_MESSAGE( str.str().c_str() );
187 str >> in_val_d;
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() );
195 stringstream str;
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() );
206 //str.str("");
207 //str << limits::max_exponent10;
208 //CPPUNIT_MESSAGE( str.str().c_str() );
210 str >> in_val_d;
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() );
217 #endif
219 stringstream str;
221 str << limits::infinity();
223 CPPUNIT_ASSERT( !str.fail() );
224 CPPUNIT_ASSERT( !limits::has_infinity || str.str() == "inf" || str.str() == "Inf" );
227 stringstream str;
229 str << -limits::infinity();
231 CPPUNIT_ASSERT( !str.fail() );
232 CPPUNIT_ASSERT( !limits::has_infinity || str.str() == "-inf" || str.str() == "-Inf" );
235 stringstream str;
237 str << limits::quiet_NaN();
239 CPPUNIT_ASSERT( !str.fail() );
240 CPPUNIT_ASSERT( !limits::has_quiet_NaN || str.str() == "nan" || str.str() == "NaN" );
243 stringstream str;
245 str << "0." << string(limits::max_exponent10, '0') << "1e" << (limits::max_exponent10 + 1);
246 CPPUNIT_ASSERT( !str.fail() );
248 str >> in_val_d;
249 CPPUNIT_ASSERT( !str.fail() );
250 CPPUNIT_ASSERT( str.eof() );
251 CPPUNIT_CHECK( in_val_d == 1 );
254 stringstream str;
256 str << "1" << string(-(limits::min_exponent10 - 1), '0') << "e" << (limits::min_exponent10 - 1);
257 CPPUNIT_ASSERT( !str.fail() );
259 str >> in_val_d;
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.
270 stringstream str;
271 str << setprecision(limits::digits10 + 2) << limits::max();
273 CPPUNIT_MESSAGE(str.str().c_str());
274 CPPUNIT_ASSERT( !str.fail() );
276 F val;
277 str >> val;
279 CPPUNIT_ASSERT( !str.fail() );
280 CPPUNIT_ASSERT( limits::infinity() > val );
283 stringstream str;
284 str << setprecision(limits::digits10 + 1) << limits::max();
286 CPPUNIT_MESSAGE(str.str().c_str());
287 CPPUNIT_ASSERT( !str.fail() );
289 F val;
290 str >> val;
292 CPPUNIT_ASSERT( !str.fail() );
293 CPPUNIT_ASSERT( limits::infinity() > val );
295 # endif
297 #else
298 # define __check_get_float( F ) \
299 void check_get_float( F v ) \
301 F in_val_d = v; \
303 stringstream str; \
305 str << "1E+" << numeric_limits<F>::max_exponent10; \
307 str >> in_val_d; \
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() ); \
314 stringstream str; \
316 str << "-1E+" << numeric_limits<F>::max_exponent10; \
318 str >> in_val_d; \
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() ); \
325 stringstream str; \
327 str << "1E" << numeric_limits<F>::min_exponent10; \
329 str >> in_val_d; \
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) ); \
336 stringstream str; \
338 str << "1E+" << (numeric_limits<F>::max_exponent10 + 1); \
340 str >> in_val_d; \
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() ); \
347 stringstream str; \
349 str << "-1E+" << (numeric_limits<F>::max_exponent10 + 1); \
351 str >> in_val_d; \
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() ); \
358 stringstream str; \
360 str << "1E" << (numeric_limits<F>::min_exponent10 - 1); \
362 str >> in_val_d; \
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 )
374 # endif
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)
384 #endif
387 // tests implementation
389 void NumPutGetTest::num_put_float()
392 string output, digits;
395 ostringstream ostr;
396 ostr << 1.23457e+17f;
397 CPPUNIT_ASSERT(ostr.good());
398 output = reset_stream(ostr);
399 digits = "17";
400 complete_digits(digits);
401 CPPUNIT_CHECK(output == string("1.23457e+") + digits );
405 ostringstream ostr;
406 ostr << setprecision(200) << 1.23457e+17f;
407 CPPUNIT_ASSERT(ostr.good());
408 output = reset_stream(ostr);
409 CPPUNIT_CHECK( output.size() < 200 );
413 ostringstream ostr;
414 ostr << setprecision(200) << numeric_limits<float>::min();
415 CPPUNIT_ASSERT(ostr.good());
416 output = reset_stream(ostr);
417 CPPUNIT_CHECK( output.size() < 200 );
421 ostringstream ostr;
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");
431 ostringstream ostr;
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");
441 ostringstream ostr;
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" );
451 ostringstream ostr;
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);
455 digits = "1";
456 complete_digits(digits);
457 CPPUNIT_CHECK(output == string("1.23456780e-") + digits );
461 ostringstream ostr;
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");
469 ostringstream ostr;
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");
477 ostringstream ostr;
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");
485 ostringstream ostr;
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");
493 ostringstream ostr;
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)
504 ostringstream ostr;
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" );
512 #endif
515 ostringstream ostr;
516 ostr << scientific << setprecision(50) << 0.0;
517 CPPUNIT_ASSERT(ostr.good());
518 output = reset_stream(ostr);
519 CPPUNIT_CHECK( output == "0.00000000000000000000000000000000000000000000000000e+00" );
522 ostringstream ostr;
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() );
530 ostringstream ostr;
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)
539 ostringstream ostr;
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() );
545 #endif
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)
564 // {
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() );
570 // }
571 //#endif
575 ostringstream str;
577 str.setf(ios::fixed, ios::floatfield);
578 str << 1.0e+5;
579 // cerr << str.str() << endl;
580 CPPUNIT_CHECK( str.str() == "100000.000000" );
582 reset_stream(str);
583 str.precision(0);
584 str << 1.0e+5;
585 CPPUNIT_CHECK( str.str() == "100000" );
587 reset_stream(str);
588 str.precision(4);
589 str << 1.0e+5;
590 CPPUNIT_CHECK( str.str() == "100000.0000" );
592 reset_stream(str);
593 str.precision(0);
594 str << 1.0e+83;
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) \
608 type tmp = val; \
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()
619 //octal outputs
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")
662 #endif
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")
673 //decimal outputs
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")
706 #endif
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")
756 #endif
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()
773 float in_val;
775 istringstream istr;
777 istr.str("1.2345");
778 istr >> in_val;
779 CPPUNIT_ASSERT(!istr.fail());
780 CPPUNIT_ASSERT(istr.eof());
781 CPPUNIT_ASSERT(check_float(in_val, 1.2345f));
782 istr.clear();
784 istr.str("-1.2345");
785 istr >> in_val;
786 CPPUNIT_ASSERT(!istr.fail());
787 CPPUNIT_ASSERT(istr.eof());
788 CPPUNIT_ASSERT(check_float(in_val, -1.2345f));
789 istr.clear();
791 istr.str("+1.2345");
792 istr >> in_val;
793 CPPUNIT_ASSERT(!istr.fail());
794 CPPUNIT_ASSERT(check_float(in_val, 1.2345f));
795 istr.clear();
797 istr.str("000000000000001.234500000000");
798 istr >> in_val;
799 CPPUNIT_ASSERT(!istr.fail());
800 CPPUNIT_ASSERT(istr.eof());
801 CPPUNIT_ASSERT(check_float(in_val, 1.2345f));
802 istr.clear();
804 istr.str("1.2345e+04");
805 istr >> in_val;
806 CPPUNIT_ASSERT(!istr.fail());
807 CPPUNIT_ASSERT(istr.eof());
808 CPPUNIT_ASSERT(check_float(in_val, 12345.0f));
809 istr.clear();
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 );
818 #endif
820 stringstream str;
822 str << "1e" << numeric_limits<double>::max_exponent10;
823 CPPUNIT_ASSERT(!str.fail());
825 float val;
826 str >> val;
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() );
833 stringstream str;
835 str << "1e" << numeric_limits<double>::min_exponent10;
836 CPPUNIT_ASSERT(!str.fail());
838 float val;
839 str >> val;
840 CPPUNIT_ASSERT(!str.fail());
841 CPPUNIT_ASSERT(str.eof());
842 CPPUNIT_ASSERT( numeric_limits<double>::min_exponent10 >= numeric_limits<float>::min_exponent10 ||
843 val == 0.0f );
845 #if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE)
847 stringstream str;
849 str << "1e" << numeric_limits<long double>::max_exponent10;
850 CPPUNIT_ASSERT(!str.fail());
852 double val;
853 str >> val;
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() );
860 stringstream str;
862 str << "1e" << numeric_limits<long double>::min_exponent10;
863 CPPUNIT_ASSERT(!str.fail());
865 double val;
866 str >> val;
867 CPPUNIT_ASSERT(!str.fail());
868 CPPUNIT_ASSERT(str.eof());
869 CPPUNIT_ASSERT( numeric_limits<long double>::min_exponent10 >= numeric_limits<double>::min_exponent10 ||
870 val == 0.0 );
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";
875 std::stringstream s;
876 s << p;
877 long double x;
878 s >> x;
879 CPPUNIT_ASSERT( x > 2.70l && x < 2.72l );
881 #endif
882 #endif
885 void NumPutGetTest::num_get_integer()
887 //octal input
889 istringstream istr;
890 istr.str("30071");
891 short val;
892 istr >> oct >> val;
893 CPPUNIT_ASSERT( !istr.fail() );
894 CPPUNIT_ASSERT( istr.eof() );
895 CPPUNIT_ASSERT( val == 12345 );
896 istr.clear();
898 if (sizeof(short) == 2) {
899 istr.str("177777");
900 istr >> oct >> val;
901 CPPUNIT_ASSERT( !istr.fail() );
902 CPPUNIT_ASSERT( istr.eof() );
903 CPPUNIT_ASSERT( val == -1 );
904 istr.clear();
908 //decimal input
910 istringstream istr;
911 istr.str("10000");
912 short val = -1;
913 istr >> val;
914 CPPUNIT_ASSERT( !istr.fail() );
915 CPPUNIT_ASSERT( istr.eof() );
916 CPPUNIT_ASSERT( val == 10000 );
917 istr.clear();
919 istr.str("+10000");
920 val = -1;
921 istr >> val;
922 CPPUNIT_ASSERT( !istr.fail() );
923 CPPUNIT_ASSERT( istr.eof() );
924 CPPUNIT_ASSERT( val == 10000 );
925 istr.clear();
927 if (sizeof(short) == 2) {
928 val = -1;
929 istr.str("10000000");
930 istr >> val;
931 CPPUNIT_ASSERT( istr.fail() );
932 CPPUNIT_ASSERT( istr.eof() );
933 CPPUNIT_ASSERT( val == -1 );
934 istr.clear();
937 val = -1;
938 istr.str("0x0");
939 istr >> val;
940 CPPUNIT_ASSERT( !istr.fail() );
941 CPPUNIT_ASSERT( !istr.eof() );
942 CPPUNIT_ASSERT( val == 0 );
943 istr.clear();
945 val = -1;
946 istr.str("000001");
947 istr >> val;
948 CPPUNIT_ASSERT( !istr.fail() );
949 CPPUNIT_ASSERT( istr.eof() );
950 CPPUNIT_ASSERT( val == 1 );
951 istr.clear();
954 //hexadecimal input
956 istringstream istr;
957 istr.str("3039");
958 short val = -1;
959 istr >> hex >> val;
960 CPPUNIT_ASSERT( !istr.fail() );
961 CPPUNIT_ASSERT( istr.eof() );
962 CPPUNIT_ASSERT( val == 12345 );
963 istr.clear();
965 istr.str("x3039");
966 val = -1;
967 istr >> hex >> val;
968 CPPUNIT_ASSERT( istr.fail() );
969 CPPUNIT_ASSERT( !istr.eof() );
970 CPPUNIT_ASSERT( val == -1 );
971 istr.clear();
973 istr.str("03039");
974 val = -1;
975 istr >> hex >> val;
976 CPPUNIT_ASSERT( !istr.fail() );
977 CPPUNIT_ASSERT( istr.eof() );
978 CPPUNIT_ASSERT( val == 12345 );
979 istr.clear();
981 istr.str("0x3039");
982 istr >> hex >> val;
983 CPPUNIT_ASSERT( !istr.fail() );
984 CPPUNIT_ASSERT( istr.eof() );
985 CPPUNIT_ASSERT( val == 12345 );
986 istr.clear();
988 if (sizeof(short) == 2) {
989 val = -1;
990 istr.str("cfc7");
991 istr >> hex >> val;
992 CPPUNIT_ASSERT( !istr.fail() );
993 CPPUNIT_ASSERT( istr.eof() );
994 CPPUNIT_ASSERT( val == -12345 );
995 istr.clear();
1000 void NumPutGetTest::inhex()
1003 ostringstream s;
1004 s << hex << 0;
1005 CPPUNIT_CHECK( s.str() == "0" );
1008 ostringstream s;
1009 s << hex << 0xff;
1010 CPPUNIT_CHECK( s.str() == "ff" );
1013 ostringstream s;
1014 s << hex << setw( 4 ) << 0xff;
1015 CPPUNIT_CHECK( s.str() == " ff" );
1018 ostringstream s;
1019 s << hex << setw( 4 ) << 0;
1020 CPPUNIT_CHECK( s.str() == " 0" );
1023 ostringstream s;
1024 s << hex << showbase << 0;
1025 CPPUNIT_CHECK( s.str() == "0" );
1028 ostringstream s;
1029 s << hex << showbase << 0xff;
1030 CPPUNIT_CHECK( s.str() == "0xff" );
1033 ostringstream s;
1034 s << hex << showbase << setw( 4 ) << 0xff;
1035 CPPUNIT_CHECK( s.str() == "0xff" );
1037 { // special case for regression (partially duplicate CHECK_COMPLETE above):
1038 ostringstream s;
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.
1058 char buf[128];
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 );
1069 } else {
1070 CPPUNIT_CHECK( sizeof( p ) == 2 || sizeof( p ) == 4 || sizeof( p ) == 8 );
1076 char buf[128];
1077 void *p = 0;
1079 // sprintf( buf, "%p", p );
1080 /* Cool. "%p" print '(nil)'; "%#x" print '0' */
1081 // sprintf( buf, "%#x", (unsigned)p );
1082 // cerr << buf << endl;
1085 ostringstream s;
1086 void *p = (void *)0xff00;
1087 s << p;
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" );
1095 } else {
1096 CPPUNIT_CHECK( sizeof( p ) == 2 || sizeof( p ) == 4 || sizeof( p ) == 8 );
1100 ostringstream s;
1101 void *p = 0;
1102 s << p;
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" );
1110 } else {
1111 CPPUNIT_CHECK( sizeof( p ) == 2 || sizeof( p ) == 4 || sizeof( p ) == 8 );
1116 void NumPutGetTest::fix_float_long()
1118 ostringstream str;
1120 str.setf(ios::fixed, ios::floatfield);
1121 str << 1.0e+5;
1122 CPPUNIT_CHECK( str.str() == "100000.000000" );
1124 reset_stream(str);
1125 str.precision(0);
1126 str << 1.0e+5;
1127 CPPUNIT_CHECK( str.str() == "100000" );
1129 reset_stream(str);
1130 str.precision(4);
1131 str << 1.0e+5;
1132 CPPUNIT_CHECK( str.str() == "100000.0000" );
1134 reset_stream(str);
1135 str.precision(0);
1136 str << 1.0e+83;
1138 istringstream istr( str.str() );
1139 double f;
1140 istr >> f;
1141 CPPUNIT_CHECK( !istr.fail() );
1142 if ( int(numeric_limits<double>::digits10) < 83 ) {
1143 double delta = 1.0;
1144 for ( int ee = 83 - int(numeric_limits<double>::digits10); ee > 0; --ee ) {
1145 delta *= 10.0;
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)) );
1149 } else {
1150 CPPUNIT_CHECK( check_double(f, 1.0e+83) );
1154 #if 0 // #ifndef _STLP_NO_LONG_DOUBLE
1155 reset_stream(str);
1156 str.precision(0);
1157 str << 1.0e+83l;
1159 istringstream istr( str.str() );
1160 long double f;
1161 istr >> f;
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 ) {
1166 delta *= 10.0l;
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;
1172 cerr << f << endl;
1173 CPPUNIT_CHECK( (f > (1.0e+83l - delta)) && (f < (1.0e+83l + delta)) );
1174 } else {
1175 CPPUNIT_CHECK( check_double(f, 1.0e+83l) );
1178 #endif
1180 reset_stream(str);
1181 str.precision(0);
1182 str << numeric_limits<double>::max();
1184 istringstream istr( str.str() );
1185 double f;
1186 istr >> f;
1187 CPPUNIT_CHECK( !istr.fail() );
1188 if ( int(numeric_limits<double>::digits10) < int(numeric_limits<double>::max_exponent10) ) {
1189 double delta = 9.0;
1190 for ( int ee = int(numeric_limits<double>::max_exponent10) - int(numeric_limits<double>::digits10); ee > 0; --ee ) {
1191 delta *= 10.0;
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
1199 reset_stream(str);
1200 str.precision(0);
1201 str << numeric_limits<long double>::max();
1203 istringstream istr( str.str() );
1204 long double f;
1205 istr >> f;
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 ) {
1210 delta *= 10.0l;
1212 // we may loss some digits here, but not more than mantissa:
1213 CPPUNIT_CHECK( (f > (numeric_limits<long double>::max() - delta)) );
1216 #endif
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());
1237 os.imbue(loc);
1239 CHECK2(1, "+1");
1240 CHECK2(10, "+1,0");
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");
1265 #endif