1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <errno.h>
17570af302Sopenharmony_ci#include <stdio.h>
18570af302Sopenharmony_ci#include <string.h>
19570af302Sopenharmony_ci#include <wchar.h>
20570af302Sopenharmony_ci#include "test.h"
21570af302Sopenharmony_ci
22570af302Sopenharmony_ci#define TEST(r, f, x, m) (errno = 0, msg = #f, ((r) = (f)) == (x) || (t_error("%s failed (" m ")\n", #f, r, x), 0))
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci#define TEST2(r, f, x, m) (((r) = (f)) == (x) || (t_error("%s failed (" m ")\n", msg, r, x), 0))
25570af302Sopenharmony_ci
26570af302Sopenharmony_ci/**
27570af302Sopenharmony_ci * @tc.name      : wcstoull_0100
28570af302Sopenharmony_ci * @tc.desc      : Convert wide string to unsigned long long type based on decimal
29570af302Sopenharmony_ci * @tc.level     : Level 0
30570af302Sopenharmony_ci */
31570af302Sopenharmony_civoid wcstoull_0100(void)
32570af302Sopenharmony_ci{
33570af302Sopenharmony_ci    unsigned long long ull;
34570af302Sopenharmony_ci    char *msg = "";
35570af302Sopenharmony_ci    wchar_t *s, *c;
36570af302Sopenharmony_ci    TEST(ull, wcstoull(s = L"250068492", &c, 10), 250068492ULL, "expect unsigned %llu != %llu");
37570af302Sopenharmony_ci}
38570af302Sopenharmony_ci
39570af302Sopenharmony_ci/**
40570af302Sopenharmony_ci * @tc.name      : wcstoull_0200
41570af302Sopenharmony_ci * @tc.desc      : Convert wide string to unsigned long long type based on hexadecimal
42570af302Sopenharmony_ci * @tc.level     : Level 1
43570af302Sopenharmony_ci */
44570af302Sopenharmony_civoid wcstoull_0200(void)
45570af302Sopenharmony_ci{
46570af302Sopenharmony_ci    unsigned long long ull;
47570af302Sopenharmony_ci    char *msg = "";
48570af302Sopenharmony_ci    wchar_t *s, *c;
49570af302Sopenharmony_ci    TEST(ull, wcstoull(s = L"7b06af00", &c, 16), 2064035584ULL, "expect unsigned %llu != %llu");
50570af302Sopenharmony_ci}
51570af302Sopenharmony_ci
52570af302Sopenharmony_ci/**
53570af302Sopenharmony_ci * @tc.name      : wcstoull_0300
54570af302Sopenharmony_ci * @tc.desc      : Convert wide string to unsigned long long type based on binary
55570af302Sopenharmony_ci * @tc.level     : Level 1
56570af302Sopenharmony_ci */
57570af302Sopenharmony_civoid wcstoull_0300(void)
58570af302Sopenharmony_ci{
59570af302Sopenharmony_ci    unsigned long long ull;
60570af302Sopenharmony_ci    char *msg = "";
61570af302Sopenharmony_ci    wchar_t *s, *c;
62570af302Sopenharmony_ci    TEST(ull, wcstoull(s = L"1100011011110101010001100000", &c, 2), 208622688ULL, "expect unsigned %llu != %llu");
63570af302Sopenharmony_ci}
64570af302Sopenharmony_ci
65570af302Sopenharmony_ci/**
66570af302Sopenharmony_ci * @tc.name      : wcstoull_0400
67570af302Sopenharmony_ci * @tc.desc      : Convert wide string to unsigned long long type based on parameter 'base' equal to 0
68570af302Sopenharmony_ci * @tc.level     : Level 1
69570af302Sopenharmony_ci */
70570af302Sopenharmony_civoid wcstoull_0400(void)
71570af302Sopenharmony_ci{
72570af302Sopenharmony_ci    unsigned long long ull;
73570af302Sopenharmony_ci    char *msg = "";
74570af302Sopenharmony_ci    wchar_t *s, *c;
75570af302Sopenharmony_ci    TEST(ull, wcstoull(s = L"0x6fffff", &c, 0), 7340031ULL, "expect unsigned %llu != %llu");
76570af302Sopenharmony_ci}
77570af302Sopenharmony_ci
78570af302Sopenharmony_ci/**
79570af302Sopenharmony_ci * @tc.name      : wcstoull_0500
80570af302Sopenharmony_ci * @tc.desc      : Convert wide string to unsigned long long type exceeds the maximum value of the type
81570af302Sopenharmony_ci * @tc.level     : Level 2
82570af302Sopenharmony_ci */
83570af302Sopenharmony_civoid wcstoull_0500(void)
84570af302Sopenharmony_ci{
85570af302Sopenharmony_ci    int i;
86570af302Sopenharmony_ci    unsigned long long ull;
87570af302Sopenharmony_ci    char *msg = "";
88570af302Sopenharmony_ci    wchar_t *s, *c;
89570af302Sopenharmony_ci    TEST(ull, wcstoull(s = L"18446744073709551616", &c, 0), 18446744073709551615ULL, "uncaught overflow %llu != %llu");
90570af302Sopenharmony_ci    TEST2(i, c - s, 20, "wrong final position %d != %d");
91570af302Sopenharmony_ci    TEST2(i, errno, ERANGE, "missing errno %d != %d");
92570af302Sopenharmony_ci}
93570af302Sopenharmony_ci
94570af302Sopenharmony_ci/**
95570af302Sopenharmony_ci * @tc.name      : wcstoull_0600
96570af302Sopenharmony_ci * @tc.desc      : The converted result value -1 is invalid
97570af302Sopenharmony_ci * @tc.level     : Level 2
98570af302Sopenharmony_ci */
99570af302Sopenharmony_civoid wcstoull_0600(void)
100570af302Sopenharmony_ci{
101570af302Sopenharmony_ci    int i;
102570af302Sopenharmony_ci    unsigned long long ull;
103570af302Sopenharmony_ci    char *msg = "";
104570af302Sopenharmony_ci    wchar_t *s, *c;
105570af302Sopenharmony_ci    TEST(ull, wcstoull(s = L"-1", &c, 0), -1ULL, "rejected negative %llu != %llu");
106570af302Sopenharmony_ci    TEST2(i, c - s, 2, "wrong final position %d != %d");
107570af302Sopenharmony_ci    TEST2(i, errno, 0, "spurious errno %d != %d");
108570af302Sopenharmony_ci}
109570af302Sopenharmony_ci
110570af302Sopenharmony_ci/**
111570af302Sopenharmony_ci * @tc.name      : wcstoull_0700
112570af302Sopenharmony_ci * @tc.desc      : The converted result value -2 is invalid
113570af302Sopenharmony_ci * @tc.level     : Level 2
114570af302Sopenharmony_ci */
115570af302Sopenharmony_civoid wcstoull_0700(void)
116570af302Sopenharmony_ci{
117570af302Sopenharmony_ci    int i;
118570af302Sopenharmony_ci    unsigned long long ull;
119570af302Sopenharmony_ci    char *msg = "";
120570af302Sopenharmony_ci    wchar_t *s, *c;
121570af302Sopenharmony_ci    TEST(ull, wcstoull(s = L"-2", &c, 0), -2ULL, "rejected negative %llu != %llu");
122570af302Sopenharmony_ci    TEST2(i, c - s, 2, "wrong final position %d != %d");
123570af302Sopenharmony_ci    TEST2(i, errno, 0, "spurious errno %d != %d");
124570af302Sopenharmony_ci}
125570af302Sopenharmony_ci
126570af302Sopenharmony_ci/**
127570af302Sopenharmony_ci * @tc.name      : wcstoull_0800
128570af302Sopenharmony_ci * @tc.desc      : The converted result value -9223372036854775808 is invalid
129570af302Sopenharmony_ci * @tc.level     : Level 2
130570af302Sopenharmony_ci */
131570af302Sopenharmony_civoid wcstoull_0800(void)
132570af302Sopenharmony_ci{
133570af302Sopenharmony_ci    int i;
134570af302Sopenharmony_ci    unsigned long long ull;
135570af302Sopenharmony_ci    char *msg = "";
136570af302Sopenharmony_ci    wchar_t *s, *c;
137570af302Sopenharmony_ci    TEST(ull, wcstoull(s = L"-9223372036854775808", &c, 0), -9223372036854775808ULL, "rejected negative %llu != %llu");
138570af302Sopenharmony_ci    TEST2(i, c - s, 20, "wrong final position %d != %d");
139570af302Sopenharmony_ci    TEST2(i, errno, 0, "spurious errno %d != %d");
140570af302Sopenharmony_ci}
141570af302Sopenharmony_ci
142570af302Sopenharmony_ciint main(int argc, char *argv[])
143570af302Sopenharmony_ci{
144570af302Sopenharmony_ci    wcstoull_0100();
145570af302Sopenharmony_ci    wcstoull_0200();
146570af302Sopenharmony_ci    wcstoull_0300();
147570af302Sopenharmony_ci    wcstoull_0400();
148570af302Sopenharmony_ci    wcstoull_0500();
149570af302Sopenharmony_ci    wcstoull_0600();
150570af302Sopenharmony_ci    wcstoull_0700();
151570af302Sopenharmony_ci    wcstoull_0800();
152570af302Sopenharmony_ci    return t_status;
153570af302Sopenharmony_ci}