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 "sal/types.h"
32
33 #include "gtest/gtest.h"
34
35 namespace {
36
testPrintf(char const * result,char const * format,T argument)37 template< typename T > void testPrintf(
38 char const * result, char const * format, T argument)
39 {
40 std::size_t const bufsize = 1000;
41 char buf[bufsize];
42 int n = snprintf(buf, bufsize, format, argument);
43 ASSERT_TRUE(n >= 0 && sal::static_int_cast< unsigned int >(n) < bufsize);
44 ASSERT_TRUE(strcmp(buf, result) == 0);
45 }
46
TEST(Sal_Test,Types_Test)47 TEST(Sal_Test, Types_Test) {
48 testPrintf("-2147483648", "%" SAL_PRIdINT32, SAL_MIN_INT32);
49 testPrintf("4294967295", "%" SAL_PRIuUINT32, SAL_MAX_UINT32);
50 testPrintf("ffffffff", "%" SAL_PRIxUINT32, SAL_MAX_UINT32);
51 testPrintf("FFFFFFFF", "%" SAL_PRIXUINT32, SAL_MAX_UINT32);
52 testPrintf("-9223372036854775808", "%" SAL_PRIdINT64, SAL_MIN_INT64);
53 testPrintf("18446744073709551615", "%" SAL_PRIuUINT64, SAL_MAX_UINT64);
54 testPrintf("ffffffffffffffff", "%" SAL_PRIxUINT64, SAL_MAX_UINT64);
55 testPrintf("FFFFFFFFFFFFFFFF", "%" SAL_PRIXUINT64, SAL_MAX_UINT64);
56 testPrintf("123", "%" SAL_PRI_SIZET "u", static_cast< std::size_t >(123));
57 testPrintf(
58 "-123", "%" SAL_PRI_PTRDIFFT "d", static_cast< std::ptrdiff_t >(-123));
59 testPrintf("-123", "%" SAL_PRIdINTPTR, static_cast< sal_IntPtr >(-123));
60 testPrintf("123", "%" SAL_PRIuUINTPTR, static_cast< sal_uIntPtr >(123));
61 testPrintf("abc", "%" SAL_PRIxUINTPTR, static_cast< sal_uIntPtr >(0xabc));
62 testPrintf("ABC", "%" SAL_PRIXUINTPTR, static_cast< sal_uIntPtr >(0xabc));
63 }
64
65 }
66
main(int argc,char ** argv)67 int main(int argc, char **argv)
68 {
69 ::testing::InitGoogleTest(&argc, argv);
70 return RUN_ALL_TESTS();
71 }
72