| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 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 <sal/types.h> |
| 21 |
#include <cppunit/TestAssert.h> |
| 22 |
#include <cppunit/TestFixture.h> |
| 23 |
#include <cppunit/extensions/HelperMacros.h> |
| 24 |
#include <rtl/math.hxx> |
| 25 |
#include <rtl/ustring.hxx> |
| 26 |
#include <limits> |
| 27 |
#include <test/nanpayload_test.hxx> |
| 28 |
|
| 29 |
template<> inline std::string CPPUNIT_NS::assertion_traits<rtl_math_ConversionStatus>::toString( |
| 30 |
const rtl_math_ConversionStatus& x ) |
| 31 |
{ |
| 32 |
OStringStream ost; |
| 33 |
ost << static_cast<unsigned int>(x); |
| 34 |
return ost.str(); |
| 35 |
} |
| 36 |
|
| 37 |
namespace { |
| 38 |
|
| 39 |
class Test: public CppUnit::TestFixture { |
| 40 |
public: |
| 41 |
void test_stringToDouble_good() { |
| 42 |
rtl_math_ConversionStatus status; |
| 43 |
sal_Int32 end; |
| 44 |
double res = rtl::math::stringToDouble( |
| 45 |
" +1.E01foo", |
| 46 |
'.', ',', &status, &end); |
| 47 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 48 |
CPPUNIT_ASSERT_EQUAL(RTL_CONSTASCII_LENGTH(" +1.E01"), end); |
| 49 |
CPPUNIT_ASSERT_EQUAL(10.0, res); |
| 50 |
|
| 51 |
res = rtl::math::stringToDouble( |
| 52 |
"NaN", |
| 53 |
'.', ',', &status, &end); |
| 54 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 55 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), end); |
| 56 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 57 |
|
| 58 |
res = rtl::math::stringToDouble( |
| 59 |
"NaN1.23", |
| 60 |
'.', ',', &status, &end); |
| 61 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 62 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), end); |
| 63 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 64 |
|
| 65 |
res = rtl::math::stringToDouble( |
| 66 |
"+NaN", |
| 67 |
'.', ',', &status, &end); |
| 68 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 69 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); |
| 70 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 71 |
|
| 72 |
res = rtl::math::stringToDouble( |
| 73 |
"-NaN", |
| 74 |
'.', ',', &status, &end); |
| 75 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 76 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); |
| 77 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 78 |
|
| 79 |
res = rtl::math::stringToDouble( |
| 80 |
"+1.#NAN", |
| 81 |
'.', ',', &status, &end); |
| 82 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 83 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(7), end); |
| 84 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 85 |
CPPUNIT_ASSERT(!std::signbit(res)); |
| 86 |
|
| 87 |
res = rtl::math::stringToDouble( |
| 88 |
"-1.#NAN", |
| 89 |
'.', ',', &status, &end); |
| 90 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 91 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(7), end); |
| 92 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 93 |
CPPUNIT_ASSERT(std::signbit(res)); |
| 94 |
|
| 95 |
res = rtl::math::stringToDouble( |
| 96 |
"INF", |
| 97 |
'.', ',', &status, &end); |
| 98 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_OutOfRange, status); |
| 99 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), end); |
| 100 |
CPPUNIT_ASSERT(std::isinf(res)); |
| 101 |
|
| 102 |
res = rtl::math::stringToDouble( |
| 103 |
"INF1.23", |
| 104 |
'.', ',', &status, &end); |
| 105 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_OutOfRange, status); |
| 106 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), end); |
| 107 |
CPPUNIT_ASSERT(std::isinf(res)); |
| 108 |
|
| 109 |
res = rtl::math::stringToDouble( |
| 110 |
".5", |
| 111 |
'.', ',', &status, &end); |
| 112 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 113 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(2), end); |
| 114 |
CPPUNIT_ASSERT_EQUAL(0.5, res); |
| 115 |
|
| 116 |
res = rtl::math::stringToDouble( |
| 117 |
"5.", |
| 118 |
'.', ',', &status, &end); |
| 119 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 120 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(2), end); |
| 121 |
CPPUNIT_ASSERT_EQUAL(5.0, res); |
| 122 |
|
| 123 |
// Leading 0 and group separator. |
| 124 |
res = rtl::math::stringToDouble( |
| 125 |
"0,123", |
| 126 |
'.', ',', &status, &end); |
| 127 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 128 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(5), end); |
| 129 |
CPPUNIT_ASSERT_EQUAL(123.0, res); |
| 130 |
|
| 131 |
// Leading 0 and two consecutive group separators are none. |
| 132 |
res = rtl::math::stringToDouble( |
| 133 |
"0,,1", |
| 134 |
'.', ',', &status, &end); |
| 135 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 136 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(1), end); |
| 137 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 138 |
|
| 139 |
// Leading 0 and group separator at end is none. |
| 140 |
res = rtl::math::stringToDouble( |
| 141 |
"0,", |
| 142 |
'.', ',', &status, &end); |
| 143 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 144 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(1), end); |
| 145 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 146 |
|
| 147 |
// Leading 0 and group separator before non-digit is none. |
| 148 |
res = rtl::math::stringToDouble( |
| 149 |
"0,x", |
| 150 |
'.', ',', &status, &end); |
| 151 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 152 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(1), end); |
| 153 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 154 |
|
| 155 |
// Trailing group separator is none. |
| 156 |
res = rtl::math::stringToDouble( |
| 157 |
"1,234,", |
| 158 |
'.', ',', &status, &end); |
| 159 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 160 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(5), end); |
| 161 |
CPPUNIT_ASSERT_EQUAL(1234.0, res); |
| 162 |
|
| 163 |
// Group separator followed by non-digit is none. |
| 164 |
res = rtl::math::stringToDouble( |
| 165 |
"1,234,x", |
| 166 |
'.', ',', &status, &end); |
| 167 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 168 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(5), end); |
| 169 |
CPPUNIT_ASSERT_EQUAL(1234.0, res); |
| 170 |
|
| 171 |
// Check that the value is the nearest double-precision representation of the decimal 0.0042 |
| 172 |
// (it was 0.0042000000000000006 instead of 0.0041999999999999997) |
| 173 |
res = rtl::math::stringToDouble("0,0042", ',', ' ', &status, &end); |
| 174 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 175 |
CPPUNIT_ASSERT_EQUAL(0.0042, res); |
| 176 |
|
| 177 |
// "- 1" is nothing |
| 178 |
res = rtl::math::stringToDouble("- 1", '.', ',', &status, &end); |
| 179 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 180 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); |
| 181 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 182 |
|
| 183 |
// "-1E+E" : no exponent |
| 184 |
res = rtl::math::stringToDouble("-1E+E", '.', ',', &status, &end); |
| 185 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 186 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(2), end); |
| 187 |
CPPUNIT_ASSERT_EQUAL(-1.0, res); |
| 188 |
|
| 189 |
// "-0" is negative zero |
| 190 |
res = rtl::math::stringToDouble("-0", '.', ',', &status, &end); |
| 191 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 192 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(2), end); |
| 193 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 194 |
CPPUNIT_ASSERT(std::signbit(res)); |
| 195 |
|
| 196 |
// Compensating: "0.001E311" is 1E308, not overflow/inf |
| 197 |
res = rtl::math::stringToDouble("0.001E311", '.', ',', &status, &end); |
| 198 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 199 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(9), end); |
| 200 |
CPPUNIT_ASSERT_EQUAL(1E308, res); |
| 201 |
|
| 202 |
res = rtl::math::stringToDouble("1E8589934590", '.', ',', &status, &end); |
| 203 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_OutOfRange, status); |
| 204 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(12), end); |
| 205 |
CPPUNIT_ASSERT_EQUAL(std::numeric_limits<double>::infinity(), res); |
| 206 |
|
| 207 |
// DBL_MAX and 4 nextafters |
| 208 |
double fValAfter = DBL_MAX; |
| 209 |
res = rtl::math::stringToDouble("1.7976931348623157e+308", '.', ',', &status, &end); |
| 210 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 211 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(23), end); |
| 212 |
CPPUNIT_ASSERT_EQUAL(fValAfter, res); |
| 213 |
|
| 214 |
fValAfter = std::nextafter( fValAfter, 0); |
| 215 |
res = rtl::math::stringToDouble("1.7976931348623155e+308", '.', ',', &status, &end); |
| 216 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 217 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(23), end); |
| 218 |
CPPUNIT_ASSERT_EQUAL(fValAfter, res); |
| 219 |
|
| 220 |
fValAfter = std::nextafter( fValAfter, 0); |
| 221 |
res = rtl::math::stringToDouble("1.7976931348623153e+308", '.', ',', &status, &end); |
| 222 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 223 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(23), end); |
| 224 |
CPPUNIT_ASSERT_EQUAL(fValAfter, res); |
| 225 |
|
| 226 |
fValAfter = std::nextafter( fValAfter, 0); |
| 227 |
res = rtl::math::stringToDouble("1.7976931348623151e+308", '.', ',', &status, &end); |
| 228 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 229 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(23), end); |
| 230 |
CPPUNIT_ASSERT_EQUAL(fValAfter, res); |
| 231 |
|
| 232 |
fValAfter = std::nextafter( fValAfter, 0); |
| 233 |
res = rtl::math::stringToDouble("1.7976931348623149e+308", '.', ',', &status, &end); |
| 234 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 235 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(23), end); |
| 236 |
CPPUNIT_ASSERT_EQUAL(fValAfter, res); |
| 237 |
|
| 238 |
// Minimal subnormal value, as documented for Basic |
| 239 |
res = rtl::math::stringToDouble("4.94065645841247E-324", '.', ',', &status, &end); |
| 240 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 241 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(21), end); |
| 242 |
CPPUNIT_ASSERT_EQUAL(0x1p-1074, res); |
| 243 |
|
| 244 |
res = rtl::math::stringToDouble("1.79769313486232E+308", '.', ',', &status, &end); |
| 245 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 246 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(21), end); |
| 247 |
CPPUNIT_ASSERT_EQUAL(DBL_MAX, res); |
| 248 |
|
| 249 |
res = rtl::math::stringToDouble("-1.79769313486232e308", '.', ',', &status, &end); |
| 250 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 251 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(21), end); |
| 252 |
CPPUNIT_ASSERT_EQUAL(-DBL_MAX, res); |
| 253 |
} |
| 254 |
|
| 255 |
void test_stringToDouble_bad() { |
| 256 |
rtl_math_ConversionStatus status; |
| 257 |
sal_Int32 end; |
| 258 |
double res = rtl::math::stringToDouble( |
| 259 |
" +Efoo", |
| 260 |
'.', ',', &status, &end); |
| 261 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 262 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); |
| 263 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 264 |
|
| 265 |
res = rtl::math::stringToDouble( |
| 266 |
".", |
| 267 |
'.', ',', &status, &end); |
| 268 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 269 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); |
| 270 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 271 |
|
| 272 |
res = rtl::math::stringToDouble( |
| 273 |
" +.Efoo", |
| 274 |
'.', ',', &status, &end); |
| 275 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 276 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); |
| 277 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 278 |
|
| 279 |
res = rtl::math::stringToDouble( |
| 280 |
" +,.Efoo", |
| 281 |
'.', ',', &status, &end); |
| 282 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 283 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); |
| 284 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 285 |
|
| 286 |
// Leading group separator is none. |
| 287 |
res = rtl::math::stringToDouble( |
| 288 |
",1234", |
| 289 |
'.', ',', &status, &end); |
| 290 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 291 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end); |
| 292 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 293 |
} |
| 294 |
|
| 295 |
void test_stringToDouble_exponent_without_digit() { |
| 296 |
rtl_math_ConversionStatus status; |
| 297 |
sal_Int32 end; |
| 298 |
double res = rtl::math::stringToDouble( |
| 299 |
"1e", |
| 300 |
'.', ',', &status, &end); |
| 301 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 302 |
CPPUNIT_ASSERT_EQUAL(RTL_CONSTASCII_LENGTH("1"), end); |
| 303 |
CPPUNIT_ASSERT_EQUAL(1.0, res); |
| 304 |
res = rtl::math::stringToDouble( |
| 305 |
"0e", |
| 306 |
'.', ',', &status, &end); |
| 307 |
CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status); |
| 308 |
CPPUNIT_ASSERT_EQUAL(RTL_CONSTASCII_LENGTH("1"), end); |
| 309 |
CPPUNIT_ASSERT_EQUAL(0.0, res); |
| 310 |
} |
| 311 |
|
| 312 |
void test_round() { |
| 313 |
double fVal = 5000000000000001.0; |
| 314 |
CPPUNIT_ASSERT_EQUAL( 5000000000000001.0, rtl::math::round( fVal, 9, rtl_math_RoundingMode_Corrected)); |
| 315 |
|
| 316 |
fVal = 8796093022188.0; |
| 317 |
CPPUNIT_ASSERT_EQUAL( 6093022188.0 , rtl::math::round( fVal, 9, rtl_math_RoundingMode_Corrected) - 8790000000000); |
| 318 |
|
| 319 |
fVal = 4503599627370491.0; |
| 320 |
CPPUNIT_ASSERT_EQUAL( 4503599627370000.0, rtl::math::round( fVal, -3, rtl_math_RoundingMode_Corrected)); |
| 321 |
|
| 322 |
// test #tdf158190: ROUND(16.83 * 650, 0) |
| 323 |
fVal = 16.83; |
| 324 |
CPPUNIT_ASSERT_EQUAL( 10940.0, rtl::math::round( fVal * 650, 0, rtl_math_RoundingMode_Corrected)); |
| 325 |
} |
| 326 |
|
| 327 |
void test_doubleToString() { |
| 328 |
double fVal = 999999999999999.0; |
| 329 |
sal_Int32 aGroups[3] = { 3, 2, 0 }; |
| 330 |
OUString aRes( rtl::math::doubleToUString( fVal, |
| 331 |
rtl_math_StringFormat_Automatic, |
| 332 |
rtl_math_DecimalPlaces_Max, |
| 333 |
'.', aGroups, ',', true)); |
| 334 |
CPPUNIT_ASSERT_EQUAL( u"99,99,99,99,99,99,999"_ustr, aRes); |
| 335 |
|
| 336 |
fVal = 949.0; |
| 337 |
aRes = rtl::math::doubleToUString( fVal, |
| 338 |
rtl_math_StringFormat_Automatic, |
| 339 |
-2, // round before decimals |
| 340 |
'.', aGroups, ',', true); |
| 341 |
CPPUNIT_ASSERT_EQUAL( u"900"_ustr, aRes); |
| 342 |
|
| 343 |
fVal = 950.0; |
| 344 |
aRes = rtl::math::doubleToUString( fVal, |
| 345 |
rtl_math_StringFormat_Automatic, |
| 346 |
-2, // round before decimals |
| 347 |
'.', aGroups, ',', true); |
| 348 |
CPPUNIT_ASSERT_EQUAL( u"1,000"_ustr, aRes); |
| 349 |
|
| 350 |
// Check non-ASCII separators: Arabic decimal separator U+066B, thousand separator U+066C |
| 351 |
fVal = 123456.78; |
| 352 |
aRes = rtl::math::doubleToUString( fVal, |
| 353 |
rtl_math_StringFormat_Automatic, |
| 354 |
2, |
| 355 |
u'٫', aGroups, u'٬', true); |
| 356 |
CPPUNIT_ASSERT_EQUAL( u"1٬23٬456٫78"_ustr, aRes); |
| 357 |
|
| 358 |
fVal = 4503599627370495.0; |
| 359 |
aRes = rtl::math::doubleToUString( fVal, |
| 360 |
rtl_math_StringFormat_Automatic, |
| 361 |
rtl_math_DecimalPlaces_Max, '.'); |
| 362 |
CPPUNIT_ASSERT_EQUAL( u"4503599627370495"_ustr, aRes); |
| 363 |
|
| 364 |
fVal = 4503599627370496.0; |
| 365 |
aRes = rtl::math::doubleToUString( fVal, |
| 366 |
rtl_math_StringFormat_Automatic, |
| 367 |
2, '.'); |
| 368 |
CPPUNIT_ASSERT_EQUAL( u"4503599627370496.00"_ustr, aRes); |
| 369 |
|
| 370 |
fVal = -4503599627370496.0; |
| 371 |
aRes = rtl::math::doubleToUString( fVal, |
| 372 |
rtl_math_StringFormat_Automatic, |
| 373 |
2, '.'); |
| 374 |
CPPUNIT_ASSERT_EQUAL( u"-4503599627370496.00"_ustr, aRes); |
| 375 |
|
| 376 |
fVal = 9007199254740991.0; // (2^53)-1 |
| 377 |
aRes = rtl::math::doubleToUString( fVal, |
| 378 |
rtl_math_StringFormat_Automatic, |
| 379 |
rtl_math_DecimalPlaces_Max, '.', true); |
| 380 |
CPPUNIT_ASSERT_EQUAL( u"9007199254740991"_ustr, aRes); |
| 381 |
|
| 382 |
fVal = 9007199254740992.0; // (2^53), algorithm switch |
| 383 |
aRes = rtl::math::doubleToUString( fVal, |
| 384 |
rtl_math_StringFormat_Automatic, |
| 385 |
rtl_math_DecimalPlaces_Max, '.', true); |
| 386 |
CPPUNIT_ASSERT_EQUAL( u"9.00719925474099E+015"_ustr, aRes); |
| 387 |
|
| 388 |
fVal = 9007199254740993.0; // (2^53)+1 would be but is 9007199254740992 |
| 389 |
aRes = rtl::math::doubleToUString( fVal, |
| 390 |
rtl_math_StringFormat_Automatic, |
| 391 |
rtl_math_DecimalPlaces_Max, '.', true); |
| 392 |
CPPUNIT_ASSERT_EQUAL( u"9.00719925474099E+015"_ustr, aRes); |
| 393 |
|
| 394 |
// Test rtl_math_StringFormat_G |
| 395 |
|
| 396 |
fVal = 0.001234567; |
| 397 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_G, 3, '.', true); |
| 398 |
CPPUNIT_ASSERT_EQUAL( u"0.00123"_ustr, aRes); |
| 399 |
|
| 400 |
fVal = 123.4567; |
| 401 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_G, 3, '.', true); |
| 402 |
CPPUNIT_ASSERT_EQUAL( u"123"_ustr, aRes); |
| 403 |
|
| 404 |
fVal = 123.4567; |
| 405 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_G, 4, '.', true); |
| 406 |
CPPUNIT_ASSERT_EQUAL( u"123.5"_ustr, aRes); |
| 407 |
|
| 408 |
fVal = 99.6; |
| 409 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_G, 3, '.', true); |
| 410 |
CPPUNIT_ASSERT_EQUAL( u"99.6"_ustr, aRes); |
| 411 |
|
| 412 |
// Expected could be 1E+03 (as 999.6 rounded to 3 significant digits |
| 413 |
// results in 1000 with an exponent equal to significant digits). |
| 414 |
// Currently we don't and output 1000 instead, negligible. |
| 415 |
fVal = 999.6; |
| 416 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_G, 3, '.', true); |
| 417 |
CPPUNIT_ASSERT_EQUAL( u"1000"_ustr, aRes); |
| 418 |
|
| 419 |
fVal = 9999.6; |
| 420 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_G, 3, '.', true); |
| 421 |
CPPUNIT_ASSERT_EQUAL( u"1E+004"_ustr, aRes); |
| 422 |
|
| 423 |
fVal = 12345.6789; |
| 424 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_G, -3, '.', true); |
| 425 |
CPPUNIT_ASSERT_EQUAL( u"1.2E+004"_ustr, aRes); |
| 426 |
|
| 427 |
fVal = 2000000528764739.0; |
| 428 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_G, 15, '.', true); |
| 429 |
CPPUNIT_ASSERT_EQUAL( u"2.00000052876474E+015"_ustr, aRes); |
| 430 |
|
| 431 |
// The original problem of tdf#160985 was nDecPlaces = 16 with |
| 432 |
// RTL_STR_MAX_VALUEOFDOUBLE - std::size("-x.E-xxx") |
| 433 |
fVal = 2000000528764739.0; |
| 434 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_G, 16, '.', true); |
| 435 |
CPPUNIT_ASSERT_EQUAL( u"2000000528764739"_ustr, aRes); |
| 436 |
|
| 437 |
fVal = 2000000528764739.0; |
| 438 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_G, 18, '.', false); |
| 439 |
CPPUNIT_ASSERT_EQUAL( u"2000000528764739.00"_ustr, aRes); |
| 440 |
|
| 441 |
// DBL_MAX and 4 nextafters |
| 442 |
fVal = DBL_MAX; |
| 443 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, |
| 444 |
rtl_math_DecimalPlaces_Max, '.', true); |
| 445 |
CPPUNIT_ASSERT_EQUAL( u"1.7976931348623157E+308"_ustr, aRes); |
| 446 |
|
| 447 |
fVal = std::nextafter( fVal, 0); |
| 448 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, |
| 449 |
rtl_math_DecimalPlaces_Max, '.', true); |
| 450 |
CPPUNIT_ASSERT_EQUAL( u"1.7976931348623155E+308"_ustr, aRes); |
| 451 |
|
| 452 |
fVal = std::nextafter( fVal, 0); |
| 453 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, |
| 454 |
rtl_math_DecimalPlaces_Max, '.', true); |
| 455 |
CPPUNIT_ASSERT_EQUAL( u"1.7976931348623153E+308"_ustr, aRes); |
| 456 |
|
| 457 |
fVal = std::nextafter( fVal, 0); |
| 458 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, |
| 459 |
rtl_math_DecimalPlaces_Max, '.', true); |
| 460 |
CPPUNIT_ASSERT_EQUAL( u"1.7976931348623151E+308"_ustr, aRes); |
| 461 |
|
| 462 |
fVal = std::nextafter( fVal, 0); |
| 463 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, |
| 464 |
rtl_math_DecimalPlaces_Max, '.', true); |
| 465 |
CPPUNIT_ASSERT_EQUAL( u"1.797693134862315E+308"_ustr, aRes); |
| 466 |
CPPUNIT_ASSERT_EQUAL(fVal, rtl::math::stringToDouble(aRes, '.', ',')); // Test roundtrip |
| 467 |
|
| 468 |
// DBL_MAX and 4 nextafters rounded to 15 decimals |
| 469 |
fVal = DBL_MAX; |
| 470 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, 15, '.', true); |
| 471 |
CPPUNIT_ASSERT_EQUAL( u"1.797693134862316E+308"_ustr, aRes); |
| 472 |
|
| 473 |
fVal = std::nextafter( fVal, 0); |
| 474 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, 15, '.', true); |
| 475 |
CPPUNIT_ASSERT_EQUAL( u"1.797693134862316E+308"_ustr, aRes); |
| 476 |
|
| 477 |
fVal = std::nextafter( fVal, 0); |
| 478 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, 15, '.', true); |
| 479 |
CPPUNIT_ASSERT_EQUAL( u"1.797693134862315E+308"_ustr, aRes); |
| 480 |
|
| 481 |
fVal = std::nextafter( fVal, 0); |
| 482 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, 15, '.', true); |
| 483 |
CPPUNIT_ASSERT_EQUAL( u"1.797693134862315E+308"_ustr, aRes); |
| 484 |
|
| 485 |
fVal = std::nextafter( fVal, 0); |
| 486 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, 15, '.', true); |
| 487 |
CPPUNIT_ASSERT_EQUAL( u"1.797693134862315E+308"_ustr, aRes); |
| 488 |
|
| 489 |
// DBL_MAX rounded to 14 decimals |
| 490 |
fVal = DBL_MAX; |
| 491 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, 14, '.', true); |
| 492 |
CPPUNIT_ASSERT_EQUAL( u"1.79769313486232E+308"_ustr, aRes); |
| 493 |
|
| 494 |
// DBL_MAX rounded to 2 decimals |
| 495 |
fVal = DBL_MAX; |
| 496 |
aRes = rtl::math::doubleToUString( fVal, rtl_math_StringFormat_Automatic, 2, '.', true); |
| 497 |
CPPUNIT_ASSERT_EQUAL( u"1.8E+308"_ustr, aRes); |
| 498 |
|
| 499 |
// Crashed after commit eae24a9488814e77254d175c11fc4a138c1dbd30 |
| 500 |
fVal = 123456.789; |
| 501 |
aRes = rtl::math::doubleToUString(fVal, rtl_math_StringFormat_E, 2, '.', false); |
| 502 |
CPPUNIT_ASSERT_EQUAL(u"1.23E+005"_ustr, aRes); |
| 503 |
|
| 504 |
fVal = 9.9999999999999929; |
| 505 |
aRes = rtl::math::doubleToUString(fVal, rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max, '.', true); |
| 506 |
CPPUNIT_ASSERT_EQUAL(u"9.99999999999999"_ustr, aRes); |
| 507 |
|
| 508 |
fVal = 0.99999999999999933; |
| 509 |
aRes = rtl::math::doubleToUString(fVal, rtl_math_StringFormat_F, rtl_math_DecimalPlaces_Max, '.', true); |
| 510 |
CPPUNIT_ASSERT_EQUAL(u"0.999999999999999"_ustr, aRes); |
| 511 |
} |
| 512 |
|
| 513 |
void test_approx() { |
| 514 |
// (2^53)-1 , (2^53)-3 |
| 515 |
CPPUNIT_ASSERT_EQUAL( false, rtl::math::approxEqual( 9007199254740991.0, 9007199254740989.0)); |
| 516 |
// (2^53)-1 , (2^53)-2 |
| 517 |
CPPUNIT_ASSERT_EQUAL( false, rtl::math::approxEqual( 9007199254740991.0, 9007199254740990.0)); |
| 518 |
// Note: the following are internally represented as 900719925474099.12 |
| 519 |
// and 900719925474098.88 and the difference is 0.25 ... |
| 520 |
CPPUNIT_ASSERT_EQUAL( true, rtl::math::approxEqual( 900719925474099.1, 900719925474098.9)); |
| 521 |
CPPUNIT_ASSERT_EQUAL( true, rtl::math::approxEqual( 72.944444444444443, 72.9444444444444)); |
| 522 |
CPPUNIT_ASSERT_EQUAL( true, rtl::math::approxEqual( 359650.27322404372, 359650.27322404401)); |
| 523 |
CPPUNIT_ASSERT_EQUAL( true, rtl::math::approxEqual( 5.3590326375710063e+238, 5.3590326375710109e+238)); |
| 524 |
CPPUNIT_ASSERT_EQUAL( true, rtl::math::approxEqual( 7.4124095894894475e+158, 7.4124095894894514e+158)); |
| 525 |
CPPUNIT_ASSERT_EQUAL( true, rtl::math::approxEqual( 1.2905754687023132e+79, 1.2905754687023098e+79)); |
| 526 |
CPPUNIT_ASSERT_EQUAL( true, rtl::math::approxEqual( 3.5612905090455637e+38, 3.5612905090455599e+38)); |
| 527 |
CPPUNIT_ASSERT_EQUAL( true, rtl::math::approxEqual( 0.001000810185185185, 0.00100081018518519)); |
| 528 |
// 0.3 - 0.2 - 0.1 == 0.0 |
| 529 |
CPPUNIT_ASSERT_EQUAL( 0.0, rtl::math::approxSub( rtl::math::approxSub( 0.3, 0.2), 0.1)); |
| 530 |
// ((2^53)-1) - ((2^53)-2) == 1.0 |
| 531 |
CPPUNIT_ASSERT_EQUAL( 1.0, rtl::math::approxSub( 9007199254740991.0, 9007199254740990.0)); |
| 532 |
// (3^31) - ((3^31)-1) == 1.0 |
| 533 |
CPPUNIT_ASSERT_EQUAL( 1.0, rtl::math::approxSub( 617673396283947.0, 617673396283946.0)); |
| 534 |
} |
| 535 |
|
| 536 |
void test_erf() { |
| 537 |
double x, res; |
| 538 |
x = 0.0; |
| 539 |
res = rtl::math::erf(x); |
| 540 |
CPPUNIT_ASSERT_EQUAL(0.0,res); |
| 541 |
rtl::math::setInf( &x, false); |
| 542 |
res = rtl::math::erf(x); |
| 543 |
CPPUNIT_ASSERT_EQUAL(1.0,res); |
| 544 |
rtl::math::setInf( &x, true); |
| 545 |
res = rtl::math::erf(x); |
| 546 |
CPPUNIT_ASSERT_EQUAL(-1.0,res); |
| 547 |
rtl::math::setNan( &x); |
| 548 |
res = rtl::math::erf(x); |
| 549 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 550 |
x = 3.0; |
| 551 |
res = rtl::math::erf(-x); |
| 552 |
CPPUNIT_ASSERT_DOUBLES_EQUAL( -rtl::math::erf(x), res, 1E-12); |
| 553 |
} |
| 554 |
|
| 555 |
void test_erfc() { |
| 556 |
double x, res; |
| 557 |
x = 0.0; |
| 558 |
res = rtl::math::erfc(x); |
| 559 |
CPPUNIT_ASSERT_EQUAL(1.0,res); |
| 560 |
rtl::math::setInf( &x, false); |
| 561 |
res = rtl::math::erfc(x); |
| 562 |
CPPUNIT_ASSERT_EQUAL(0.0,res); |
| 563 |
rtl::math::setInf( &x, true); |
| 564 |
res = rtl::math::erfc(x); |
| 565 |
CPPUNIT_ASSERT_EQUAL(2.0,res); |
| 566 |
rtl::math::setNan( &x); |
| 567 |
res = rtl::math::erfc(x); |
| 568 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 569 |
x = 3.0; |
| 570 |
res = rtl::math::erfc(-x); |
| 571 |
CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0 - rtl::math::erfc(x), res, 1E-12); |
| 572 |
} |
| 573 |
|
| 574 |
void test_expm1() { |
| 575 |
double x, res; |
| 576 |
x = 0.0; |
| 577 |
res = rtl::math::expm1(x); |
| 578 |
CPPUNIT_ASSERT_EQUAL(0.0,res); |
| 579 |
x = -0.0; |
| 580 |
res = rtl::math::expm1(x); |
| 581 |
CPPUNIT_ASSERT_EQUAL(-0.0,res); |
| 582 |
CPPUNIT_ASSERT(std::signbit(res)); |
| 583 |
rtl::math::setInf( &x, false); |
| 584 |
res = rtl::math::expm1(x); |
| 585 |
CPPUNIT_ASSERT_EQUAL(true, std::isinf(res) && !std::signbit(res)); |
| 586 |
rtl::math::setInf( &x, true); |
| 587 |
res = rtl::math::expm1(x); |
| 588 |
CPPUNIT_ASSERT_EQUAL(-1.0,res); |
| 589 |
rtl::math::setNan( &x); |
| 590 |
res = rtl::math::expm1(x); |
| 591 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 592 |
} |
| 593 |
|
| 594 |
void test_log1p() { |
| 595 |
double x, res; |
| 596 |
x = 0.0; |
| 597 |
res = rtl::math::log1p(x); |
| 598 |
CPPUNIT_ASSERT_EQUAL(0.0,res); |
| 599 |
x = -0.0; |
| 600 |
res = rtl::math::log1p(x); |
| 601 |
CPPUNIT_ASSERT_EQUAL(-0.0,res); |
| 602 |
CPPUNIT_ASSERT(std::signbit(res)); |
| 603 |
rtl::math::setInf( &x, false); |
| 604 |
res = rtl::math::log1p(x); |
| 605 |
CPPUNIT_ASSERT_EQUAL(true, std::isinf(res) && !std::signbit(res)); |
| 606 |
x = -1.0; |
| 607 |
res = rtl::math::log1p(x); |
| 608 |
CPPUNIT_ASSERT_EQUAL(true, std::isinf(res) && std::signbit(res)); |
| 609 |
x = -1.1; |
| 610 |
res = rtl::math::log1p(x); |
| 611 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 612 |
rtl::math::setInf( &x, true); |
| 613 |
res = rtl::math::log1p(x); |
| 614 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 615 |
rtl::math::setNan( &x); |
| 616 |
res = rtl::math::log1p(x); |
| 617 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 618 |
} |
| 619 |
|
| 620 |
void test_acosh() { |
| 621 |
double res; |
| 622 |
|
| 623 |
res = rtl::math::acosh(-1.0); // NaN |
| 624 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 625 |
|
| 626 |
res = rtl::math::acosh(0.0); // NaN |
| 627 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 628 |
|
| 629 |
res = rtl::math::acosh(0.5); // NaN |
| 630 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 631 |
|
| 632 |
CPPUNIT_ASSERT_EQUAL(0.0, rtl::math::acosh(1.0)); |
| 633 |
|
| 634 |
res = rtl::math::acosh(std::numeric_limits<double>::infinity()); // +Inf |
| 635 |
CPPUNIT_ASSERT(!std::signbit(res)); |
| 636 |
CPPUNIT_ASSERT(std::isinf(res)); |
| 637 |
|
| 638 |
// #i97605 |
| 639 |
CPPUNIT_ASSERT_DOUBLES_EQUAL(692.56728736744176, rtl::math::acosh(3e+300), 1e-15); |
| 640 |
CPPUNIT_ASSERT_DOUBLES_EQUAL(0.014142017775252324, rtl::math::acosh(1.0001), 1e-15); |
| 641 |
} |
| 642 |
|
| 643 |
void test_asinh() { |
| 644 |
double res; |
| 645 |
|
| 646 |
res = rtl::math::asinh(-std::numeric_limits<double>::infinity()); // -Inf |
| 647 |
CPPUNIT_ASSERT(std::signbit(res)); |
| 648 |
CPPUNIT_ASSERT(std::isinf(res)); |
| 649 |
|
| 650 |
CPPUNIT_ASSERT_EQUAL(0.0, rtl::math::asinh(0.0)); |
| 651 |
|
| 652 |
res = rtl::math::asinh(std::numeric_limits<double>::infinity()); // +Inf |
| 653 |
CPPUNIT_ASSERT(!std::signbit(res)); |
| 654 |
CPPUNIT_ASSERT(std::isinf(res)); |
| 655 |
|
| 656 |
// #i97605 |
| 657 |
CPPUNIT_ASSERT_DOUBLES_EQUAL(691.67568924815798, rtl::math::asinh(1.23e+300), 1e-15); |
| 658 |
CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0350378961923076, rtl::math::asinh(1.23), 1e-16); |
| 659 |
CPPUNIT_ASSERT_DOUBLES_EQUAL(1.23e-300, rtl::math::asinh(1.23e-300), 1e-303); |
| 660 |
|
| 661 |
// asinh is an odd function |
| 662 |
CPPUNIT_ASSERT_EQUAL(-rtl::math::asinh(1.23e+300), rtl::math::asinh(-1.23e+300)); |
| 663 |
CPPUNIT_ASSERT_EQUAL(-rtl::math::asinh(1.23), rtl::math::asinh(-1.23)); |
| 664 |
CPPUNIT_ASSERT_EQUAL(-rtl::math::asinh(1.23e-300), rtl::math::asinh(-1.23e-300)); |
| 665 |
} |
| 666 |
|
| 667 |
void test_atanh() { |
| 668 |
double res; |
| 669 |
|
| 670 |
res = rtl::math::atanh(-2.0); // NaN |
| 671 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 672 |
|
| 673 |
res = rtl::math::atanh(-1.0); // -Inf |
| 674 |
CPPUNIT_ASSERT(std::signbit(res)); |
| 675 |
CPPUNIT_ASSERT(std::isinf(res)); |
| 676 |
|
| 677 |
CPPUNIT_ASSERT_EQUAL(0.0, rtl::math::atanh(0.0)); |
| 678 |
|
| 679 |
res = rtl::math::atanh(1.0); // +Inf |
| 680 |
CPPUNIT_ASSERT(!std::signbit(res)); |
| 681 |
CPPUNIT_ASSERT(std::isinf(res)); |
| 682 |
|
| 683 |
res = rtl::math::atanh(2.0); // NaN |
| 684 |
CPPUNIT_ASSERT(std::isnan(res)); |
| 685 |
} |
| 686 |
|
| 687 |
void test_payloadNaN() { |
| 688 |
NanPayloadTest nanTest; |
| 689 |
CPPUNIT_ASSERT_MESSAGE(nanTest.getMessage(), nanTest.getIfSupported()); |
| 690 |
} |
| 691 |
|
| 692 |
CPPUNIT_TEST_SUITE(Test); |
| 693 |
CPPUNIT_TEST(test_stringToDouble_good); |
| 694 |
CPPUNIT_TEST(test_stringToDouble_bad); |
| 695 |
CPPUNIT_TEST(test_stringToDouble_exponent_without_digit); |
| 696 |
CPPUNIT_TEST(test_round); |
| 697 |
CPPUNIT_TEST(test_doubleToString); |
| 698 |
CPPUNIT_TEST(test_erf); |
| 699 |
CPPUNIT_TEST(test_erfc); |
| 700 |
CPPUNIT_TEST(test_expm1); |
| 701 |
CPPUNIT_TEST(test_log1p); |
| 702 |
CPPUNIT_TEST(test_approx); |
| 703 |
CPPUNIT_TEST(test_acosh); |
| 704 |
CPPUNIT_TEST(test_asinh); |
| 705 |
CPPUNIT_TEST(test_atanh); |
| 706 |
#ifndef DISABLE_NAN_TESTS |
| 707 |
CPPUNIT_TEST(test_payloadNaN); |
| 708 |
#endif |
| 709 |
CPPUNIT_TEST_SUITE_END(); |
| 710 |
}; |
| 711 |
|
| 712 |
CPPUNIT_TEST_SUITE_REGISTRATION(Test); |
| 713 |
|
| 714 |
} |
| 715 |
|
| 716 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |