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_DOUBLE_EQ(a, b) \
25 do { \
26 if (!(fabs(a - b) < DBL_EPSILON)) \
27 t_error("%s failed: %f is not equal to %f\n", __func__, a, b); \
28 } while (0)
29
30 /**
31 * @tc.name : wcstod_0100
32 * @tc.desc : Convert wide string to dobule type
33 * @tc.level : Level 0
34 */
wcstod_0100(void)35 void wcstod_0100(void)
36 {
37 wchar_t str0[] = L"3.14wcstod";
38 wchar_t *end = NULL;
39 const double target = 3.14;
40 wchar_t str1[] = L"wcstod";
41 double val = wcstod(str0, &end);
42 EXPECT_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 : wcstod_0200
50 * @tc.desc : Convert wide string to dobule type with no end string characters
51 * @tc.level : Level 1
52 */
wcstod_0200(void)53 void wcstod_0200(void)
54 {
55 wchar_t str[] = L"3.14";
56 wchar_t *end = NULL;
57 const double target = 3.14;
58 double val = wcstod(str, &end);
59 EXPECT_DOUBLE_EQ(val, target);
60 }
61
62 /**
63 * @tc.name : wcstod_0300
64 * @tc.desc : Convert wide string to dobule type with whitespace present at the beginning
65 * @tc.level : Level 1
66 */
wcstod_0300(void)67 void wcstod_0300(void)
68 {
69 wchar_t str[] = L" 3.14";
70 wchar_t *end = NULL;
71 const double target = 3.14;
72 double val = wcstod(str, &end);
73 EXPECT_DOUBLE_EQ(val, target);
74 }
75
76 /**
77 * @tc.name : wcstod_0400
78 * @tc.desc : Convert wide string to dobule type with string characters at the beginning
79 * @tc.level : Level 2
80 */
wcstod_0400(void)81 void wcstod_0400(void)
82 {
83 wchar_t str[] = L"wcstod3.14";
84 wchar_t *end = NULL;
85 const double target = 3.14;
86 double val = wcstod(str, &end);
87 if (val != 0) {
88 t_error("%s invalid conversion", __func__);
89 }
90 }
91
92 /**
93 * @tc.name : wcstod_0500
94 * @tc.desc : Convert wide string to negative number of type double
95 * @tc.level : Level 1
96 */
wcstod_0500(void)97 void wcstod_0500(void)
98 {
99 wchar_t str[] = L"-3.14";
100 wchar_t *end = NULL;
101 const double target = -3.14;
102 double val = wcstod(str, &end);
103 EXPECT_DOUBLE_EQ(val, target);
104 }
105
106 /**
107 * @tc.name : wcstod_0600
108 * @tc.desc : Convert the hexadecimal string in the wide string to double type
109 * @tc.level : Level 1
110 */
wcstod_0600(void)111 void wcstod_0600(void)
112 {
113 wchar_t str[] = L"0X1.BC70A3D70A3D7P+6";
114 wchar_t *end = NULL;
115 double val = wcstod(str, &end);
116 const double target = 111.11;
117 EXPECT_DOUBLE_EQ(val, target);
118 }
119
120 /**
121 * @tc.name : wcstod_0700
122 * @tc.desc : The converted value is greater than double max
123 * @tc.level : Level 2
124 */
wcstod_0700(void)125 void wcstod_0700(void)
126 {
127 wchar_t str[] = L"1.18973e+4932";
128 wchar_t *end = NULL;
129 double val = wcstod(str, &end);
130 if (errno != ERANGE) {
131 t_error("%s errno is not set", __func__);
132 }
133 if (val != INFINITY) {
134 t_error("%s val is not equal to inf", __func__);
135 }
136 }
137
main(int argc,char * argv[])138 int main(int argc, char *argv[])
139 {
140 wcstod_0100();
141 wcstod_0200();
142 wcstod_0300();
143 wcstod_0400();
144 wcstod_0500();
145 wcstod_0600();
146 wcstod_0700();
147 return t_status;
148 }