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 <float.h>
18#include <stdio.h>
19#include <string.h>
20#include <wchar.h>
21#include "math.h"
22#include "test.h"
23
24#define EXPECT_LONG_DOUBLE_EQ(a, b)                                        \
25    do {                                                                   \
26        if (!(fabsl(a - b) < LDBL_EPSILON))                                \
27            t_error("%s failed: %f is not equal to %f\n", __func__, a, b); \
28    } while (0)
29
30/**
31 * @tc.name      : wcstold_0100
32 * @tc.desc      : Convert wide string to long double type
33 * @tc.level     : Level 0
34 */
35void wcstold_0100(void)
36{
37    wchar_t str0[] = L"3.14wcstold";
38    wchar_t *end = NULL;
39    const long double target = 3.14L;
40    wchar_t str1[] = L"wcstold";
41    long double val = wcstold(str0, &end);
42    EXPECT_LONG_DOUBLE_EQ(val, target);
43    if (wcscmp(end, str1)) {
44        t_error("%s the result of comparing two strings should be equal", __func__);
45    }
46}
47
48/**
49 * @tc.name      : wcstold_0200
50 * @tc.desc      : Convert wide string to long double type with no end string characters
51 * @tc.level     : Level 1
52 */
53void wcstold_0200(void)
54{
55    wchar_t str[] = L"3.14";
56    wchar_t *end = NULL;
57    const long double target = 3.14L;
58    long double val = wcstold(str, &end);
59    EXPECT_LONG_DOUBLE_EQ(val, target);
60}
61
62/**
63 * @tc.name      : wcstold_0300
64 * @tc.desc      : Convert wide string to long double type with whitespace present at the beginning
65 * @tc.level     : Level 1
66 */
67void wcstold_0300(void)
68{
69    wchar_t str[] = L"          3.14";
70    wchar_t *end = NULL;
71    const long double target = 3.14L;
72    long double val = wcstold(str, &end);
73    EXPECT_LONG_DOUBLE_EQ(val, target);
74}
75
76/**
77 * @tc.name      : wcstold_0400
78 * @tc.desc      : Convert wide string to long double type with string characters at the beginning
79 * @tc.level     : Level 2
80 */
81void wcstold_0400(void)
82{
83    wchar_t str[] = L"wcstold3.14";
84    wchar_t *end = NULL;
85    const long double target = 3.14L;
86    long double val = wcstold(str, &end);
87    if (val != 0) {
88        t_error("%s invalid conversion", __func__);
89    }
90}
91
92/**
93 * @tc.name      : wcstold_0500
94 * @tc.desc      : Convert wide string to negative number of type long double
95 * @tc.level     : Level 1
96 */
97void wcstold_0500(void)
98{
99    wchar_t str[] = L"-3.14";
100    wchar_t *end = NULL;
101    const long double target = -3.14L;
102    long double val = wcstold(str, &end);
103    EXPECT_LONG_DOUBLE_EQ(val, target);
104}
105
106/**
107 * @tc.name      : wcstold_0600
108 * @tc.desc      : The converted value is greater than long double max
109 * @tc.level     : Level 2
110 */
111void wcstold_0600(void)
112{
113    errno = 0;
114    wchar_t str[] = L"1.18973e+49322";
115    wchar_t *end = NULL;
116    long double val = wcstold(str, &end);
117    if (errno != ERANGE) {
118        t_error("%s errno is not set\n", __func__);
119    }
120    if (val != HUGE_VALL) {
121        t_error("%s val is not equal to inf\n", __func__);
122    }
123}
124
125int main(int argc, char *argv[])
126{
127    wcstold_0100();
128    wcstold_0200();
129    wcstold_0300();
130    wcstold_0400();
131    wcstold_0500();
132    wcstold_0600();
133    return t_status;
134}