• 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 <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_FLOAT_EQ(a, b)                                              \
25     do {                                                                   \
26         if (!(fabsf(a - b) < FLT_EPSILON))                                 \
27             t_error("%s failed: %f is not equal to %f\n", __func__, a, b); \
28     } while (0)
29 
30 /**
31  * @tc.name      : wcstof_0100
32  * @tc.desc      : Convert wide string to float type
33  * @tc.level     : Level 0
34  */
wcstof_0100(void)35 void wcstof_0100(void)
36 {
37     wchar_t str0[] = L"3.14wcstof";
38     wchar_t *end = NULL;
39     const float target = 3.14f;
40     wchar_t str1[] = L"wcstof";
41     float val = wcstof(str0, &end);
42     EXPECT_FLOAT_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      : wcstof_0200
50  * @tc.desc      : Convert wide string to float type with no end string characters
51  * @tc.level     : Level 1
52  */
wcstof_0200(void)53 void wcstof_0200(void)
54 {
55     wchar_t str[] = L"3.14";
56     wchar_t *end = NULL;
57     const float target = 3.14f;
58     float val = wcstof(str, &end);
59     EXPECT_FLOAT_EQ(val, target);
60 }
61 
62 /**
63  * @tc.name      : wcstof_0300
64  * @tc.desc      : Convert wide string to float type with whitespace present at the beginning
65  * @tc.level     : Level 1
66  */
wcstof_0300(void)67 void wcstof_0300(void)
68 {
69     wchar_t str[] = L"          3.14";
70     wchar_t *end = NULL;
71     const float target = 3.14f;
72     float val = wcstof(str, &end);
73     EXPECT_FLOAT_EQ(val, target);
74 }
75 
76 /**
77  * @tc.name      : wcstof_0400
78  * @tc.desc      : Convert wide string to float type with string characters at the beginning
79  * @tc.level     : Level 2
80  */
wcstof_0400(void)81 void wcstof_0400(void)
82 {
83     wchar_t str[] = L"wcstof3.14";
84     wchar_t *end = NULL;
85     const float target = 3.14f;
86     float val = wcstof(str, &end);
87     if (val != 0) {
88         t_error("%s invalid conversion", __func__);
89     }
90 }
91 
92 /**
93  * @tc.name      : wcstof_0500
94  * @tc.desc      : Convert wide string to negative number of type float
95  * @tc.level     : Level 1
96  */
wcstof_0500(void)97 void wcstof_0500(void)
98 {
99     wchar_t str[] = L"-3.14";
100     wchar_t *end = NULL;
101     const float target = -3.14f;
102     float val = wcstof(str, &end);
103     EXPECT_FLOAT_EQ(val, target);
104 }
105 
106 /**
107  * @tc.name      : wcstof_0600
108  * @tc.desc      : Convert wide string to negative number of type float
109  * @tc.level     : Level 1
110  */
wcstof_0600(void)111 void wcstof_0600(void)
112 {
113     wchar_t str[] = L"0X1.BC70A3D70A3D7P+6";
114     wchar_t *end = NULL;
115     float val = wcstof(str, &end);
116     const float target = 111.11f;
117     EXPECT_FLOAT_EQ(val, target);
118 }
119 
120 /**
121  * @tc.name      : wcstof_0700
122  * @tc.desc      : The converted value is greater than float max
123  * @tc.level     : Level 2
124  */
wcstof_0700(void)125 void wcstof_0700(void)
126 {
127     wchar_t str[] = L"1.18973e+4932";
128     wchar_t *end = NULL;
129     float val = wcstof(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     wcstof_0100();
141     wcstof_0200();
142     wcstof_0300();
143     wcstof_0400();
144     wcstof_0500();
145     wcstof_0600();
146     wcstof_0700();
147     return t_status;
148 }