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 <locale.h> 17#include <stdlib.h> 18#include <stdio.h> 19#include <time.h> 20#include <stdio.h> 21#include "functionalext.h" 22 23#define TEST_LC_COUNT 7 24#define TEST_LC_LENGTH 18 25#define TEST_LC_OFFSET 6 26#define PARAM_ERROR_VALUE_1 13 27#define PARAM_ERROR_VALUE_2 (-1) 28 29static const int LcArry[TEST_LC_COUNT] = { 30 LC_PAPER, 31 LC_NAME, 32 LC_ADDRESS, 33 LC_TELEPHONE, 34 LC_MEASUREMENT, 35 LC_IDENTIFICATION, 36 LC_ALL 37}; 38 39static const char envforlocale[][TEST_LC_LENGTH] = { 40 "LC_PAPER", 41 "LC_NAME", 42 "LC_ADDRESS", 43 "LC_TELEPHONE", 44 "LC_MEASUREMENT", 45 "LC_IDENTIFICATION", 46}; 47 48/** 49 * @tc.name : setlocaletest_0100 50 * @tc.desc : Determines whether setlocale returns the default value C 51 * when the character set passed in for different data types is NULL 52 * @tc.level : Level 0 53 */ 54void setlocale_0100(void) 55{ 56 const int num = sizeof(LcArry) / sizeof(LcArry[0]); 57 for (int i = 0; i < num; i++) { 58 const char *locale = setlocale(LcArry[i], NULL); 59 if (!locale) { 60 t_error("[%s] failed\n", "setlocale_0100"); 61 return; 62 } 63 EXPECT_EQ("SetlocaleTest_0100", strcmp(locale, "C"), 0); 64 } 65} 66 67/** 68 * @tc.name : setlocaletest_0200 69 * @tc.desc : Determines whether setlocale returns the default value "C" 70 * when the default value "C" is passed in for different LC data types 71 * @tc.level : Level 0 72 */ 73void setlocale_0200(void) 74{ 75 const int num = sizeof(LcArry) / sizeof(LcArry[0]); 76 for (int i = 0; i < num; i++) { 77 setenv(envforlocale[i], "en-US", 1); 78 const char *locale = setlocale(LcArry[i], "C"); 79 if (!locale) { 80 t_error("[%s] failed\n", "setlocale_0200"); 81 return; 82 } 83 EXPECT_STREQ("SetlocaleTest_0200", locale, "C"); 84 } 85} 86 87/** 88 * @tc.name : setlocaletest_0300 89 * @tc.desc : Asserts whether the result returned is null when an exception LC data type is passed in 90 * @tc.level : Level 2 91 */ 92void setlocale_0300(void) 93{ 94 const char *locale1 = setlocale(PARAM_ERROR_VALUE_1, NULL); 95 if (locale1) { 96 t_error("[%s] failed\n", "SetlocaleTest_0300"); 97 } 98 99 const char *locale2 = setlocale(PARAM_ERROR_VALUE_2, NULL); 100 if (locale2) { 101 t_error("[%s] failed\n", "SetlocaleTest_0300"); 102 } 103} 104 105/** 106 * @tc.name : setlocaletest_0400 107 * @tc.desc : Determines whether setlocale returns da_DK 108 * when the environment variable is set to da_DK for different LC data types 109 * @tc.level : Level 0 110 */ 111void setlocale_0400(void) 112{ 113 for (unsigned int i = 0; i < sizeof(envforlocale) / sizeof(envforlocale[0]); i++) { 114 setenv(envforlocale[i], "da_DK", 1); 115 const char *locale = setlocale(LcArry[i], ""); 116 if (locale) { 117 t_error("[%s] failed\n", "setlocale_0400"); 118 return; 119 } 120 } 121} 122 123/** 124 * @tc.name : setlocaletest_0500 125 * @tc.desc : Determines whether setlocale returns NULL 126 * when the character set passed in for invalid LC data types is set to en_ZA 127 * @tc.level : Level 0 128 */ 129void setlocale_0500(void) 130{ 131 char *rev = setlocale(LC_ALL, "C"); 132 if (!rev) { 133 t_error("[%s] failed\n", "setlocale_0500"); 134 return; 135 } 136 for (unsigned int i = 0; i < sizeof(LcArry) / sizeof(LcArry[0]); i++) { 137 const char *locale = setlocale(LcArry[i], "en_ZA"); 138 if (locale) { 139 t_error("[%s] failed\n", "setlocale_0500"); 140 return; 141 } 142 } 143} 144 145/** 146 * @tc.name : setlocaletest_0600 147 * @tc.desc : Verify that the environment variable of different LC data types is set to ar_QA. 148 * When the setlocale function is used to pass LC_ALL, the corresponding field returned by the function is ar_QA 149 * @tc.level : Level 0 150 */ 151void setlocale_0600(void) 152{ 153 for (unsigned int i = 0; i < sizeof(envforlocale) / sizeof(envforlocale[0]); i++) { 154 int count = 0; 155 char *vec[LC_ALL]; 156 const char *flag = ";"; 157 158 setenv(envforlocale[i], "ar_QA", 1); 159 char *locale = setlocale(LC_ALL, ""); 160 if (locale) { 161 t_error("[%s] failed\n", "setlocale_0600"); 162 return; 163 } 164 } 165} 166 167void setlocale_0700(void) 168{ 169 char *str = setlocale(LC_ALL, "sss123456"); 170 if (str) { 171 t_error("setlocale_0700 failed [%s] != NULL\n", str); 172 } 173} 174 175int main(void) 176{ 177 setlocale_0100(); 178 setlocale_0200(); 179 setlocale_0300(); 180 setlocale_0400(); 181 setlocale_0500(); 182 setlocale_0600(); 183 setlocale_0700(); 184 185 return t_status; 186} 187