1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #include "precompiled_sal.hxx"
25 #include "sal/config.h"
26
27 #include <cstddef>
28 #include <stdio.h> // C99 snprintf not necessarily in <cstdio>
29 #include <string.h> // wntmsci10 does not know <cstring> std::strcmp
30
31 #include "testshl/simpleheader.hxx"
32 #include "sal/types.h"
33
34 namespace {
35
testPrintf(char const * result,char const * format,T argument)36 template< typename T > void testPrintf(
37 char const * result, char const * format, T argument)
38 {
39 std::size_t const bufsize = 1000;
40 char buf[bufsize];
41 int n = snprintf(buf, bufsize, format, argument);
42 CPPUNIT_ASSERT(n >= 0 && sal::static_int_cast< unsigned int >(n) < bufsize);
43 CPPUNIT_ASSERT(strcmp(buf, result) == 0);
44 }
45
46 class Test: public CppUnit::TestFixture {
47 public:
48 void test();
49
50 CPPUNIT_TEST_SUITE(Test);
51 CPPUNIT_TEST(test);
52 CPPUNIT_TEST_SUITE_END();
53 };
54
test()55 void Test::test() {
56 testPrintf("-2147483648", "%" SAL_PRIdINT32, SAL_MIN_INT32);
57 testPrintf("4294967295", "%" SAL_PRIuUINT32, SAL_MAX_UINT32);
58 testPrintf("ffffffff", "%" SAL_PRIxUINT32, SAL_MAX_UINT32);
59 testPrintf("FFFFFFFF", "%" SAL_PRIXUINT32, SAL_MAX_UINT32);
60 testPrintf("-9223372036854775808", "%" SAL_PRIdINT64, SAL_MIN_INT64);
61 testPrintf("18446744073709551615", "%" SAL_PRIuUINT64, SAL_MAX_UINT64);
62 testPrintf("ffffffffffffffff", "%" SAL_PRIxUINT64, SAL_MAX_UINT64);
63 testPrintf("FFFFFFFFFFFFFFFF", "%" SAL_PRIXUINT64, SAL_MAX_UINT64);
64 testPrintf("123", "%" SAL_PRI_SIZET "u", static_cast< std::size_t >(123));
65 testPrintf(
66 "-123", "%" SAL_PRI_PTRDIFFT "d", static_cast< std::ptrdiff_t >(-123));
67 testPrintf("-123", "%" SAL_PRIdINTPTR, static_cast< sal_IntPtr >(-123));
68 testPrintf("123", "%" SAL_PRIuUINTPTR, static_cast< sal_uIntPtr >(123));
69 testPrintf("abc", "%" SAL_PRIxUINTPTR, static_cast< sal_uIntPtr >(0xabc));
70 testPrintf("ABC", "%" SAL_PRIXUINTPTR, static_cast< sal_uIntPtr >(0xabc));
71 }
72
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "alltests");
74
75 }
76
77 NOADDITIONAL;
78