LCOV - code coverage report
Current view: top level - tests - test_as2js_float64.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 259 266 97.4 %
Date: 2014-11-22 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* test_as2js_float64.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : #include    "test_as2js_float64.h"
      37             : #include    "test_as2js_main.h"
      38             : 
      39             : #include    "as2js/float64.h"
      40             : #include    "as2js/exceptions.h"
      41             : 
      42             : #include    <cstring>
      43             : #include    <algorithm>
      44             : 
      45             : #include    <cppunit/config/SourcePrefix.h>
      46           4 : CPPUNIT_TEST_SUITE_REGISTRATION( As2JsFloat64UnitTests );
      47             : 
      48             : 
      49             : 
      50             : #pragma GCC diagnostic push
      51             : #pragma GCC diagnostic ignored "-Wfloat-equal"
      52             : 
      53             : 
      54           1 : void As2JsFloat64UnitTests::test_float64()
      55             : {
      56             :     // default constructor gives us zero
      57             :     {
      58           1 :         as2js::Float64 zero;
      59           1 :         CPPUNIT_ASSERT(zero.get() == 0.0);
      60             :     }
      61             : 
      62             :     // float constructor, copy constructor, copy assignment
      63        1001 :     for(int i(0); i < 1000; ++i)
      64             :     {
      65             :         // generate a random 64 bit number
      66        1000 :         float s1(rand() & 1 ? -1 : 1);
      67        1000 :         float n1(static_cast<float>((static_cast<int64_t>(rand()) << 48)
      68        1000 :                                   ^ (static_cast<int64_t>(rand()) << 32)
      69        1000 :                                   ^ (static_cast<int64_t>(rand()) << 16)
      70        1000 :                                   ^ (static_cast<int64_t>(rand()) <<  0)));
      71        1000 :         float d1(static_cast<float>((static_cast<int64_t>(rand()) << 48)
      72        1000 :                                   ^ (static_cast<int64_t>(rand()) << 32)
      73        1000 :                                   ^ (static_cast<int64_t>(rand()) << 16)
      74        1000 :                                   ^ (static_cast<int64_t>(rand()) <<  0)));
      75        1000 :         float r(n1 / d1 * s1);
      76        1000 :         as2js::Float64 random(r);
      77        1000 :         CPPUNIT_ASSERT(random.get() == r);
      78        1000 :         CPPUNIT_ASSERT(!random.is_NaN());
      79        1000 :         CPPUNIT_ASSERT(!random.is_infinity());
      80        1000 :         CPPUNIT_ASSERT(!random.is_positive_infinity());
      81        1000 :         CPPUNIT_ASSERT(!random.is_negative_infinity());
      82        1000 :         CPPUNIT_ASSERT(random.classified_infinity() == 0);
      83             : 
      84        1000 :         as2js::Float64 copy(random);
      85        1000 :         CPPUNIT_ASSERT(copy.get() == r);
      86        1000 :         CPPUNIT_ASSERT(!copy.is_NaN());
      87        1000 :         CPPUNIT_ASSERT(!copy.is_infinity());
      88        1000 :         CPPUNIT_ASSERT(!copy.is_positive_infinity());
      89        1000 :         CPPUNIT_ASSERT(!copy.is_negative_infinity());
      90        1000 :         CPPUNIT_ASSERT(copy.classified_infinity() == 0);
      91             : 
      92        1000 :         float s2(rand() & 1 ? -1 : 1);
      93        1000 :         float n2(static_cast<float>((static_cast<int64_t>(rand()) << 48)
      94        1000 :                                   ^ (static_cast<int64_t>(rand()) << 32)
      95        1000 :                                   ^ (static_cast<int64_t>(rand()) << 16)
      96        1000 :                                   ^ (static_cast<int64_t>(rand()) <<  0)));
      97        1000 :         float d2(static_cast<float>((static_cast<int64_t>(rand()) << 48)
      98        1000 :                                   ^ (static_cast<int64_t>(rand()) << 32)
      99        1000 :                                   ^ (static_cast<int64_t>(rand()) << 16)
     100        1000 :                                   ^ (static_cast<int64_t>(rand()) <<  0)));
     101        1000 :         float q(n2 / d2 * s2);
     102             : 
     103        1000 :         random = q;
     104        1000 :         CPPUNIT_ASSERT(random.get() == q);
     105        1000 :         CPPUNIT_ASSERT(!random.is_NaN());
     106        1000 :         CPPUNIT_ASSERT(!random.is_infinity());
     107        1000 :         CPPUNIT_ASSERT(!random.is_positive_infinity());
     108        1000 :         CPPUNIT_ASSERT(!random.is_negative_infinity());
     109        1000 :         CPPUNIT_ASSERT(random.classified_infinity() == 0);
     110             : 
     111       12000 :         for(int j(0); j <= 10; ++j)
     112             :         {
     113             :             // 1.0, 0.1, 0.01, ... 0.000000001
     114       11000 :             double const epsilon(pow(10.0, static_cast<double>(-j)));
     115             : 
     116       11000 :             bool nearly_equal(false);
     117             :             {
     118       11000 :                 as2js::Float64::float64_type const diff = fabs(random.get() - copy.get());
     119       22000 :                 if(random.get() == 0.0
     120       11000 :                 || copy.get() == 0.0
     121       22000 :                 || diff < std::numeric_limits<double>::min())
     122             :                 {
     123           0 :                     nearly_equal = diff < (epsilon * std::numeric_limits<double>::min());
     124             :                 }
     125             :                 else
     126             :                 {
     127       11000 :                     nearly_equal = diff / (fabs(random.get()) + fabs(copy.get())) < epsilon;
     128             :                 }
     129             :             }
     130             : 
     131       11000 :             CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(random.compare(copy)));
     132       11000 :             CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(copy.compare(random)));
     133       11000 :             if(q < r)
     134             :             {
     135        5544 :                 CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_LESS);
     136        5544 :                 CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_GREATER);
     137        5544 :                 CPPUNIT_ASSERT(!(random.nearly_equal(copy, epsilon) ^ nearly_equal));
     138        5544 :                 CPPUNIT_ASSERT(!(copy.nearly_equal(random, epsilon) ^ nearly_equal));
     139             :             }
     140        5456 :             else if(q > r)
     141             :             {
     142        5456 :                 CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_GREATER);
     143        5456 :                 CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_LESS);
     144        5456 :                 CPPUNIT_ASSERT(!(random.nearly_equal(copy, epsilon) ^ nearly_equal));
     145        5456 :                 CPPUNIT_ASSERT(!(copy.nearly_equal(random, epsilon) ^ nearly_equal));
     146             :             }
     147             :             else
     148             :             {
     149           0 :                 CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_EQUAL);
     150           0 :                 CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_EQUAL);
     151           0 :                 CPPUNIT_ASSERT(random.nearly_equal(copy, epsilon));
     152           0 :                 CPPUNIT_ASSERT(copy.nearly_equal(random, epsilon));
     153             :             }
     154             :         }
     155             : 
     156        1000 :         float s3(rand() & 1 ? -1 : 1);
     157        1000 :         float n3(static_cast<float>((static_cast<int64_t>(rand()) << 48)
     158        1000 :                                   ^ (static_cast<int64_t>(rand()) << 32)
     159        1000 :                                   ^ (static_cast<int64_t>(rand()) << 16)
     160        1000 :                                   ^ (static_cast<int64_t>(rand()) <<  0)));
     161        1000 :         float d3(static_cast<float>((static_cast<int64_t>(rand()) << 48)
     162        1000 :                                   ^ (static_cast<int64_t>(rand()) << 32)
     163        1000 :                                   ^ (static_cast<int64_t>(rand()) << 16)
     164        1000 :                                   ^ (static_cast<int64_t>(rand()) <<  0)));
     165        1000 :         float p(n3 / d3 * s3);
     166             : 
     167        1000 :         random.set(p);
     168        1000 :         CPPUNIT_ASSERT(random.get() == p);
     169        1000 :         CPPUNIT_ASSERT(!random.is_NaN());
     170        1000 :         CPPUNIT_ASSERT(!random.is_infinity());
     171        1000 :         CPPUNIT_ASSERT(!random.is_positive_infinity());
     172        1000 :         CPPUNIT_ASSERT(!random.is_negative_infinity());
     173        1000 :         CPPUNIT_ASSERT(random.classified_infinity() == 0);
     174             :     }
     175             : 
     176             :     // double constructor, copy constructor, copy assignment
     177        1001 :     for(int i(0); i < 1000; ++i)
     178             :     {
     179             :         // generate a random 64 bit number
     180        1000 :         double s1(rand() & 1 ? -1 : 1);
     181        1000 :         double n1(static_cast<double>((static_cast<int64_t>(rand()) << 48)
     182        1000 :                                     ^ (static_cast<int64_t>(rand()) << 32)
     183        1000 :                                     ^ (static_cast<int64_t>(rand()) << 16)
     184        1000 :                                     ^ (static_cast<int64_t>(rand()) <<  0)));
     185        1000 :         double d1(static_cast<double>((static_cast<int64_t>(rand()) << 48)
     186        1000 :                                     ^ (static_cast<int64_t>(rand()) << 32)
     187        1000 :                                     ^ (static_cast<int64_t>(rand()) << 16)
     188        1000 :                                     ^ (static_cast<int64_t>(rand()) <<  0)));
     189        1000 :         double r(n1 / d1 * s1);
     190        1000 :         as2js::Float64 random(r);
     191        1000 :         CPPUNIT_ASSERT(random.get() == r);
     192        1000 :         CPPUNIT_ASSERT(!random.is_NaN());
     193        1000 :         CPPUNIT_ASSERT(!random.is_infinity());
     194        1000 :         CPPUNIT_ASSERT(!random.is_positive_infinity());
     195        1000 :         CPPUNIT_ASSERT(!random.is_negative_infinity());
     196        1000 :         CPPUNIT_ASSERT(random.get() != std::numeric_limits<double>::quiet_NaN());
     197        1000 :         CPPUNIT_ASSERT(random.classified_infinity() == 0);
     198             : 
     199        1000 :         as2js::Float64 copy(random);
     200        1000 :         CPPUNIT_ASSERT(copy.get() == r);
     201        1000 :         CPPUNIT_ASSERT(!copy.is_NaN());
     202        1000 :         CPPUNIT_ASSERT(!copy.is_infinity());
     203        1000 :         CPPUNIT_ASSERT(!copy.is_positive_infinity());
     204        1000 :         CPPUNIT_ASSERT(!copy.is_negative_infinity());
     205        1000 :         CPPUNIT_ASSERT(copy.get() != std::numeric_limits<double>::quiet_NaN());
     206        1000 :         CPPUNIT_ASSERT(copy.classified_infinity() == 0);
     207             : 
     208        1000 :         double s2(rand() & 1 ? -1 : 1);
     209        1000 :         double n2(static_cast<double>((static_cast<int64_t>(rand()) << 48)
     210        1000 :                                     ^ (static_cast<int64_t>(rand()) << 32)
     211        1000 :                                     ^ (static_cast<int64_t>(rand()) << 16)
     212        1000 :                                     ^ (static_cast<int64_t>(rand()) <<  0)));
     213        1000 :         double d2(static_cast<double>((static_cast<int64_t>(rand()) << 48)
     214        1000 :                                     ^ (static_cast<int64_t>(rand()) << 32)
     215        1000 :                                     ^ (static_cast<int64_t>(rand()) << 16)
     216        1000 :                                     ^ (static_cast<int64_t>(rand()) <<  0)));
     217        1000 :         double q(n2 / d2 * s2);
     218             : 
     219        1000 :         random = q;
     220        1000 :         CPPUNIT_ASSERT(random.get() == q);
     221        1000 :         CPPUNIT_ASSERT(!random.is_NaN());
     222        1000 :         CPPUNIT_ASSERT(!random.is_infinity());
     223        1000 :         CPPUNIT_ASSERT(!random.is_positive_infinity());
     224        1000 :         CPPUNIT_ASSERT(!random.is_negative_infinity());
     225        1000 :         CPPUNIT_ASSERT(random.get() != std::numeric_limits<double>::quiet_NaN());
     226        1000 :         CPPUNIT_ASSERT(random.classified_infinity() == 0);
     227             : 
     228        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(random.compare(copy)));
     229        1000 :         CPPUNIT_ASSERT(as2js::compare_utils::is_ordered(copy.compare(random)));
     230        1000 :         if(q < r)
     231             :         {
     232         491 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_LESS);
     233         491 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_GREATER);
     234             :         }
     235         509 :         else if(q > r)
     236             :         {
     237         509 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_GREATER);
     238         509 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_LESS);
     239             :         }
     240             :         else
     241             :         {
     242           0 :             CPPUNIT_ASSERT(random.compare(copy) == as2js::compare_t::COMPARE_EQUAL);
     243           0 :             CPPUNIT_ASSERT(copy.compare(random) == as2js::compare_t::COMPARE_EQUAL);
     244             :         }
     245             : 
     246        1000 :         double s3(rand() & 1 ? -1 : 1);
     247        1000 :         double n3(static_cast<double>((static_cast<int64_t>(rand()) << 48)
     248        1000 :                                     ^ (static_cast<int64_t>(rand()) << 32)
     249        1000 :                                     ^ (static_cast<int64_t>(rand()) << 16)
     250        1000 :                                     ^ (static_cast<int64_t>(rand()) <<  0)));
     251        1000 :         double d3(static_cast<double>((static_cast<int64_t>(rand()) << 48)
     252        1000 :                                     ^ (static_cast<int64_t>(rand()) << 32)
     253        1000 :                                     ^ (static_cast<int64_t>(rand()) << 16)
     254        1000 :                                     ^ (static_cast<int64_t>(rand()) <<  0)));
     255        1000 :         double p(n3 / d3 * s3);
     256             : 
     257        1000 :         random.set(p);
     258        1000 :         CPPUNIT_ASSERT(random.get() == p);
     259        1000 :         CPPUNIT_ASSERT(!random.is_NaN());
     260        1000 :         CPPUNIT_ASSERT(!random.is_infinity());
     261        1000 :         CPPUNIT_ASSERT(!random.is_positive_infinity());
     262        1000 :         CPPUNIT_ASSERT(!random.is_negative_infinity());
     263        1000 :         CPPUNIT_ASSERT(random.get() != std::numeric_limits<double>::quiet_NaN());
     264        1000 :         CPPUNIT_ASSERT(random.classified_infinity() == 0);
     265             :     }
     266           1 : }
     267             : 
     268             : 
     269           1 : void As2JsFloat64UnitTests::test_special_numbers()
     270             : {
     271           1 :     as2js::Float64 special;
     272             : 
     273             :     // start with zero
     274           1 :     CPPUNIT_ASSERT(special.get() == 0.0);
     275           1 :     CPPUNIT_ASSERT(special.nearly_equal(0.0));
     276             : 
     277             :     // create a random number to compare with
     278           1 :     double s1(rand() & 1 ? -1 : 1);
     279           1 :     double n1(static_cast<double>((static_cast<int64_t>(rand()) << 48)
     280           1 :                                 ^ (static_cast<int64_t>(rand()) << 32)
     281           1 :                                 ^ (static_cast<int64_t>(rand()) << 16)
     282           1 :                                 ^ (static_cast<int64_t>(rand()) <<  0)));
     283           1 :     double d1(static_cast<double>((static_cast<int64_t>(rand()) << 48)
     284           1 :                                 ^ (static_cast<int64_t>(rand()) << 32)
     285           1 :                                 ^ (static_cast<int64_t>(rand()) << 16)
     286           1 :                                 ^ (static_cast<int64_t>(rand()) <<  0)));
     287           1 :     double p(n1 / d1 * s1);
     288           1 :     as2js::Float64 r(p);
     289             : 
     290             :     // test NaN
     291           1 :     special.set_NaN();
     292           1 :     CPPUNIT_ASSERT(special.is_NaN());
     293           1 :     CPPUNIT_ASSERT(!special.is_infinity());
     294           1 :     CPPUNIT_ASSERT(!special.is_positive_infinity());
     295           1 :     CPPUNIT_ASSERT(!special.is_negative_infinity());
     296           1 :     CPPUNIT_ASSERT(special.get() != 0.0);
     297           1 :     CPPUNIT_ASSERT(special.get() != p);
     298           1 :     CPPUNIT_ASSERT(!(special.get() == p));
     299           1 :     CPPUNIT_ASSERT(!(special.get() > p));
     300           1 :     CPPUNIT_ASSERT(!(special.get() >= p));
     301           1 :     CPPUNIT_ASSERT(!(special.get() < p));
     302           1 :     CPPUNIT_ASSERT(!(special.get() <= p));
     303             :     // We do not offer those yet
     304             :     //CPPUNIT_ASSERT(special != r);
     305             :     //CPPUNIT_ASSERT(!(special == r));
     306             :     //CPPUNIT_ASSERT(!(special > r));
     307             :     //CPPUNIT_ASSERT(!(special >= r));
     308             :     //CPPUNIT_ASSERT(!(special < r));
     309             :     //CPPUNIT_ASSERT(!(special <= r));
     310           1 :     CPPUNIT_ASSERT(special.get() != std::numeric_limits<double>::quiet_NaN());
     311           1 :     CPPUNIT_ASSERT(special.compare(p) == as2js::compare_t::COMPARE_UNORDERED);
     312           1 :     CPPUNIT_ASSERT(special.compare(r) == as2js::compare_t::COMPARE_UNORDERED);
     313           1 :     CPPUNIT_ASSERT(r.compare(special) == as2js::compare_t::COMPARE_UNORDERED);
     314           1 :     CPPUNIT_ASSERT(special.classified_infinity() == 0);
     315           1 :     CPPUNIT_ASSERT(!special.nearly_equal(p));
     316           1 :     CPPUNIT_ASSERT(!special.nearly_equal(special));
     317             : 
     318             :     // test Infinity
     319           1 :     special.set_infinity(); // +inf
     320           1 :     CPPUNIT_ASSERT(!special.is_NaN());
     321           1 :     CPPUNIT_ASSERT(special.is_infinity());
     322           1 :     CPPUNIT_ASSERT(special.is_positive_infinity());
     323           1 :     CPPUNIT_ASSERT(!special.is_negative_infinity());
     324           1 :     CPPUNIT_ASSERT(special.get() != 0.0);
     325           1 :     CPPUNIT_ASSERT(special.get() != p);
     326           1 :     CPPUNIT_ASSERT(!(special.get() == p));
     327           1 :     CPPUNIT_ASSERT(special.get() > p);
     328           1 :     CPPUNIT_ASSERT(special.get() >= p);
     329           1 :     CPPUNIT_ASSERT(!(special.get() < p));
     330           1 :     CPPUNIT_ASSERT(!(special.get() <= p));
     331             :     // We do not offer those yet
     332             :     //CPPUNIT_ASSERT(special != r);
     333             :     //CPPUNIT_ASSERT(!(special == r));
     334             :     //CPPUNIT_ASSERT(!(special > r));
     335             :     //CPPUNIT_ASSERT(!(special >= r));
     336             :     //CPPUNIT_ASSERT(!(special < r));
     337             :     //CPPUNIT_ASSERT(!(special <= r));
     338           1 :     CPPUNIT_ASSERT(special.get() != std::numeric_limits<double>::quiet_NaN());
     339           1 :     CPPUNIT_ASSERT(special.compare(p) == as2js::compare_t::COMPARE_GREATER);
     340           1 :     CPPUNIT_ASSERT(special.compare(r) == as2js::compare_t::COMPARE_GREATER);
     341           1 :     CPPUNIT_ASSERT(r.compare(special) == as2js::compare_t::COMPARE_LESS);
     342           1 :     CPPUNIT_ASSERT(special.classified_infinity() == 1);
     343           1 :     CPPUNIT_ASSERT(!special.nearly_equal(p));
     344           1 :     CPPUNIT_ASSERT(special.nearly_equal(special));
     345             : 
     346           1 :     as2js::Float64 pinf;
     347           1 :     pinf.set_infinity();
     348           1 :     CPPUNIT_ASSERT(pinf.compare(special) == as2js::compare_t::COMPARE_EQUAL);
     349           1 :     CPPUNIT_ASSERT(special.compare(pinf) == as2js::compare_t::COMPARE_EQUAL);
     350             : 
     351           1 :     special.set(-special.get()); // -inf
     352           1 :     CPPUNIT_ASSERT(!special.is_NaN());
     353           1 :     CPPUNIT_ASSERT(special.is_infinity());
     354           1 :     CPPUNIT_ASSERT(!special.is_positive_infinity());
     355           1 :     CPPUNIT_ASSERT(special.is_negative_infinity());
     356           1 :     CPPUNIT_ASSERT(special.get() != 0.0);
     357           1 :     CPPUNIT_ASSERT(special.get() != p);
     358           1 :     CPPUNIT_ASSERT(!(special.get() == p));
     359           1 :     CPPUNIT_ASSERT(!(special.get() > p));
     360           1 :     CPPUNIT_ASSERT(!(special.get() >= p));
     361           1 :     CPPUNIT_ASSERT(special.get() < p);
     362           1 :     CPPUNIT_ASSERT(special.get() <= p);
     363             :     // We do not offer those yet
     364             :     //CPPUNIT_ASSERT(special != r);
     365             :     //CPPUNIT_ASSERT(!(special == r));
     366             :     //CPPUNIT_ASSERT(!(special > r));
     367             :     //CPPUNIT_ASSERT(!(special >= r));
     368             :     //CPPUNIT_ASSERT(!(special < r));
     369             :     //CPPUNIT_ASSERT(!(special <= r));
     370           1 :     CPPUNIT_ASSERT(special.get() != std::numeric_limits<double>::quiet_NaN());
     371           1 :     CPPUNIT_ASSERT(special.compare(p) == as2js::compare_t::COMPARE_LESS);
     372           1 :     CPPUNIT_ASSERT(special.compare(r) == as2js::compare_t::COMPARE_LESS);
     373           1 :     CPPUNIT_ASSERT(r.compare(special) == as2js::compare_t::COMPARE_GREATER);
     374           1 :     CPPUNIT_ASSERT(special.classified_infinity() == -1);
     375           1 :     CPPUNIT_ASSERT(!special.nearly_equal(p));
     376           1 :     CPPUNIT_ASSERT(special.nearly_equal(special));
     377             : 
     378           1 :     CPPUNIT_ASSERT(pinf.compare(special) != as2js::compare_t::COMPARE_EQUAL);
     379           1 :     CPPUNIT_ASSERT(special.compare(pinf) != as2js::compare_t::COMPARE_EQUAL);
     380           1 :     CPPUNIT_ASSERT(!pinf.nearly_equal(special));
     381           1 :     CPPUNIT_ASSERT(!special.nearly_equal(pinf));
     382           1 : }
     383             : 
     384             : 
     385           1 : void As2JsFloat64UnitTests::test_nearly_equal()
     386             : {
     387             :     // exactly equal
     388             :     {
     389           1 :         as2js::Float64 f1(3.14159);
     390           1 :         as2js::Float64 f2(3.14159);
     391           1 :         CPPUNIT_ASSERT(f1.nearly_equal(f2));
     392             :     }
     393             : 
     394             :     // nearly equal at +/-1e-5
     395             :     {
     396           1 :         as2js::Float64 f1(3.14159);
     397           1 :         as2js::Float64 f2(3.14158);
     398           1 :         CPPUNIT_ASSERT(f1.nearly_equal(f2));
     399             :     }
     400             : 
     401             :     // nearly equal at +/-1e-6
     402             :     {
     403           1 :         as2js::Float64 f1(3.1415926);
     404           1 :         as2js::Float64 f2(3.1415936);
     405           1 :         CPPUNIT_ASSERT(f1.nearly_equal(f2));
     406             :     }
     407             : 
     408             :     // nearly equal at +/-1e-4 -- fails
     409             :     {
     410           1 :         as2js::Float64 f1(3.1415926);
     411           1 :         as2js::Float64 f2(3.1416926);
     412           1 :         CPPUNIT_ASSERT(!f1.nearly_equal(f2));
     413             :     }
     414             : 
     415             :     // nearly equal, very different
     416             :     {
     417           1 :         as2js::Float64 f1(3.1415926);
     418           1 :         as2js::Float64 f2(-3.1415926);
     419           1 :         CPPUNIT_ASSERT(!f1.nearly_equal(f2));
     420             :     }
     421             :     {
     422           1 :         as2js::Float64 f1(3.1415926);
     423           1 :         as2js::Float64 f2(0.0);
     424           1 :         CPPUNIT_ASSERT(!f1.nearly_equal(f2));
     425             :     }
     426             :     {
     427           1 :         as2js::Float64 f1(0.0);
     428           1 :         as2js::Float64 f2(3.1415926);
     429           1 :         CPPUNIT_ASSERT(!f1.nearly_equal(f2));
     430             :     }
     431          13 : }
     432             : 
     433             : 
     434             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10