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 <gtest/gtest.h>
17 #include <map>
18 #include <vector>
19
20 #include "accesstoken_kit.h"
21 #include "character.h"
22 #include "collator.h"
23 #include "date_time_filter.h"
24 #include "date_time_format.h"
25 #include "date_time_rule.h"
26 #include "holiday_manager.h"
27 #include "i18n_break_iterator.h"
28 #include "i18n_calendar.h"
29 #include "i18n_service_ability_client.h"
30 #include "i18n_timezone.h"
31 #include "i18n_types.h"
32 #include "index_util.h"
33 #include "locale_compare.h"
34 #include "locale_config.h"
35 #include "locale_info.h"
36 #include "nativetoken_kit.h"
37 #include "measure_data.h"
38 #include "number_format.h"
39 #include "plural_rules.h"
40 #include "preferred_language.h"
41 #include "regex_rule.h"
42 #include "relative_time_format.h"
43 #include "system_locale_manager.h"
44 #include "taboo_utils.h"
45 #include "taboo.h"
46 #include "token_setproc.h"
47 #include "utils.h"
48 #include "intl_test.h"
49 #include "generate_ics_file.h"
50 #include <unistd.h>
51
52 using namespace OHOS::Global::I18n;
53 using testing::ext::TestSize;
54 using namespace std;
55
56 namespace OHOS {
57 namespace Global {
58 namespace I18n {
59 static const uint64_t SELF_TOKEN_ID = GetSelfTokenID();
60 static constexpr int32_t I18N_UID = 3013;
61 static constexpr int32_t ROOT_UID = 0;
62
63 string IntlTest::originalLanguage;
64 string IntlTest::originalRegion;
65 string IntlTest::originalLocale;
66
SetUpTestCase(void)67 void IntlTest::SetUpTestCase(void)
68 {
69 originalLanguage = LocaleConfig::GetSystemLanguage();
70 originalRegion = LocaleConfig::GetSystemRegion();
71 originalLocale = LocaleConfig::GetSystemLocale();
72 LocaleConfig::SetSystemLanguage("zh-Hans");
73 LocaleConfig::SetSystemRegion("CN");
74 LocaleConfig::SetSystemLocale("zh-Hans-CN");
75 }
76
TearDownTestCase(void)77 void IntlTest::TearDownTestCase(void)
78 {
79 LocaleConfig::SetSystemLanguage(originalLanguage);
80 LocaleConfig::SetSystemRegion(originalRegion);
81 LocaleConfig::SetSystemLocale(originalLocale);
82 }
83
SetUp(void)84 void IntlTest::SetUp(void)
85 {}
86
TearDown(void)87 void IntlTest::TearDown(void)
88 {}
89
RemoveTokenAndPermissions()90 void RemoveTokenAndPermissions()
91 {
92 SetSelfTokenID(SELF_TOKEN_ID);
93 seteuid(ROOT_UID);
94 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
95 seteuid(I18N_UID);
96 }
97
AddTokenAndPermissions()98 void AddTokenAndPermissions()
99 {
100 const char* i18nPermissions[] = {
101 "ohos.permission.UPDATE_CONFIGURATION"
102 };
103 NativeTokenInfoParams i18nInfoInstance = {
104 .dcapsNum = 0,
105 .permsNum = sizeof(i18nPermissions) / sizeof(i18nPermissions[0]),
106 .aclsNum = 0,
107 .dcaps = nullptr,
108 .perms = i18nPermissions,
109 .acls = nullptr,
110 .aplStr = "system_basic",
111 };
112 i18nInfoInstance.processName = "I18nTest";
113 SetSelfTokenID(GetAccessTokenId(&i18nInfoInstance));
114 seteuid(ROOT_UID);
115 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
116 seteuid(I18N_UID);
117 }
118
InitTestEnvironment()119 void InitTestEnvironment()
120 {
121 AddTokenAndPermissions();
122 I18nServiceAbilityClient::SetSystemLocale("zh-Hans-CN");
123 RemoveTokenAndPermissions();
124 }
125
RestoreEnvironment(const std::string & originLocaleTag)126 void RestoreEnvironment(const std::string &originLocaleTag)
127 {
128 AddTokenAndPermissions();
129 I18nServiceAbilityClient::SetSystemLocale(originLocaleTag);
130 RemoveTokenAndPermissions();
131 }
132
133 /**
134 * @tc.name: IntlFuncTest001
135 * @tc.desc: Test Intl DateTimeFormat.format
136 * @tc.type: FUNC
137 */
138 HWTEST_F(IntlTest, IntlFuncTest001, TestSize.Level1)
139 {
140 string locale = "zh-CN-u-hc-h12";
141 string expects = "公元1970年1月1日星期四 上午8:20:34";
142 vector<string> locales;
143 locales.push_back("jessie");
144 locales.push_back(locale);
145 map<string, string> options = { { "year", "numeric" },
146 { "month", "narrow" },
147 { "day", "numeric" },
148 { "hour", "numeric" },
149 { "minute", "2-digit" },
150 { "second", "numeric" },
151 { "weekday", "long" },
152 { "era", "short"} };
153 DateTimeFormat *dateFormat = new (std::nothrow) DateTimeFormat(locales, options);
154 if (!dateFormat) {
155 EXPECT_TRUE(false);
156 return;
157 }
158 int64_t milliseconds = 1234567;
159 string out = dateFormat->Format(milliseconds);
160 EXPECT_EQ(out, expects);
161 EXPECT_EQ(dateFormat->GetYear(), "numeric");
162 EXPECT_EQ(dateFormat->GetMonth(), "narrow");
163 EXPECT_EQ(dateFormat->GetDay(), "numeric");
164 EXPECT_EQ(dateFormat->GetHour(), "numeric");
165 EXPECT_EQ(dateFormat->GetMinute(), "2-digit");
166 EXPECT_EQ(dateFormat->GetSecond(), "numeric");
167 EXPECT_EQ(dateFormat->GetWeekday(), "long");
168 EXPECT_EQ(dateFormat->GetEra(), "short");
169 EXPECT_EQ(dateFormat->GetHourCycle(), "h12");
170 delete dateFormat;
171 }
172
173 /**
174 * @tc.name: IntlFuncTest002
175 * @tc.desc: Test Intl LocaleInfo
176 * @tc.type: FUNC
177 */
178 HWTEST_F(IntlTest, IntlFuncTest002, TestSize.Level0)
179 {
180 string locale = "ja-Jpan-JP-u-ca-japanese-hc-h12-co-emoji";
181 map<string, string> options = { { "hourCycle", "h11" },
182 { "numeric", "true" },
183 { "numberingSystem", "jpan" } };
184 LocaleInfo *loc = new (std::nothrow) LocaleInfo(locale, options);
185 if (!loc) {
186 EXPECT_TRUE(false);
187 return;
188 }
189 EXPECT_EQ(loc->GetLanguage(), "ja");
190 EXPECT_EQ(loc->GetScript(), "Jpan");
191 EXPECT_EQ(loc->GetRegion(), "JP");
192 EXPECT_EQ(loc->GetBaseName(), "ja-Jpan-JP");
193 EXPECT_EQ(loc->GetCalendar(), "japanese");
194 EXPECT_EQ(loc->GetHourCycle(), "h11");
195 EXPECT_EQ(loc->GetNumberingSystem(), "jpan");
196 EXPECT_EQ(loc->Minimize(), "ja-u-hc-h11-nu-jpan-ca-japanese-co-emoji-kn-true");
197 EXPECT_EQ(loc->Maximize(), "ja-Jpan-JP-u-hc-h11-nu-jpan-ca-japanese-co-emoji-kn-true");
198 EXPECT_EQ(loc->GetNumeric(), "true");
199 EXPECT_EQ(loc->GetCaseFirst(), "");
200 delete loc;
201 }
202
203 /**
204 * @tc.name: IntlFuncTest003
205 * @tc.desc: Test Intl LocaleInfo
206 * @tc.type: FUNC
207 */
208 HWTEST_F(IntlTest, IntlFuncTest003, TestSize.Level1)
209 {
210 string locale = "en-GB";
211 LocaleInfo *loc = new (std::nothrow) LocaleInfo(locale);
212 if (!loc) {
213 EXPECT_TRUE(false);
214 return;
215 }
216 string language = "en";
217 string script = "";
218 string region = "GB";
219 string baseName = "en-GB";
220 EXPECT_EQ(loc->GetLanguage(), language);
221 EXPECT_EQ(loc->GetScript(), script);
222 EXPECT_EQ(loc->GetRegion(), region);
223 EXPECT_EQ(loc->GetBaseName(), baseName);
224 locale = "ja-u-hc-h12-nu-Jpan-JP-kf-japanese-co-emoji-kn-true";
225 LocaleInfo *localeInfo = new (std::nothrow) LocaleInfo(locale);
226 EXPECT_EQ(localeInfo->GetBaseName(), "ja");
227 delete localeInfo;
228 delete loc;
229 }
230
231 /**
232 * @tc.name: IntlFuncTest004
233 * @tc.desc: Test Intl DateTimeFormat.format
234 * @tc.type: FUNC
235 */
236 HWTEST_F(IntlTest, IntlFuncTest004, TestSize.Level1)
237 {
238 string locale = "en-GB";
239 string expects = "2 January 1970, 18:17 – 12 January 1970, 18:20";
240 vector<string> locales;
241 locales.push_back(locale);
242 string dateStyle = "long";
243 string timeStyle = "short";
244 string hourCycle = "h24";
245 string hour12 = "false";
246 map<string, string> options = { { "dateStyle", dateStyle },
247 { "hour12", hour12 },
248 { "hourCycle", hourCycle },
249 { "timeStyle", timeStyle } };
250 DateTimeFormat *dateFormat = new (std::nothrow) DateTimeFormat(locales, options);
251 if (!dateFormat) {
252 EXPECT_TRUE(false);
253 return;
254 }
255 int64_t fromMilliseconds = 123456789;
256 int64_t toMilliseconds = 987654321;
257 string out = dateFormat->FormatRange(fromMilliseconds, toMilliseconds);
258 EXPECT_EQ(out, expects);
259 EXPECT_EQ(dateFormat->GetDateStyle(), dateStyle);
260 EXPECT_EQ(dateFormat->GetTimeStyle(), timeStyle);
261 delete dateFormat;
262 }
263
264 /**
265 * @tc.name: IntlFuncTest005
266 * @tc.desc: Test Intl DateTimeFormat.format
267 * @tc.type: FUNC
268 */
269 HWTEST_F(IntlTest, IntlFuncTest005, TestSize.Level1)
270 {
271 string locale = "ja";
272 string expects = "1970年1月2日金曜日";
273 vector<string> locales;
274 locales.push_back(locale);
275 map<string, string> options = { { "year", "numeric" },
276 { "month", "long" },
277 { "day", "numeric" },
278 { "weekday", "long"} };
279 DateTimeFormat *dateFormat = new (std::nothrow) DateTimeFormat(locales, options);
280 if (!dateFormat) {
281 EXPECT_TRUE(false);
282 return;
283 }
284 int64_t milliseconds = 123456789;
285 string out = dateFormat->Format(milliseconds);
286 EXPECT_EQ(out, expects);
287 int64_t fromMilliseconds = 123456789;
288 int64_t toMilliseconds = 987654321;
289 expects = "1970/01/02(金曜日)~1970/01/12(月曜日)";
290 out = dateFormat->FormatRange(fromMilliseconds, toMilliseconds);
291 EXPECT_EQ(out, expects);
292 delete dateFormat;
293 }
294
295 /**
296 * @tc.name: IntlFuncTest006
297 * @tc.desc: Test Intl NumberFormat.format
298 * @tc.type: FUNC
299 */
300 HWTEST_F(IntlTest, IntlFuncTest006, TestSize.Level1)
301 {
302 string locale = "en-IN";
303 string expects = "+1,23,456.79 euros";
304 vector<string> locales;
305 locales.push_back(locale);
306 string useGrouping = "true";
307 string minimumIntegerDigits = "7";
308 string maximumFractionDigits = "2";
309 string style = "currency";
310 string currency = "EUR";
311 map<string, string> options = { { "useGrouping", useGrouping },
312 { "style", style },
313 { "currency", currency },
314 { "currencyDisplay", "name" },
315 { "currencySign", "accounting" },
316 { "signDisplay", "always" } };
317 NumberFormat *numFmt = new (std::nothrow) NumberFormat(locales, options);
318 if (!numFmt) {
319 EXPECT_TRUE(false);
320 return;
321 }
322 string out = numFmt->Format(123456.789);
323 EXPECT_EQ(out, expects);
324 EXPECT_EQ(numFmt->GetUseGrouping(), useGrouping);
325 EXPECT_EQ(numFmt->GetStyle(), style);
326 EXPECT_EQ(numFmt->GetCurrency(), currency);
327 delete numFmt;
328 }
329
330 /**
331 * @tc.name: IntlFuncTest007
332 * @tc.desc: Test Intl NumberFormat.format
333 * @tc.type: FUNC
334 */
335 HWTEST_F(IntlTest, IntlFuncTest007, TestSize.Level1)
336 {
337 string locale = "zh-CN";
338 string expects = "0,123,456.79米";
339 vector<string> locales;
340 locales.push_back("ban");
341 locales.push_back(locale);
342 string minimumIntegerDigits = "7";
343 string maximumFractionDigits = "2";
344 string style = "unit";
345 map<string, string> options = { { "style", style },
346 { "minimumIntegerDigits", minimumIntegerDigits },
347 { "maximumFractionDigits", maximumFractionDigits },
348 { "unit", "meter" },
349 { "unitDisplay", "long"} };
350 NumberFormat *numFmt = new (std::nothrow) NumberFormat(locales, options);
351 if (!numFmt) {
352 EXPECT_TRUE(false);
353 return;
354 }
355 string out = numFmt->Format(123456.789);
356 EXPECT_EQ(out, expects);
357 EXPECT_EQ(numFmt->GetStyle(), style);
358 delete numFmt;
359 }
360
361 /**
362 * @tc.name: IntlFuncTest008
363 * @tc.desc: Test Intl NumberFormat.format
364 * @tc.type: FUNC
365 */
366 HWTEST_F(IntlTest, IntlFuncTest008, TestSize.Level1)
367 {
368 string locale = "en-CN";
369 string expects = "12,345,678.9%";
370 vector<string> locales;
371 locales.push_back(locale);
372 string minimumIntegerDigits = "7";
373 string maximumFractionDigits = "2";
374 string style = "percent";
375 map<string, string> options = { { "style", style },
376 { "minimumIntegerDigits", minimumIntegerDigits },
377 { "maximumFractionDigits", maximumFractionDigits } };
378 NumberFormat *numFmt = new (std::nothrow) NumberFormat(locales, options);
379 if (!numFmt) {
380 EXPECT_TRUE(false);
381 return;
382 }
383 string out = numFmt->Format(123456.789);
384 EXPECT_EQ(out, expects);
385 EXPECT_EQ(numFmt->GetStyle(), style);
386 delete numFmt;
387 }
388
389 /**
390 * @tc.name: IntlFuncTest009
391 * @tc.desc: Test Intl NumberFormat.format
392 * @tc.type: FUNC
393 */
394 HWTEST_F(IntlTest, IntlFuncTest009, TestSize.Level1)
395 {
396 string locale = "en-CN";
397 string expects = "0,123,456.79";
398 vector<string> locales;
399 locales.push_back(locale);
400 string minimumIntegerDigits = "7";
401 string maximumFractionDigits = "2";
402 string style = "decimal";
403 map<string, string> options = { { "style", style },
404 { "minimumIntegerDigits", minimumIntegerDigits },
405 { "maximumFractionDigits", maximumFractionDigits } };
406 NumberFormat *numFmt = new (std::nothrow) NumberFormat(locales, options);
407 if (!numFmt) {
408 EXPECT_TRUE(false);
409 return;
410 }
411 string out = numFmt->Format(123456.789);
412 EXPECT_EQ(out, expects);
413 EXPECT_EQ(numFmt->GetStyle(), style);
414 options = { { "style", "unit" },
415 { "unit", "meter" },
416 { "currency", "USD" },
417 { "currencyDisplay", "symbol" },
418 { "compactDisplay", "long" },
419 { "useGrouping", "true" },
420 { "unitUsage", "length-person" },
421 { "unitDisplay", "long" } };
422 NumberFormat *numFormat = new (std::nothrow) NumberFormat(locales, options);
423 map<string, string> res;
424 numFormat->GetResolvedOptions(res);
425 delete numFormat;
426 delete numFmt;
427 }
428
429 /**
430 * @tc.name: IntlFuncTest0010
431 * @tc.desc: Test Intl NumberFormat.format
432 * @tc.type: FUNC
433 */
434 HWTEST_F(IntlTest, IntlFuncTest010, TestSize.Level1)
435 {
436 string locale = "en-CN";
437 string expects = "1.234568E5";
438 vector<string> locales;
439 locales.push_back(locale);
440 string style = "decimal";
441 map<string, string> options = { { "style", style },
442 { "notation", "scientific" } };
443 NumberFormat *numFmt = new (std::nothrow) NumberFormat(locales, options);
444 if (!numFmt) {
445 EXPECT_TRUE(false);
446 return;
447 }
448 string out = numFmt->Format(123456.789);
449 EXPECT_EQ(out, expects);
450 EXPECT_EQ(numFmt->GetStyle(), style);
451 delete numFmt;
452 }
453
454 /**
455 * @tc.name: IntlFuncTest0011
456 * @tc.desc: Test Intl NumberFormat.format
457 * @tc.type: FUNC
458 */
459 HWTEST_F(IntlTest, IntlFuncTest011, TestSize.Level1)
460 {
461 string locale = "en-CN";
462 string expects = "123 thousand";
463 vector<string> locales;
464 locales.push_back(locale);
465 string style = "decimal";
466 map<string, string> options = { { "style", style },
467 { "notation", "compact" },
468 { "compactDisplay", "long" } };
469 NumberFormat *numFmt = new (std::nothrow) NumberFormat(locales, options);
470 if (!numFmt) {
471 EXPECT_TRUE(false);
472 return;
473 }
474 string out = numFmt->Format(123456.789);
475 EXPECT_EQ(out, expects);
476 EXPECT_EQ(numFmt->GetStyle(), style);
477 delete numFmt;
478 }
479
480 /**
481 * @tc.name: IntlFuncTest0012
482 * @tc.desc: Test Intl DateTimeFormat
483 * @tc.type: FUNC
484 */
485 HWTEST_F(IntlTest, IntlFuncTest0012, TestSize.Level1)
486 {
487 string locale = "en";
488 map<string, string> options = {
489 { "dateStyle", "short" }
490 };
491 vector<string> locales;
492 locales.push_back(locale);
493 std::string expects = "3/11/82";
494 DateTimeFormat *dateFormat = new (std::nothrow) DateTimeFormat(locales, options);
495 if (!dateFormat) {
496 EXPECT_TRUE(false);
497 return;
498 }
499 int64_t milliseconds = 123456789123456;
500 string out = dateFormat->Format(milliseconds);
501 EXPECT_EQ(out, expects);
502 options = {
503 { "dateStyle", "long" },
504 { "hourCycle", "h11" }
505 };
506 DateTimeFormat *dateFormatH11 = new (std::nothrow) DateTimeFormat(locales, options);
507 options.insert({ "hourCycle", "h12" });
508 DateTimeFormat *dateFormatH12 = new (std::nothrow) DateTimeFormat(locales, options);
509 options.insert({ "hourCycle", "h23" });
510 DateTimeFormat *dateFormatH23 = new (std::nothrow) DateTimeFormat(locales, options);
511 options.insert({ "hourCycle", "h24" });
512 DateTimeFormat *dateFormatH24 = new (std::nothrow) DateTimeFormat(locales, options);
513 delete dateFormatH11;
514 delete dateFormatH12;
515 delete dateFormatH23;
516 delete dateFormatH24;
517 locales.clear();
518 std::unique_ptr<DateTimeFormat> dateFormatEmpty = std::make_unique<DateTimeFormat>(locales, options);
519 locales.push_back("@@@&&");
520 options = {
521 { "dateStyle", "long" },
522 { "dayPeriod", "short" }
523 };
524 std::unique_ptr<DateTimeFormat> dateFormatFake = std::make_unique<DateTimeFormat>(locales, options);
525 options.insert({ "dayPeriod", "long" });
526 std::unique_ptr<DateTimeFormat> dateFormatDayPeriod = std::make_unique<DateTimeFormat>(locales, options);
527 options.insert({ "dayPeriod", "narrow" });
528 std::unique_ptr<DateTimeFormat> dateFormatDayPeriod2 = std::make_unique<DateTimeFormat>(locales, options);
529 options.insert({ "dateStyle", "long" });
530 std::unique_ptr<DateTimeFormat> dateFormatFake2 = std::make_unique<DateTimeFormat>(locales, options);
531 delete dateFormat;
532 }
533
534 /**
535 * @tc.name: IntlFuncTest0013
536 * @tc.desc: Test Intl LocaleInfo
537 * @tc.type: FUNC
538 */
539 HWTEST_F(IntlTest, IntlFuncTest0013, TestSize.Level1)
540 {
541 string locale = "en-CN";
542 vector<string> locales;
543 locales.push_back(locale);
544 map<string, string> options = {};
545 std::string expects = "123,456.789";
546 NumberFormat *numFmt = new (std::nothrow) NumberFormat(locales, options);
547 if (!numFmt) {
548 EXPECT_TRUE(false);
549 return;
550 }
551 string out = numFmt->Format(123456.789);
552 EXPECT_EQ(out, expects);
553 delete numFmt;
554 }
555
556 /**
557 * @tc.name: IntlFuncTest0014
558 * @tc.desc: Test Intl LocaleInfo
559 * @tc.type: FUNC
560 */
561 HWTEST_F(IntlTest, IntlFuncTest0014, TestSize.Level1)
562 {
563 string locale = "zh-CN-u-hc-h12";
564 string expects = "北美太平洋标准时间";
565 vector<string> locales;
566 locales.push_back("jessie");
567 locales.push_back(locale);
568 map<string, string> options = { { "timeZone", "America/Los_Angeles" }, { "timeZoneName", "long" } };
569 DateTimeFormat *dateFormat = new (std::nothrow) DateTimeFormat(locales, options);
570 if (!dateFormat) {
571 EXPECT_TRUE(false);
572 return;
573 }
574 int64_t milliseconds = 123456789;
575 string out = dateFormat->Format(milliseconds);
576 EXPECT_TRUE(out.find(expects) != out.npos);
577 delete dateFormat;
578 }
579
580 /**
581 * @tc.name: IntlFuncTest0015
582 * @tc.desc: Test Intl LocaleInfo
583 * @tc.type: FUNC
584 */
585 HWTEST_F(IntlTest, IntlFuncTest0015, TestSize.Level1)
586 {
587 string locale = "zh-CN-u-hc-h12";
588 vector<string> locales;
589 locales.push_back("jessie");
590 locales.push_back(locale);
591 map<string, string> options = {
592 { "timeZone", "America/Los_Angeles" },
593 { "timeZoneName", "short" }
594 };
595 DateTimeFormat *dateFormat = new (std::nothrow) DateTimeFormat(locales, options);
596 if (!dateFormat) {
597 EXPECT_TRUE(false);
598 return;
599 }
600 EXPECT_EQ(dateFormat->GetTimeZone(), "America/Los_Angeles");
601 EXPECT_EQ(dateFormat->GetTimeZoneName(), "short");
602 delete dateFormat;
603 }
604
605 /**
606 * @tc.name: IntlFuncTest0016
607 * @tc.desc: Test Intl Collator
608 * @tc.type: FUNC
609 */
610 HWTEST_F(IntlTest, IntlFuncTest0016, TestSize.Level1)
611 {
612 // normal test case
613 vector<string> locales;
614 locales.push_back("en-US");
615 map<string, string> inputOptions;
616 std::unique_ptr<Collator> collator = std::make_unique<Collator>(locales, inputOptions);
617 const string param1 = "abc";
618 const string param2 = "cba";
619 CompareResult result = collator->Compare(param1, param2);
620 EXPECT_EQ(result, CompareResult::SMALLER);
621 vector<string> localeVec;
622 localeVec.push_back("en-US-u-co-$$@");
623 std::unique_ptr<Collator> collatorPtr = std::make_unique<Collator>(localeVec, inputOptions);
624
625 map<string, string> options;
626 collator->ResolvedOptions(options);
627 EXPECT_EQ(options.size(), 8);
628 map<string, string>::iterator it = options.find("localeMatcher");
629 if (it != options.end()) {
630 EXPECT_EQ(it->second, "best fit");
631 }
632 it = options.find("locale");
633 if (it != options.end()) {
634 EXPECT_EQ(it->second, "en-US");
635 }
636 it = options.find("usage");
637 if (it != options.end()) {
638 EXPECT_EQ(it->second, "sort");
639 }
640 it = options.find("sensitivity");
641 if (it != options.end()) {
642 EXPECT_EQ(it->second, "variant");
643 }
644 it = options.find("ignorePunctuation");
645 if (it != options.end()) {
646 EXPECT_EQ(it->second, "false");
647 }
648 it = options.find("numeric");
649 if (it != options.end()) {
650 EXPECT_EQ(it->second, "false");
651 }
652 it = options.find("caseFirst");
653 if (it != options.end()) {
654 EXPECT_EQ(it->second, "false");
655 }
656 it = options.find("collation");
657 if (it != options.end()) {
658 EXPECT_EQ(it->second, "default");
659 }
660 }
661
662 /**
663 * @tc.name: IntlFuncTest0017
664 * @tc.desc: Test Intl Collator
665 * @tc.type: FUNC
666 */
667 HWTEST_F(IntlTest, IntlFuncTest0017, TestSize.Level1)
668 {
669 // normal test case
670 vector<string> locales;
671 locales.push_back("zh-Hans");
672 locales.push_back("en-US");
673 map<string, string> inputOptions = {
674 { "localeMatcher", "lookup" },
675 { "usage", "search"},
676 { "sensitivity", "case"},
677 { "ignorePunctuation", "true" },
678 { "collation", "pinyin"},
679 { "numeric", "true"},
680 { "caseFirst", "upper"}
681 };
682 Collator *collator = new Collator(locales, inputOptions);
683 const string param1 = "啊";
684 const string param2 = "播";
685 CompareResult result = collator->Compare(param1, param2);
686 EXPECT_EQ(result, CompareResult::SMALLER);
687 map<string, string> options;
688 collator->ResolvedOptions(options);
689 EXPECT_EQ(options.size(), 8);
690 map<string, string>::iterator it = options.find("localeMatcher");
691 if (it != options.end()) {
692 EXPECT_EQ(it->second, "lookup");
693 }
694 it = options.find("locale");
695 if (it != options.end()) {
696 EXPECT_EQ(it->second, "zh-Hans");
697 }
698 it = options.find("usage");
699 if (it != options.end()) {
700 EXPECT_EQ(it->second, "search");
701 }
702 it = options.find("sensitivity");
703 if (it != options.end()) {
704 EXPECT_EQ(it->second, "case");
705 }
706 delete collator;
707 }
708
709 /**
710 * @tc.name: IntlFuncTest0018
711 * @tc.desc: Test Intl Collator
712 * @tc.type: FUNC
713 */
714 HWTEST_F(IntlTest, IntlFuncTest0018, TestSize.Level1)
715 {
716 // normal test case
717 vector<string> locales;
718 locales.push_back("zh-Hans-u-co-pinyin");
719 locales.push_back("en-US");
720 map<string, string> inputOptions = {
721 { "ignorePunctuation", "true" },
722 { "collation", "pinyin"},
723 { "numeric", "true"},
724 { "caseFirst", "upper"}
725 };
726 Collator *collator = new Collator(locales, inputOptions);
727 map<string, string> options;
728 collator->ResolvedOptions(options);
729 map<string, string>::iterator it = options.find("ignorePunctuation");
730 if (it != options.end()) {
731 EXPECT_EQ(it->second, "true");
732 }
733 it = options.find("numeric");
734 if (it != options.end()) {
735 EXPECT_EQ(it->second, "true");
736 }
737 it = options.find("caseFirst");
738 if (it != options.end()) {
739 EXPECT_EQ(it->second, "upper");
740 }
741 it = options.find("collation");
742 if (it != options.end()) {
743 EXPECT_EQ(it->second, "pinyin");
744 }
745 inputOptions = {
746 { "sensitivity", "base"},
747 { "caseFirst", "lower"}
748 };
749 Collator *collator2 = new Collator(locales, inputOptions);
750 inputOptions = {
751 { "sensitivity", "accent"},
752 { "caseFirst", "lower"}
753 };
754 Collator *collator3 = new Collator(locales, inputOptions);
755 delete collator;
756 delete collator2;
757 delete collator3;
758 }
759
760 /**
761 * @tc.name: IntlFuncTest0019
762 * @tc.desc: Test Intl Collator
763 * @tc.type: FUNC
764 */
765 HWTEST_F(IntlTest, IntlFuncTest0019, TestSize.Level1)
766 {
767 // abnormal test case
768 vector<string> locales;
769 locales.push_back("7776@");
770 map<string, string> inputOptions = {
771 { "localeMatcher", "fake value" },
772 { "usage", "fake value"},
773 { "sensitivity", "fake value"},
774 { "ignorePunctuation", "fake value" },
775 { "collation", "fake value"},
776 { "numeric", "fake value"},
777 { "caseFirst", "fake value"},
778 { "fake key", "fake value"}
779 };
780 Collator *collator = new Collator(locales, inputOptions);
781 const string param1 = "abc";
782 const string param2 = "cba";
783 CompareResult result = collator->Compare(param1, param2);
784 EXPECT_EQ(result, CompareResult::SMALLER);
785
786 map<string, string> options;
787 collator->ResolvedOptions(options);
788 EXPECT_EQ(options.size(), 8);
789 map<string, string>::iterator it = options.find("localeMatcher");
790 if (it != options.end()) {
791 EXPECT_EQ(it->second, "fake value");
792 }
793 std::string systemLocale = LocaleConfig::GetSystemLocale();
794 it = options.find("locale");
795 if (it != options.end()) {
796 EXPECT_EQ(it->second, systemLocale);
797 }
798 it = options.find("usage");
799 if (it != options.end()) {
800 EXPECT_EQ(it->second, "fake value");
801 }
802 it = options.find("sensitivity");
803 if (it != options.end()) {
804 EXPECT_EQ(it->second, "fake value");
805 }
806 delete collator;
807 }
808
809 /**
810 * @tc.name: IntlFuncTest0020
811 * @tc.desc: Test Intl Collator
812 * @tc.type: FUNC
813 */
814 HWTEST_F(IntlTest, IntlFuncTest0020, TestSize.Level1)
815 {
816 // abnormal test case
817 vector<string> locales;
818 locales.push_back("$$##");
819 map<string, string> inputOptions = {
820 { "ignorePunctuation", "fake value" },
821 { "collation", "fake value"},
822 { "numeric", "fake value"},
823 { "caseFirst", "fake value"},
824 { "fake key", "fake value"}
825 };
826 Collator *collator = new Collator(locales, inputOptions);
827 map<string, string> options;
828 collator->ResolvedOptions(options);
829 map<string, string>::iterator it = options.find("ignorePunctuation");
830 if (it != options.end()) {
831 EXPECT_EQ(it->second, "fake value");
832 }
833 it = options.find("numeric");
834 if (it != options.end()) {
835 EXPECT_EQ(it->second, "fake value");
836 }
837 it = options.find("caseFirst");
838 if (it != options.end()) {
839 EXPECT_EQ(it->second, "fake value");
840 }
841 it = options.find("collation");
842 if (it != options.end()) {
843 EXPECT_EQ(it->second, "default");
844 }
845 delete collator;
846 }
847
848 /**
849 * @tc.name: IntlFuncTest0021
850 * @tc.desc: Test Intl PluralRules
851 * @tc.type: FUNC
852 */
853 HWTEST_F(IntlTest, IntlFuncTest0021, TestSize.Level1)
854 {
855 // normal test case
856 vector<string> locales;
857 locales.push_back("en-US");
858 map<string, string> options;
859 PluralRules *plurals = new PluralRules(locales, options);
860 string res = plurals->Select(0);
861 EXPECT_EQ(res, "other");
862 res = plurals->Select(1);
863 EXPECT_EQ(res, "one");
864 res = plurals->Select(2);
865 EXPECT_EQ(res, "other");
866 res = plurals->Select(5);
867 EXPECT_EQ(res, "other");
868 res = plurals->Select(20);
869 EXPECT_EQ(res, "other");
870 res = plurals->Select(200);
871 EXPECT_EQ(res, "other");
872 res = plurals->Select(12.34);
873 EXPECT_EQ(res, "other");
874 delete plurals;
875 }
876
877 /**
878 * @tc.name: IntlFuncTest0022
879 * @tc.desc: Test Intl PluralRules
880 * @tc.type: FUNC
881 */
882 HWTEST_F(IntlTest, IntlFuncTest0022, TestSize.Level1)
883 {
884 // normal test case
885 vector<string> locales;
886 locales.push_back("ar");
887 map<string, string> options;
888 PluralRules *plurals = new PluralRules(locales, options);
889 string res = plurals->Select(0);
890 EXPECT_EQ(res, "zero");
891 res = plurals->Select(1);
892 EXPECT_EQ(res, "one");
893 res = plurals->Select(2);
894 EXPECT_EQ(res, "two");
895 res = plurals->Select(5);
896 EXPECT_EQ(res, "few");
897 res = plurals->Select(20);
898 EXPECT_EQ(res, "many");
899 res = plurals->Select(200);
900 EXPECT_EQ(res, "other");
901 res = plurals->Select(12.34);
902 EXPECT_EQ(res, "other");
903 options = {
904 { "localeMatcher", "best fit" },
905 { "type", "cardinal" },
906 { "minimumIntegerDigits", "1" },
907 { "minimumFractionDigits", "0" },
908 { "maximumFractionDigits", "21" },
909 };
910 PluralRules *pluralRules = new PluralRules(locales, options);
911 delete pluralRules;
912 delete plurals;
913 }
914
915 /**
916 * @tc.name: IntlFuncTest0023
917 * @tc.desc: Test Intl PluralRules
918 * @tc.type: FUNC
919 */
920 HWTEST_F(IntlTest, IntlFuncTest0023, TestSize.Level1)
921 {
922 // normal test case
923 vector<string> locales;
924 locales.push_back("ar");
925 map<string, string> options = {
926 { "localeMatcher", "best fit" },
927 { "type", "cardinal" },
928 { "minimumIntegerDigits", "1" },
929 { "minimumFractionDigits", "0" },
930 { "maximumFractionDigits", "21" },
931 { "minimumSignificantDigits", "21" },
932 { "maximumSignificantDigits", "21" },
933 };
934 PluralRules *plurals = new PluralRules(locales, options);
935 string res = plurals->Select(0);
936 EXPECT_EQ(res, "zero");
937 res = plurals->Select(1);
938 EXPECT_EQ(res, "one");
939 res = plurals->Select(2);
940 EXPECT_EQ(res, "two");
941 res = plurals->Select(5);
942 EXPECT_EQ(res, "few");
943 res = plurals->Select(20);
944 EXPECT_EQ(res, "many");
945 res = plurals->Select(200);
946 EXPECT_EQ(res, "other");
947 res = plurals->Select(12.34);
948 EXPECT_EQ(res, "other");
949 delete plurals;
950 }
951
952 /**
953 * @tc.name: IntlFuncTest0024
954 * @tc.desc: Test Intl PluralRules
955 * @tc.type: FUNC
956 */
957 HWTEST_F(IntlTest, IntlFuncTest0024, TestSize.Level1)
958 {
959 // abnormal test cast
960 // normal test case
961 std::string currentSystemLocale = LocaleConfig::GetSystemLocale();
962 InitTestEnvironment();
963 vector<string> locales;
964 locales.push_back("$$$###");
965 map<string, string> options = {
966 { "fake_localeMatcher", "best fit" },
967 { "type", "fake_cardinal" },
968 { "minimumIntegerDigits", "11111" },
969 { "minimumFractionDigits", "-111" },
970 { "maximumFractionDigits", "-111" },
971 { "minimumSignificantDigits", "11111" },
972 { "maximumSignificantDigits", "11111" },
973 };
974 PluralRules *plurals = new PluralRules(locales, options);
975 string res = plurals->Select(0);
976 EXPECT_EQ(res, "other");
977 res = plurals->Select(1);
978 EXPECT_EQ(res, "other");
979 res = plurals->Select(2);
980 EXPECT_EQ(res, "other");
981 res = plurals->Select(5);
982 EXPECT_EQ(res, "other");
983 res = plurals->Select(20);
984 EXPECT_EQ(res, "other");
985 res = plurals->Select(200);
986 EXPECT_EQ(res, "other");
987 res = plurals->Select(12.34);
988 EXPECT_EQ(res, "other");
989 delete plurals;
990 RestoreEnvironment(currentSystemLocale);
991 }
992
993 /**
994 * @tc.name: IntlFuncTest0025
995 * @tc.desc: Test Intl RelativeTimeFormat
996 * @tc.type: FUNC
997 */
998 HWTEST_F(IntlTest, IntlFuncTest0025, TestSize.Level1)
999 {
1000 vector<string> locales;
1001 locales.push_back("en-US");
1002 map<string, string> options;
1003 RelativeTimeFormat *formatter = new RelativeTimeFormat(locales, options);
1004
1005 double number = 2022;
1006 string unit = "year";
1007 string res = formatter->Format(number, unit);
1008 EXPECT_EQ(res, "in 2,022 years");
1009 string fakeUnit = "abc";
1010 EXPECT_EQ(formatter->Format(number, fakeUnit), "");
1011 vector<vector<string>> timeVector;
1012 formatter->FormatToParts(number, unit, timeVector);
1013 EXPECT_EQ(timeVector.size(), 5);
1014
1015 number = 3;
1016 unit = "quarter";
1017 res = formatter->Format(number, unit);
1018 EXPECT_EQ(res, "in 3 quarters");
1019 formatter->FormatToParts(number, unit, timeVector);
1020 EXPECT_EQ(timeVector.size(), 8);
1021
1022 number = 11;
1023 unit = "month";
1024 res = formatter->Format(number, unit);
1025 EXPECT_EQ(res, "in 11 months");
1026 formatter->FormatToParts(number, unit, timeVector);
1027 EXPECT_EQ(timeVector.size(), 11);
1028
1029 number = 2;
1030 unit = "week";
1031 res = formatter->Format(number, unit);
1032 EXPECT_EQ(res, "in 2 weeks");
1033 formatter->FormatToParts(number, unit, timeVector);
1034 EXPECT_EQ(timeVector.size(), 14);
1035
1036 number = 18;
1037 unit = "day";
1038 res = formatter->Format(number, unit);
1039 EXPECT_EQ(res, "in 18 days");
1040 formatter->FormatToParts(number, unit, timeVector);
1041 EXPECT_EQ(timeVector.size(), 17);
1042
1043 delete formatter;
1044 }
1045
1046 /**
1047 * @tc.name: IntlFuncTest0026
1048 * @tc.desc: Test Intl RelativeTimeFormat
1049 * @tc.type: FUNC
1050 */
1051 HWTEST_F(IntlTest, IntlFuncTest0026, TestSize.Level1)
1052 {
1053 vector<string> locales;
1054 locales.push_back("en-US");
1055 map<string, string> options;
1056 RelativeTimeFormat *formatter = new RelativeTimeFormat(locales, options);
1057
1058 double number = 23;
1059 string unit = "hour";
1060 string res = formatter->Format(number, unit);
1061 EXPECT_EQ(res, "in 23 hours");
1062 vector<vector<string>> timeVector;
1063 formatter->FormatToParts(number, unit, timeVector);
1064 EXPECT_EQ(timeVector.size(), 3);
1065
1066 number = 59;
1067 unit = "minute";
1068 res = formatter->Format(number, unit);
1069 EXPECT_EQ(res, "in 59 minutes");
1070 formatter->FormatToParts(number, unit, timeVector);
1071 EXPECT_EQ(timeVector.size(), 6);
1072
1073 number = 1;
1074 unit = "second";
1075 res = formatter->Format(number, unit);
1076 EXPECT_EQ(res, "in 1 second");
1077 formatter->FormatToParts(number, unit, timeVector);
1078 EXPECT_EQ(timeVector.size(), 9);
1079 string fakeUnit = "abc";
1080 vector<vector<string>> fakeVector;
1081 formatter->FormatToParts(number, fakeUnit, fakeVector);
1082 EXPECT_EQ(fakeVector.size(), 0);
1083
1084 delete formatter;
1085 }
1086
1087 /**
1088 * @tc.name: IntlFuncTest0027
1089 * @tc.desc: Test Intl RelativeTimeFormat
1090 * @tc.type: FUNC
1091 */
1092 HWTEST_F(IntlTest, IntlFuncTest0027, TestSize.Level1)
1093 {
1094 vector<string> locales;
1095 locales.push_back("zh-Hans");
1096 map<string, string> options = {
1097 { "localeMatcher", "best fit" },
1098 { "numeric", "auto" },
1099 { "style", "long" }
1100 };
1101 RelativeTimeFormat *formatter = new RelativeTimeFormat(locales, options);
1102
1103 double number = 2022;
1104 string unit = "year";
1105 string res = formatter->Format(number, unit);
1106 EXPECT_EQ(res, "2,022年后");
1107 vector<vector<string>> timeVector;
1108 formatter->FormatToParts(number, unit, timeVector);
1109 EXPECT_EQ(timeVector.size(), 4);
1110
1111 number = 3;
1112 unit = "quarter";
1113 res = formatter->Format(number, unit);
1114 EXPECT_EQ(res, "3个季度后");
1115 formatter->FormatToParts(number, unit, timeVector);
1116 EXPECT_EQ(timeVector.size(), 6);
1117
1118 number = 11;
1119 unit = "month";
1120 res = formatter->Format(number, unit);
1121 EXPECT_EQ(res, "11个月后");
1122 formatter->FormatToParts(number, unit, timeVector);
1123 EXPECT_EQ(timeVector.size(), 8);
1124
1125 number = 2;
1126 unit = "week";
1127 res = formatter->Format(number, unit);
1128 EXPECT_EQ(res, "2周后");
1129 formatter->FormatToParts(number, unit, timeVector);
1130 EXPECT_EQ(timeVector.size(), 10);
1131 delete formatter;
1132 locales.clear();
1133 locales.push_back("##$$");
1134 RelativeTimeFormat *fmt = new RelativeTimeFormat(locales, options);
1135 delete fmt;
1136 }
1137
1138 /**
1139 * @tc.name: IntlFuncTest0028
1140 * @tc.desc: Test Intl RelativeTimeFormat
1141 * @tc.type: FUNC
1142 */
1143 HWTEST_F(IntlTest, IntlFuncTest0028, TestSize.Level1)
1144 {
1145 vector<string> locales;
1146 locales.push_back("zh-Hans");
1147 map<string, string> options = {
1148 { "localeMatcher", "lookup" },
1149 { "numeric", "auto" },
1150 { "style", "long" }
1151 };
1152 RelativeTimeFormat *formatter = new RelativeTimeFormat(locales, options);
1153
1154 double number = 18;
1155 string unit = "day";
1156 string res = formatter->Format(number, unit);
1157 EXPECT_EQ(res, "18天后");
1158 vector<vector<string>> timeVector;
1159 formatter->FormatToParts(number, unit, timeVector);
1160 EXPECT_EQ(timeVector.size(), 2);
1161
1162 number = 23;
1163 unit = "hour";
1164 res = formatter->Format(number, unit);
1165 EXPECT_EQ(res, "23小时后");
1166 formatter->FormatToParts(number, unit, timeVector);
1167 EXPECT_EQ(timeVector.size(), 4);
1168
1169 number = 59;
1170 unit = "minute";
1171 res = formatter->Format(number, unit);
1172 EXPECT_EQ(res, "59分钟后");
1173 formatter->FormatToParts(number, unit, timeVector);
1174 EXPECT_EQ(timeVector.size(), 6);
1175
1176 number = 1;
1177 unit = "second";
1178 res = formatter->Format(number, unit);
1179 EXPECT_EQ(res, "1秒钟后");
1180 formatter->FormatToParts(number, unit, timeVector);
1181 EXPECT_EQ(timeVector.size(), 8);
1182
1183 delete formatter;
1184 }
1185
1186 /**
1187 * @tc.name: IntlFuncTest0029
1188 * @tc.desc: Test Intl RelativeTimeFormat
1189 * @tc.type: FUNC
1190 */
1191 HWTEST_F(IntlTest, IntlFuncTest0029, TestSize.Level1)
1192 {
1193 vector<string> locales;
1194 locales.push_back("####");
1195 locales.push_back("zh-Hans-u-nu-latn");
1196 map<string, string> options = {
1197 { "localeMatcher", "best fit" },
1198 { "numeric", "auto" },
1199 { "style", "long" }
1200 };
1201 RelativeTimeFormat *formatter = new RelativeTimeFormat(locales, options);
1202 map<string, string> res;
1203 formatter->GetResolvedOptions(res);
1204 EXPECT_EQ(res.size(), 4);
1205
1206 map<string, string>::iterator it = res.find("locale");
1207 if (it != res.end()) {
1208 EXPECT_EQ(it->second, "zh-Hans");
1209 }
1210 it = res.find("style");
1211 if (it != res.end()) {
1212 EXPECT_EQ(it->second, "long");
1213 }
1214 it = res.find("numeric");
1215 if (it != res.end()) {
1216 EXPECT_EQ(it->second, "auto");
1217 }
1218 it = res.find("numberingSystem");
1219 if (it != res.end()) {
1220 EXPECT_EQ(it->second, "latn");
1221 }
1222 delete formatter;
1223 }
1224
1225 /**
1226 * @tc.name: IntlFuncTest0030
1227 * @tc.desc: Test Intl NumberFormat
1228 * @tc.type: FUNC
1229 */
1230 HWTEST_F(IntlTest, IntlFuncTest0030, TestSize.Level1)
1231 {
1232 vector<string> locales;
1233 locales.push_back("en-US");
1234 map<string, string> options = {
1235 { "unitUsage", "default" }
1236 };
1237 NumberFormat *formatter = new NumberFormat(locales, options);
1238
1239 string res = formatter->Format(123);
1240 EXPECT_EQ(res, "123");
1241 res = formatter->Format(123.456);
1242 EXPECT_EQ(res, "123.456");
1243 locales.clear();
1244 locales.push_back("$$@@");
1245 NumberFormat *formatter2 = new NumberFormat(locales, options);
1246 string res2 = formatter2->Format(123);
1247 EXPECT_EQ(res2, "123");
1248 delete formatter;
1249 delete formatter2;
1250 }
1251
1252 /**
1253 * @tc.name: IntlFuncTest0031
1254 * @tc.desc: Test Intl NumberFormat
1255 * @tc.type: FUNC
1256 */
1257 HWTEST_F(IntlTest, IntlFuncTest0031, TestSize.Level1)
1258 {
1259 vector<string> locales;
1260 locales.push_back("zh-Hans");
1261 map<string, string> options = {
1262 { "locale", "zh-Hans" },
1263 { "currency", "EUR" },
1264 { "currencySign", "narrowSymbol" },
1265 { "currencyDisplay", "symbol" },
1266 { "unit", "meter" },
1267 { "unitDisplay", "long" },
1268 { "unitUsage", "length-person" },
1269 { "signDisplay", "always" },
1270 { "compactDisplay", "long" },
1271 { "notation", "standard" },
1272 { "localeMatcher", "lookup" },
1273 { "style", "decimal" },
1274 { "numberingSystem", "latn" },
1275 { "useGroup", "true" },
1276 { "minimumIntegerDigits", "1" },
1277 { "minimumFractionDigits", "0" },
1278 { "maximumFractionDigits", "20" },
1279 { "minimumSignificantDigits", "1" },
1280 { "maximumSignificantDigits", "20" }
1281 };
1282 NumberFormat *formatter = new NumberFormat(locales, options);
1283
1284 string formatRes = formatter->Format(123);
1285 EXPECT_EQ(formatRes, "+123");
1286 formatRes = formatter->Format(123.456);
1287 EXPECT_EQ(formatRes, "+123.456");
1288
1289 map<string, string> res;
1290 formatter->GetResolvedOptions(res);
1291 EXPECT_EQ(res.size(), 12);
1292 map<string, string>::iterator it = res.find("locale");
1293 if (it != res.end()) {
1294 EXPECT_EQ(it->second, "zh-Hans");
1295 }
1296 it = res.find("signDisplay");
1297 if (it != res.end()) {
1298 EXPECT_EQ(it->second, "always");
1299 }
1300 it = res.find("notation");
1301 if (it != res.end()) {
1302 EXPECT_EQ(it->second, "standard");
1303 }
1304 delete formatter;
1305 }
1306
1307 /**
1308 * @tc.name: IntlFuncTest0032
1309 * @tc.desc: Test Intl NumberFormat
1310 * @tc.type: FUNC
1311 */
1312 HWTEST_F(IntlTest, IntlFuncTest0032, TestSize.Level1)
1313 {
1314 vector<string> locales;
1315 locales.push_back("zh-Hans");
1316 map<string, string> options = {
1317 { "locale", "zh-Hans" },
1318 { "currency", "CNY" },
1319 { "currencySign", "symbol" },
1320 { "currencyDisplay", "symbol" },
1321 { "unit", "meter" },
1322 { "unitDisplay", "long" },
1323 { "unitUsage", "length-person" },
1324 { "signDisplay", "always" },
1325 { "compactDisplay", "long" },
1326 { "notation", "standard" },
1327 { "localeMatcher", "best fit" },
1328 { "style", "decimal" },
1329 { "numberingSystem", "latn" },
1330 { "useGroup", "true" },
1331 { "minimumIntegerDigits", "1" },
1332 { "minimumFractionDigits", "0" },
1333 { "maximumFractionDigits", "21" },
1334 { "minimumSignificantDigits", "1" },
1335 { "maximumSignificantDigits", "21" }
1336 };
1337 NumberFormat *formatter = new NumberFormat(locales, options);
1338 map<string, string> res;
1339 formatter->GetResolvedOptions(res);
1340 map<string, string>::iterator it = res.find("style");
1341 if (it != res.end()) {
1342 EXPECT_EQ(it->second, "decimal");
1343 }
1344 it = res.find("numberingSystem");
1345 if (it != res.end()) {
1346 EXPECT_EQ(it->second, "latn");
1347 }
1348 it = res.find("useGrouping");
1349 if (it != res.end()) {
1350 EXPECT_EQ(it->second, "true");
1351 }
1352 it = res.find("minimumIntegerDigits");
1353 if (it != res.end()) {
1354 EXPECT_EQ(it->second, "1");
1355 }
1356 it = res.find("minimumFractionDigits");
1357 if (it != res.end()) {
1358 EXPECT_EQ(it->second, "0");
1359 }
1360 delete formatter;
1361 }
1362
1363 /**
1364 * @tc.name: IntlFuncTest0033
1365 * @tc.desc: Test Intl NumberFormat
1366 * @tc.type: FUNC
1367 */
1368 HWTEST_F(IntlTest, IntlFuncTest0033, TestSize.Level1)
1369 {
1370 vector<string> locales;
1371 locales.push_back("zh-Hans");
1372 map<string, string> options = {
1373 { "locale", "@@##" },
1374 { "currency", "fake currency" },
1375 { "currencySign", "fake sign" },
1376 { "currencyDisplay", "symbol" },
1377 { "unit", "meter" },
1378 { "unitDisplay", "fake value" },
1379 { "unitUsage", "length-person" },
1380 { "signDisplay", "always" },
1381 { "compactDisplay", "long" },
1382 { "notation", "fake value" },
1383 { "localeMatcher", "best fit" },
1384 { "style", "decimal" },
1385 { "numberingSystem", "latn" },
1386 { "useGroup", "fake value" },
1387 { "minimumIntegerDigits", "1" },
1388 { "minimumFractionDigits", "0" },
1389 { "maximumFractionDigits", "21" },
1390 { "minimumSignificantDigits", "1" },
1391 { "maximumSignificantDigits", "21" }
1392 };
1393 NumberFormat *formatter = new NumberFormat(locales, options);
1394 map<string, string> res;
1395 formatter->GetResolvedOptions(res);
1396 map<string, string>::iterator it = res.find("maximumFractionDigits");
1397 if (it != res.end()) {
1398 EXPECT_EQ(it->second, "21");
1399 }
1400 it = res.find("minimumSignificantDigits");
1401 if (it != res.end()) {
1402 EXPECT_EQ(it->second, "1");
1403 }
1404 it = res.find("maximumSignificantDigits");
1405 if (it != res.end()) {
1406 EXPECT_EQ(it->second, "21");
1407 }
1408 delete formatter;
1409 }
1410
1411 /**
1412 * @tc.name: IntlFuncTest0034
1413 * @tc.desc: Test Intl NumberFormat
1414 * @tc.type: FUNC
1415 */
1416 HWTEST_F(IntlTest, IntlFuncTest0034, TestSize.Level1)
1417 {
1418 vector<string> locales;
1419 locales.push_back("en-US");
1420 map<string, string> options = {
1421 { "locale", "$$$##43" },
1422 { "currency", "USD" },
1423 { "currencySign", "symbol" },
1424 { "currencyDisplay", "symbol" },
1425 { "unit", "fake unit" },
1426 { "unitDisplay", "long" },
1427 { "unitUsage", "fake usage" },
1428 { "signDisplay", "always" },
1429 { "compactDisplay", "long" },
1430 { "notation", "standard" },
1431 { "localeMatcher", "lookup" },
1432 { "style", "currency" },
1433 { "numberingSystem", "latn" },
1434 { "useGrouping", "true" },
1435 { "minimumIntegerDigits", "1" },
1436 { "minimumFractionDigits", "0" },
1437 { "maximumFractionDigits", "20" },
1438 { "minimumSignificantDigits", "1" },
1439 { "maximumSignificantDigits", "20" }
1440 };
1441 NumberFormat *formatter = new NumberFormat(locales, options);
1442 string res = formatter->Format(123);
1443 EXPECT_EQ(res, "+$123");
1444 res = formatter->Format(123.456);
1445 EXPECT_EQ(res, "+$123.456");
1446 res = formatter->GetCurrency();
1447 EXPECT_EQ(res, "USD");
1448 res = formatter->GetCurrencySign();
1449 EXPECT_EQ(res, "symbol");
1450 res = formatter->GetStyle();
1451 EXPECT_EQ(res, "currency");
1452 res = formatter->GetNumberingSystem();
1453 EXPECT_EQ(res, "latn");
1454 delete formatter;
1455 }
1456
1457 /**
1458 * @tc.name: IntlFuncTest0035
1459 * @tc.desc: Test Intl NumberFormat
1460 * @tc.type: FUNC
1461 */
1462 HWTEST_F(IntlTest, IntlFuncTest0035, TestSize.Level1)
1463 {
1464 vector<string> locales;
1465 locales.push_back("en-US");
1466 map<string, string> options = {
1467 { "locale", "fake locale" },
1468 { "currency", "USD" },
1469 { "currencySign", "fake sign" },
1470 { "currencyDisplay", "symbol" },
1471 { "unit", "fake unit" },
1472 { "unitDisplay", "long" },
1473 { "unitUsage", "length-person" },
1474 { "signDisplay", "fake display" },
1475 { "compactDisplay", "long" },
1476 { "notation", "standard" },
1477 { "localeMatcher", "lookup" },
1478 { "style", "currency" },
1479 { "numberingSystem", "latn" },
1480 { "useGrouping", "false" },
1481 { "minimumIntegerDigits", "1" },
1482 { "minimumFractionDigits", "0" },
1483 { "maximumFractionDigits", "17" },
1484 { "minimumSignificantDigits", "1" },
1485 { "maximumSignificantDigits", "17" }
1486 };
1487 NumberFormat *formatter = new NumberFormat(locales, options);
1488 string res = formatter->GetUseGrouping();
1489 EXPECT_EQ(res, "false");
1490 res = formatter->GetMinimumIntegerDigits();
1491 EXPECT_EQ(res, "1");
1492 res = formatter->GetMinimumFractionDigits();
1493 EXPECT_EQ(res, "0");
1494 res = formatter->GetMaximumFractionDigits();
1495 EXPECT_EQ(res, "17");
1496 res = formatter->GetMinimumSignificantDigits();
1497 EXPECT_EQ(res, "1");
1498 res = formatter->GetMaximumSignificantDigits();
1499 EXPECT_EQ(res, "17");
1500 delete formatter;
1501 }
1502
1503 /**
1504 * @tc.name: IntlFuncTest0036
1505 * @tc.desc: Test Intl DateTimeFormat
1506 * @tc.type: FUNC
1507 */
1508 HWTEST_F(IntlTest, IntlFuncTest0036, TestSize.Level1)
1509 {
1510 vector<string> locales;
1511 locales.push_back("en-US");
1512 map<string, string> options = {
1513 { "dateStyle", "short" }
1514 };
1515 DateTimeFormat *formatter = new DateTimeFormat(locales, options);
1516
1517 int64_t milliseconds = 123456789;
1518 string res = formatter->Format(milliseconds);
1519 EXPECT_EQ(res, "1/2/70");
1520
1521 int64_t milliseconds2 = 987654321;
1522 res = formatter->FormatRange(milliseconds, milliseconds2);
1523 EXPECT_EQ(res, "1/2/70 \xE2\x80\x93 1/12/70");
1524 delete formatter;
1525 }
1526
1527 /**
1528 * @tc.name: IntlFuncTest0037
1529 * @tc.desc: Test Intl DateTimeFormat
1530 * @tc.type: FUNC
1531 */
1532 HWTEST_F(IntlTest, IntlFuncTest0037, TestSize.Level1)
1533 {
1534 vector<string> locales;
1535 locales.push_back("zh-CN");
1536 map<string, string> options = {
1537 { "locale", "zh-CN" },
1538 { "dateStyle", "medium" },
1539 { "timeStyle", "long" },
1540 { "hourCycle", "h24" },
1541 { "timeZone", "Asia/Shanghai" },
1542 { "numberingSystem", "latn" },
1543 { "hour12", "false" },
1544 { "weekday", "long" },
1545 { "era", "long" },
1546 { "year", "2-digit" },
1547 { "month", "2-digit" },
1548 { "day", "2-digit" },
1549 { "hour", "2-digit" },
1550 { "minute", "2-digit" },
1551 { "second", "2-digit" },
1552 { "timeZoneName", "long" },
1553 { "dayPeriod", "long" },
1554 { "localeMatcher", "lookup" },
1555 { "formatMatcher", "basic" }
1556 };
1557 DateTimeFormat *formatter = new DateTimeFormat(locales, options);
1558
1559 int64_t milliseconds = 123456789;
1560 string res = formatter->Format(milliseconds);
1561 // 2022年12月19日 GMT+8 15:18:24
1562 EXPECT_TRUE(res.length() > 0);
1563
1564 int64_t milliseconds2 = 987654321;
1565 res = formatter->FormatRange(milliseconds, milliseconds2);
1566 // 2022/12/19 GMT+8 15:18:24 \xE2\x80\x93 2023/11/18 GMT+8 14:17:23
1567 EXPECT_TRUE(res.length() > 0);
1568
1569 res = formatter->GetDateStyle();
1570 EXPECT_EQ(res, "medium");
1571 res = formatter->GetTimeStyle();
1572 EXPECT_EQ(res, "long");
1573 res = formatter->GetHourCycle();
1574 EXPECT_EQ(res, "h24");
1575 res = formatter->GetTimeZone();
1576 EXPECT_EQ(res, "Asia/Shanghai");
1577 res = formatter->GetTimeZoneName();
1578 EXPECT_EQ(res, "long");
1579 delete formatter;
1580 }
1581
1582 /**
1583 * @tc.name: IntlFuncTest0038
1584 * @tc.desc: Test Intl DateTimeFormat
1585 * @tc.type: FUNC
1586 */
1587 HWTEST_F(IntlTest, IntlFuncTest0038, TestSize.Level1)
1588 {
1589 vector<string> locales;
1590 locales.push_back("zh-CN");
1591 map<string, string> options = {
1592 { "locale", "zh-CN" },
1593 { "dateStyle", "full" },
1594 { "timeStyle", "full" },
1595 { "hourCycle", "h24" },
1596 { "timeZone", "Asia/Shanghai" },
1597 { "numberingSystem", "latn" },
1598 { "hour12", "false" },
1599 { "weekday", "long" },
1600 { "era", "long" },
1601 { "year", "numeric" },
1602 { "month", "numeric" },
1603 { "day", "numeric" },
1604 { "hour", "numeric" },
1605 { "minute", "numeric" },
1606 { "second", "numeric" },
1607 { "timeZoneName", "long" },
1608 { "dayPeriod", "long" },
1609 { "localeMatcher", "lookup" },
1610 { "formatMatcher", "basic" }
1611 };
1612 DateTimeFormat *formatter = new DateTimeFormat(locales, options);
1613 string res = formatter->GetNumberingSystem();
1614 EXPECT_EQ(res, "latn");
1615 res = formatter->GetHour12();
1616 EXPECT_EQ(res, "false");
1617 res = formatter->GetWeekday();
1618 EXPECT_EQ(res, "long");
1619 res = formatter->GetEra();
1620 EXPECT_EQ(res, "long");
1621 res = formatter->GetYear();
1622 EXPECT_EQ(res, "numeric");
1623 res = formatter->GetMonth();
1624 EXPECT_EQ(res, "numeric");
1625 res = formatter->GetDay();
1626 EXPECT_EQ(res, "numeric");
1627 res = formatter->GetHour();
1628 EXPECT_EQ(res, "numeric");
1629 res = formatter->GetMinute();
1630 EXPECT_EQ(res, "numeric");
1631 res = formatter->GetSecond();
1632 EXPECT_EQ(res, "numeric");
1633 delete formatter;
1634 }
1635
1636 /**
1637 * @tc.name: IntlFuncTest0039
1638 * @tc.desc: Test Intl DateTimeFormat
1639 * @tc.type: FUNC
1640 */
1641 HWTEST_F(IntlTest, IntlFuncTest0039, TestSize.Level1)
1642 {
1643 vector<string> locales;
1644 locales.push_back("en-US");
1645 map<string, string> inputOptions = {
1646 { "locale", "en-US" },
1647 { "dateStyle", "medium" },
1648 { "timeStyle", "long" },
1649 { "hourCycle", "h12" },
1650 { "numberingSystem", "latn" },
1651 { "hour12", "true" },
1652 { "weekday", "long" },
1653 { "era", "long" },
1654 { "year", "2-digit" },
1655 { "month", "2-digit" },
1656 { "day", "2-digit" },
1657 { "hour", "2-digit" },
1658 { "minute", "2-digit" },
1659 { "second", "2-digit" },
1660 { "timeZoneName", "long" },
1661 { "dayPeriod", "long" },
1662 { "localeMatcher", "lookup" },
1663 { "formatMatcher", "basic" }
1664 };
1665 std::unique_ptr<DateTimeFormat> formatter = DateTimeFormat::CreateInstance(locales, inputOptions);
1666 int64_t milliseconds = 123456789;
1667 string res = formatter->Format(milliseconds);
1668 // Dec 19, 2022, 3:18:24 PM GMT+8
1669 EXPECT_TRUE(res.length() > 0);
1670 int64_t milliseconds2 = 987654321;
1671 res = formatter->FormatRange(milliseconds, milliseconds2);
1672 // Dec 19, 2022, 3:18:24 PM GMT+8 \xE2\x80\x93 Nov 18, 2023, 2:17:23 PM GMT+8
1673 EXPECT_TRUE(res.length() > 0);
1674 map<string, string> options;
1675 formatter->GetResolvedOptions(options);
1676 EXPECT_EQ(options.size(), 20);
1677 map<string, string>::iterator it = options.find("locale");
1678 if (it != options.end()) {
1679 EXPECT_EQ(it->second, "en-US");
1680 }
1681 it = options.find("dateStyle");
1682 if (it != options.end()) {
1683 EXPECT_EQ(it->second, "medium");
1684 }
1685 it = options.find("timeStyle");
1686 if (it != options.end()) {
1687 EXPECT_EQ(it->second, "long");
1688 }
1689 }
1690
1691 /**
1692 * @tc.name: IntlFuncTest0040
1693 * @tc.desc: Test Intl DateTimeFormat
1694 * @tc.type: FUNC
1695 */
1696 HWTEST_F(IntlTest, IntlFuncTest0040, TestSize.Level1)
1697 {
1698 vector<string> locales;
1699 locales.push_back("en-GB");
1700 map<string, string> inputOptions = {
1701 { "locale", "en-GB" },
1702 { "dateStyle", "medium" },
1703 { "timeStyle", "long" },
1704 { "hourCycle", "h12" },
1705 { "timeZone", "Asia/Shanghai" },
1706 { "numberingSystem", "latn" },
1707 { "hour12", "true" },
1708 { "weekday", "long" },
1709 { "era", "long" },
1710 { "year", "numeric" },
1711 { "month", "numeric" },
1712 { "day", "numeric" },
1713 { "hour", "numeric" },
1714 { "minute", "numeric" },
1715 { "second", "numeric" },
1716 { "timeZoneName", "long" },
1717 { "dayPeriod", "long" },
1718 { "localeMatcher", "lookup" },
1719 { "formatMatcher", "basic" }
1720 };
1721 std::unique_ptr<DateTimeFormat> formatter = DateTimeFormat::CreateInstance(locales, inputOptions);
1722 map<string, string> options;
1723 formatter->GetResolvedOptions(options);
1724 map<string, string>::iterator it = options.find("hourCycle");
1725 if (it != options.end()) {
1726 EXPECT_EQ(it->second, "h12");
1727 }
1728 it = options.find("timeZone");
1729 if (it != options.end()) {
1730 EXPECT_EQ(it->second, "Asia/Shanghai");
1731 }
1732 it = options.find("numberingSystem");
1733 if (it != options.end()) {
1734 EXPECT_EQ(it->second, "latn");
1735 }
1736 it = options.find("hour12");
1737 if (it != options.end()) {
1738 EXPECT_EQ(it->second, "true");
1739 }
1740 it = options.find("weekday");
1741 if (it != options.end()) {
1742 EXPECT_EQ(it->second, "long");
1743 }
1744 }
1745
1746 /**
1747 * @tc.name: IntlFuncTest0041
1748 * @tc.desc: Test Intl DateTimeFormat
1749 * @tc.type: FUNC
1750 */
1751 HWTEST_F(IntlTest, IntlFuncTest0041, TestSize.Level1)
1752 {
1753 vector<string> locales;
1754 locales.push_back("en-US");
1755 map<string, string> inputOptions = {
1756 { "locale", "en-US" },
1757 { "dateStyle", "medium" },
1758 { "timeStyle", "medium" },
1759 { "hourCycle", "h24" },
1760 { "timeZone", "Asia/Shanghai" },
1761 { "numberingSystem", "latn" },
1762 { "hour12", "false" },
1763 { "weekday", "long" },
1764 { "era", "long" },
1765 { "year", "2-digit" },
1766 { "month", "2-digit" },
1767 { "day", "2-digit" },
1768 { "hour", "2-digit" },
1769 { "minute", "2-digit" },
1770 { "second", "2-digit" },
1771 { "timeZoneName", "long" },
1772 { "dayPeriod", "long" },
1773 { "localeMatcher", "best fit" },
1774 { "formatMatcher", "best fit" }
1775 };
1776 std::unique_ptr<DateTimeFormat> formatter = DateTimeFormat::CreateInstance(locales, inputOptions);
1777 map<string, string> options;
1778 formatter->GetResolvedOptions(options);
1779 map<string, string>::iterator it = options.find("era");
1780 if (it != options.end()) {
1781 EXPECT_EQ(it->second, "long");
1782 }
1783 it = options.find("year");
1784 if (it != options.end()) {
1785 EXPECT_EQ(it->second, "2-digit");
1786 }
1787 it = options.find("month");
1788 if (it != options.end()) {
1789 EXPECT_EQ(it->second, "2-digit");
1790 }
1791 it = options.find("day");
1792 if (it != options.end()) {
1793 EXPECT_EQ(it->second, "2-digit");
1794 }
1795 it = options.find("hour");
1796 if (it != options.end()) {
1797 EXPECT_EQ(it->second, "2-digit");
1798 }
1799 }
1800
1801 /**
1802 * @tc.name: IntlFuncTest0042
1803 * @tc.desc: Test Intl DateTimeFormat
1804 * @tc.type: FUNC
1805 */
1806 HWTEST_F(IntlTest, IntlFuncTest0042, TestSize.Level1)
1807 {
1808 vector<string> locales;
1809 locales.push_back("en-US");
1810 map<string, string> inputOptions = {
1811 { "locale", "en-US" },
1812 { "dateStyle", "full" },
1813 { "timeStyle", "long" },
1814 { "hourCycle", "h12" },
1815 { "timeZone", "Asia/Singapore" },
1816 { "numberingSystem", "latn" },
1817 { "hour12", "true" },
1818 { "weekday", "long" },
1819 { "era", "long" },
1820 { "year", "2-digit" },
1821 { "month", "2-digit" },
1822 { "day", "2-digit" },
1823 { "hour", "numeric" },
1824 { "minute", "numeric" },
1825 { "second", "numeric" },
1826 { "timeZoneName", "long" },
1827 { "dayPeriod", "long" },
1828 { "localeMatcher", "lookup" },
1829 { "formatMatcher", "basic" }
1830 };
1831 std::unique_ptr<DateTimeFormat> formatter = DateTimeFormat::CreateInstance(locales, inputOptions);
1832 map<string, string> options;
1833 formatter->GetResolvedOptions(options);
1834 map<string, string>::iterator it = options.find("minute");
1835 if (it != options.end()) {
1836 EXPECT_EQ(it->second, "numeric");
1837 }
1838 it = options.find("second");
1839 if (it != options.end()) {
1840 EXPECT_EQ(it->second, "numeric");
1841 }
1842 it = options.find("timeZoneName");
1843 if (it != options.end()) {
1844 EXPECT_EQ(it->second, "long");
1845 }
1846 it = options.find("dayPeriod");
1847 if (it != options.end()) {
1848 EXPECT_EQ(it->second, "long");
1849 }
1850 }
1851
1852 /**
1853 * @tc.name: IntlFuncTest0043
1854 * @tc.desc: Test Intl DateTimeFormat
1855 * @tc.type: FUNC
1856 */
1857 HWTEST_F(IntlTest, IntlFuncTest0043, TestSize.Level1)
1858 {
1859 vector<string> locales;
1860 locales.push_back("en-US");
1861 map<string, string> inputOptions = {
1862 { "locale", "en-US" },
1863 { "dateStyle", "long" },
1864 { "timeStyle", "long" },
1865 { "hourCycle", "h12" },
1866 { "timeZone", "America/Los_Angeles" },
1867 { "numberingSystem", "latn" },
1868 { "hour12", "true" },
1869 { "weekday", "long" },
1870 { "era", "long" },
1871 { "year", "numeric" },
1872 { "month", "numeric" },
1873 { "day", "numeric" },
1874 { "hour", "2-digit" },
1875 { "minute", "2-digit" },
1876 { "second", "2-digit" },
1877 { "timeZoneName", "long" },
1878 { "dayPeriod", "long" },
1879 { "localeMatcher", "lookup" },
1880 { "formatMatcher", "basic" }
1881 };
1882 std::unique_ptr<DateTimeFormat> formatter = DateTimeFormat::CreateInstance(locales, inputOptions);
1883 map<string, string> options;
1884 formatter->GetResolvedOptions(options);
1885 map<string, string>::iterator it = options.find("localeMatcher");
1886 if (it != options.end()) {
1887 EXPECT_EQ(it->second, "lookup");
1888 }
1889 it = options.find("formatMatcher");
1890 if (it != options.end()) {
1891 EXPECT_EQ(it->second, "basic");
1892 }
1893 }
1894
1895 /**
1896 * @tc.name: IntlFuncTest0044
1897 * @tc.desc: Test Intl LocaleInfo
1898 * @tc.type: FUNC
1899 */
1900 HWTEST_F(IntlTest, IntlFuncTest0044, TestSize.Level1)
1901 {
1902 string localeTag = "zh-Hans-CN";
1903 map<string, string> configs = {
1904 { "calendar", "chinese" },
1905 { "collation", "pinyin" },
1906 { "hourCycle", "h12" },
1907 { "numberingSystem", "latn" },
1908 { "numeric", "true" },
1909 { "caseFirst", "upper" }
1910 };
1911 LocaleInfo *locale = new LocaleInfo(localeTag, configs);
1912 string res = locale->GetLanguage();
1913 EXPECT_EQ(res, "zh");
1914 res = locale->GetScript();
1915 EXPECT_EQ(res, "Hans");
1916 res = locale->GetRegion();
1917 EXPECT_EQ(res, "CN");
1918 res = locale->GetBaseName();
1919 EXPECT_EQ(res, "zh-Hans-CN");
1920 res = locale->GetCalendar();
1921 EXPECT_EQ(res, "chinese");
1922 res = locale->GetCollation();
1923 EXPECT_EQ(res, "pinyin");
1924 res = locale->GetHourCycle();
1925 EXPECT_EQ(res, "h12");
1926 res = locale->GetNumberingSystem();
1927 EXPECT_EQ(res, "latn");
1928 res = locale->GetNumeric();
1929 EXPECT_EQ(res, "true");
1930 res = locale->GetCaseFirst();
1931 EXPECT_EQ(res, "upper");
1932 icu::Locale icuLocale = locale->GetLocale();
1933 res = locale->ToString();
1934 EXPECT_EQ(res, "zh-Hans-CN-u-hc-h12-nu-latn-ca-chinese-co-pinyin-kf-upper-kn-true");
1935 delete locale;
1936 }
1937
1938 /**
1939 * @tc.name: IntlFuncTest0045
1940 * @tc.desc: Test Intl ReadSystemParameter
1941 * @tc.type: FUNC
1942 */
1943 HWTEST_F(IntlTest, IntlFuncTest0045, TestSize.Level1)
1944 {
1945 string paramKey = "const.global.language";
1946 int paramLength = 128;
1947 string res = ReadSystemParameter(paramKey.c_str(), paramLength);
1948 EXPECT_TRUE(res.length() > 0);
1949
1950 paramKey = "fake system param";
1951 res = ReadSystemParameter(paramKey.c_str(), paramLength);
1952 EXPECT_TRUE(res.length() == 0);
1953 }
1954
1955 /**
1956 * @tc.name: IntlFuncTest0046
1957 * @tc.desc: Test Intl GetTimezoneIdByLocation
1958 * @tc.type: FUNC
1959 */
1960 HWTEST_F(IntlTest, IntlFuncTest0046, TestSize.Level1)
1961 {
1962 //北京
1963 vector<std::string> Beijing = I18nTimeZone::GetTimezoneIdByLocation(116.3, 39.5);
1964 EXPECT_TRUE(Beijing.size() == 1 && Beijing[0] == "Asia/Shanghai");
1965 //洛杉矶
1966 vector<std::string> LosAngeles = I18nTimeZone::GetTimezoneIdByLocation(-118.1, 34.0);
1967 EXPECT_TRUE(LosAngeles.size() == 1 && LosAngeles[0] == "America/Los_Angeles");
1968 //里约热内卢
1969 vector<std::string> RIO = I18nTimeZone::GetTimezoneIdByLocation(-43.1, -22.5);
1970 EXPECT_TRUE(RIO.size() == 1 && RIO[0] == "America/Sao_Paulo");
1971 //悉尼
1972 vector<std::string> Sydney = I18nTimeZone::GetTimezoneIdByLocation(150.5, -33.55);
1973 EXPECT_TRUE(Sydney.size() == 1 && Sydney[0] == "Australia/Sydney");
1974 //乌鲁木齐
1975 vector<std::string> Urumqi = I18nTimeZone::GetTimezoneIdByLocation(87.7, 43.8);
1976 EXPECT_TRUE(Urumqi.size() == 2);
1977 bool containsShanghai = false;
1978 bool containsUrumqi = false;
1979 for (unsigned int i = 0; i < Urumqi.size(); i++) {
1980 if (Urumqi[i] == "Asia/Shanghai") {
1981 containsShanghai = true;
1982 }
1983 if (Urumqi[i] == "Asia/Urumqi") {
1984 containsUrumqi = true;
1985 }
1986 }
1987 EXPECT_TRUE(containsShanghai);
1988 EXPECT_TRUE(containsUrumqi);
1989 }
1990 } // namespace I18n
1991 } // namespace Global
1992 } // namespace OHOS