Kaydet (Commit) 968cadb8 authored tarafından David Tardon's avatar David Tardon

allow construction of BigInt from sal_Int64 on 32 bit

Change-Id: Ib68920fc9bd693d2f2679b4fc27d9956dc42fc86
üst 08280034
...@@ -99,6 +99,9 @@ public: ...@@ -99,6 +99,9 @@ public:
} }
BigInt( sal_uInt32 nVal ); BigInt( sal_uInt32 nVal );
#if SAL_TYPES_SIZEOFLONG < SAL_TYPES_SIZEOFLONGLONG
BigInt( long long nVal );
#endif
BigInt( const BigInt& rBigInt ); BigInt( const BigInt& rBigInt );
BigInt( const OUString& rString ); BigInt( const OUString& rString );
......
...@@ -14,6 +14,7 @@ $(eval $(call gb_CppunitTest_CppunitTest,tools_test)) ...@@ -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_use_external,tools_test,boost_headers))
$(eval $(call gb_CppunitTest_add_exception_objects,tools_test, \ $(eval $(call gb_CppunitTest_add_exception_objects,tools_test, \
tools/qa/cppunit/test_bigint \
tools/qa/cppunit/test_inetmime \ tools/qa/cppunit/test_inetmime \
tools/qa/cppunit/test_pathutils \ tools/qa/cppunit/test_pathutils \
tools/qa/cppunit/test_rational \ tools/qa/cppunit/test_rational \
......
/* -*- 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: */
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 . * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/ */
#include <limits>
#include <math.h> #include <math.h>
#include <rtl/ustrbuf.hxx> #include <rtl/ustrbuf.hxx>
...@@ -577,6 +578,33 @@ BigInt::BigInt( sal_uInt32 nValue ) ...@@ -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 BigInt::operator sal_uIntPtr() const
{ {
if ( !bIsBig ) if ( !bIsBig )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment