double/long double inserters test cases
Kevin Ediger
kediger@licor.com
Fri Nov 12 14:07:00 GMT 1999
Here are the test cases. I wasn't sure how to integrate my
tests into the testsuite so I'm just sending my file. If there's
something I can do to integrate these, please let me know.
File: numput.cc
===================================================================
/*
* numput.cc
*
* test the floating point inserters (facet num_put)
*/
#include <iostream>
#include <sstream>
#include <limits>
#include <assert.h>
using namespace std;
//#define __TEST_NUMPUT_VERBOSE 1
struct _TestCase
{
double val;
int precision;
int width;
char decimal;
char fill;
bool fixed;
bool scientific;
bool showpos;
bool showpoint;
bool uppercase;
bool internal;
bool left;
bool right;
const char * result;
const wchar_t * wresult;
};
static bool T=true;
static bool F=false;
static _TestCase testcases[] =
{
// standard output (no formatting applied)
{ 1.2, 6,0,'.',' ', F,F,F,F,F,F,F,F, "1.2",L"1.2" },
{ 54, 6,0,'.',' ', F,F,F,F,F,F,F,F, "54",L"54" },
{ -.012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-0.012",L"-0.012" },
{ -.00000012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-1.2e-07",L"-1.2e-07" },
// fixed formatting
{ 10.2345, 0,0,'.',' ', T,F,F,F,F,F,F,F, "10",L"10" },
{ 10.2345, 0,0,'.',' ', T,F,F,T,F,F,F,F, "10.",L"10." },
{ 10.2345, 1,0,'.',' ', T,F,F,F,F,F,F,F, "10.2",L"10.2" },
{ 10.2345, 4,0,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
{ 10.2345, 6,0,'.',' ', T,F,T,F,F,F,F,F, "+10.234500",L"+10.234500" },
{ -10.2345, 6,0,'.',' ', T,F,F,F,F,F,F,F, "-10.234500",L"-10.234500" },
{ -10.2345, 6,0,',',' ', T,F,F,F,F,F,F,F, "-10,234500",L"-10,234500" },
// fixed formatting with width
{ 10.2345, 4,5,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
{ 10.2345, 4,6,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
{ 10.2345, 4,7,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
{ 10.2345, 4,8,'.',' ', T,F,F,F,F,F,F,F, " 10.2345",L" 10.2345" },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,F, " 10.2345",L" 10.2345" },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,F,T,F, "10.2345 ",L"10.2345 " },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,T, " 10.2345",L" 10.2345" },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, " 10.2345",L" 10.2345" },
{ -10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, "- 10.2345",L"- 10.2345" },
{ -10.2345, 4,10,'.','A', T,F,F,F,F,T,F,F, "-AA10.2345",L"-AA10.2345" },
{ 10.2345, 4,10,'.','#', T,F,T,F,F,T,F,F, "+##10.2345",L"+##10.2345" },
// scientific formatting
{ 1.23e+12, 1,0,'.',' ', F,T,F,F,F,F,F,F, "1.2e+12",L"1.2e+12" },
{ 1.23e+12, 1,0,'.',' ', F,T,F,F,T,F,F,F, "1.2E+12",L"1.2E+12" },
{ 1.23e+12, 2,0,'.',' ', F,T,F,F,F,F,F,F, "1.23e+12",L"1.23e+12" },
{ 1.23e+12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "1.230e+12",L"1.230e+12" },
{ 1.23e+12, 3,0,'.',' ', F,T,T,F,F,F,F,F, "+1.230e+12",L"+1.230e+12" },
{ -1.23e-12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "-1.230e-12",L"-1.230e-12" },
{ 1.23e+12, 3,0,',',' ', F,T,F,F,F,F,F,F, "1,230e+12",L"1,230e+12" },
};
template<typename _CharT>
class testpunct : public numpunct<_CharT>
{
public:
typedef _CharT char_type;
explicit
testpunct(char_type decimal_char) : numpunct<_CharT>()
{
_M_init(decimal_char,',',"");
}
};
template<typename _CharT>
void apply_formatting( const _TestCase & tc,basic_ostream<_CharT> & os )
{
os.precision(tc.precision);
os.width(tc.width);
os.fill(static_cast<_CharT>(tc.fill));
if ( tc.fixed )
os.setf( ios::fixed );
if ( tc.scientific )
os.setf( ios::scientific );
if ( tc.showpos )
os.setf( ios::showpos );
if ( tc.showpoint )
os.setf( ios::showpoint );
if ( tc.uppercase )
os.setf( ios::uppercase );
if ( tc.internal )
os.setf( ios::internal );
if ( tc.left )
os.setf( ios::left );
if ( tc.right )
os.setf( ios::right );
}
void test01()
{
for ( int j=0;j<sizeof(testcases)/sizeof(testcases[0]);j++ )
{
_TestCase & tc = testcases[j];
#ifdef __TEST_NUMPUT_VERBOSE
cout << "expect: " << tc.result << endl;
#endif
/* test double with char type */
{
testpunct<char> * __tp = new testpunct<char>(tc.decimal);
ostringstream os;
locale __loc(os.getloc(),__tp);
os.imbue(__loc);
apply_formatting(tc,os);
os << tc.val;
#ifdef __TEST_NUMPUT_VERBOSE
cout << "result: " << os.str() << endl;
#endif
assert( os && os.str() == tc.result );
}
/* test long double with char type */
{
testpunct<char> * __tp = new testpunct<char>(tc.decimal);
ostringstream os;
locale __loc(os.getloc(),__tp);
os.imbue(__loc);
apply_formatting(tc,os);
os << (long double)tc.val;
#ifdef __TEST_NUMPUT_VERBOSE
cout << "result: " << os.str() << endl;
#endif
assert( os && os.str() == tc.result );
}
/* test double with wchar_t type */
{
testpunct<wchar_t> * __tp = new testpunct<wchar_t>(tc.decimal);
wostringstream os;
locale __loc(os.getloc(),__tp);
os.imbue(__loc);
apply_formatting(tc,os);
os << tc.val;
assert( os && os.str() == tc.wresult );
}
/* test long double with wchar_t type */
{
testpunct<wchar_t> * __tp = new testpunct<wchar_t>(tc.decimal);
wostringstream os;
locale __loc(os.getloc(),__tp);
os.imbue(__loc);
apply_formatting(tc,os);
os << (long double)tc.val;
assert( os && os.str() == tc.wresult );
}
}
}
void test02()
{
long double val = 1.2345678901234567890123456789e+1000L; // make sure we can output a very long float
int prec = numeric_limits<long double>::digits10;
ostringstream os;
os.precision(prec);
os.setf( ios::scientific );
os << val;
char largebuf[512];
sprintf(largebuf,"%.*Le",prec,val);
#ifdef __TEST_NUMPUT_VERBOSE
cout << "expect: " << largebuf << endl;
cout << "result: " << os.str() << endl;
#endif
assert( os && os.str() == largebuf );
}
int main()
{
test01();
test02();
#ifdef __TEST_NUMPUT_VERBOSE
cout << "Test passed!" << endl;
#endif
return 0;
}
===================================================================
Best Regards,
Kevin Ediger
--
LI-COR, Inc.
Voice: 402.467.3576 ext 3695, Fax: 402.467.0872
mailto:kediger@licor.com
http://www.licor.com
More information about the Libstdc++
mailing list