#include <limits>
#if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
# include <iomanip>
# include <string>
# include <sstream>
# include <cstdio>
/*
# include <iostream>
# include <ieee754.h>
*/
# include "complete_digits.h"
# include "cppunit/cppunit_proxy.h"
# if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
using namespace std;
# endif
//
// TestCase class
//
class NumPutGetTest : public CPPUNIT_NS::TestCase
{
CPPUNIT_TEST_SUITE(NumPutGetTest);
# if defined (__BORLANDC__)
/* Ignore FPU exceptions, set FPU precision to 64 bits */
unsigned int _float_control_word = _control87(0, 0);
_control87(PC_64|MCW_EM|IC_AFFINE, MCW_PC|MCW_EM|MCW_IC);
# endif
CPPUNIT_TEST(num_put_float);
CPPUNIT_TEST(num_put_integer);
CPPUNIT_TEST(num_get_float);
CPPUNIT_TEST(num_get_integer);
CPPUNIT_TEST(inhex);
CPPUNIT_TEST(pointer);
CPPUNIT_TEST(fix_float_long);
CPPUNIT_TEST(custom_numpunct);
# if defined (__BORLANDC__)
/* Reset floating point control word */
_clear87();
_control87(_float_control_word, MCW_PC|MCW_EM|MCW_IC);
# endif
CPPUNIT_TEST_SUITE_END();
private:
void num_put_float();
void num_put_integer();
void num_get_float();
void num_get_integer();
void inhex();
void pointer();
void fix_float_long();
void custom_numpunct();
static bool check_float(float val, float ref)
{
float epsilon = numeric_limits<float>::epsilon();
return val <= ref + epsilon && val >= ref - epsilon;
}
static bool check_double(double val, double ref)
{
double epsilon = numeric_limits<double>::epsilon();
return val <= ref + epsilon && val >= ref - epsilon;
}
static string reset_stream(ostringstream &ostr)
{
string tmp = ostr.str();
ostr.str("");
return tmp;
}
#if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES)
template <class F>
void check_get_float( F v )
{
F in_val_d = v;
typedef numeric_limits<F> limits;
{
stringstream str;
str << "1E+" << limits::max_exponent10;
str >> in_val_d;
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_ASSERT(str.eof());
CPPUNIT_CHECK( in_val_d == in_val_d );
CPPUNIT_CHECK( in_val_d != limits::infinity() );
}
{
stringstream str;
str << "-1E+" << limits::max_exponent10;
str >> in_val_d;
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_ASSERT(str.eof());
CPPUNIT_CHECK( in_val_d == in_val_d );
CPPUNIT_CHECK( in_val_d != -limits::infinity() );
}
{
stringstream str;
str << "1E" << limits::min_exponent10;
str >> in_val_d;
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_ASSERT(str.eof());
CPPUNIT_CHECK( in_val_d == in_val_d );
CPPUNIT_CHECK( in_val_d != F(0.0) );
}
{
stringstream str;
str << "1E+" << (limits::max_exponent10 + 1);
str >> in_val_d;
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_ASSERT(str.eof());
CPPUNIT_CHECK( in_val_d == in_val_d );
CPPUNIT_CHECK( in_val_d == limits::infinity() );
}
{
stringstream str;
str << "-1E+" << (limits::max_exponent10 + 1);
str >> in_val_d;
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_ASSERT(str.eof());
CPPUNIT_CHECK( in_val_d == in_val_d );
CPPUNIT_CHECK( in_val_d == -limits::infinity() );
}
{
stringstream str;
str << "1E" << (limits::min_exponent10 - 1);
str >> in_val_d;
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_ASSERT(str.eof());
CPPUNIT_CHECK( in_val_d == in_val_d );
CPPUNIT_CHECK( in_val_d >= F(0.0) && in_val_d <= limits::min() );
}
#if !defined (__MINGW32__)
{
stringstream str;
str << limits::max();
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_CHECK( str.str() != "inf" );
CPPUNIT_CHECK( str.str() != "-inf" );
CPPUNIT_CHECK( str.str() != "nan" );
CPPUNIT_CHECK( str.str() != "-nan" );
//CPPUNIT_MESSAGE( str.str().c_str() );
//str.str("");
//str << limits::max_exponent10;
//CPPUNIT_MESSAGE( str.str().c_str() );
str >> in_val_d;
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_ASSERT(str.eof());
CPPUNIT_CHECK( in_val_d == in_val_d );
CPPUNIT_CHECK( in_val_d != limits::infinity() );
}
{
stringstream str;
str << fixed << limits::max();
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_CHECK( str.str() != "inf" );
CPPUNIT_CHECK( str.str() != "-inf" );
CPPUNIT_CHECK( str.str() != "nan" );
CPPUNIT_CHECK( str.str() != "-nan" );
//CPPUNIT_MESSAGE( str.str().c_str() );
//str.str("");
//str << limits::max_exponent10;
//CPPUNIT_MESSAGE( str.str().c_str() );
str >> in_val_d;
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_ASSERT(str.eof());
CPPUNIT_CHECK( in_val_d == in_val_d );
CPPUNIT_CHECK( in_val_d != limits::infinity() );
}
{
stringstream str;
str << scientific << setprecision(50) << limits::max();
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_CHECK( str.str() != "inf" );
CPPUNIT_CHECK( str.str() != "-inf" );
CPPUNIT_CHECK( str.str() != "nan" );
CPPUNIT_CHECK( str.str() != "-nan" );
//CPPUNIT_MESSAGE( str.str().c_str() );
//str.str("");
//str << limits::max_exponent10;
//CPPUNIT_MESSAGE( str.str().c_str() );
str >> in_val_d;
CPPUNIT_ASSERT(!str.fail());
CPPUNIT_ASSERT(str.eof());
CPPUNIT_CHECK( in_val_d == in_val_d );
CPPUNIT_CHECK( in_val_d != limits::infinity() );
}
#endif
{
stringstream str;
str << limits::infinity();
CPPUNIT_ASSERT( !str.fail() );
CPPUNIT_ASSERT( !limits::has_infinity || str.str() == "inf" );
}
{
stringstream str;
str << -limits::infinity();
CPPUNIT_ASSERT( !str.fail() );
CPPUNIT_ASSERT( !limits::has_infinity || str.str() == "-inf" );
}
{
stringstream str;
str << limits::quiet_NaN();
CPPUNIT_ASSERT( !str.fail() );
CPPUNIT_ASSERT( !limits::has_quiet_NaN || str.str() == "nan" );
}
{
stringstream str;
str << -limits::quiet_NaN();
CPPUNIT_ASSERT( !str.fail() );
CPPUNIT_ASSERT( !limits::has_quiet_NaN || str.str() == "-nan" );
}
{
stringstream str;
str << "0." << string(limits::max_exponent10, '0') << "1e" << (limits::max_exponent10 + 1);
CPPUNIT_ASSERT( !str.fail() );
str >> in_val_d;
CPPUNIT_ASSERT( !str.fail() );
CPPUNIT_ASSERT( str.eof() );
CPPUNIT_CHECK( in_val_d == 1 );
}
{
stringstream str;
str << "1" << string(-(limits::min_exponent10 - 1), '0') << "e" << (limits::min_exponent10 - 1);
CPPUNIT_ASSERT( !str.fail() );
str >> in_val_d;
CPPUNIT_ASSERT( !str.fail() );
CPPUNIT_ASSERT( str.eof() );
CPPUNIT_CHECK( in_val_d == 1 );
}
# if defined (_STLPORT_VERSION) && (_STLPORT_VERSION >= 0x530)
// The following tests are showing that simply changing stream
// precision lead to different result. Do not seems to be a real
// problem, simply rounding approximation but additional study should
// be done after 5.2 release.
{
stringstream str;
str << setprecision(limits::digits10 + 2) << limits::max();
CPPUNIT_MESSAGE(str.str().c_str());
CPPUNIT_ASSERT( !str.fail() );
F val;
str >> val;
CPPUNIT_ASSERT( !str.fail() );
CPPUNIT_ASSERT( limits::infinity() > val );
}
{
stringstream str;
str << setprecision(limits::digits10 + 1) << limits::max();
CPPUNIT_MESSAGE(str.str().c_str());
CPPUNIT_ASSERT( !str.fail() );
F val;
str >> val;
CPPUNIT_ASSERT( !str.fail() );
CPPUNIT_ASSERT( limits::infinity() > val );
}
# endif
}
#else
# define __check_get_float( F ) \
void check_get_float( F v ) \
{ \
F in_val_d = v; \
{ \
stringstream str; \
\
str << "1E+" << numeric_limits<F
没有合适的资源?快使用搜索试试~ 我知道了~
num_put_get_test.rar_Get Real
共1个文件
c:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 181 浏览量
2022-09-23
17:20:42
上传
评论
收藏 5KB RAR 举报
温馨提示
The following tests are showing that simply changing stream precision lead to different result. Do not seems to be a real problem, simply rounding approximation but additional study should be done after 5.2 release.
资源推荐
资源详情
资源评论
收起资源包目录
num_put_get_test.rar (1个子文件)
num_put_get_test.c 35KB
共 1 条
- 1
资源评论
Kinonoyomeo
- 粉丝: 91
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功