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 <cstddef>
21 : #include <stdio.h>
22 : #include <string.h>
23 :
24 : #include <sal/types.h>
25 : #include <cppunit/TestFixture.h>
26 : #include <cppunit/extensions/HelperMacros.h>
27 : #include <cppunit/plugin/TestPlugIn.h>
28 :
29 : namespace {
30 :
31 14 : template< typename T > void testPrintf(
32 : char const * result, char const * format, T argument)
33 : {
34 14 : std::size_t const bufsize = 1000;
35 : char buf[bufsize];
36 14 : int n = snprintf(buf, bufsize, format, argument);
37 14 : CPPUNIT_ASSERT(n >= 0 && sal::static_int_cast< unsigned int >(n) < bufsize);
38 14 : CPPUNIT_ASSERT(strcmp(buf, result) == 0);
39 14 : }
40 :
41 3 : class Test: public CppUnit::TestFixture {
42 : public:
43 : void test();
44 :
45 2 : CPPUNIT_TEST_SUITE(Test);
46 1 : CPPUNIT_TEST(test);
47 5 : CPPUNIT_TEST_SUITE_END();
48 : };
49 :
50 1 : void Test::test() {
51 1 : testPrintf("-2147483648", "%" SAL_PRIdINT32, SAL_MIN_INT32);
52 1 : testPrintf("4294967295", "%" SAL_PRIuUINT32, SAL_MAX_UINT32);
53 1 : testPrintf("ffffffff", "%" SAL_PRIxUINT32, SAL_MAX_UINT32);
54 1 : testPrintf("FFFFFFFF", "%" SAL_PRIXUINT32, SAL_MAX_UINT32);
55 1 : testPrintf("-9223372036854775808", "%" SAL_PRIdINT64, SAL_MIN_INT64);
56 1 : testPrintf("18446744073709551615", "%" SAL_PRIuUINT64, SAL_MAX_UINT64);
57 1 : testPrintf("ffffffffffffffff", "%" SAL_PRIxUINT64, SAL_MAX_UINT64);
58 1 : testPrintf("FFFFFFFFFFFFFFFF", "%" SAL_PRIXUINT64, SAL_MAX_UINT64);
59 1 : testPrintf("123", "%" SAL_PRI_SIZET "u", static_cast< std::size_t >(123));
60 : testPrintf(
61 1 : "-123", "%" SAL_PRI_PTRDIFFT "d", static_cast< std::ptrdiff_t >(-123));
62 1 : testPrintf("-123", "%" SAL_PRIdINTPTR, static_cast< sal_IntPtr >(-123));
63 1 : testPrintf("123", "%" SAL_PRIuUINTPTR, static_cast< sal_uIntPtr >(123));
64 1 : testPrintf("abc", "%" SAL_PRIxUINTPTR, static_cast< sal_uIntPtr >(0xabc));
65 1 : testPrintf("ABC", "%" SAL_PRIXUINTPTR, static_cast< sal_uIntPtr >(0xabc));
66 1 : }
67 :
68 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
69 :
70 : }
71 :
72 4 : CPPUNIT_PLUGIN_IMPLEMENT();
73 :
74 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|