2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <gtest/gtest.h>
23 #define PRINTF_TYPED(FMT_SUFFIX, TYPE_SUFFIX) \
26 memset(buf, 0, sizeof(buf)); \
27 snprintf(buf, sizeof(buf), "%" PRId##FMT_SUFFIX, int##TYPE_SUFFIX(123)); \
28 EXPECT_STREQ("123", buf); \
29 memset(buf, 0, sizeof(buf)); \
30 snprintf(buf, sizeof(buf), "%" PRIi##FMT_SUFFIX, int##TYPE_SUFFIX(123)); \
31 EXPECT_STREQ("123", buf); \
32 memset(buf, 0, sizeof(buf)); \
33 snprintf(buf, sizeof(buf), "%" PRIo##FMT_SUFFIX, int##TYPE_SUFFIX(123)); \
34 EXPECT_STREQ("173", buf); \
35 memset(buf, 0, sizeof(buf)); \
36 snprintf(buf, sizeof(buf), "%" PRIu##FMT_SUFFIX, uint##TYPE_SUFFIX(123)); \
37 EXPECT_STREQ("123", buf); \
38 memset(buf, 0, sizeof(buf)); \
39 snprintf(buf, sizeof(buf), "%" PRIx##FMT_SUFFIX, uint##TYPE_SUFFIX(123)); \
40 EXPECT_STREQ("7b", buf); \
41 memset(buf, 0, sizeof(buf)); \
42 snprintf(buf, sizeof(buf), "%" PRIX##FMT_SUFFIX, uint##TYPE_SUFFIX(123)); \
43 EXPECT_STREQ("7B", buf); \
46 #define PRINTF_SIZED(WIDTH) \
47 PRINTF_TYPED(WIDTH, WIDTH##_t); \
48 PRINTF_TYPED(FAST##WIDTH, _fast##WIDTH##_t); \
49 PRINTF_TYPED(LEAST##WIDTH, _least##WIDTH##_t) \
52 #define SCANF_TYPED(FMT_SUFFIX, TYPE_SUFFIX) \
54 int##TYPE_SUFFIX dst_int##TYPE_SUFFIX = 0; \
55 uint##TYPE_SUFFIX dst_uint##TYPE_SUFFIX = 0u; \
57 sscanf("123", "%" SCNd##FMT_SUFFIX, &dst_int##TYPE_SUFFIX); \
58 EXPECT_EQ(123, dst_int##TYPE_SUFFIX); \
59 dst_int##TYPE_SUFFIX = 0; \
60 sscanf("123", "%" SCNi##FMT_SUFFIX, &dst_int##TYPE_SUFFIX); \
61 EXPECT_EQ(123, dst_int##TYPE_SUFFIX); \
62 dst_int##TYPE_SUFFIX = 0; \
63 sscanf("173", "%" SCNo##FMT_SUFFIX, &dst_int##TYPE_SUFFIX); \
64 EXPECT_EQ(123, dst_int##TYPE_SUFFIX); \
65 dst_int##TYPE_SUFFIX = 0; \
66 sscanf("123", "%" SCNu##FMT_SUFFIX, &dst_uint##TYPE_SUFFIX); \
67 EXPECT_EQ(123u, dst_uint##TYPE_SUFFIX); \
68 dst_uint##TYPE_SUFFIX = 0; \
69 sscanf("7B", "%" SCNx##FMT_SUFFIX, &dst_uint##TYPE_SUFFIX); \
70 EXPECT_EQ(123u, dst_uint##TYPE_SUFFIX); \
71 dst_uint##TYPE_SUFFIX = 0; \
74 #define SCANF_SIZED(SIZE) \
75 SCANF_TYPED(SIZE, SIZE##_t); \
76 SCANF_TYPED(FAST##SIZE, _fast##SIZE##_t); \
77 SCANF_TYPED(LEAST##SIZE, _least##SIZE##_t) \
80 TEST(inttypes, printf_macros) {
86 PRINTF_TYPED(MAX, max_t);
87 PRINTF_TYPED(PTR, ptr_t);
90 TEST(inttypes, scanf_macros) {
96 SCANF_TYPED(MAX, max_t);
97 SCANF_TYPED(PTR, ptr_t);
100 TEST(inttypes, wcstoimax) {
101 wchar_t* end = nullptr;
102 EXPECT_EQ(123, wcstoimax(L" +123x", &end, 10));
103 EXPECT_EQ(L'x', *end);
106 TEST(inttypes, wcstoumax) {
107 wchar_t* end = nullptr;
108 EXPECT_EQ(123U, wcstoumax(L" +123x", &end, 10));
109 EXPECT_EQ(L'x', *end);
112 TEST(inttypes, strtoimax_EINVAL) {
114 strtoimax("123", NULL, -1);
115 ASSERT_EQ(EINVAL, errno);
117 strtoimax("123", NULL, 1);
118 ASSERT_EQ(EINVAL, errno);
120 strtoimax("123", NULL, 37);
121 ASSERT_EQ(EINVAL, errno);
124 TEST(inttypes, strtoumax_EINVAL) {
126 strtoumax("123", NULL, -1);
127 ASSERT_EQ(EINVAL, errno);
129 strtoumax("123", NULL, 1);
130 ASSERT_EQ(EINVAL, errno);
132 strtoumax("123", NULL, 37);
133 ASSERT_EQ(EINVAL, errno);
136 TEST(inttypes, wcstoimax_EINVAL) {
138 wcstoimax(L"123", NULL, -1);
139 ASSERT_EQ(EINVAL, errno);
141 wcstoimax(L"123", NULL, 1);
142 ASSERT_EQ(EINVAL, errno);
144 wcstoimax(L"123", NULL, 37);
145 ASSERT_EQ(EINVAL, errno);
148 TEST(inttypes, wcstoumax_EINVAL) {
150 wcstoumax(L"123", NULL, -1);
151 ASSERT_EQ(EINVAL, errno);
153 wcstoumax(L"123", NULL, 1);
154 ASSERT_EQ(EINVAL, errno);
156 wcstoumax(L"123", NULL, 37);
157 ASSERT_EQ(EINVAL, errno);