android: Update app-specific/MIME type icons
[LibreOffice.git] / sal / qa / rtl / math / test-std-math.cxx
blob214e1c792dbe0bc51cc70f4c57b52becd29d4bfa
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.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/types.h>
21 #include <cppunit/TestAssert.h>
22 #include <cppunit/TestFixture.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/plugin/TestPlugIn.h>
25 #include <rtl/math.hxx>
26 #include <rtl/ustring.h>
27 #include <rtl/ustring.hxx>
28 #include <limits>
29 #include <cmath>
32 In tdf#148430, we try to replace rtl math functions to std functions,
33 this unit test is to demonstrate this replacement will not change
34 the behavior of code and no other unexpected results.
36 You can see more discussions in https://gerrit.libreoffice.org/c/core/+/138294.
39 class Test : public CppUnit::TestFixture
41 public:
42 void test_erf()
44 double x, rtl_res, std_res;
45 x = 0.0;
46 rtl_res = rtl::math::erf(x);
47 std_res = std::erf(x);
48 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
49 rtl::math::setInf(&x, false);
50 rtl_res = rtl::math::erf(x);
51 std_res = std::erf(x);
52 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
53 rtl::math::setInf(&x, true);
54 rtl_res = rtl::math::erf(x);
55 std_res = std::erf(x);
56 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
57 rtl::math::setNan(&x);
58 rtl_res = rtl::math::erf(x);
59 std_res = std::erf(x);
60 CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res));
61 x = 3.0;
62 rtl_res = rtl::math::erf(-x);
63 std_res = std::erf(-x);
64 CPPUNIT_ASSERT_DOUBLES_EQUAL(-std::erf(x), rtl_res, 1E-12);
65 CPPUNIT_ASSERT_DOUBLES_EQUAL(-rtl::math::erf(x), std_res, 1E-12);
68 void test_erfc()
70 double x, rtl_res, std_res;
71 x = 0.0;
72 rtl_res = rtl::math::erfc(x);
73 std_res = std::erfc(x);
74 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
75 rtl::math::setInf(&x, false);
76 rtl_res = rtl::math::erfc(x);
77 std_res = std::erfc(x);
78 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
79 rtl::math::setInf(&x, true);
80 rtl_res = rtl::math::erfc(x);
81 std_res = std::erfc(x);
82 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
83 rtl::math::setNan(&x);
84 rtl_res = rtl::math::erfc(x);
85 std_res = std::erfc(x);
86 CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res));
87 x = 3.0;
88 rtl_res = rtl::math::erfc(-x);
89 std_res = std::erfc(-x);
90 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0 - std::erfc(x), rtl_res, 1E-12);
91 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0 - rtl::math::erfc(x), std_res, 1E-12);
94 void test_expm1()
96 double x, rtl_res, std_res;
97 x = 0.0;
98 rtl_res = rtl::math::expm1(x);
99 std_res = std::expm1(x);
100 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
101 x = -0.0;
102 rtl_res = rtl::math::expm1(x);
103 std_res = std::expm1(x);
104 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
105 CPPUNIT_ASSERT_EQUAL(std::signbit(rtl_res), std::signbit(std_res));
106 rtl::math::setInf(&x, false);
107 rtl_res = rtl::math::expm1(x);
108 std_res = std::expm1(x);
109 CPPUNIT_ASSERT_EQUAL(std::isinf(rtl_res) && !std::signbit(rtl_res),
110 std::isinf(std_res) && !std::signbit(std_res));
111 rtl::math::setInf(&x, true);
112 rtl_res = rtl::math::expm1(x);
113 std_res = std::expm1(x);
114 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
115 rtl::math::setNan(&x);
116 rtl_res = rtl::math::expm1(x);
117 std_res = std::expm1(x);
118 CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res));
121 void test_log1p()
123 double x, rtl_res, std_res;
124 x = 0.0;
125 rtl_res = rtl::math::log1p(x);
126 std_res = std::log1p(x);
127 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
128 x = -0.0;
129 rtl_res = rtl::math::log1p(x);
130 std_res = std::log1p(x);
131 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
132 CPPUNIT_ASSERT_EQUAL(std::signbit(rtl_res), std::signbit(std_res));
133 rtl::math::setInf(&x, false);
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.0;
139 rtl_res = rtl::math::log1p(x);
140 std_res = std::log1p(x);
141 CPPUNIT_ASSERT_EQUAL(std::isinf(rtl_res) && std::signbit(rtl_res),
142 std::isinf(std_res) && std::signbit(std_res));
143 x = -1.1;
144 rtl_res = rtl::math::log1p(x);
145 std_res = std::log1p(x);
146 CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res));
147 rtl::math::setInf(&x, true);
148 rtl_res = rtl::math::log1p(x);
149 std_res = std::log1p(x);
150 CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res));
151 rtl::math::setNan(&x);
152 rtl_res = rtl::math::log1p(x);
153 std_res = std::log1p(x);
154 CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res));
157 void test_atanh()
159 double x, rtl_res, std_res;
160 x = -2.0;
161 rtl_res = rtl::math::atanh(x); // NaN
162 std_res = std::atanh(x);
163 CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res));
164 x = -1.0;
165 rtl_res = rtl::math::atanh(x); // -Inf
166 std_res = std::atanh(x);
167 CPPUNIT_ASSERT_EQUAL(std::signbit(rtl_res), std::signbit(std_res));
168 CPPUNIT_ASSERT_EQUAL(std::isinf(rtl_res), std::isinf(std_res));
169 x = 0.0;
170 rtl_res = rtl::math::atanh(x);
171 std_res = std::atanh(x);
172 CPPUNIT_ASSERT_EQUAL(rtl_res, std_res);
173 x = 1.0;
174 rtl_res = rtl::math::atanh(1.0); // +Inf
175 std_res = std::atanh(x);
176 CPPUNIT_ASSERT_EQUAL(std::signbit(rtl_res), std::signbit(std_res));
177 CPPUNIT_ASSERT_EQUAL(std::isinf(rtl_res), std::isinf(std_res));
178 x = 2.0;
179 rtl_res = rtl::math::atanh(2.0); // NaN
180 std_res = std::atanh(x);
181 CPPUNIT_ASSERT_EQUAL(std::isnan(rtl_res), std::isnan(std_res));
184 CPPUNIT_TEST_SUITE(Test);
185 CPPUNIT_TEST(test_erf);
186 CPPUNIT_TEST(test_erfc);
187 CPPUNIT_TEST(test_expm1);
188 CPPUNIT_TEST(test_log1p);
189 CPPUNIT_TEST(test_atanh);
190 CPPUNIT_TEST_SUITE_END();
193 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */