| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <cppunit/TestAssert.h> |
| 21 |
#include <cppunit/TestFixture.h> |
| 22 |
#include <cppunit/extensions/HelperMacros.h> |
| 23 |
#include <rtl/math.hxx> |
| 24 |
#include <cmath> |
| 25 |
|
| 26 |
/* |
| 27 |
In tdf#148430, we try to replace rtl math functions to std functions, |
| 28 |
this unit test is to demonstrate this replacement will not change |
| 29 |
the behavior of code and no other unexpected results. |
| 30 |
|
| 31 |
You can see more discussions in https://gerrit.libreoffice.org/c/core/+/138294. |
| 32 |
*/ |
| 33 |
|
| 34 |
class Test : public CppUnit::TestFixture |
| 35 |
{ |
| 36 |
public: |
| 37 |
void test_erf() |
| 38 |
{ |
| 39 |
double x, rtl_res, std_res; |
| 40 |
x = 0.0; |
| 41 |
rtl_res = rtl::math::erf(x); |
| 42 |
std_res = std::erf(x); |
| 43 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 44 |
rtl::math::setInf(&x, false); |
| 45 |
rtl_res = rtl::math::erf(x); |
| 46 |
std_res = std::erf(x); |
| 47 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 48 |
rtl::math::setInf(&x, true); |
| 49 |
rtl_res = rtl::math::erf(x); |
| 50 |
std_res = std::erf(x); |
| 51 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 52 |
rtl::math::setNan(&x); |
| 53 |
rtl_res = rtl::math::erf(x); |
| 54 |
std_res = std::erf(x); |
| 55 |
CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res)); |
| 56 |
x = 3.0; |
| 57 |
rtl_res = rtl::math::erf(-x); |
| 58 |
std_res = std::erf(-x); |
| 59 |
CPPUNIT_ASSERT_DOUBLES_EQUAL(-std::erf(x), rtl_res, 1E-12); |
| 60 |
CPPUNIT_ASSERT_DOUBLES_EQUAL(-rtl::math::erf(x), std_res, 1E-12); |
| 61 |
} |
| 62 |
|
| 63 |
void test_erfc() |
| 64 |
{ |
| 65 |
double x, rtl_res, std_res; |
| 66 |
x = 0.0; |
| 67 |
rtl_res = rtl::math::erfc(x); |
| 68 |
std_res = std::erfc(x); |
| 69 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 70 |
rtl::math::setInf(&x, false); |
| 71 |
rtl_res = rtl::math::erfc(x); |
| 72 |
std_res = std::erfc(x); |
| 73 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 74 |
rtl::math::setInf(&x, true); |
| 75 |
rtl_res = rtl::math::erfc(x); |
| 76 |
std_res = std::erfc(x); |
| 77 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 78 |
rtl::math::setNan(&x); |
| 79 |
rtl_res = rtl::math::erfc(x); |
| 80 |
std_res = std::erfc(x); |
| 81 |
CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res)); |
| 82 |
x = 3.0; |
| 83 |
rtl_res = rtl::math::erfc(-x); |
| 84 |
std_res = std::erfc(-x); |
| 85 |
CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0 - std::erfc(x), rtl_res, 1E-12); |
| 86 |
CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0 - rtl::math::erfc(x), std_res, 1E-12); |
| 87 |
} |
| 88 |
|
| 89 |
void test_expm1() |
| 90 |
{ |
| 91 |
double x, rtl_res, std_res; |
| 92 |
x = 0.0; |
| 93 |
rtl_res = rtl::math::expm1(x); |
| 94 |
std_res = std::expm1(x); |
| 95 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 96 |
x = -0.0; |
| 97 |
rtl_res = rtl::math::expm1(x); |
| 98 |
std_res = std::expm1(x); |
| 99 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 100 |
CPPUNIT_ASSERT_EQUAL(std::signbit(rtl_res), std::signbit(std_res)); |
| 101 |
rtl::math::setInf(&x, false); |
| 102 |
rtl_res = rtl::math::expm1(x); |
| 103 |
std_res = std::expm1(x); |
| 104 |
CPPUNIT_ASSERT_EQUAL(std::isinf(rtl_res) && !std::signbit(rtl_res), |
| 105 |
std::isinf(std_res) && !std::signbit(std_res)); |
| 106 |
rtl::math::setInf(&x, true); |
| 107 |
rtl_res = rtl::math::expm1(x); |
| 108 |
std_res = std::expm1(x); |
| 109 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 110 |
rtl::math::setNan(&x); |
| 111 |
rtl_res = rtl::math::expm1(x); |
| 112 |
std_res = std::expm1(x); |
| 113 |
CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res)); |
| 114 |
} |
| 115 |
|
| 116 |
void test_log1p() |
| 117 |
{ |
| 118 |
double x, rtl_res, std_res; |
| 119 |
x = 0.0; |
| 120 |
rtl_res = rtl::math::log1p(x); |
| 121 |
std_res = std::log1p(x); |
| 122 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 123 |
x = -0.0; |
| 124 |
rtl_res = rtl::math::log1p(x); |
| 125 |
std_res = std::log1p(x); |
| 126 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 127 |
CPPUNIT_ASSERT_EQUAL(std::signbit(rtl_res), std::signbit(std_res)); |
| 128 |
rtl::math::setInf(&x, false); |
| 129 |
rtl_res = rtl::math::log1p(x); |
| 130 |
std_res = std::log1p(x); |
| 131 |
CPPUNIT_ASSERT_EQUAL(std::isinf(rtl_res) && !std::signbit(rtl_res), |
| 132 |
std::isinf(std_res) && !std::signbit(std_res)); |
| 133 |
x = -1.0; |
| 134 |
rtl_res = rtl::math::log1p(x); |
| 135 |
std_res = std::log1p(x); |
| 136 |
CPPUNIT_ASSERT_EQUAL(std::isinf(rtl_res) && std::signbit(rtl_res), |
| 137 |
std::isinf(std_res) && std::signbit(std_res)); |
| 138 |
x = -1.1; |
| 139 |
rtl_res = rtl::math::log1p(x); |
| 140 |
std_res = std::log1p(x); |
| 141 |
CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res)); |
| 142 |
rtl::math::setInf(&x, true); |
| 143 |
rtl_res = rtl::math::log1p(x); |
| 144 |
std_res = std::log1p(x); |
| 145 |
CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res)); |
| 146 |
rtl::math::setNan(&x); |
| 147 |
rtl_res = rtl::math::log1p(x); |
| 148 |
std_res = std::log1p(x); |
| 149 |
CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res)); |
| 150 |
} |
| 151 |
|
| 152 |
void test_atanh() |
| 153 |
{ |
| 154 |
double x, rtl_res, std_res; |
| 155 |
x = -2.0; |
| 156 |
rtl_res = rtl::math::atanh(x); // NaN |
| 157 |
std_res = std::atanh(x); |
| 158 |
CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res)); |
| 159 |
x = -1.0; |
| 160 |
rtl_res = rtl::math::atanh(x); // -Inf |
| 161 |
std_res = std::atanh(x); |
| 162 |
CPPUNIT_ASSERT_EQUAL(std::signbit(rtl_res), std::signbit(std_res)); |
| 163 |
CPPUNIT_ASSERT_EQUAL(std::isinf(rtl_res), std::isinf(std_res)); |
| 164 |
x = 0.0; |
| 165 |
rtl_res = rtl::math::atanh(x); |
| 166 |
std_res = std::atanh(x); |
| 167 |
CPPUNIT_ASSERT_EQUAL(rtl_res, std_res); |
| 168 |
x = 1.0; |
| 169 |
rtl_res = rtl::math::atanh(1.0); // +Inf |
| 170 |
std_res = std::atanh(x); |
| 171 |
CPPUNIT_ASSERT_EQUAL(std::signbit(rtl_res), std::signbit(std_res)); |
| 172 |
CPPUNIT_ASSERT_EQUAL(std::isinf(rtl_res), std::isinf(std_res)); |
| 173 |
x = 2.0; |
| 174 |
rtl_res = rtl::math::atanh(2.0); // NaN |
| 175 |
std_res = std::atanh(x); |
| 176 |
CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res)); |
| 177 |
} |
| 178 |
|
| 179 |
CPPUNIT_TEST_SUITE(Test); |
| 180 |
CPPUNIT_TEST(test_erf); |
| 181 |
CPPUNIT_TEST(test_erfc); |
| 182 |
CPPUNIT_TEST(test_expm1); |
| 183 |
CPPUNIT_TEST(test_log1p); |
| 184 |
CPPUNIT_TEST(test_atanh); |
| 185 |
CPPUNIT_TEST_SUITE_END(); |
| 186 |
}; |
| 187 |
|
| 188 |
CPPUNIT_TEST_SUITE_REGISTRATION(Test); |
| 189 |
|
| 190 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |