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 string locale = "en-CN"; 422 string expects = "123K"; 423 vector<string> locales; 424 locales.push_back(locale); 425 string style = "decimal"; 426 map<string, string> options = { { "style", style }, 427 { "notation", "compact" } }; 428 NumberFormat *numFmt = new (std::nothrow) NumberFormat(locales, options); 429 ASSERT_TRUE(numFmt != nullptr); 430 string out = numFmt->Format(123456.789); 431 EXPECT_EQ(out, expects); 432 EXPECT_EQ(numFmt->GetStyle(), style); 433 delete numFmt; 434 435 locale = "en-GB"; 436 locales.clear(); 437 locales.push_back(locale); 438 options = { { "style", "unit" }, 439 { "unit", "day" }, 440 { "unitUsage", "elapsed-time-second"} }; 441 NumberFormat *numFormat = new (std::nothrow) NumberFormat(locales, options); 442 ASSERT_TRUE(numFormat != nullptr); 443 std::string nowRes = numFormat->Format(0.00001); 444 std::string secondRes = numFormat->Format(0.00004); 445 std::string dayRes = numFormat->Format(1.5); 446 std::string monthRes = numFormat->Format(-62.5); 447 448 EXPECT_EQ(nowRes, "now"); 449 EXPECT_EQ(secondRes, "3 seconds ago"); 450 EXPECT_EQ(dayRes, "yesterday"); 451 EXPECT_EQ(monthRes, "2 months ago"); 452 delete numFormat; 453 } 454 455 /** 456 * @tc.name: NumberFormatFuncTest0061 457 * @tc.desc: Test Intl NumberFormat.format 458 * @tc.type: FUNC 459 */ 460 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0061, TestSize.Level1) 461 { 462 int bufferLen = 10; 463 char value[bufferLen]; 464 vector<string> locales; 465 locales.push_back("en-GB"); 466 int code = GetParameter("const.product.devicetype", "", value, bufferLen); 467 std::string deviceType; 468 if (code > 0) { 469 deviceType = value; 470 } 471 map<string, string> unitOptions = { 472 { "style", "unit" }, 473 { "unit", "hectare" } 474 }; 475 NumberFormat *unitFormatter = new NumberFormat(locales, unitOptions); 476 ASSERT_TRUE(unitFormatter != nullptr); 477 string unitRes = unitFormatter->Format(123); 478 map<string, string> currencyOptions = { 479 { "style", "currency" }, 480 { "currency", "USD" } 481 }; 482 NumberFormat *currencyFormatter = new NumberFormat(locales, currencyOptions); 483 ASSERT_TRUE(currencyFormatter != nullptr); 484 string currencyRes = currencyFormatter->Format(123); 485 if (deviceType == "wearable" || deviceType == "liteWearable" || deviceType == "watch") { 486 EXPECT_EQ(currencyRes, "$123.00"); 487 EXPECT_EQ(unitRes, "123ha"); 488 } else if (deviceType == "tablet" || deviceType == "2in1" || deviceType == "tv" || deviceType == "pc") { 489 EXPECT_EQ(currencyRes, "US$123.00"); 490 EXPECT_EQ(unitRes, "123 hectares"); 491 } else { 492 EXPECT_EQ(currencyRes, "US$123.00"); 493 EXPECT_EQ(unitRes, "123 ha"); 494 } 495 delete unitFormatter; 496 delete currencyFormatter; 497 } 498 499 /** 500 * @tc.name: NumberFormatFuncTest0062 501 * @tc.desc: Test Intl NumberFormat.format 502 * @tc.type: FUNC 503 */ 504 HWTEST_F(NumberFormatTest, NumberFormatFuncTest0062, TestSize.Level1) 505 { 506 std::string locale = "en-GB"; 507 std::vector<std::string> locales; 508 locales.push_back(locale); 509 map<string, string> options = { { "style", "unit" }, 510 { "unit", "megabyte" }, 511 { "unitUsage", "size-file-byte"} }; 512 NumberFormat *numFmt = new (std::nothrow) NumberFormat(locales, options); 513 ASSERT_TRUE(numFmt != nullptr); 514 std::string byteRes = numFmt->Format(0.00000812); 515 std::string kbRes = numFmt->Format(0.125); 516 std::string mbRes = numFmt->Format(3.5); 517 std::string gbRes = numFmt->Format(23122); 518 519 EXPECT_EQ(byteRes, "8 byte"); 520 EXPECT_EQ(kbRes, "125 kB"); 521 EXPECT_EQ(mbRes, "3.50 MB"); 522 EXPECT_EQ(gbRes, "23.12 GB"); 523 delete numFmt; 524 525 locales.clear(); 526 locales.push_back(locale); 527 options = { { "style", "unit" }, 528 { "unit", "megabyte" }, 529 { "unitUsage", "size-shortfile-byte"} }; 530 NumberFormat *numFormat = new (std::nothrow) NumberFormat(locales, options); 531 ASSERT_TRUE(numFormat != nullptr); 532 byteRes = numFormat->Format(0.00000812); 533 kbRes = numFormat->Format(0.125); 534 mbRes = numFormat->Format(3.5); 535 gbRes = numFormat->Format(23122); 536 537 EXPECT_EQ(byteRes, "8 byte"); 538 EXPECT_EQ(kbRes, "125 kB"); 539 EXPECT_EQ(mbRes, "3.5 MB"); 540 EXPECT_EQ(gbRes, "23 GB"); 541 delete numFormat; 542 } 543 } // namespace I18n 544 } // namespace Global 545 } // namespace OHOS