1 /*
2 * Copyright (c) 2021 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 <climits>
17 #include <gtest/gtest.h>
18 #include "UnitNumberRecallTest.h"
19
20 using namespace std;
21 using namespace testing::ext;
22 namespace OHOS {
23 using namespace I18N;
24 using namespace testing;
25
26 class UnitNumberRecallTest : public testing::Test {};
27
28
29 /* *
30 * get locale index
31 *
32 * expectResult return locale index
33 */
GetLocaleIndex(LocaleInfo locale,vector<LocaleInfo> locales)34 static int GetLocaleIndex(LocaleInfo locale, vector<LocaleInfo> locales)
35 {
36 for (unsigned int index = 0; index < locales.size(); index++) {
37 if (locale == locales[index]) {
38 return index;
39 }
40 }
41 return -1;
42 }
43
44 /* *
45 * @tc.number SUB_GLOBAL_GLOBALIZATIONBUSINESS_NUMBER_FORMAT_LOCALE_0100
46 * @tc.name test NumberFormat Recall,locale is LocaleInfo en,ZZ
47 * @tc.desc [C- SOFTWARE -0200]
48 */
49 HWTEST_F(UnitNumberRecallTest, GLOBAL_NumberFormat_LocaleRecall_0100, Function | MediumTest | Level0)
50 {
51 int intNum = 1234567;
52 LocaleInfo *localeInfo = new LocaleInfo("en", "ZZ");
53 int status = I18nStatus::ISUCCESS;
54 NumberFormat *numberForm = new OHOS::I18N::NumberFormat(*localeInfo, status);
55 std::string numberFormOut = numberForm->Format(intNum, status);
56 LocaleInfo *localeRecall = new LocaleInfo("en", "", "US");
57 int localeIndex = GetLocaleIndex(*localeRecall, g_locales);
58 if (localeIndex >= 0) {
59 EXPECT_EQ(numberFormOut, g_numberFormExpect[localeIndex][0]);
60 }
61 free(localeRecall);
62 free(numberForm);
63 free(localeInfo);
64 }
65
66 /* *
67 * @tc.number SUB_GLOBAL_GLOBALIZATIONBUSINESS_NUMBER_FORMAT_LOCALE_0200
68 * @tc.name test NumberFormat Recall,locale is LocaleInfo zh,ZZ
69 * @tc.desc [C- SOFTWARE -0200]
70 */
71 HWTEST_F(UnitNumberRecallTest, GLOBAL_NumberFormat_LocaleRecall_0200, Function | MediumTest | Level0)
72 {
73 int intNum = 1234567;
74 LocaleInfo *localeInfo = new LocaleInfo("zh", "ZZ");
75 int status = I18nStatus::ISUCCESS;
76 NumberFormat *numberForm = new OHOS::I18N::NumberFormat(*localeInfo, status);
77 std::string numberFormOut = numberForm->Format(intNum, status);
78 LocaleInfo *localeRecall = new LocaleInfo("zh", "", "");
79 int localeIndex = GetLocaleIndex(*localeRecall, g_locales);
80 if (localeIndex >= 0) {
81 EXPECT_EQ(numberFormOut, g_numberFormExpect[localeIndex][0]);
82 }
83 free(localeRecall);
84 free(numberForm);
85 free(localeInfo);
86 }
87
88 /* *
89 * @tc.number SUB_GLOBAL_GLOBALIZATIONBUSINESS_NUMBER_FORMAT_LOCALE_0300
90 * @tc.name test NumberFormat Recall,locale is LocaleInfo zh,Hans,ZZ
91 * @tc.desc [C- SOFTWARE -0200]
92 */
93 HWTEST_F(UnitNumberRecallTest, GLOBAL_NumberFormat_LocaleRecall_0300, Function | MediumTest | Level0)
94 {
95 int intNum = 1234567;
96 LocaleInfo *localeInfo = new LocaleInfo("zh", "Hans", "ZZ");
97 int status = I18nStatus::ISUCCESS;
98 NumberFormat *numberForm = new OHOS::I18N::NumberFormat(*localeInfo, status);
99 std::string numberFormOut = numberForm->Format(intNum, status);
100 LocaleInfo *localeRecall = new LocaleInfo("zh", "Hans", "");
101 int localeIndex = GetLocaleIndex(*localeRecall, g_locales);
102 if (localeIndex >= 0) {
103 EXPECT_EQ(numberFormOut, g_numberFormExpect[localeIndex][0]);
104 }
105 free(localeRecall);
106 free(numberForm);
107 free(localeInfo);
108 }
109
110 /* *
111 * @tc.number SUB_GLOBAL_GLOBALIZATIONBUSINESS_NUMBER_FORMAT_LOCALE_0400
112 * @tc.name test NumberFormat Recall,locale is LocaleInfo zz
113 * @tc.desc [C- SOFTWARE -0200]
114 */
115 HWTEST_F(UnitNumberRecallTest, GLOBAL_NumberFormat_LocaleRecall_0400, Function | MediumTest | Level0)
116 {
117 int intNum = 1234567;
118 LocaleInfo *localeInfo = new LocaleInfo("zz", "");
119 int status = I18nStatus::ISUCCESS;
120 NumberFormat *numberForm = new OHOS::I18N::NumberFormat(*localeInfo, status);
121 std::string numberFormOut = numberForm->Format(intNum, status);
122 LocaleInfo *localeRecall = new LocaleInfo("en", "US");
123 int localeIndex = GetLocaleIndex(*localeRecall, g_locales);
124 if (localeIndex >= 0) {
125 EXPECT_EQ(numberFormOut, g_numberFormExpect[localeIndex][0]);
126 }
127 free(localeRecall);
128 free(numberForm);
129 free(localeInfo);
130 }
131 } // namespace OHOS