~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/testsuite/src/FPETest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// FPETest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/testsuite/src/FPETest.cpp#1 $
 
5
//
 
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person or organization
 
10
// obtaining a copy of the software and accompanying documentation covered by
 
11
// this license (the "Software") to use, reproduce, display, distribute,
 
12
// execute, and transmit the Software, and to prepare derivative works of the
 
13
// Software, and to permit third-parties to whom the Software is furnished to
 
14
// do so, all subject to the following:
 
15
// 
 
16
// The copyright notices in the Software and this entire statement, including
 
17
// the above license grant, this restriction and the following disclaimer,
 
18
// must be included in all copies of the Software, in whole or in part, and
 
19
// all derivative works of the Software, unless such copies or derivative
 
20
// works are solely in the form of machine-executable object code generated by
 
21
// a source language processor.
 
22
// 
 
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
29
// DEALINGS IN THE SOFTWARE.
 
30
//
 
31
 
 
32
 
 
33
#include "FPETest.h"
 
34
#include "CppUnit/TestCaller.h"
 
35
#include "CppUnit/TestSuite.h"
 
36
#include "Poco/FPEnvironment.h"
 
37
 
 
38
 
 
39
using Poco::FPE;
 
40
 
 
41
 
 
42
FPETest::FPETest(const std::string& name): CppUnit::TestCase(name)
 
43
{
 
44
}
 
45
 
 
46
 
 
47
FPETest::~FPETest()
 
48
{
 
49
}
 
50
 
 
51
 
 
52
void FPETest::testClassify()
 
53
{
 
54
        {
 
55
                float a = 0.0f;
 
56
                float b = 0.0f;
 
57
                float nan = a/b;
 
58
                float inf = 1.0f/b;
 
59
                
 
60
                assert (FPE::isNaN(nan));
 
61
                assert (!FPE::isNaN(a));
 
62
                assert (FPE::isInfinite(inf));
 
63
                assert (!FPE::isInfinite(a));
 
64
        }
 
65
        {
 
66
                double a = 0;
 
67
                double b = 0;
 
68
                double nan = a/b;
 
69
                double inf = 1.0/b;
 
70
                
 
71
                assert (FPE::isNaN(nan));
 
72
                assert (!FPE::isNaN(a));
 
73
                assert (FPE::isInfinite(inf));
 
74
                assert (!FPE::isInfinite(a));
 
75
        }
 
76
}
 
77
 
 
78
 
 
79
#if defined(__HP_aCC)
 
80
#pragma OPTIMIZE OFF
 
81
#elif defined(_MSC_VER)
 
82
#pragma optimize("", off)
 
83
#endif
 
84
 
 
85
 
 
86
double mult(double a, double b)
 
87
{
 
88
        return a*b;
 
89
}
 
90
 
 
91
 
 
92
double div(double a, double b)
 
93
{
 
94
        return a/b;
 
95
}
 
96
 
 
97
 
 
98
void FPETest::testFlags()
 
99
{
 
100
        FPE::clearFlags();
 
101
 
 
102
        // some compilers are intelligent enough to optimize the calculations below away.
 
103
        // unfortunately this leads to a failing test, so we have to trick out the
 
104
        // compiler's optimizer a little bit by doing something with the results.
 
105
        double a = 10;
 
106
        double b = 0;
 
107
        double c = div(a, b);
 
108
 
 
109
        assert (FPE::isFlag(FPE::FP_DIVIDE_BY_ZERO));
 
110
        assert (FPE::isInfinite(c)); 
 
111
 
 
112
        FPE::clearFlags();
 
113
        a = 1.23456789e210;
 
114
        b = 9.87654321e210;
 
115
        c = mult(a, b);
 
116
        assert (FPE::isFlag(FPE::FP_OVERFLOW));
 
117
        assertEqualDelta(c, c, 0);
 
118
 
 
119
        FPE::clearFlags();
 
120
        a = 1.23456789e-99;
 
121
        b = 9.87654321e210;
 
122
        c = div(a, b);  
 
123
        assert (FPE::isFlag(FPE::FP_UNDERFLOW));
 
124
        assertEqualDelta(c, c, 0);
 
125
}
 
126
 
 
127
 
 
128
#if defined(__HP_aCC)
 
129
#pragma OPTIMIZE ON
 
130
#elif defined(_MSC_VER)
 
131
#pragma optimize("", on)
 
132
#endif
 
133
 
 
134
 
 
135
void FPETest::testRound()
 
136
{
 
137
        #if !defined(__osf__) && !defined(__VMS)
 
138
        FPE::setRoundingMode(FPE::FP_ROUND_TONEAREST);                  
 
139
        assert (FPE::getRoundingMode() == FPE::FP_ROUND_TONEAREST);
 
140
        {
 
141
                FPE env(FPE::FP_ROUND_TOWARDZERO);
 
142
                assert (FPE::getRoundingMode() == FPE::FP_ROUND_TOWARDZERO);
 
143
        }
 
144
        assert (FPE::getRoundingMode() == FPE::FP_ROUND_TONEAREST);     
 
145
        #endif
 
146
}
 
147
 
 
148
 
 
149
void FPETest::setUp()
 
150
{
 
151
}
 
152
 
 
153
 
 
154
void FPETest::tearDown()
 
155
{
 
156
}
 
157
 
 
158
 
 
159
CppUnit::Test* FPETest::suite()
 
160
{
 
161
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FPETest");
 
162
 
 
163
        CppUnit_addTest(pSuite, FPETest, testClassify);
 
164
        CppUnit_addTest(pSuite, FPETest, testFlags);
 
165
        CppUnit_addTest(pSuite, FPETest, testRound);
 
166
 
 
167
        return pSuite;
 
168
}