#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
Kinonoyomeo
- 粉丝: 92
- 资源: 1万+
最新资源
- 基于S7-200 PLC和MCGS组态的水箱水位控制系统设计 组态王动画仿真,带PLC源代码,plc程序每一条都带着解释,组态王源代码,图纸,IO地址分配
- 猫狗识别 matlab代码.rar
- 模拟了一个QAM系统,生成格雷编码的QAM星座,并使用信号处理技术提取载波。matlab代码.rar
- 生成地震动反应谱的matlab函数,输入加速度时程,阻尼比,输出:1列:加速度反应谱,2列:速度反应谱,3列:位移反应谱 matlab代码.rar
- 设计了一个非线性模型预测控制器,以优化双电机电池电动汽车的扭矩分配simulink.rar
- 使用DH参数分析了6自由度UR-5e机器人的正向运动学,并使用牛顿-拉夫森法计算了椭圆轨迹的反向运动学MATLAB中代码.rar
- 实现了一个决策架构和一个自动驾驶汽车的控制律在Carla模拟器中附matlab代码.rar
- 使用Matlab实现卷积深度信念网络(CDBN)Matlab代码.rar
- 使用连续Hopfield网络解决旅行商问题 matlab代码.rar
- 使用STM32和轴编码器创建了闭环直流电机控制系统MATLAB设计和验证PID控制器的稳定性.rar
- 提供了一个MATLAB接口,可用于将MATLAB Production Server的发现服务的专有JSON格式转换为OpenAPI规范.rar
- 数值方法技术在金融中的应用。定量金融与Black-Scholes偏微分方程导论,matlab代码.rar
- 算例说明:上方平板具有z向速度,与静止下表面高度差h=sin(t)+2;其中间牛顿流体用雷诺方程建模,并通过有限差分法求解,包含1D和2D matlab代码.rar
- 湍流MC通道中空气传播特性的CFD方法,matlab代码.rar
- 图像集的视觉搜索 matlab代码.rar
- 通过B样条基函数设计基于知识的潜力用于天然蛋白质检测 matlab代码.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈