diff options
-rw-r--r-- | include/tools/bigint.hxx | 3 | ||||
-rw-r--r-- | tools/CppunitTest_tools_test.mk | 1 | ||||
-rw-r--r-- | tools/qa/cppunit/test_bigint.cxx | 113 | ||||
-rw-r--r-- | tools/source/generic/bigint.cxx | 28 |
4 files changed, 145 insertions, 0 deletions
diff --git a/include/tools/bigint.hxx b/include/tools/bigint.hxx index c4c1c6d7b9fe..4236692b51bb 100644 --- a/include/tools/bigint.hxx +++ b/include/tools/bigint.hxx @@ -99,6 +99,9 @@ public: } BigInt( sal_uInt32 nVal ); +#if SAL_TYPES_SIZEOFLONG < SAL_TYPES_SIZEOFLONGLONG + BigInt( long long nVal ); +#endif BigInt( const BigInt& rBigInt ); BigInt( const OUString& rString ); diff --git a/tools/CppunitTest_tools_test.mk b/tools/CppunitTest_tools_test.mk index e5fda5fc92b8..87abf590a8db 100644 --- a/tools/CppunitTest_tools_test.mk +++ b/tools/CppunitTest_tools_test.mk @@ -14,6 +14,7 @@ $(eval $(call gb_CppunitTest_CppunitTest,tools_test)) $(eval $(call gb_CppunitTest_use_external,tools_test,boost_headers)) $(eval $(call gb_CppunitTest_add_exception_objects,tools_test, \ + tools/qa/cppunit/test_bigint \ tools/qa/cppunit/test_inetmime \ tools/qa/cppunit/test_pathutils \ tools/qa/cppunit/test_rational \ diff --git a/tools/qa/cppunit/test_bigint.cxx b/tools/qa/cppunit/test_bigint.cxx new file mode 100644 index 000000000000..39ab2503f4ac --- /dev/null +++ b/tools/qa/cppunit/test_bigint.cxx @@ -0,0 +1,113 @@ +/* -*- 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 . + */ + +#include <limits> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> + +#include <rtl/math.hxx> + +#include <tools/bigint.hxx> + +namespace tools +{ + +class BigIntTest : public CppUnit::TestFixture +{ +public: +#if SAL_TYPES_SIZEOFLONG < SAL_TYPES_SIZEOFLONGLONG + void testConstructionFromInt64(); +#endif + + CPPUNIT_TEST_SUITE(BigIntTest); +#if SAL_TYPES_SIZEOFLONG < SAL_TYPES_SIZEOFLONGLONG + CPPUNIT_TEST(testConstructionFromLongLong); +#endif + CPPUNIT_TEST_SUITE_END(); +}; + +#if SAL_TYPES_SIZEOFLONG < SAL_TYPES_SIZEOFLONGLONG +void BigIntTest::testConstructionFromLongLong() +{ + // small positive number + { + BigInt bi(static_cast<sal_Int64>(42)); + CPPUNIT_ASSERT(bi.IsSet()); + CPPUNIT_ASSERT(!bi.IsZero()); + CPPUNIT_ASSERT(!bi.IsNeg()); + CPPUNIT_ASSERT(bi.IsLong()); + CPPUNIT_ASSERT_EQUAL(42L, bi); + } + + // small negative number + { + BigInt bi(static_cast<sal_Int64>(-42)); + CPPUNIT_ASSERT(bi.IsSet()); + CPPUNIT_ASSERT(!bi.IsZero()); + CPPUNIT_ASSERT(bi.IsNeg()); + CPPUNIT_ASSERT(bi.IsLong()); + CPPUNIT_ASSERT_EQUAL(-42L, bi); + } + + // positive number just fitting to long + { + BigInt bi(static_cast<sal_Int64>(std::numeric_limits<long>::max())); + CPPUNIT_ASSERT(bi.IsSet()); + CPPUNIT_ASSERT(!bi.IsZero()); + CPPUNIT_ASSERT(!bi.IsNeg()); + CPPUNIT_ASSERT(bi.IsLong()); + CPPUNIT_ASSERT_EQUAL(std::numeric_limits<long>::max(), bi); + } + + // negative number just fitting to long + { + BigInt bi(static_cast<sal_Int64>(std::numeric_limits<long>::min())); + CPPUNIT_ASSERT(bi.IsSet()); + CPPUNIT_ASSERT(!bi.IsZero()); + CPPUNIT_ASSERT(bi.IsNeg()); + CPPUNIT_ASSERT(bi.IsLong()); + CPPUNIT_ASSERT_EQUAL(std::numeric_limits<long>::min(), bi); + } + + // positive number not fitting to long + { + BigInt bi(static_cast<sal_Int64>(std::numeric_limits<long>::max() + 1)); + CPPUNIT_ASSERT(bi.IsSet()); + CPPUNIT_ASSERT(!bi.IsZero()); + CPPUNIT_ASSERT(!bi.IsNeg()); + CPPUNIT_ASSERT(!bi.IsLong()); + } + + // negative number not fitting to long + { + BigInt bi(static_cast<sal_Int64>(std::numeric_limits<long>::min() - 1)); + BigInt bi(static_cast<sal_Int64>(42)); + CPPUNIT_ASSERT(bi.IsSet()); + CPPUNIT_ASSERT(!bi.IsZero()); + CPPUNIT_ASSERT(bi.IsNeg()); + CPPUNIT_ASSERT(!bi.IsLong()); + } +} +#endif + +CPPUNIT_TEST_SUITE_REGISTRATION(BigIntTest); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/tools/source/generic/bigint.cxx b/tools/source/generic/bigint.cxx index 3311efd459b5..43c0f68c3425 100644 --- a/tools/source/generic/bigint.cxx +++ b/tools/source/generic/bigint.cxx @@ -17,6 +17,7 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ +#include <limits> #include <math.h> #include <rtl/ustrbuf.hxx> @@ -577,6 +578,33 @@ BigInt::BigInt( sal_uInt32 nValue ) } } +#if SAL_TYPES_SIZEOFLONG < SAL_TYPES_SIZEOFLONGLONG +BigInt::BigInt( long long nValue ) + : nVal(0) +{ + bIsSet = true; + bIsNeg = nValue < 0; + nLen = 0; + + unsigned long long nUValue = static_cast<unsigned long long>(bIsNeg ? -nValue : nValue); + if (nUValue >= std::numeric_limits<long>::max()) + { + bIsBig = true; + for (int i = 0; (i != sizeof(unsigned long long) / 2) && (nUValue != 0); ++i) + { + nNum[i] = static_cast<sal_uInt16>(nUValue & 0xffffUL); + nUValue = nUValue >> 16; + ++nLen; + } + } + else + { + bIsBig = false; + nVal = static_cast<long>(nValue); + } +} +#endif + BigInt::operator sal_uIntPtr() const { if ( !bIsBig ) |