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 
TEST(inttypes,misc)23 TEST(inttypes, misc) {
24   char buf[512];
25 
26   intptr_t i = 0;
27   uintptr_t u = 0;
28 
29   snprintf(buf, sizeof(buf), "%08" PRIdPTR, i);
30   snprintf(buf, sizeof(buf), "%08" PRIiPTR, i);
31   snprintf(buf, sizeof(buf), "%08" PRIoPTR, i);
32   snprintf(buf, sizeof(buf), "%08" PRIuPTR, u);
33   snprintf(buf, sizeof(buf), "%08" PRIxPTR, u);
34   snprintf(buf, sizeof(buf), "%08" PRIXPTR, u);
35 
36   sscanf(buf, "%08" SCNdPTR, &i);
37   sscanf(buf, "%08" SCNiPTR, &i);
38   sscanf(buf, "%08" SCNoPTR, &u);
39   sscanf(buf, "%08" SCNuPTR, &u);
40   sscanf(buf, "%08" SCNxPTR, &u);
41 }
42 
TEST(inttypes,wcstoimax)43 TEST(inttypes, wcstoimax) {
44   ASSERT_EQ(123, wcstoimax(L"123", NULL, 10));
45 }
46 
TEST(inttypes,wcstoumax)47 TEST(inttypes, wcstoumax) {
48   ASSERT_EQ(123U, wcstoumax(L"123", NULL, 10));
49 }
50 
TEST(inttypes,strtoimax_EINVAL)51 TEST(inttypes, strtoimax_EINVAL) {
52   errno = 0;
53   strtoimax("123", NULL, -1);
54   ASSERT_EQ(EINVAL, errno);
55   errno = 0;
56   strtoimax("123", NULL, 1);
57   ASSERT_EQ(EINVAL, errno);
58   errno = 0;
59   strtoimax("123", NULL, 37);
60   ASSERT_EQ(EINVAL, errno);
61 }
62 
TEST(inttypes,strtoumax_EINVAL)63 TEST(inttypes, strtoumax_EINVAL) {
64   errno = 0;
65   strtoumax("123", NULL, -1);
66   ASSERT_EQ(EINVAL, errno);
67   errno = 0;
68   strtoumax("123", NULL, 1);
69   ASSERT_EQ(EINVAL, errno);
70   errno = 0;
71   strtoumax("123", NULL, 37);
72   ASSERT_EQ(EINVAL, errno);
73 }
74 
TEST(inttypes,wcstoimax_EINVAL)75 TEST(inttypes, wcstoimax_EINVAL) {
76   errno = 0;
77   wcstoimax(L"123", NULL, -1);
78   ASSERT_EQ(EINVAL, errno);
79   errno = 0;
80   wcstoimax(L"123", NULL, 1);
81   ASSERT_EQ(EINVAL, errno);
82   errno = 0;
83   wcstoimax(L"123", NULL, 37);
84   ASSERT_EQ(EINVAL, errno);
85 }
86 
TEST(inttypes,wcstoumax_EINVAL)87 TEST(inttypes, wcstoumax_EINVAL) {
88   errno = 0;
89   wcstoumax(L"123", NULL, -1);
90   ASSERT_EQ(EINVAL, errno);
91   errno = 0;
92   wcstoumax(L"123", NULL, 1);
93   ASSERT_EQ(EINVAL, errno);
94   errno = 0;
95   wcstoumax(L"123", NULL, 37);
96   ASSERT_EQ(EINVAL, errno);
97 }
98