📄 fpetest.cpp
字号:
//
// FPETest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/FPETest.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "FPETest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Foundation/FPEnvironment.h"
using Foundation::FPE;
FPETest::FPETest(const std::string& name): CppUnit::TestCase(name)
{
}
FPETest::~FPETest()
{
}
void FPETest::testClassify()
{
{
float a = 0.0f;
float b = 0.0f;
float nan = a/b;
float inf = 1.0f/b;
assert (FPE::isNaN(nan));
assert (!FPE::isNaN(a));
assert (FPE::isInfinite(inf));
assert (!FPE::isInfinite(a));
}
{
double a = 0;
double b = 0;
double nan = a/b;
double inf = 1.0/b;
assert (FPE::isNaN(nan));
assert (!FPE::isNaN(a));
assert (FPE::isInfinite(inf));
assert (!FPE::isInfinite(a));
}
}
#if defined(__HP_aCC)
#pragma OPTIMIZE OFF
#elif defined(_MSC_VER)
#pragma optimize("", off)
#endif
double mult(double a, double b)
{
return a*b;
}
double div(double a, double b)
{
return a/b;
}
void FPETest::testFlags()
{
FPE::clearFlags();
// some compilers are intelligent enough to optimize the calculations below away.
// unfortunately this leads to a failing test, so we have to trick out the
// compiler's optimizer a little bit by doing something with the results.
double a = 10;
double b = 0;
double c = div(a, b);
assert (FPE::isFlag(FPE::FP_DIVIDE_BY_ZERO));
assert (FPE::isInfinite(c));
FPE::clearFlags();
a = 1.23456789e210;
b = 9.87654321e210;
c = mult(a, b);
assert (FPE::isFlag(FPE::FP_OVERFLOW));
assertEqualDelta(c, c, 0);
FPE::clearFlags();
a = 1.23456789e-99;
b = 9.87654321e210;
c = div(a, b);
assert (FPE::isFlag(FPE::FP_UNDERFLOW));
assertEqualDelta(c, c, 0);
}
#if defined(__HP_aCC)
#pragma OPTIMIZE ON
#elif defined(_MSC_VER)
#pragma optimize("", on)
#endif
void FPETest::testRound()
{
#if !defined(__osf__) && !defined(__VMS)
FPE::setRoundingMode(FPE::FP_ROUND_TONEAREST);
assert (FPE::getRoundingMode() == FPE::FP_ROUND_TONEAREST);
{
FPE env(FPE::FP_ROUND_TOWARDZERO);
assert (FPE::getRoundingMode() == FPE::FP_ROUND_TOWARDZERO);
}
assert (FPE::getRoundingMode() == FPE::FP_ROUND_TONEAREST);
#endif
}
void FPETest::setUp()
{
}
void FPETest::tearDown()
{
}
CppUnit::Test* FPETest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FPETest");
CppUnit_addTest(pSuite, FPETest, testClassify);
CppUnit_addTest(pSuite, FPETest, testFlags);
CppUnit_addTest(pSuite, FPETest, testRound);
return pSuite;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -