setlocale.c 5.3 KB
Newer Older
G
ganlan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
/**
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include "functionalext.h"

#define TEST_LC_COUNT 7
#define TEST_LC_LENGTH 18
#define TEST_LC_OFFSET 6
#define PARAM_ERROR_VALUE_1 13
#define PARAM_ERROR_VALUE_2 (-1)

static const int LcArry[TEST_LC_COUNT] = {
    LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT, LC_IDENTIFICATION, LC_ALL};

static const char envforlocale[][TEST_LC_LENGTH] = {
    "LC_PAPER",
    "LC_NAME",
    "LC_ADDRESS",
    "LC_TELEPHONE",
    "LC_MEASUREMENT",
    "LC_IDENTIFICATION",
};

/**
 * @tc.name      : setlocaletest_0100
 * @tc.desc      : Determines whether setlocale returns the default value C
 *                 when the character set passed in for different data types is NULL
 * @tc.level     : Level 0
 */
void setlocale_0100(void)
{
    const int num = sizeof(LcArry) / sizeof(LcArry[0]);
    for (int i = 0; i < num; i++) {
        const char *locale = setlocale(LcArry[i], NULL);
        if (!locale) {
            t_error("[%s] failed\n", "setlocale_0100");
            return;
        }
        EXPECT_EQ("SetlocaleTest_0100", strcmp(locale, "C"), 0);
    }
}

/**
 * @tc.name      : setlocaletest_0200
 * @tc.desc      : Determines whether setlocale returns the default value "C"
 *                 when the default value "C" is passed in for different LC data types
 * @tc.level     : Level 0
 */
void setlocale_0200(void)
{
    const int num = sizeof(LcArry) / sizeof(LcArry[0]);
    for (int i = 0; i < num; i++) {
        setenv(envforlocale[i], "en-US", 1);
        const char *locale = setlocale(LcArry[i], "C");
        if (!locale) {
            t_error("[%s] failed\n", "setlocale_0200");
            return;
        }
        EXPECT_STREQ("SetlocaleTest_0200", locale, "C");
    }
}

/**
 * @tc.name      : setlocaletest_0300
 * @tc.desc      : Asserts whether the result returned is null when an exception LC data type is passed in
 * @tc.level     : Level 2
 */
void setlocale_0300(void)
{
    const char *locale1 = setlocale(PARAM_ERROR_VALUE_1, NULL);
    if (locale1) {
        t_error("[%s] failed\n", "SetlocaleTest_0300");
    }

    const char *locale2 = setlocale(PARAM_ERROR_VALUE_2, NULL);
    if (locale2) {
        t_error("[%s] failed\n", "SetlocaleTest_0300");
    }
}

/**
 * @tc.name      : setlocaletest_0400
 * @tc.desc      : Determines whether setlocale returns da_DK
 *                 when the environment variable is set to da_DK for different LC data types
 * @tc.level     : Level 0
 */
void setlocale_0400(void)
{
    for (unsigned int i = 0; i < sizeof(envforlocale) / sizeof(envforlocale[0]); i++) {
        setenv(envforlocale[i], "da_DK", 1);
        const char *locale = setlocale(LcArry[i], "");
        if (!locale) {
            t_error("[%s] failed\n", "setlocale_0400");
            return;
        }
        EXPECT_STREQ("SetlocaleTest_0400", locale, "da_DK");
    }
}

/**
 * @tc.name      : setlocaletest_0500
 * @tc.desc      : Determines whether setlocale returns en_ZA
 *                 when the character set passed in for different LC data types is set to en_ZA
 * @tc.level     : Level 0
 */
void setlocale_0500(void)
{
    char *rev = setlocale(LC_ALL, "C");
    if (!rev) {
        t_error("[%s] failed\n", "setlocale_0500");
        return;
    }
    for (unsigned int i = 0; i < sizeof(LcArry) / sizeof(LcArry[0]); i++) {
        const char *locale = setlocale(LcArry[i], "en_ZA");
        if (locale == NULL) {
            t_error("[%s] failed\n", "setlocale_0500");
            return;
        }
        EXPECT_STREQ("SetlocaleTest_0600", locale, "en_ZA");
    }
}

/**
 * @tc.name      : setlocaletest_0600
 * @tc.desc      : Verify that the environment variable of different LC data types is set to ar_QA. When the setlocale
 *                 function is used to pass LC_ALL, the corresponding field returned by the function is ar_QA
 * @tc.level     : Level 0
 */
void setlocale_0600(void)
{
    for (unsigned int i = 0; i < sizeof(envforlocale) / sizeof(envforlocale[0]); i++) {
        int count = 0;
        char *vec[LC_ALL];
        const char *flag = ";";

        setenv(envforlocale[i], "ar_QA", 1);
        char *locale = setlocale(LC_ALL, "");
        if (locale == NULL) {
            t_error("[%s] failed\n", "setlocale_0600");
            return;
        }

        char *token = strtok(locale, flag);
        while (token != NULL && count < LC_ALL) {
            vec[count] = token;
            token = strtok(NULL, flag);
            count++;
        }

        EXPECT_NE("setlocale_0600", count, 0);

        int expectPos = i + TEST_LC_OFFSET;
        EXPECT_STREQ("setlocale_0600", "ar_QA", vec[expectPos]);
    }
}

int main(void)
{
    setlocale_0100();
    setlocale_0200();
    setlocale_0300();
    setlocale_0400();
    setlocale_0500();
    setlocale_0600();

    return t_status;
}