From 940b4d1848e8c70ab7642901a68594e8016caffc Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 18:51:28 +0200 Subject: Adding upstream version 1:7.0.4. Signed-off-by: Daniel Baumann --- sal/qa/inc/stringhelper.hxx | 33 ++++++++++++ sal/qa/inc/valueequal.hxx | 120 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 sal/qa/inc/stringhelper.hxx create mode 100644 sal/qa/inc/valueequal.hxx (limited to 'sal/qa/inc') diff --git a/sal/qa/inc/stringhelper.hxx b/sal/qa/inc/stringhelper.hxx new file mode 100644 index 000000000..5bce6ebbb --- /dev/null +++ b/sal/qa/inc/stringhelper.hxx @@ -0,0 +1,33 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_SAL_QA_INC_STRINGHELPER_HXX +#define INCLUDED_SAL_QA_INC_STRINGHELPER_HXX + +#include +#include + +inline void operator <<= (OString& _rAsciiString, OUString const & _rUnicodeString) +{ + _rAsciiString = OUStringToOString(_rUnicodeString,RTL_TEXTENCODING_ASCII_US); +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/qa/inc/valueequal.hxx b/sal/qa/inc/valueequal.hxx new file mode 100644 index 000000000..5e6d47d30 --- /dev/null +++ b/sal/qa/inc/valueequal.hxx @@ -0,0 +1,120 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_SAL_QA_INC_VALUEEQUAL_HXX +#define INCLUDED_SAL_QA_INC_VALUEEQUAL_HXX + +#include + +#include + +#define PREC_float 1 +#define PREC_double 2 +#define PREC_long_double 3 + +template +bool is_equal(T x, T y, sal_Int16 _nPrec) +{ + if (!(std::isfinite(x) && std::isfinite(y))) { + return x == y; + } + + // due to the fact that this check looks only if both values are equal + // we only need to look on one value + + // 14 digits will announce the checkPrecisionSize + + sal_Int32 nPRECISION; + switch(_nPrec) + { + case PREC_float: + nPRECISION = 6; + break; + case PREC_double: + nPRECISION = 14; + break; + case PREC_long_double: + nPRECISION = 20; + break; + default: + nPRECISION = 2; + } + + if (x < 0) + { + x = -x; + } + if (y < 0) + { + y = -y; + } + + if (_nPrec != PREC_long_double) + { + printf("double equal: %.20f\n", x); + printf(" %.20f\n", y); + } + //here nPrecOfN is the number after dot + sal_Int32 nBeforeDot = sal_Int32( log10(x) ); + if ( nBeforeDot < 0) + { + nBeforeDot = 0; + } + //printf("nPRECISION is %d\n", nPRECISION); + sal_Int32 nPrecOfN = -nPRECISION + nBeforeDot; + + if (_nPrec != PREC_long_double) + printf("nPrecOfN is %" SAL_PRIdINT32 "\n", nPrecOfN); + + double nPrec = pow(0.1, -nPrecOfN); + + if (_nPrec != PREC_long_double) + printf(" prec: %.20f\n", nPrec); + + double nDelta = fabs( x - y ) ; + + if (_nPrec != PREC_long_double) + { + printf(" delta: %.20f\n", nDelta); + printf(" nPrec: %.20f\n", nPrec); + printf("delta must be less or equal to prec\n\n"); + } + + if (nDelta > nPrec) + { + // values are not equal + return false; + } + + // values are equal + return true; +} + +bool is_float_equal(float x, float y) +{ + return is_equal(x, y, PREC_float); +} +bool is_double_equal(double x, double y) +{ + return is_equal(x, y, PREC_double); +} + +#endif // INCLUDED_SAL_QA_INC_VALUEEQUAL_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3