• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <errno.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <wchar.h>
20 #include "test.h"
21 
22 #define TEST(r, f, x, m) (errno = 0, msg = #f, ((r) = (f)) == (x) || (t_error("%s failed (" m ")\n", #f, r, x), 0))
23 
24 #define TEST2(r, f, x, m) (((r) = (f)) == (x) || (t_error("%s failed (" m ")\n", msg, r, x), 0))
25 
26 /**
27  * @tc.name      : wcstoull_0100
28  * @tc.desc      : Convert wide string to unsigned long long type based on decimal
29  * @tc.level     : Level 0
30  */
wcstoull_0100(void)31 void wcstoull_0100(void)
32 {
33     unsigned long long ull;
34     char *msg = "";
35     wchar_t *s, *c;
36     TEST(ull, wcstoull(s = L"250068492", &c, 10), 250068492ULL, "expect unsigned %llu != %llu");
37 }
38 
39 /**
40  * @tc.name      : wcstoull_0200
41  * @tc.desc      : Convert wide string to unsigned long long type based on hexadecimal
42  * @tc.level     : Level 1
43  */
wcstoull_0200(void)44 void wcstoull_0200(void)
45 {
46     unsigned long long ull;
47     char *msg = "";
48     wchar_t *s, *c;
49     TEST(ull, wcstoull(s = L"7b06af00", &c, 16), 2064035584ULL, "expect unsigned %llu != %llu");
50 }
51 
52 /**
53  * @tc.name      : wcstoull_0300
54  * @tc.desc      : Convert wide string to unsigned long long type based on binary
55  * @tc.level     : Level 1
56  */
wcstoull_0300(void)57 void wcstoull_0300(void)
58 {
59     unsigned long long ull;
60     char *msg = "";
61     wchar_t *s, *c;
62     TEST(ull, wcstoull(s = L"1100011011110101010001100000", &c, 2), 208622688ULL, "expect unsigned %llu != %llu");
63 }
64 
65 /**
66  * @tc.name      : wcstoull_0400
67  * @tc.desc      : Convert wide string to unsigned long long type based on parameter 'base' equal to 0
68  * @tc.level     : Level 1
69  */
wcstoull_0400(void)70 void wcstoull_0400(void)
71 {
72     unsigned long long ull;
73     char *msg = "";
74     wchar_t *s, *c;
75     TEST(ull, wcstoull(s = L"0x6fffff", &c, 0), 7340031ULL, "expect unsigned %llu != %llu");
76 }
77 
78 /**
79  * @tc.name      : wcstoull_0500
80  * @tc.desc      : Convert wide string to unsigned long long type exceeds the maximum value of the type
81  * @tc.level     : Level 2
82  */
wcstoull_0500(void)83 void wcstoull_0500(void)
84 {
85     int i;
86     unsigned long long ull;
87     char *msg = "";
88     wchar_t *s, *c;
89     TEST(ull, wcstoull(s = L"18446744073709551616", &c, 0), 18446744073709551615ULL, "uncaught overflow %llu != %llu");
90     TEST2(i, c - s, 20, "wrong final position %d != %d");
91     TEST2(i, errno, ERANGE, "missing errno %d != %d");
92 }
93 
94 /**
95  * @tc.name      : wcstoull_0600
96  * @tc.desc      : The converted result value -1 is invalid
97  * @tc.level     : Level 2
98  */
wcstoull_0600(void)99 void wcstoull_0600(void)
100 {
101     int i;
102     unsigned long long ull;
103     char *msg = "";
104     wchar_t *s, *c;
105     TEST(ull, wcstoull(s = L"-1", &c, 0), -1ULL, "rejected negative %llu != %llu");
106     TEST2(i, c - s, 2, "wrong final position %d != %d");
107     TEST2(i, errno, 0, "spurious errno %d != %d");
108 }
109 
110 /**
111  * @tc.name      : wcstoull_0700
112  * @tc.desc      : The converted result value -2 is invalid
113  * @tc.level     : Level 2
114  */
wcstoull_0700(void)115 void wcstoull_0700(void)
116 {
117     int i;
118     unsigned long long ull;
119     char *msg = "";
120     wchar_t *s, *c;
121     TEST(ull, wcstoull(s = L"-2", &c, 0), -2ULL, "rejected negative %llu != %llu");
122     TEST2(i, c - s, 2, "wrong final position %d != %d");
123     TEST2(i, errno, 0, "spurious errno %d != %d");
124 }
125 
126 /**
127  * @tc.name      : wcstoull_0800
128  * @tc.desc      : The converted result value -9223372036854775808 is invalid
129  * @tc.level     : Level 2
130  */
wcstoull_0800(void)131 void wcstoull_0800(void)
132 {
133     int i;
134     unsigned long long ull;
135     char *msg = "";
136     wchar_t *s, *c;
137     TEST(ull, wcstoull(s = L"-9223372036854775808", &c, 0), -9223372036854775808ULL, "rejected negative %llu != %llu");
138     TEST2(i, c - s, 20, "wrong final position %d != %d");
139     TEST2(i, errno, 0, "spurious errno %d != %d");
140 }
141 
main(int argc,char * argv[])142 int main(int argc, char *argv[])
143 {
144     wcstoull_0100();
145     wcstoull_0200();
146     wcstoull_0300();
147     wcstoull_0400();
148     wcstoull_0500();
149     wcstoull_0600();
150     wcstoull_0700();
151     wcstoull_0800();
152     return t_status;
153 }