OSDN Git Service

Merge "Disable compat_elf_hash_and_relocation_tables test for x86" am: fe96db819b...
[android-x86/bionic.git] / tests / inttypes_test.cpp
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <inttypes.h>
18
19 #include <errno.h>
20 #include <gtest/gtest.h>
21 #include <stdio.h>
22
23 #define PRINTF_TYPED(FMT_SUFFIX, TYPE_SUFFIX) \
24   do { \
25     char buf[512]; \
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); \
44   } while (false) \
45
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) \
50
51
52 #define SCANF_TYPED(FMT_SUFFIX, TYPE_SUFFIX) \
53   do { \
54     int##TYPE_SUFFIX dst_int##TYPE_SUFFIX = 0; \
55     uint##TYPE_SUFFIX dst_uint##TYPE_SUFFIX = 0u; \
56     \
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; \
72   } while (false) \
73
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) \
78
79
80 TEST(inttypes, printf_macros) {
81   PRINTF_SIZED(8);
82   PRINTF_SIZED(16);
83   PRINTF_SIZED(32);
84   PRINTF_SIZED(64);
85
86   PRINTF_TYPED(MAX, max_t);
87   PRINTF_TYPED(PTR, ptr_t);
88 }
89
90 TEST(inttypes, scanf_macros) {
91   SCANF_SIZED(8);
92   SCANF_SIZED(16);
93   SCANF_SIZED(32);
94   SCANF_SIZED(64);
95
96   SCANF_TYPED(MAX, max_t);
97   SCANF_TYPED(PTR, ptr_t);
98 }
99
100 TEST(inttypes, wcstoimax) {
101   wchar_t* end = nullptr;
102   EXPECT_EQ(123, wcstoimax(L"  +123x", &end, 10));
103   EXPECT_EQ(L'x', *end);
104 }
105
106 TEST(inttypes, wcstoumax) {
107   wchar_t* end = nullptr;
108   EXPECT_EQ(123U, wcstoumax(L"  +123x", &end, 10));
109   EXPECT_EQ(L'x', *end);
110 }
111
112 TEST(inttypes, strtoimax_EINVAL) {
113   errno = 0;
114   strtoimax("123", NULL, -1);
115   ASSERT_EQ(EINVAL, errno);
116   errno = 0;
117   strtoimax("123", NULL, 1);
118   ASSERT_EQ(EINVAL, errno);
119   errno = 0;
120   strtoimax("123", NULL, 37);
121   ASSERT_EQ(EINVAL, errno);
122 }
123
124 TEST(inttypes, strtoumax_EINVAL) {
125   errno = 0;
126   strtoumax("123", NULL, -1);
127   ASSERT_EQ(EINVAL, errno);
128   errno = 0;
129   strtoumax("123", NULL, 1);
130   ASSERT_EQ(EINVAL, errno);
131   errno = 0;
132   strtoumax("123", NULL, 37);
133   ASSERT_EQ(EINVAL, errno);
134 }
135
136 TEST(inttypes, wcstoimax_EINVAL) {
137   errno = 0;
138   wcstoimax(L"123", NULL, -1);
139   ASSERT_EQ(EINVAL, errno);
140   errno = 0;
141   wcstoimax(L"123", NULL, 1);
142   ASSERT_EQ(EINVAL, errno);
143   errno = 0;
144   wcstoimax(L"123", NULL, 37);
145   ASSERT_EQ(EINVAL, errno);
146 }
147
148 TEST(inttypes, wcstoumax_EINVAL) {
149   errno = 0;
150   wcstoumax(L"123", NULL, -1);
151   ASSERT_EQ(EINVAL, errno);
152   errno = 0;
153   wcstoumax(L"123", NULL, 1);
154   ASSERT_EQ(EINVAL, errno);
155   errno = 0;
156   wcstoumax(L"123", NULL, 37);
157   ASSERT_EQ(EINVAL, errno);
158 }