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 <float.h> 18570af302Sopenharmony_ci#include <stdio.h> 19570af302Sopenharmony_ci#include <string.h> 20570af302Sopenharmony_ci#include <wchar.h> 21570af302Sopenharmony_ci#include "math.h" 22570af302Sopenharmony_ci#include "test.h" 23570af302Sopenharmony_ci 24570af302Sopenharmony_ci#define EXPECT_DOUBLE_EQ(a, b) \ 25570af302Sopenharmony_ci do { \ 26570af302Sopenharmony_ci if (!(fabs(a - b) < DBL_EPSILON)) \ 27570af302Sopenharmony_ci t_error("%s failed: %f is not equal to %f\n", __func__, a, b); \ 28570af302Sopenharmony_ci } while (0) 29570af302Sopenharmony_ci 30570af302Sopenharmony_ci/** 31570af302Sopenharmony_ci * @tc.name : wcstod_0100 32570af302Sopenharmony_ci * @tc.desc : Convert wide string to dobule type 33570af302Sopenharmony_ci * @tc.level : Level 0 34570af302Sopenharmony_ci */ 35570af302Sopenharmony_civoid wcstod_0100(void) 36570af302Sopenharmony_ci{ 37570af302Sopenharmony_ci wchar_t str0[] = L"3.14wcstod"; 38570af302Sopenharmony_ci wchar_t *end = NULL; 39570af302Sopenharmony_ci const double target = 3.14; 40570af302Sopenharmony_ci wchar_t str1[] = L"wcstod"; 41570af302Sopenharmony_ci double val = wcstod(str0, &end); 42570af302Sopenharmony_ci EXPECT_DOUBLE_EQ(val, target); 43570af302Sopenharmony_ci if (wcscmp(end, str1)) { 44570af302Sopenharmony_ci t_error("%s the result of comparing two strings should be equal", __func__); 45570af302Sopenharmony_ci } 46570af302Sopenharmony_ci} 47570af302Sopenharmony_ci 48570af302Sopenharmony_ci/** 49570af302Sopenharmony_ci * @tc.name : wcstod_0200 50570af302Sopenharmony_ci * @tc.desc : Convert wide string to dobule type with no end string characters 51570af302Sopenharmony_ci * @tc.level : Level 1 52570af302Sopenharmony_ci */ 53570af302Sopenharmony_civoid wcstod_0200(void) 54570af302Sopenharmony_ci{ 55570af302Sopenharmony_ci wchar_t str[] = L"3.14"; 56570af302Sopenharmony_ci wchar_t *end = NULL; 57570af302Sopenharmony_ci const double target = 3.14; 58570af302Sopenharmony_ci double val = wcstod(str, &end); 59570af302Sopenharmony_ci EXPECT_DOUBLE_EQ(val, target); 60570af302Sopenharmony_ci} 61570af302Sopenharmony_ci 62570af302Sopenharmony_ci/** 63570af302Sopenharmony_ci * @tc.name : wcstod_0300 64570af302Sopenharmony_ci * @tc.desc : Convert wide string to dobule type with whitespace present at the beginning 65570af302Sopenharmony_ci * @tc.level : Level 1 66570af302Sopenharmony_ci */ 67570af302Sopenharmony_civoid wcstod_0300(void) 68570af302Sopenharmony_ci{ 69570af302Sopenharmony_ci wchar_t str[] = L" 3.14"; 70570af302Sopenharmony_ci wchar_t *end = NULL; 71570af302Sopenharmony_ci const double target = 3.14; 72570af302Sopenharmony_ci double val = wcstod(str, &end); 73570af302Sopenharmony_ci EXPECT_DOUBLE_EQ(val, target); 74570af302Sopenharmony_ci} 75570af302Sopenharmony_ci 76570af302Sopenharmony_ci/** 77570af302Sopenharmony_ci * @tc.name : wcstod_0400 78570af302Sopenharmony_ci * @tc.desc : Convert wide string to dobule type with string characters at the beginning 79570af302Sopenharmony_ci * @tc.level : Level 2 80570af302Sopenharmony_ci */ 81570af302Sopenharmony_civoid wcstod_0400(void) 82570af302Sopenharmony_ci{ 83570af302Sopenharmony_ci wchar_t str[] = L"wcstod3.14"; 84570af302Sopenharmony_ci wchar_t *end = NULL; 85570af302Sopenharmony_ci const double target = 3.14; 86570af302Sopenharmony_ci double val = wcstod(str, &end); 87570af302Sopenharmony_ci if (val != 0) { 88570af302Sopenharmony_ci t_error("%s invalid conversion", __func__); 89570af302Sopenharmony_ci } 90570af302Sopenharmony_ci} 91570af302Sopenharmony_ci 92570af302Sopenharmony_ci/** 93570af302Sopenharmony_ci * @tc.name : wcstod_0500 94570af302Sopenharmony_ci * @tc.desc : Convert wide string to negative number of type double 95570af302Sopenharmony_ci * @tc.level : Level 1 96570af302Sopenharmony_ci */ 97570af302Sopenharmony_civoid wcstod_0500(void) 98570af302Sopenharmony_ci{ 99570af302Sopenharmony_ci wchar_t str[] = L"-3.14"; 100570af302Sopenharmony_ci wchar_t *end = NULL; 101570af302Sopenharmony_ci const double target = -3.14; 102570af302Sopenharmony_ci double val = wcstod(str, &end); 103570af302Sopenharmony_ci EXPECT_DOUBLE_EQ(val, target); 104570af302Sopenharmony_ci} 105570af302Sopenharmony_ci 106570af302Sopenharmony_ci/** 107570af302Sopenharmony_ci * @tc.name : wcstod_0600 108570af302Sopenharmony_ci * @tc.desc : Convert the hexadecimal string in the wide string to double type 109570af302Sopenharmony_ci * @tc.level : Level 1 110570af302Sopenharmony_ci */ 111570af302Sopenharmony_civoid wcstod_0600(void) 112570af302Sopenharmony_ci{ 113570af302Sopenharmony_ci wchar_t str[] = L"0X1.BC70A3D70A3D7P+6"; 114570af302Sopenharmony_ci wchar_t *end = NULL; 115570af302Sopenharmony_ci double val = wcstod(str, &end); 116570af302Sopenharmony_ci const double target = 111.11; 117570af302Sopenharmony_ci EXPECT_DOUBLE_EQ(val, target); 118570af302Sopenharmony_ci} 119570af302Sopenharmony_ci 120570af302Sopenharmony_ci/** 121570af302Sopenharmony_ci * @tc.name : wcstod_0700 122570af302Sopenharmony_ci * @tc.desc : The converted value is greater than double max 123570af302Sopenharmony_ci * @tc.level : Level 2 124570af302Sopenharmony_ci */ 125570af302Sopenharmony_civoid wcstod_0700(void) 126570af302Sopenharmony_ci{ 127570af302Sopenharmony_ci wchar_t str[] = L"1.18973e+4932"; 128570af302Sopenharmony_ci wchar_t *end = NULL; 129570af302Sopenharmony_ci double val = wcstod(str, &end); 130570af302Sopenharmony_ci if (errno != ERANGE) { 131570af302Sopenharmony_ci t_error("%s errno is not set", __func__); 132570af302Sopenharmony_ci } 133570af302Sopenharmony_ci if (val != INFINITY) { 134570af302Sopenharmony_ci t_error("%s val is not equal to inf", __func__); 135570af302Sopenharmony_ci } 136570af302Sopenharmony_ci} 137570af302Sopenharmony_ci 138570af302Sopenharmony_ciint main(int argc, char *argv[]) 139570af302Sopenharmony_ci{ 140570af302Sopenharmony_ci wcstod_0100(); 141570af302Sopenharmony_ci wcstod_0200(); 142570af302Sopenharmony_ci wcstod_0300(); 143570af302Sopenharmony_ci wcstod_0400(); 144570af302Sopenharmony_ci wcstod_0500(); 145570af302Sopenharmony_ci wcstod_0600(); 146570af302Sopenharmony_ci wcstod_0700(); 147570af302Sopenharmony_ci return t_status; 148570af302Sopenharmony_ci}