Line data Source code
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 :
10 : #include "sal/config.h"
11 :
12 : #include "cppunit/TestAssert.h"
13 : #include "cppunit/TestFixture.h"
14 : #include "cppunit/extensions/HelperMacros.h"
15 : #include "rtl/string.hxx"
16 : #include "rtl/ustring.hxx"
17 : #include "sal/types.h"
18 :
19 : namespace {
20 :
21 24 : template< typename T > class Test: public CppUnit::TestFixture {
22 : private:
23 4 : CPPUNIT_TEST_SUITE(Test);
24 2 : CPPUNIT_TEST(testToInt32Overflow);
25 2 : CPPUNIT_TEST(testToUInt32Overflow);
26 2 : CPPUNIT_TEST(testToInt64Overflow);
27 2 : CPPUNIT_TEST(testToUInt64Overflow);
28 10 : CPPUNIT_TEST_SUITE_END();
29 :
30 2 : void testToInt32Overflow() {
31 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), T("-2147483649").toInt32());
32 2 : CPPUNIT_ASSERT_EQUAL(SAL_MIN_INT32, T("-2147483648").toInt32());
33 2 : CPPUNIT_ASSERT_EQUAL(SAL_MIN_INT32 + 1, T("-2147483647").toInt32());
34 2 : CPPUNIT_ASSERT_EQUAL(SAL_MAX_INT32 - 1, T("2147483646").toInt32());
35 2 : CPPUNIT_ASSERT_EQUAL(SAL_MAX_INT32, T("2147483647").toInt32());
36 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), T("2147483648").toInt32());
37 2 : }
38 :
39 2 : void testToUInt32Overflow() {
40 2 : CPPUNIT_ASSERT_EQUAL(SAL_MAX_UINT32 - 1, T("4294967294").toUInt32());
41 2 : CPPUNIT_ASSERT_EQUAL(SAL_MAX_UINT32, T("4294967295").toUInt32());
42 2 : CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), T("4294967296").toUInt32());
43 2 : }
44 :
45 2 : void testToInt64Overflow() {
46 2 : CPPUNIT_ASSERT_EQUAL(sal_Int64(0), T("-9223372036854775809").toInt64());
47 2 : CPPUNIT_ASSERT_EQUAL(
48 : SAL_MIN_INT64, T("-9223372036854775808").toInt64());
49 2 : CPPUNIT_ASSERT_EQUAL(
50 : SAL_MIN_INT64 + 1, T("-9223372036854775807").toInt64());
51 2 : CPPUNIT_ASSERT_EQUAL(
52 : SAL_MAX_INT64 - 1, T("9223372036854775806").toInt64());
53 2 : CPPUNIT_ASSERT_EQUAL(SAL_MAX_INT64, T("9223372036854775807").toInt64());
54 2 : CPPUNIT_ASSERT_EQUAL(sal_Int64(0), T("9223372036854775808").toInt64());
55 2 : }
56 :
57 2 : void testToUInt64Overflow() {
58 2 : CPPUNIT_ASSERT_EQUAL(
59 : SAL_MAX_UINT64 - 1, T("18446744073709551614").toUInt64());
60 2 : CPPUNIT_ASSERT_EQUAL(
61 : SAL_MAX_UINT64, T("18446744073709551615").toUInt64());
62 2 : CPPUNIT_ASSERT_EQUAL(
63 : sal_uInt64(0), T("18446744073709551616").toUInt64());
64 2 : }
65 : };
66 :
67 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test< rtl::OString >);
68 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test< rtl::OUString >);
69 :
70 3 : }
71 :
72 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|