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/types.h>
11 : #include <cppunit/TestFixture.h>
12 : #include <cppunit/extensions/HelperMacros.h>
13 : #include "rtl/ustring.hxx"
14 : #include <iostream>
15 :
16 : namespace test { namespace strings {
17 :
18 36 : class valueX : public CppUnit::TestFixture {
19 : public:
20 : void testOBoolean();
21 : void testOUBoolean();
22 : void testOUInt();
23 : void testOInt();
24 : void testOUFloat();
25 : void testOFloat();
26 :
27 4 : CPPUNIT_TEST_SUITE(valueX);
28 2 : CPPUNIT_TEST(testOBoolean);
29 2 : CPPUNIT_TEST(testOUBoolean);
30 2 : CPPUNIT_TEST(testOUInt);
31 2 : CPPUNIT_TEST(testOInt);
32 2 : CPPUNIT_TEST(testOUFloat);
33 2 : CPPUNIT_TEST(testOFloat);
34 4 : CPPUNIT_TEST_SUITE_END();
35 : };
36 :
37 : } }
38 :
39 2 : CPPUNIT_TEST_SUITE_REGISTRATION(test::strings::valueX);
40 :
41 : namespace {
42 :
43 : template< typename T >
44 4 : void testBoolean() {
45 4 : CPPUNIT_ASSERT_EQUAL( T( "false" ), T::boolean( false ) );
46 4 : CPPUNIT_ASSERT_EQUAL( T( "false" ), T::boolean( sal_False ) );
47 4 : CPPUNIT_ASSERT_EQUAL( T( "true" ), T::boolean( true ) );
48 4 : CPPUNIT_ASSERT_EQUAL( T( "true" ), T::boolean( sal_True ) );
49 4 : }
50 :
51 : }
52 :
53 2 : void test::strings::valueX::testOBoolean() {
54 2 : testBoolean<rtl::OString>();
55 2 : }
56 :
57 2 : void test::strings::valueX::testOUBoolean() {
58 2 : testBoolean<rtl::OUString>();
59 2 : }
60 :
61 : template< typename T >
62 4 : void testInt() {
63 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( 30039062 ));
64 :
65 : // test the overloading resolution
66 :
67 4 : CPPUNIT_ASSERT_EQUAL( T( "30" ), T::number( static_cast< signed char >( 30 )));
68 4 : CPPUNIT_ASSERT_EQUAL( T( "30" ), T::number( static_cast< unsigned char >( 30 )));
69 4 : CPPUNIT_ASSERT_EQUAL( T( "30039" ), T::number( static_cast< short >( 30039 )));
70 4 : CPPUNIT_ASSERT_EQUAL( T( "30039" ), T::number( static_cast< unsigned short >( 30039 )));
71 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( static_cast< int >( 30039062 )));
72 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( static_cast< unsigned int >( 30039062 )));
73 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( static_cast< long >( 30039062 )));
74 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( static_cast< unsigned long >( 30039062 )));
75 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( static_cast< long long >( 30039062 )));
76 : // The highest bit set in unsigned long long may not actually work.
77 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( static_cast< unsigned long long >( 30039062 )));
78 :
79 4 : CPPUNIT_ASSERT_EQUAL( T( "30" ), T::number( static_cast< sal_Int8 >( 30 )));
80 4 : CPPUNIT_ASSERT_EQUAL( T( "30" ), T::number( static_cast< sal_uInt8 >( 30 )));
81 4 : CPPUNIT_ASSERT_EQUAL( T( "30039" ), T::number( static_cast< sal_Int16 >( 30039 )));
82 4 : CPPUNIT_ASSERT_EQUAL( T( "30039" ), T::number( static_cast< sal_uInt16 >( 30039 )));
83 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( static_cast< sal_Int32 >( 30039062 )));
84 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( static_cast< sal_uInt32 >( 30039062 )));
85 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( static_cast< sal_Int64 >( 30039062 )));
86 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062" ), T::number( static_cast< sal_uInt64 >( 30039062 )));
87 :
88 : // The implementation internally uses sal_Int64 etc. types, so check ranges.
89 : assert( sizeof( int ) <= sizeof( sal_Int32 ));
90 : assert( sizeof( long ) <= sizeof( sal_Int64 ));
91 : assert( sizeof( long long ) <= sizeof( sal_Int64 ));
92 : assert( sizeof( unsigned int ) < sizeof( sal_Int64 ));
93 4 : }
94 :
95 2 : void test::strings::valueX::testOUInt() {
96 2 : testInt<rtl::OUString>();
97 2 : }
98 :
99 2 : void test::strings::valueX::testOInt() {
100 2 : testInt<rtl::OString>();
101 2 : }
102 :
103 : template< typename T >
104 4 : void testFloat() {
105 4 : CPPUNIT_ASSERT_EQUAL( T( "39062.2" ), T::number( 39062.2f ));
106 4 : CPPUNIT_ASSERT_EQUAL( T( "30039062.2" ), T::number( 30039062.2 ));
107 : // long double not supported
108 4 : }
109 :
110 2 : void test::strings::valueX::testOUFloat() {
111 2 : testFloat<rtl::OUString>();
112 2 : }
113 :
114 2 : void test::strings::valueX::testOFloat() {
115 2 : testFloat<rtl::OString>();
116 8 : }
117 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|