1 /* 2 * Copyright (c) 2024 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 "number_format_test_extend.h" 17 #include <gtest/gtest.h> 18 #include "locale_config.h" 19 #include "number_format.h" 20 #include "simple_number_format.h" 21 #include "unicode/locid.h" 22 #include "parameter.h" 23 24 using namespace OHOS::Global::I18n; 25 using testing::ext::TestSize; 26 using namespace std; 27 28 namespace OHOS { 29 namespace Global { 30 namespace I18n { 31 /** 32 * @tc.name: NumberFormatFuncTest0050 33 * @tc.desc: Test Intl NumberFormat.format 34 * @tc.type: FUNC 35 */ 36 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0050, TestSize.Level1) 37 { 38 string localeAR = "ar"; 39 vector<string> localesAR{localeAR}; 40 string localeHE = "HE"; 41 vector<string> localesHE{localeHE}; 42 string localeMY = "my"; 43 vector<string> localesMY{localeMY}; 44 string style = "unit"; 45 string unit = "beat-per-minute"; 46 map<string, string> options = { { "style", style}, 47 { "unit", unit }, 48 { "numberingSystem", "latn" } }; 49 std::unique_ptr<NumberFormat> numFmtAR = std::make_unique<NumberFormat>(localesAR, options); 50 ASSERT_TRUE(numFmtAR != nullptr); 51 EXPECT_EQ(numFmtAR->Format(0), "0 نبضات/د"); 52 EXPECT_EQ(numFmtAR->Format(1), "نبضة واحدة/د"); 53 EXPECT_EQ(numFmtAR->Format(2), "نبضتان/د"); 54 EXPECT_EQ(numFmtAR->Format(3), "3 نبضات/د"); 55 EXPECT_EQ(numFmtAR->Format(11), "11 نبضة/د"); 56 EXPECT_EQ(numFmtAR->Format(100), "100 نبضة/د"); 57 std::unique_ptr<NumberFormat> numFmtHE = std::make_unique<NumberFormat>(localesHE, options); 58 ASSERT_TRUE(numFmtHE != nullptr); 59 EXPECT_EQ(numFmtHE->Format(0), "0 פעימות לדקה"); 60 EXPECT_EQ(numFmtHE->Format(1), "פעימה 1 לדקה"); 61 EXPECT_EQ(numFmtHE->Format(2), "2 פעימות לדקה"); 62 EXPECT_EQ(numFmtHE->Format(3), "3 פעימות לדקה"); 63 std::unique_ptr<NumberFormat> numFmtMY = std::make_unique<NumberFormat>(localesMY, options); 64 ASSERT_TRUE(numFmtMY != nullptr); 65 EXPECT_EQ(numFmtMY->Format(123), "123 bpm"); 66 } 67 68 /** 69 * @tc.name: NumberFormatFuncTest0051 70 * @tc.desc: Test Intl NumberFormat.format 71 * @tc.type: FUNC 72 */ 73 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0051, TestSize.Level1) 74 { 75 string localeAR = "ar"; 76 vector<string> localesAR{localeAR}; 77 string localeHE = "HE"; 78 vector<string> localesHE{localeHE}; 79 string localeMY = "my"; 80 vector<string> localesMY{localeMY}; 81 string style = "unit"; 82 string unit = "body-weight-per-second"; 83 map<string, string> options = { { "style", style}, 84 { "unit", unit }, 85 { "numberingSystem", "latn" } }; 86 std::unique_ptr<NumberFormat> numFmtAR = std::make_unique<NumberFormat>(localesAR, options); 87 ASSERT_TRUE(numFmtAR != nullptr); 88 EXPECT_EQ(numFmtAR->Format(0), "وزن 0 أجسام/ث"); 89 EXPECT_EQ(numFmtAR->Format(1), "وزن جسم واحد/ث"); 90 EXPECT_EQ(numFmtAR->Format(2), "وزن جسمين/ث"); 91 EXPECT_EQ(numFmtAR->Format(3), "وزن 3 أجسام/ث"); 92 EXPECT_EQ(numFmtAR->Format(11), "وزن 11 جسمًا/ث"); 93 EXPECT_EQ(numFmtAR->Format(100), "وزن 100 جسم/ث"); 94 std::unique_ptr<NumberFormat> numFmtHE = std::make_unique<NumberFormat>(localesHE, options); 95 ASSERT_TRUE(numFmtHE != nullptr); 96 EXPECT_EQ(numFmtHE->Format(0), "0 BW/s"); 97 EXPECT_EQ(numFmtHE->Format(1), "1 BW/s"); 98 EXPECT_EQ(numFmtHE->Format(2), "2 BW/s"); 99 EXPECT_EQ(numFmtHE->Format(3), "3 BW/s"); 100 std::unique_ptr<NumberFormat> numFmtMY = std::make_unique<NumberFormat>(localesMY, options); 101 ASSERT_TRUE(numFmtMY != nullptr); 102 EXPECT_EQ(numFmtMY->Format(123), "123 BW/s"); 103 } 104 105 /** 106 * @tc.name: NumberFormatFuncTest0052 107 * @tc.desc: Test Intl NumberFormat.format 108 * @tc.type: FUNC 109 */ 110 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0052, TestSize.Level1) 111 { 112 string localeAR = "ar"; 113 vector<string> localesAR{localeAR}; 114 string localeHE = "HE"; 115 vector<string> localesHE{localeHE}; 116 string localeMY = "my"; 117 vector<string> localesMY{localeMY}; 118 string style = "unit"; 119 string unit = "breath-per-minute"; 120 map<string, string> options = { { "style", style}, 121 { "unit", unit }, 122 { "numberingSystem", "latn" } }; 123 std::unique_ptr<NumberFormat> numFmtAR = std::make_unique<NumberFormat>(localesAR, options); 124 ASSERT_TRUE(numFmtAR != nullptr); 125 EXPECT_EQ(numFmtAR->Format(0), "0 أنفاس/د"); 126 EXPECT_EQ(numFmtAR->Format(1), "نفس واحد/د"); 127 EXPECT_EQ(numFmtAR->Format(2), "نفسان/د"); 128 EXPECT_EQ(numFmtAR->Format(3), "3 أنفاس/د"); 129 EXPECT_EQ(numFmtAR->Format(11), "11 نفسًا/د"); 130 EXPECT_EQ(numFmtAR->Format(100), "100 نفس/د"); 131 std::unique_ptr<NumberFormat> numFmtHE = std::make_unique<NumberFormat>(localesHE, options); 132 ASSERT_TRUE(numFmtHE != nullptr); 133 EXPECT_EQ(numFmtHE->Format(0), "0 נשימות לדקה"); 134 EXPECT_EQ(numFmtHE->Format(1), "נשימה 1 לדקה"); 135 EXPECT_EQ(numFmtHE->Format(2), "2 נשימות לדקה"); 136 EXPECT_EQ(numFmtHE->Format(3), "3 נשימות לדקה"); 137 std::unique_ptr<NumberFormat> numFmtMY = std::make_unique<NumberFormat>(localesMY, options); 138 ASSERT_TRUE(numFmtMY != nullptr); 139 EXPECT_EQ(numFmtMY->Format(123), "123 brpm"); 140 } 141 142 /** 143 * @tc.name: NumberFormatFuncTest0053 144 * @tc.desc: Test Intl NumberFormat.format 145 * @tc.type: FUNC 146 */ 147 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0053, TestSize.Level1) 148 { 149 string localeAR = "ar"; 150 vector<string> localesAR{localeAR}; 151 string localeHE = "HE"; 152 vector<string> localesHE{localeHE}; 153 string localeMY = "my"; 154 vector<string> localesMY{localeMY}; 155 string style = "unit"; 156 string unit = "foot-per-hour"; 157 map<string, string> options = { { "style", style}, 158 { "unit", unit }, 159 { "numberingSystem", "latn" } }; 160 std::unique_ptr<NumberFormat> numFmtAR = std::make_unique<NumberFormat>(localesAR, options); 161 ASSERT_TRUE(numFmtAR != nullptr); 162 EXPECT_EQ(numFmtAR->Format(0), "0 أقدام/س"); 163 EXPECT_EQ(numFmtAR->Format(1), "قدم واحدة/س"); 164 EXPECT_EQ(numFmtAR->Format(2), "قدمان/س"); 165 EXPECT_EQ(numFmtAR->Format(3), "3 أقدام/س"); 166 EXPECT_EQ(numFmtAR->Format(11), "11 قدمًا/س"); 167 EXPECT_EQ(numFmtAR->Format(100), "100 قدم/س"); 168 std::unique_ptr<NumberFormat> numFmtHE = std::make_unique<NumberFormat>(localesHE, options); 169 ASSERT_TRUE(numFmtHE != nullptr); 170 EXPECT_EQ(numFmtHE->Format(0), "0 רגל לשעה"); 171 EXPECT_EQ(numFmtHE->Format(1), "1 רגל לשעה"); 172 EXPECT_EQ(numFmtHE->Format(2), "2 רגל לשעה"); 173 EXPECT_EQ(numFmtHE->Format(3), "3 רגל לשעה"); 174 std::unique_ptr<NumberFormat> numFmtMY = std::make_unique<NumberFormat>(localesMY, options); 175 ASSERT_TRUE(numFmtMY != nullptr); 176 EXPECT_EQ(numFmtMY->Format(123), "123 ft/h"); 177 } 178 179 /** 180 * @tc.name: NumberFormatFuncTest0054 181 * @tc.desc: Test Intl NumberFormat.format 182 * @tc.type: FUNC 183 */ 184 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0054, TestSize.Level1) 185 { 186 string localeAR = "ar"; 187 vector<string> localesAR{localeAR}; 188 string localeHE = "HE"; 189 vector<string> localesHE{localeHE}; 190 string localeMY = "my"; 191 vector<string> localesMY{localeMY}; 192 string style = "unit"; 193 string unit = "jump-rope-per-minute"; 194 map<string, string> options = { { "style", style}, 195 { "unit", unit }, 196 { "numberingSystem", "latn" } }; 197 std::unique_ptr<NumberFormat> numFmtAR = std::make_unique<NumberFormat>(localesAR, options); 198 ASSERT_TRUE(numFmtAR != nullptr); 199 EXPECT_EQ(numFmtAR->Format(0), "0 قفزات/د"); 200 EXPECT_EQ(numFmtAR->Format(1), "قفزة/د"); 201 EXPECT_EQ(numFmtAR->Format(2), "قفزتان/د"); 202 EXPECT_EQ(numFmtAR->Format(3), "3 قفزات/د"); 203 EXPECT_EQ(numFmtAR->Format(11), "11 قفزة/د"); 204 EXPECT_EQ(numFmtAR->Format(100), "100 قفزة/د"); 205 std::unique_ptr<NumberFormat> numFmtHE = std::make_unique<NumberFormat>(localesHE, options); 206 ASSERT_TRUE(numFmtHE != nullptr); 207 EXPECT_EQ(numFmtHE->Format(0), "0 קפיצות לדקה"); 208 EXPECT_EQ(numFmtHE->Format(1), "קפיצה 1 לדקה"); 209 EXPECT_EQ(numFmtHE->Format(2), "2 קפיצות לדקה"); 210 EXPECT_EQ(numFmtHE->Format(3), "3 קפיצות לדקה"); 211 std::unique_ptr<NumberFormat> numFmtMY = std::make_unique<NumberFormat>(localesMY, options); 212 ASSERT_TRUE(numFmtMY != nullptr); 213 EXPECT_EQ(numFmtMY->Format(123), "တစ်မိနစ်လျှင် 123 ကြိမ်"); 214 } 215 216 /** 217 * @tc.name: NumberFormatFuncTest0055 218 * @tc.desc: Test Intl NumberFormat.format 219 * @tc.type: FUNC 220 */ 221 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0055, TestSize.Level1) 222 { 223 string localeAR = "ar"; 224 vector<string> localesAR{localeAR}; 225 string localeHE = "HE"; 226 vector<string> localesHE{localeHE}; 227 string localeMY = "my"; 228 vector<string> localesMY{localeMY}; 229 string style = "unit"; 230 string unit = "meter-per-hour"; 231 map<string, string> options = { { "style", style}, 232 { "unit", unit }, 233 { "numberingSystem", "latn" } }; 234 std::unique_ptr<NumberFormat> numFmtAR = std::make_unique<NumberFormat>(localesAR, options); 235 ASSERT_TRUE(numFmtAR != nullptr); 236 EXPECT_EQ(numFmtAR->Format(0), "0 م/س"); 237 EXPECT_EQ(numFmtAR->Format(1), "1 م/س"); 238 EXPECT_EQ(numFmtAR->Format(2), "2 م/س"); 239 EXPECT_EQ(numFmtAR->Format(3), "3 م/س"); 240 EXPECT_EQ(numFmtAR->Format(11), "11 م/س"); 241 EXPECT_EQ(numFmtAR->Format(100), "100 م/س"); 242 std::unique_ptr<NumberFormat> numFmtHE = std::make_unique<NumberFormat>(localesHE, options); 243 ASSERT_TRUE(numFmtHE != nullptr); 244 EXPECT_EQ(numFmtHE->Format(0), "0 מטר לשעה"); 245 EXPECT_EQ(numFmtHE->Format(1), "1 מטר לשעה"); 246 EXPECT_EQ(numFmtHE->Format(2), "2 מטר לשעה"); 247 EXPECT_EQ(numFmtHE->Format(3), "3 מטר לשעה"); 248 std::unique_ptr<NumberFormat> numFmtMY = std::make_unique<NumberFormat>(localesMY, options); 249 ASSERT_TRUE(numFmtMY != nullptr); 250 EXPECT_EQ(numFmtMY->Format(123), "123 m/h"); 251 } 252 253 /** 254 * @tc.name: NumberFormatFuncTest0056 255 * @tc.desc: Test Intl NumberFormat.format 256 * @tc.type: FUNC 257 */ 258 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0056, TestSize.Level1) 259 { 260 string localeAR = "ar"; 261 vector<string> localesAR{localeAR}; 262 string localeHE = "HE"; 263 vector<string> localesHE{localeHE}; 264 string localeMY = "my"; 265 vector<string> localesMY{localeMY}; 266 string style = "unit"; 267 string unit = "milliliter-per-minute-per-kilogram"; 268 map<string, string> options = { { "style", style}, 269 { "unit", unit }, 270 { "numberingSystem", "latn" } }; 271 std::unique_ptr<NumberFormat> numFmtAR = std::make_unique<NumberFormat>(localesAR, options); 272 ASSERT_TRUE(numFmtAR != nullptr); 273 EXPECT_EQ(numFmtAR->Format(0), "0 ملم/كغم/د"); 274 EXPECT_EQ(numFmtAR->Format(1), "1 ملم/كغم/د"); 275 EXPECT_EQ(numFmtAR->Format(2), "2 ملم/كغم/د"); 276 EXPECT_EQ(numFmtAR->Format(3), "3 ملم/كغم/د"); 277 EXPECT_EQ(numFmtAR->Format(11), "11 ملم/كغم/د"); 278 EXPECT_EQ(numFmtAR->Format(100), "100 ملم/كغم/د"); 279 std::unique_ptr<NumberFormat> numFmtHE = std::make_unique<NumberFormat>(localesHE, options); 280 ASSERT_TRUE(numFmtHE != nullptr); 281 EXPECT_EQ(numFmtHE->Format(0), "0 ml/kg/min"); 282 EXPECT_EQ(numFmtHE->Format(1), "1 ml/kg/min"); 283 EXPECT_EQ(numFmtHE->Format(2), "2 ml/kg/min"); 284 EXPECT_EQ(numFmtHE->Format(3), "3 ml/kg/min"); 285 std::unique_ptr<NumberFormat> numFmtMY = std::make_unique<NumberFormat>(localesMY, options); 286 ASSERT_TRUE(numFmtMY != nullptr); 287 EXPECT_EQ(numFmtMY->Format(123), "123 ml/kg/min"); 288 } 289 290 /** 291 * @tc.name: NumberFormatFuncTest0057 292 * @tc.desc: Test Intl NumberFormat.format 293 * @tc.type: FUNC 294 */ 295 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0057, TestSize.Level1) 296 { 297 string localeAR = "ar"; 298 vector<string> localesAR{localeAR}; 299 string localeHE = "HE"; 300 vector<string> localesHE{localeHE}; 301 string localeMY = "my"; 302 vector<string> localesMY{localeMY}; 303 string style = "unit"; 304 string unit = "rotation-per-minute"; 305 map<string, string> options = { { "style", style}, 306 { "unit", unit }, 307 { "numberingSystem", "latn" } }; 308 std::unique_ptr<NumberFormat> numFmtAR = std::make_unique<NumberFormat>(localesAR, options); 309 ASSERT_TRUE(numFmtAR != nullptr); 310 EXPECT_EQ(numFmtAR->Format(0), "0 دورات/د"); 311 EXPECT_EQ(numFmtAR->Format(1), "دورة واحدة/د"); 312 EXPECT_EQ(numFmtAR->Format(2), "دورتان/د"); 313 EXPECT_EQ(numFmtAR->Format(3), "3 دورات/د"); 314 EXPECT_EQ(numFmtAR->Format(11), "11 دورة/د"); 315 EXPECT_EQ(numFmtAR->Format(100), "100 دورة/د"); 316 std::unique_ptr<NumberFormat> numFmtHE = std::make_unique<NumberFormat>(localesHE, options); 317 ASSERT_TRUE(numFmtHE != nullptr); 318 EXPECT_EQ(numFmtHE->Format(0), "0 סל\u0022ד"); 319 EXPECT_EQ(numFmtHE->Format(1), "1 סל\u0022ד"); 320 EXPECT_EQ(numFmtHE->Format(2), "2 סל\u0022ד"); 321 EXPECT_EQ(numFmtHE->Format(3), "3 סל\u0022ד"); 322 std::unique_ptr<NumberFormat> numFmtMY = std::make_unique<NumberFormat>(localesMY, options); 323 ASSERT_TRUE(numFmtMY != nullptr); 324 EXPECT_EQ(numFmtMY->Format(123), "123 rpm"); 325 } 326 327 /** 328 * @tc.name: NumberFormatFuncTest0058 329 * @tc.desc: Test Intl NumberFormat.format 330 * @tc.type: FUNC 331 */ 332 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0058, TestSize.Level1) 333 { 334 string localeAR = "ar"; 335 vector<string> localesAR{localeAR}; 336 string localeHE = "HE"; 337 vector<string> localesHE{localeHE}; 338 string localeMY = "my"; 339 vector<string> localesMY{localeMY}; 340 string style = "unit"; 341 string unit = "step-per-minute"; 342 map<string, string> options = { { "style", style}, 343 { "unit", unit }, 344 { "numberingSystem", "latn" } }; 345 std::unique_ptr<NumberFormat> numFmtAR = std::make_unique<NumberFormat>(localesAR, options); 346 ASSERT_TRUE(numFmtAR != nullptr); 347 EXPECT_EQ(numFmtAR->Format(0), "0 خطوات/د"); 348 EXPECT_EQ(numFmtAR->Format(1), "خطوة واحدة/د"); 349 EXPECT_EQ(numFmtAR->Format(2), "خطوتان/د"); 350 EXPECT_EQ(numFmtAR->Format(3), "3 خطوات/د"); 351 EXPECT_EQ(numFmtAR->Format(11), "11 خطوة/د"); 352 EXPECT_EQ(numFmtAR->Format(100), "100 خطوة/د"); 353 std::unique_ptr<NumberFormat> numFmtHE = std::make_unique<NumberFormat>(localesHE, options); 354 ASSERT_TRUE(numFmtHE != nullptr); 355 EXPECT_EQ(numFmtHE->Format(0), "0 צעדים לדקה"); 356 EXPECT_EQ(numFmtHE->Format(1), "צעד 1 לדקה"); 357 EXPECT_EQ(numFmtHE->Format(2), "2 צעדים לדקה"); 358 EXPECT_EQ(numFmtHE->Format(3), "3 צעדים לדקה"); 359 std::unique_ptr<NumberFormat> numFmtMY = std::make_unique<NumberFormat>(localesMY, options); 360 ASSERT_TRUE(numFmtMY != nullptr); 361 EXPECT_EQ(numFmtMY->Format(123), "တစ်မိနစ်လျှင် 123 လှမ်း"); 362 } 363 364 /** 365 * @tc.name: NumberFormatFuncTest0059 366 * @tc.desc: Test Intl NumberFormat.format 367 * @tc.type: FUNC 368 */ 369 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0059, TestSize.Level1) 370 { 371 string localeAR = "ar"; 372 vector<string> localesAR{localeAR}; 373 string localeHE = "HE"; 374 vector<string> localesHE{localeHE}; 375 string localeMY = "my"; 376 vector<string> localesMY{localeMY}; 377 string style = "unit"; 378 string unit = "stroke-per-minute"; 379 map<string, string> options = { { "style", style}, 380 { "unit", unit }, 381 { "numberingSystem", "latn" } }; 382 std::unique_ptr<NumberFormat> numFmtAR = std::make_unique<NumberFormat>(localesAR, options); 383 ASSERT_TRUE(numFmtAR != nullptr); 384 EXPECT_EQ(numFmtAR->Format(0), "0 ضربات/د"); 385 EXPECT_EQ(numFmtAR->Format(1), "ضربة واحدة/د"); 386 EXPECT_EQ(numFmtAR->Format(2), "ضربتان/د"); 387 EXPECT_EQ(numFmtAR->Format(3), "3 ضربات/د"); 388 EXPECT_EQ(numFmtAR->Format(11), "11 ضربة/د"); 389 EXPECT_EQ(numFmtAR->Format(100), "100 ضربة/د"); 390 std::unique_ptr<NumberFormat> numFmtHE = std::make_unique<NumberFormat>(localesHE, options); 391 ASSERT_TRUE(numFmtHE != nullptr); 392 EXPECT_EQ(numFmtHE->Format(0), "0 תנועות לדקה"); 393 EXPECT_EQ(numFmtHE->Format(1), "תנועה 1 לדקה"); 394 EXPECT_EQ(numFmtHE->Format(2), "2 תנועות לדקה"); 395 EXPECT_EQ(numFmtHE->Format(3), "3 תנועות לדקה"); 396 std::unique_ptr<NumberFormat> numFmtMY = std::make_unique<NumberFormat>(localesMY, options); 397 ASSERT_TRUE(numFmtMY != nullptr); 398 EXPECT_EQ(numFmtMY->Format(123), "တစ်မိနစ်လျှင် 123 ချက်"); 399 } 400 401 /** 402 * @tc.name: NumberFormatFuncTest0060 403 * @tc.desc: Test Intl NumberFormat.format 404 * @tc.type: FUNC 405 */ 406 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0060, TestSize.Level1) 407 { 408 std::vector<std::string> localeTags = { "$$@@#" }; 409 std::map<std::string, std::string> configs = { 410 {"style", "unit"}, {"unit", "fake unit"} 411 }; 412 NumberFormat formatter(localeTags, configs); 413 std::string res = formatter.Format(12); 414 EXPECT_EQ(res, "12"); 415 localeTags = { "zh-Hans-u-nu-latn" }; 416 NumberFormat* format = new (std::nothrow) NumberFormat(localeTags, configs); 417 ASSERT_TRUE(format != nullptr); 418 EXPECT_EQ(res, format->Format(12)); 419 delete format; 420 421 vector<string> locales = { "en-CN" }; 422 string style = "decimal"; 423 map<string, string> options = { { "style", style }, 424 { "notation", "compact" } }; 425 NumberFormat *numFmt = new (std::nothrow) NumberFormat(locales, options); 426 ASSERT_TRUE(numFmt != nullptr); 427 string out = numFmt->Format(123456.789); 428 EXPECT_EQ(out, "123K"); 429 EXPECT_EQ(numFmt->GetStyle(), style); 430 delete numFmt; 431 432 locales.clear(); 433 locales.push_back("en-GB"); 434 options = { { "style", "unit" }, 435 { "unit", "day" }, 436 { "unitUsage", "elapsed-time-second"} }; 437 NumberFormat *numFormat = new (std::nothrow) NumberFormat(locales, options); 438 ASSERT_TRUE(numFormat != nullptr); 439 std::string nowRes = numFormat->Format(0.00001); 440 std::string secondRes = numFormat->Format(0.00004); 441 std::string dayRes = numFormat->Format(1.5); 442 std::string monthRes = numFormat->Format(-62.5); 443 444 EXPECT_EQ(nowRes, "now"); 445 EXPECT_EQ(dayRes, "yesterday"); 446 delete numFormat; 447 if (NumberFormatTest::deviceType == "wearable" || NumberFormatTest::deviceType == "liteWearable" || 448 NumberFormatTest::deviceType == "watch") { 449 EXPECT_EQ(secondRes, "3 sec ago"); 450 EXPECT_EQ(monthRes, "2 mo ago"); 451 } else if (NumberFormatTest::deviceType == "tablet" || NumberFormatTest::deviceType == "2in1" || 452 NumberFormatTest::deviceType == "tv" || NumberFormatTest::deviceType == "pc") { 453 EXPECT_EQ(secondRes, "3 seconds ago"); 454 EXPECT_EQ(monthRes, "2 months ago"); 455 } else { 456 EXPECT_EQ(secondRes, "3 seconds ago"); 457 EXPECT_EQ(monthRes, "2 months ago"); 458 } 459 } 460 461 /** 462 * @tc.name: NumberFormatFuncTest0061 463 * @tc.desc: Test Intl NumberFormat.format 464 * @tc.type: FUNC 465 */ 466 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0061, TestSize.Level1) 467 { 468 vector<string> locales; 469 locales.push_back("en-GB"); 470 map<string, string> unitOptions = { 471 { "style", "unit" }, 472 { "unit", "hectare" } 473 }; 474 NumberFormat *unitFormatter = new NumberFormat(locales, unitOptions); 475 ASSERT_TRUE(unitFormatter != nullptr); 476 string unitRes = unitFormatter->Format(123); 477 map<string, string> currencyOptions = { 478 { "style", "currency" }, 479 { "currency", "USD" } 480 }; 481 NumberFormat *currencyFormatter = new NumberFormat(locales, currencyOptions); 482 ASSERT_TRUE(currencyFormatter != nullptr); 483 string currencyRes = currencyFormatter->Format(123); 484 if (NumberFormatTest::deviceType == "wearable" || NumberFormatTest::deviceType == "liteWearable" || 485 NumberFormatTest::deviceType == "watch") { 486 EXPECT_EQ(currencyRes, "$123.00"); 487 EXPECT_EQ(unitRes, "123ha"); 488 } else if (NumberFormatTest::deviceType == "tablet" || NumberFormatTest::deviceType == "2in1" || 489 NumberFormatTest::deviceType == "tv" || NumberFormatTest::deviceType == "pc") { 490 EXPECT_EQ(currencyRes, "US$123.00"); 491 EXPECT_EQ(unitRes, "123 hectares"); 492 } else { 493 EXPECT_EQ(currencyRes, "US$123.00"); 494 EXPECT_EQ(unitRes, "123 ha"); 495 } 496 delete unitFormatter; 497 delete currencyFormatter; 498 } 499 500 /** 501 * @tc.name: NumberFormatFuncTest0062 502 * @tc.desc: Test Intl NumberFormat.format 503 * @tc.type: FUNC 504 */ 505 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0062, TestSize.Level1) 506 { 507 std::vector<std::string> locales = { "en-GB" }; 508 map<string, string> options = { { "style", "unit" }, 509 { "unit", "megabyte" }, 510 { "unitUsage", "size-file-byte"} }; 511 std::unique_ptr<NumberFormat> numFmt = std::make_unique<NumberFormat>(locales, options); 512 ASSERT_TRUE(numFmt != nullptr); 513 std::string byteRes = numFmt->Format(0.00000812); 514 std::string kbRes = numFmt->Format(0.125); 515 std::string mbRes = numFmt->Format(3.5); 516 std::string gbRes = numFmt->Format(23122); 517 518 options["unitUsage"] = "size-shortfile-byte"; 519 std::unique_ptr<NumberFormat> numFormat = std::make_unique<NumberFormat>(locales, options); 520 std::string shortByteRes = numFormat->Format(0.00000812); 521 std::string shortKbRes = numFormat->Format(0.125); 522 std::string shortMbRes = numFormat->Format(3.5); 523 std::string shortGbRes = numFormat->Format(23122); 524 525 if (NumberFormatTest::deviceType == "wearable" || NumberFormatTest::deviceType == "liteWearable" || 526 NumberFormatTest::deviceType == "watch") { 527 EXPECT_EQ(byteRes, "8B"); 528 EXPECT_EQ(kbRes, "125kB"); 529 EXPECT_EQ(mbRes, "3.50MB"); 530 EXPECT_EQ(gbRes, "23.12GB"); 531 EXPECT_EQ(shortByteRes, "8B"); 532 EXPECT_EQ(shortKbRes, "125kB"); 533 EXPECT_EQ(shortMbRes, "3.5MB"); 534 EXPECT_EQ(shortGbRes, "23GB"); 535 } else if (NumberFormatTest::deviceType == "tablet" || NumberFormatTest::deviceType == "2in1" || 536 NumberFormatTest::deviceType == "tv" || NumberFormatTest::deviceType == "pc") { 537 EXPECT_EQ(byteRes, "8 bytes"); 538 EXPECT_EQ(kbRes, "125 kilobytes"); 539 EXPECT_EQ(mbRes, "3.50 megabytes"); 540 EXPECT_EQ(gbRes, "23.12 gigabytes"); 541 EXPECT_EQ(shortByteRes, "8 bytes"); 542 EXPECT_EQ(shortKbRes, "125 kilobytes"); 543 EXPECT_EQ(shortMbRes, "3.5 megabytes"); 544 EXPECT_EQ(shortGbRes, "23 gigabytes"); 545 } else { 546 EXPECT_EQ(byteRes, "8 byte"); 547 EXPECT_EQ(kbRes, "125 kB"); 548 EXPECT_EQ(mbRes, "3.50 MB"); 549 EXPECT_EQ(gbRes, "23.12 GB"); 550 EXPECT_EQ(shortByteRes, "8 byte"); 551 EXPECT_EQ(shortKbRes, "125 kB"); 552 EXPECT_EQ(shortMbRes, "3.5 MB"); 553 EXPECT_EQ(shortGbRes, "23 GB"); 554 } 555 } 556 557 /** 558 * @tc.name: NumberFormatFuncTest0063 559 * @tc.desc: Test Intl NumberFormat.FormatRangeToParts 560 * @tc.type: FUNC 561 */ 562 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0063, TestSize.Level1) 563 { 564 std::string locale = "de-DE"; 565 std::vector<std::string> locales; 566 locales.push_back(locale); 567 map<string, string> options = { { "style", "currency" }, 568 { "currency", "EUR" } }; 569 std::unique_ptr<NumberFormat> numFmt = std::make_unique<NumberFormat>(locales, options); 570 ASSERT_TRUE(numFmt != nullptr); 571 std::vector<std::vector<std::string>> result; 572 numFmt->FormatRangeToParts(3500, 9500, result); 573 EXPECT_EQ(result.size(), 13); 574 ASSERT_TRUE(result.size() > 0 && result[0].size() > 1); 575 EXPECT_EQ(result[0][0], "integer"); 576 EXPECT_EQ(result[0][1], "3"); 577 std::vector<std::vector<std::string>> result2; 578 locale = "en-US"; 579 locales.clear(); 580 locales.push_back(locale); 581 options = { { "style", "currency" }, 582 { "currency", "USD" } }; 583 double amount = 654321.987; 584 std::unique_ptr<NumberFormat> numFmt2 = std::make_unique<NumberFormat>(locales, options); 585 ASSERT_TRUE(numFmt2 != nullptr); 586 numFmt2->FormatToParts(amount, result2); 587 EXPECT_EQ(result2.size(), 6); 588 std::string formatResult = numFmt2->Format(amount); 589 EXPECT_EQ(formatResult, "$654,321.99"); 590 591 std::vector<std::vector<std::string>> result3; 592 locales.clear(); 593 locales.push_back("ar"); 594 options = {}; 595 std::unique_ptr<NumberFormat> numFmt3 = std::make_unique<NumberFormat>(locales, options); 596 ASSERT_TRUE(numFmt3 != nullptr); 597 numFmt3->FormatToParts(result3); 598 EXPECT_EQ(result3.size(), 1); 599 } 600 601 /** 602 * @tc.name: NumberFormatFuncTest0064 603 * @tc.desc: Test Intl NumberFormat.FormatRangeToParts 604 * @tc.type: FUNC 605 */ 606 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0064, TestSize.Level1) 607 { 608 std::string locale = "de-DE"; 609 std::vector<std::string> locales; 610 locales.push_back(locale); 611 map<string, string> options = { { "style", "currency" }, 612 { "currency", "EUR" } }; 613 std::vector<std::vector<std::string>> result; 614 locales.clear(); 615 locales.push_back("en-US"); 616 options = {}; 617 std::unique_ptr<NumberFormat> numFmt2 = std::make_unique<NumberFormat>(locales, options); 618 ASSERT_TRUE(numFmt2 != nullptr); 619 result.clear(); 620 //"87,654,321–98,765,432"; 621 numFmt2->FormatRangeToParts(87654321, 98765432, result); 622 EXPECT_EQ(result.size(), 11); 623 ASSERT_TRUE(result.size() > 0 && result[0].size() > 1); 624 EXPECT_EQ(result[0][0], "integer"); 625 EXPECT_EQ(result[0][1], "87"); 626 result.clear(); 627 numFmt2->FormatRangeToParts(-100, -100, result); 628 EXPECT_EQ(result.size(), 3); 629 } 630 631 /** 632 * @tc.name: NumberFormatFuncTest0065 633 * @tc.desc: Test Intl NumberFormat.FormatRangeToParts 634 * @tc.type: FUNC 635 */ 636 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0065, TestSize.Level1) 637 { 638 std::string locale = "es-ES"; 639 std::vector<std::string> locales; 640 locales.push_back(locale); 641 map<string, string> options = { { "style", "currency" }, 642 { "currency", "EUR" }, 643 { "maximumFractionDigits", "0" } }; 644 std::vector<std::vector<std::string>> result; 645 std::unique_ptr<NumberFormat> numFmt2 = std::make_unique<NumberFormat>(locales, options); 646 ASSERT_TRUE(numFmt2 != nullptr); 647 result.clear(); 648 // "3-5 €" 649 numFmt2->FormatRangeToParts(3, 5, result); 650 EXPECT_EQ(result.size(), 5); 651 ASSERT_TRUE(result.size() > 4 && result[4].size() > 1); 652 EXPECT_EQ(result[4][0], "currency"); 653 EXPECT_EQ(result[4][1], "€"); 654 result.clear(); 655 656 // "~3 €" 657 numFmt2->FormatRangeToParts(2.9, 3.1, result); 658 EXPECT_EQ(result.size(), 4); 659 660 result.clear(); 661 numFmt2->FormatRangeToParts(-123, 65, result); 662 EXPECT_EQ(result.size(), 8); 663 std::vector<std::string> localeTags = {"ban", "id-u-co-pinyin", "de-ID"}; 664 std::map<std::string, std::string> map = { 665 {"localeMatcher", "lookup"} 666 }; 667 I18nErrorCode i18nStatus = I18nErrorCode::SUCCESS; 668 std::vector<std::string> resultLocales = NumberFormat::SupportedLocalesOf(localeTags, map, i18nStatus); 669 EXPECT_EQ(resultLocales.size(), 2); 670 } 671 672 /** 673 * @tc.name: NumberFormatFuncTest0066 674 * @tc.desc: Test Intl NumberFormat.FormatRange 675 * @tc.type: FUNC 676 */ 677 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0066, TestSize.Level1) 678 { 679 std::string locale = "en"; 680 std::vector<std::string> locales; 681 locales.push_back(locale); 682 map<string, string> options = {}; 683 std::unique_ptr<NumberFormat> numFmt2 = std::make_unique<NumberFormat>(locales, options); 684 ASSERT_TRUE(numFmt2 != nullptr); 685 std::string result = numFmt2->FormatRange(543.245, 100000000000000011); 686 EXPECT_EQ(result, "543.245–100,000,000,000,000,020"); 687 std::string result2 = numFmt2->FormatRange(543.245, 543.245); 688 EXPECT_EQ(result2, "~543.245"); 689 } 690 691 /** 692 * @tc.name: NumberFormatFuncTest0067 693 * @tc.desc: Test Intl NumberFormat.FormatRange 694 * @tc.type: FUNC 695 */ 696 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0067, TestSize.Level1) 697 { 698 std::string systemLanguage = LocaleConfig::GetSystemLanguage(); 699 std::string systemLocale = LocaleConfig::GetSystemLocale(); 700 LocaleConfig::SetSystemLanguage("zh-Hans"); 701 LocaleConfig::SetSystemLocale("zh-Hans-CN-u-ms-ussystem-mu-kelvin"); 702 703 std::vector<std::string> locales { "en-Latn-US-u-mu-celsius" }; 704 map<string, string> options = { 705 { "style", "unit" }, 706 { "unit", "kelvin" }, 707 { "unitUsage", "temperature" } 708 }; 709 std::unique_ptr<NumberFormat> numberFormat = std::make_unique<NumberFormat>(locales, options); 710 ASSERT_TRUE(numberFormat != nullptr); 711 std::string result = numberFormat->Format(100); 712 std::string resultOfRange = numberFormat->FormatRange(100, 200); 713 714 if (NumberFormatTest::deviceType == "wearable" || NumberFormatTest::deviceType == "liteWearable" || 715 NumberFormatTest::deviceType == "watch") { 716 EXPECT_EQ(result, "-173.15°C"); 717 EXPECT_EQ(resultOfRange, "-173.15 – -73.15°C"); 718 } else if (NumberFormatTest::deviceType == "tablet" || NumberFormatTest::deviceType == "2in1" || 719 NumberFormatTest::deviceType == "tv" || NumberFormatTest::deviceType == "pc") { 720 EXPECT_EQ(result, "-173.15 degrees Celsius"); 721 EXPECT_EQ(resultOfRange, "-173.15 – -73.15 degrees Celsius"); 722 } else { 723 EXPECT_EQ(result, "-173.15°C"); 724 EXPECT_EQ(resultOfRange, "-173.15 – -73.15°C"); 725 } 726 727 LocaleConfig::SetSystemLanguage(systemLanguage); 728 LocaleConfig::SetSystemLocale(systemLocale); 729 } 730 731 /** 732 * @tc.name: NumberFormatFuncTest0068 733 * @tc.desc: Test Intl NumberFormat 734 * @tc.type: FUNC 735 */ 736 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0068, TestSize.Level1) 737 { 738 std::vector<std::string> locales { "ar" }; 739 map<string, string> options = { 740 { "numeric", "auto" }, 741 { "localeMatcher", "best fit" } 742 }; 743 std::unique_ptr<NumberFormat> numberFormat = std::make_unique<NumberFormat>(locales, options, true); 744 ASSERT_TRUE(numberFormat != nullptr); 745 map<std::string, std::string> resultMap; 746 numberFormat->GetResolvedOptions(resultMap); 747 EXPECT_EQ(resultMap.at("numberingSystem"), "arab"); 748 int32_t code = 0; 749 map<std::string, std::string> optionsMap = { 750 { "currency", "###" }, 751 }; 752 std::string message = NumberFormat::CheckNumberFormatOptions(optionsMap, code); 753 EXPECT_EQ(message, "not a wellformed code"); 754 EXPECT_EQ(code, -1); 755 options = { { "style", "unit" } }; 756 code = 0; 757 std::string msg = NumberFormat::CheckNumberFormatOptions(options, code); 758 EXPECT_EQ(msg, "style is unit but unit is undefined"); 759 options = { { "style", "currency" } }; 760 std::string checkCurrencyMsg = NumberFormat::CheckNumberFormatOptions(options, code); 761 EXPECT_EQ(checkCurrencyMsg, "style is currency but currency is undefined"); 762 } 763 764 /** 765 * @tc.name: NumberFormatFuncTest0069 766 * @tc.desc: Test Intl NumberFormat 767 * @tc.type: FUNC 768 */ 769 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0069, TestSize.Level1) 770 { 771 std::pair<std::string, std::string> numberPattern = { "", "," }; 772 std::string sample = NumberFormat::GetNumberPatternSample("ar-CN-u-nu-latn", numberPattern); 773 EXPECT_EQ(sample, "12345,67"); 774 } 775 } // namespace I18n 776 } // namespace Global 777 } // namespace OHOS