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 4 : static bool equal( const rtl_math_ConversionStatus& x, const rtl_math_ConversionStatus& y )
34 : {
35 4 : 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 9 : class Test: public CppUnit::TestFixture {
51 : public:
52 1 : 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 1 : '.', ',', &status, &end);
58 1 : CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
59 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(RTL_CONSTASCII_LENGTH(" +1.E01")), end);
60 1 : CPPUNIT_ASSERT_EQUAL(10.0, res);
61 1 : }
62 :
63 1 : void test_stringToDouble_bad() {
64 : rtl_math_ConversionStatus status;
65 : sal_Int32 end;
66 : double res = rtl::math::stringToDouble(
67 : rtl::OUString(" +Efoo"),
68 1 : '.', ',', &status, &end);
69 1 : CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
70 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), end);
71 1 : CPPUNIT_ASSERT_EQUAL(0.0, res);
72 1 : }
73 :
74 1 : 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 1 : '.', ',', &status, &end);
80 1 : CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
81 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(RTL_CONSTASCII_LENGTH("1")), end);
82 1 : CPPUNIT_ASSERT_EQUAL(1.0, res);
83 : res = rtl::math::stringToDouble(
84 : rtl::OUString("0e"),
85 1 : '.', ',', &status, &end);
86 1 : CPPUNIT_ASSERT_EQUAL(rtl_math_ConversionStatus_Ok, status);
87 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(RTL_CONSTASCII_LENGTH("1")), end);
88 1 : CPPUNIT_ASSERT_EQUAL(0.0, res);
89 1 : }
90 :
91 2 : CPPUNIT_TEST_SUITE(Test);
92 1 : CPPUNIT_TEST(test_stringToDouble_good);
93 1 : CPPUNIT_TEST(test_stringToDouble_bad);
94 1 : CPPUNIT_TEST(test_stringToDouble_exponent_without_digit);
95 5 : CPPUNIT_TEST_SUITE_END();
96 : };
97 :
98 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
99 :
100 : }
101 :
102 4 : CPPUNIT_PLUGIN_IMPLEMENT();
103 :
104 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|