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      : wcstoll_0100
28570af302Sopenharmony_ci * @tc.desc      : Convert wide string "2147483647" to long long type
29570af302Sopenharmony_ci * @tc.level     : Level 0
30570af302Sopenharmony_ci */
31570af302Sopenharmony_civoid wcstoll_0100(void)
32570af302Sopenharmony_ci{
33570af302Sopenharmony_ci    long long ll;
34570af302Sopenharmony_ci    char *msg = "";
35570af302Sopenharmony_ci    TEST(ll, wcstoll(L"2147483647", 0, 0), 2147483647LL, "expect signed %lld != %lld");
36570af302Sopenharmony_ci}
37570af302Sopenharmony_ci
38570af302Sopenharmony_ci/**
39570af302Sopenharmony_ci * @tc.name      : wcstoll_0200
40570af302Sopenharmony_ci * @tc.desc      : Convert wide string "10" to long long type
41570af302Sopenharmony_ci * @tc.level     : Level 0
42570af302Sopenharmony_ci */
43570af302Sopenharmony_civoid wcstoll_0200(void)
44570af302Sopenharmony_ci{
45570af302Sopenharmony_ci    long long ll;
46570af302Sopenharmony_ci    char *msg = "";
47570af302Sopenharmony_ci    TEST(ll, wcstoll(L"10", 0, 0), 10LL, "expect signed %lld != %lld");
48570af302Sopenharmony_ci}
49570af302Sopenharmony_ci
50570af302Sopenharmony_ci/**
51570af302Sopenharmony_ci * @tc.name      : wcstoll_0300
52570af302Sopenharmony_ci * @tc.desc      : Convert wide string "z" to long long type
53570af302Sopenharmony_ci * @tc.level     : Level 1
54570af302Sopenharmony_ci */
55570af302Sopenharmony_civoid wcstoll_0300(void)
56570af302Sopenharmony_ci{
57570af302Sopenharmony_ci    long long ll;
58570af302Sopenharmony_ci    char *msg = "";
59570af302Sopenharmony_ci    TEST(ll, wcstoll(L"z", 0, 36), 35LL, "%lld != %lld");
60570af302Sopenharmony_ci}
61570af302Sopenharmony_ci
62570af302Sopenharmony_ci/**
63570af302Sopenharmony_ci * @tc.name      : wcstoll_0400
64570af302Sopenharmony_ci * @tc.desc      : Convert wide string "00010010001101000101011001111000" to long long type
65570af302Sopenharmony_ci * @tc.level     : Level 1
66570af302Sopenharmony_ci */
67570af302Sopenharmony_civoid wcstoll_0400(void)
68570af302Sopenharmony_ci{
69570af302Sopenharmony_ci    long long ll;
70570af302Sopenharmony_ci    char *msg = "";
71570af302Sopenharmony_ci    TEST(ll, wcstoll(L"00010010001101000101011001111000", 0, 2), 0x12345678, "%lld != %lld");
72570af302Sopenharmony_ci}
73570af302Sopenharmony_ci
74570af302Sopenharmony_ci/**
75570af302Sopenharmony_ci * @tc.name      : wcstoll_0500
76570af302Sopenharmony_ci * @tc.desc      : Convert wide string "0xz" to long long type
77570af302Sopenharmony_ci * @tc.level     : Level 1
78570af302Sopenharmony_ci */
79570af302Sopenharmony_civoid wcstoll_0500(void)
80570af302Sopenharmony_ci{
81570af302Sopenharmony_ci    int i;
82570af302Sopenharmony_ci    long long ll;
83570af302Sopenharmony_ci    char *msg = "";
84570af302Sopenharmony_ci    wchar_t *s, *c;
85570af302Sopenharmony_ci    TEST(ll, wcstoll(s = L"0xz", &c, 16), 0, "%lld != %lld");
86570af302Sopenharmony_ci    TEST2(i, c - s, 1, "wrong final position %lld != %lld");
87570af302Sopenharmony_ci}
88570af302Sopenharmony_ci
89570af302Sopenharmony_ci/**
90570af302Sopenharmony_ci * @tc.name      : wcstoll_0600
91570af302Sopenharmony_ci * @tc.desc      : Convert wide string "0x1234" to long long type
92570af302Sopenharmony_ci * @tc.level     : Level 1
93570af302Sopenharmony_ci */
94570af302Sopenharmony_civoid wcstoll_0600(void)
95570af302Sopenharmony_ci{
96570af302Sopenharmony_ci    int i;
97570af302Sopenharmony_ci    long long ll;
98570af302Sopenharmony_ci    char *msg = "";
99570af302Sopenharmony_ci    wchar_t *s, *c;
100570af302Sopenharmony_ci    TEST(ll, wcstoll(s = L"0x1234", &c, 16), 0x1234, "%lld != %ld");
101570af302Sopenharmony_ci    TEST2(i, c - s, 6, "wrong final position %lld != %lld");
102570af302Sopenharmony_ci}
103570af302Sopenharmony_ci
104570af302Sopenharmony_ci/**
105570af302Sopenharmony_ci * @tc.name      : wcstoll_0700
106570af302Sopenharmony_ci * @tc.desc      : Convert wide string "9223372036854775808" to long long type exceeds the maximum value of the type
107570af302Sopenharmony_ci * @tc.level     : Level 2
108570af302Sopenharmony_ci */
109570af302Sopenharmony_civoid wcstoll_0700(void)
110570af302Sopenharmony_ci{
111570af302Sopenharmony_ci    int i;
112570af302Sopenharmony_ci    long long ll;
113570af302Sopenharmony_ci    char *msg = "";
114570af302Sopenharmony_ci    wchar_t *s, *c;
115570af302Sopenharmony_ci    TEST(ll, wcstoll(s = L"9223372036854775808", &c, 0), 9223372036854775807LL, "uncaught overflow %lld != %lld");
116570af302Sopenharmony_ci    TEST2(i, c - s, 19, "wrong final position %d != %d");
117570af302Sopenharmony_ci    TEST2(i, errno, ERANGE, "missing errno %d != %d");
118570af302Sopenharmony_ci}
119570af302Sopenharmony_ci
120570af302Sopenharmony_ci/**
121570af302Sopenharmony_ci * @tc.name      : wcstoll_0800
122570af302Sopenharmony_ci * @tc.desc      : Convert wide string "-9223372036854775809" to long long type exceeds the maximum value of the type
123570af302Sopenharmony_ci * @tc.level     : Level 2
124570af302Sopenharmony_ci */
125570af302Sopenharmony_civoid wcstoll_0800(void)
126570af302Sopenharmony_ci{
127570af302Sopenharmony_ci    int i;
128570af302Sopenharmony_ci    long long ll;
129570af302Sopenharmony_ci    char *msg = "";
130570af302Sopenharmony_ci    wchar_t *s, *c;
131570af302Sopenharmony_ci    TEST(ll, wcstoll(s = L"-9223372036854775809", &c, 0), -9223372036854775807LL - 1, "overflow %lld != %lld");
132570af302Sopenharmony_ci    TEST2(i, c - s, 20, "wrong final position %d != %d");
133570af302Sopenharmony_ci    TEST2(i, errno, ERANGE, "missing errno %d != %d");
134570af302Sopenharmony_ci}
135570af302Sopenharmony_ci
136570af302Sopenharmony_ciint main(int argc, char *argv[])
137570af302Sopenharmony_ci{
138570af302Sopenharmony_ci    wcstoll_0100();
139570af302Sopenharmony_ci    wcstoll_0200();
140570af302Sopenharmony_ci    wcstoll_0300();
141570af302Sopenharmony_ci    wcstoll_0400();
142570af302Sopenharmony_ci    wcstoll_0500();
143570af302Sopenharmony_ci    wcstoll_0600();
144570af302Sopenharmony_ci    wcstoll_0700();
145570af302Sopenharmony_ci    wcstoll_0800();
146570af302Sopenharmony_ci    return t_status;
147570af302Sopenharmony_ci}