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 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/types.h>
21 : #include "cppunit/TestAssert.h"
22 : #include "cppunit/TestFixture.h"
23 : #include "cppunit/extensions/HelperMacros.h"
24 : #include "cppunit/plugin/TestPlugIn.h"
25 : #include "rtl/math.hxx"
26 : #include "rtl/ustring.h"
27 : #include "rtl/ustring.hxx"
28 :
29 : CPPUNIT_NS_BEGIN
30 :
31 : template<> struct assertion_traits<rtl_math_ConversionStatus>
32 : {
33 8 : static bool equal( const rtl_math_ConversionStatus& x, const rtl_math_ConversionStatus& y )
34 : {
35 8 : return x == y;
36 : }
37 :
38 0 : static std::string toString( const rtl_math_ConversionStatus& x )
39 : {
40 0 : OStringStream ost;
41 0 : ost << static_cast<unsigned int>(x);
42 0 : return ost.str();
43 : }
44 : };
45 :
46 : CPPUNIT_NS_END
47 :
48 : namespace {
49 :
50 18 : class Test: public CppUnit::TestFixture {
51 : public:
52 2 : void test_stringToDouble_good() {
53 : rtl_math_ConversionStatus status;
54 : sal_Int32 end;
55 : double res = rtl::math::stringToDouble(
56 : rtl::OUString(" +1.E01foo"),
57 2 : '.', ',', &status, &end);
58 2 : CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
59 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(RTL_CONSTASCII_LENGTH(" +1.E01")), end);
60 2 : CPPUNIT_ASSERT_EQUAL(10.0, res);
61 2 : }
62 :
63 2 : void test_stringToDouble_bad() {
64 : rtl_math_ConversionStatus status;
65 : sal_Int32 end;
66 : double res = rtl::math::stringToDouble(
67 : rtl::OUString(" +Efoo"),
68 2 : '.', ',', &status, &end);
69 2 : CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
70 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end);
71 2 : CPPUNIT_ASSERT_EQUAL(0.0, res);
72 2 : }
73 :
74 2 : void test_stringToDouble_exponent_without_digit() {
75 : rtl_math_ConversionStatus status;
76 : sal_Int32 end;
77 : double res = rtl::math::stringToDouble(
78 : rtl::OUString("1e"),
79 2 : '.', ',', &status, &end);
80 2 : CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
81 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(RTL_CONSTASCII_LENGTH("1")), end);
82 2 : CPPUNIT_ASSERT_EQUAL(1.0, res);
83 : res = rtl::math::stringToDouble(
84 : rtl::OUString("0e"),
85 2 : '.', ',', &status, &end);
86 2 : CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
87 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(RTL_CONSTASCII_LENGTH("1")), end);
88 2 : CPPUNIT_ASSERT_EQUAL(0.0, res);
89 2 : }
90 :
91 4 : CPPUNIT_TEST_SUITE(Test);
92 2 : CPPUNIT_TEST(test_stringToDouble_good);
93 2 : CPPUNIT_TEST(test_stringToDouble_bad);
94 2 : CPPUNIT_TEST(test_stringToDouble_exponent_without_digit);
95 4 : CPPUNIT_TEST_SUITE_END();
96 : };
97 :
98 2 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
99 :
100 : }
101 :
102 8 : CPPUNIT_PLUGIN_IMPLEMENT();
103 :
104 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|