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_LONG_DOUBLE_EQ(a, b) \ 25570af302Sopenharmony_ci do { \ 26570af302Sopenharmony_ci if (!(fabsl(a - b) < LDBL_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 : wcstold_0100 32570af302Sopenharmony_ci * @tc.desc : Convert wide string to long double type 33570af302Sopenharmony_ci * @tc.level : Level 0 34570af302Sopenharmony_ci */ 35570af302Sopenharmony_civoid wcstold_0100(void) 36570af302Sopenharmony_ci{ 37570af302Sopenharmony_ci wchar_t str0[] = L"3.14wcstold"; 38570af302Sopenharmony_ci wchar_t *end = NULL; 39570af302Sopenharmony_ci const long double target = 3.14L; 40570af302Sopenharmony_ci wchar_t str1[] = L"wcstold"; 41570af302Sopenharmony_ci long double val = wcstold(str0, &end); 42570af302Sopenharmony_ci EXPECT_LONG_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 : wcstold_0200 50570af302Sopenharmony_ci * @tc.desc : Convert wide string to long double type with no end string characters 51570af302Sopenharmony_ci * @tc.level : Level 1 52570af302Sopenharmony_ci */ 53570af302Sopenharmony_civoid wcstold_0200(void) 54570af302Sopenharmony_ci{ 55570af302Sopenharmony_ci wchar_t str[] = L"3.14"; 56570af302Sopenharmony_ci wchar_t *end = NULL; 57570af302Sopenharmony_ci const long double target = 3.14L; 58570af302Sopenharmony_ci long double val = wcstold(str, &end); 59570af302Sopenharmony_ci EXPECT_LONG_DOUBLE_EQ(val, target); 60570af302Sopenharmony_ci} 61570af302Sopenharmony_ci 62570af302Sopenharmony_ci/** 63570af302Sopenharmony_ci * @tc.name : wcstold_0300 64570af302Sopenharmony_ci * @tc.desc : Convert wide string to long double type with whitespace present at the beginning 65570af302Sopenharmony_ci * @tc.level : Level 1 66570af302Sopenharmony_ci */ 67570af302Sopenharmony_civoid wcstold_0300(void) 68570af302Sopenharmony_ci{ 69570af302Sopenharmony_ci wchar_t str[] = L" 3.14"; 70570af302Sopenharmony_ci wchar_t *end = NULL; 71570af302Sopenharmony_ci const long double target = 3.14L; 72570af302Sopenharmony_ci long double val = wcstold(str, &end); 73570af302Sopenharmony_ci EXPECT_LONG_DOUBLE_EQ(val, target); 74570af302Sopenharmony_ci} 75570af302Sopenharmony_ci 76570af302Sopenharmony_ci/** 77570af302Sopenharmony_ci * @tc.name : wcstold_0400 78570af302Sopenharmony_ci * @tc.desc : Convert wide string to long double type with string characters at the beginning 79570af302Sopenharmony_ci * @tc.level : Level 2 80570af302Sopenharmony_ci */ 81570af302Sopenharmony_civoid wcstold_0400(void) 82570af302Sopenharmony_ci{ 83570af302Sopenharmony_ci wchar_t str[] = L"wcstold3.14"; 84570af302Sopenharmony_ci wchar_t *end = NULL; 85570af302Sopenharmony_ci const long double target = 3.14L; 86570af302Sopenharmony_ci long double val = wcstold(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 : wcstold_0500 94570af302Sopenharmony_ci * @tc.desc : Convert wide string to negative number of type long double 95570af302Sopenharmony_ci * @tc.level : Level 1 96570af302Sopenharmony_ci */ 97570af302Sopenharmony_civoid wcstold_0500(void) 98570af302Sopenharmony_ci{ 99570af302Sopenharmony_ci wchar_t str[] = L"-3.14"; 100570af302Sopenharmony_ci wchar_t *end = NULL; 101570af302Sopenharmony_ci const long double target = -3.14L; 102570af302Sopenharmony_ci long double val = wcstold(str, &end); 103570af302Sopenharmony_ci EXPECT_LONG_DOUBLE_EQ(val, target); 104570af302Sopenharmony_ci} 105570af302Sopenharmony_ci 106570af302Sopenharmony_ci/** 107570af302Sopenharmony_ci * @tc.name : wcstold_0600 108570af302Sopenharmony_ci * @tc.desc : The converted value is greater than long double max 109570af302Sopenharmony_ci * @tc.level : Level 2 110570af302Sopenharmony_ci */ 111570af302Sopenharmony_civoid wcstold_0600(void) 112570af302Sopenharmony_ci{ 113570af302Sopenharmony_ci errno = 0; 114570af302Sopenharmony_ci wchar_t str[] = L"1.18973e+49322"; 115570af302Sopenharmony_ci wchar_t *end = NULL; 116570af302Sopenharmony_ci long double val = wcstold(str, &end); 117570af302Sopenharmony_ci if (errno != ERANGE) { 118570af302Sopenharmony_ci t_error("%s errno is not set\n", __func__); 119570af302Sopenharmony_ci } 120570af302Sopenharmony_ci if (val != HUGE_VALL) { 121570af302Sopenharmony_ci t_error("%s val is not equal to inf\n", __func__); 122570af302Sopenharmony_ci } 123570af302Sopenharmony_ci} 124570af302Sopenharmony_ci 125570af302Sopenharmony_ciint main(int argc, char *argv[]) 126570af302Sopenharmony_ci{ 127570af302Sopenharmony_ci wcstold_0100(); 128570af302Sopenharmony_ci wcstold_0200(); 129570af302Sopenharmony_ci wcstold_0300(); 130570af302Sopenharmony_ci wcstold_0400(); 131570af302Sopenharmony_ci wcstold_0500(); 132570af302Sopenharmony_ci wcstold_0600(); 133570af302Sopenharmony_ci return t_status; 134570af302Sopenharmony_ci}