summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorDavid Tardon <dtardon@redhat.com>2014-10-17 13:08:32 +0200
committerDavid Tardon <dtardon@redhat.com>2014-10-17 13:17:21 +0200
commit968cadb82542b23271b8c472a2e6ec9248343925 (patch)
treea86a6c4fa87f43c3b053e38c0294ae5cbb234719 /tools
parent08280034197b577abf994cc05322c010fd1f9895 (diff)
allow construction of BigInt from sal_Int64 on 32 bit
Change-Id: Ib68920fc9bd693d2f2679b4fc27d9956dc42fc86
Diffstat (limited to 'tools')
-rw-r--r--tools/CppunitTest_tools_test.mk1
-rw-r--r--tools/qa/cppunit/test_bigint.cxx113
-rw-r--r--tools/source/generic/bigint.cxx28
3 files changed, 142 insertions, 0 deletions
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 )