TestPluralRules.cpp (25219B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "gtest/gtest.h" 6 #include "mozilla/intl/PluralRules.h" 7 8 #define TEST_SELECT(actual, expected) \ 9 do { \ 10 ASSERT_TRUE(actual.isOk()); \ 11 ASSERT_EQ(actual.unwrap(), expected); \ 12 } while (false) 13 14 namespace mozilla { 15 namespace intl { 16 17 TEST(IntlPluralRules, CategoriesEnCardinal) 18 { 19 PluralRulesOptions defaultOptions; 20 21 auto prResult = PluralRules::TryCreate("en", defaultOptions); 22 ASSERT_TRUE(prResult.isOk()); 23 auto pr = prResult.unwrap(); 24 25 auto catResult = pr->Categories(); 26 ASSERT_TRUE(catResult.isOk()); 27 auto categories = catResult.unwrap(); 28 29 ASSERT_EQ(categories.size(), 2u); 30 ASSERT_TRUE(categories.contains(PluralRules::Keyword::One)); 31 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Other)); 32 } 33 34 TEST(IntlPluralRules, CategoriesEnOrdinal) 35 { 36 PluralRulesOptions options; 37 options.mPluralType = PluralRules::Type::Ordinal; 38 39 auto prResult = PluralRules::TryCreate("en", options); 40 ASSERT_TRUE(prResult.isOk()); 41 auto pr = prResult.unwrap(); 42 43 auto catResult = pr->Categories(); 44 ASSERT_TRUE(catResult.isOk()); 45 auto categories = catResult.unwrap(); 46 47 ASSERT_EQ(categories.size(), 4u); 48 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Few)); 49 ASSERT_TRUE(categories.contains(PluralRules::Keyword::One)); 50 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Other)); 51 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Two)); 52 } 53 54 TEST(IntlPluralRules, CategoriesCyCardinal) 55 { 56 PluralRulesOptions defaultOptions; 57 58 auto prResult = PluralRules::TryCreate("cy", defaultOptions); 59 ASSERT_TRUE(prResult.isOk()); 60 auto pr = prResult.unwrap(); 61 62 auto catResult = pr->Categories(); 63 ASSERT_TRUE(catResult.isOk()); 64 auto categories = catResult.unwrap(); 65 66 ASSERT_EQ(categories.size(), 6u); 67 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Few)); 68 ASSERT_TRUE(categories.contains(PluralRules::Keyword::One)); 69 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Other)); 70 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Many)); 71 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Two)); 72 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Zero)); 73 } 74 75 TEST(IntlPluralRules, CategoriesCyOrdinal) 76 { 77 PluralRulesOptions options; 78 options.mPluralType = PluralRules::Type::Ordinal; 79 80 auto prResult = PluralRules::TryCreate("cy", options); 81 ASSERT_TRUE(prResult.isOk()); 82 auto pr = prResult.unwrap(); 83 84 auto catResult = pr->Categories(); 85 ASSERT_TRUE(catResult.isOk()); 86 auto categories = catResult.unwrap(); 87 88 ASSERT_EQ(categories.size(), 6u); 89 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Few)); 90 ASSERT_TRUE(categories.contains(PluralRules::Keyword::One)); 91 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Other)); 92 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Many)); 93 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Two)); 94 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Zero)); 95 } 96 97 TEST(IntlPluralRules, CategoriesBrCardinal) 98 { 99 PluralRulesOptions defaultOptions; 100 101 auto prResult = PluralRules::TryCreate("br", defaultOptions); 102 ASSERT_TRUE(prResult.isOk()); 103 auto pr = prResult.unwrap(); 104 105 auto catResult = pr->Categories(); 106 ASSERT_TRUE(catResult.isOk()); 107 auto categories = catResult.unwrap(); 108 109 ASSERT_EQ(categories.size(), 5u); 110 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Few)); 111 ASSERT_TRUE(categories.contains(PluralRules::Keyword::One)); 112 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Other)); 113 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Many)); 114 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Two)); 115 } 116 117 TEST(IntlPluralRules, CategoriesBrOrdinal) 118 { 119 PluralRulesOptions options; 120 options.mPluralType = PluralRules::Type::Ordinal; 121 122 auto prResult = PluralRules::TryCreate("br", options); 123 ASSERT_TRUE(prResult.isOk()); 124 auto pr = prResult.unwrap(); 125 126 auto catResult = pr->Categories(); 127 ASSERT_TRUE(catResult.isOk()); 128 auto categories = catResult.unwrap(); 129 130 ASSERT_EQ(categories.size(), 1u); 131 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Other)); 132 } 133 134 TEST(IntlPluralRules, CategoriesHsbCardinal) 135 { 136 PluralRulesOptions defaultOptions; 137 138 auto prResult = PluralRules::TryCreate("hsb", defaultOptions); 139 ASSERT_TRUE(prResult.isOk()); 140 auto pr = prResult.unwrap(); 141 142 auto catResult = pr->Categories(); 143 ASSERT_TRUE(catResult.isOk()); 144 auto categories = catResult.unwrap(); 145 146 ASSERT_EQ(categories.size(), 4u); 147 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Few)); 148 ASSERT_TRUE(categories.contains(PluralRules::Keyword::One)); 149 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Other)); 150 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Two)); 151 } 152 153 TEST(IntlPluralRules, CategoriesHsbOrdinal) 154 { 155 PluralRulesOptions options; 156 options.mPluralType = PluralRules::Type::Ordinal; 157 158 auto prResult = PluralRules::TryCreate("hsb", options); 159 ASSERT_TRUE(prResult.isOk()); 160 auto pr = prResult.unwrap(); 161 162 auto catResult = pr->Categories(); 163 ASSERT_TRUE(catResult.isOk()); 164 auto categories = catResult.unwrap(); 165 166 ASSERT_EQ(categories.size(), 1u); 167 ASSERT_TRUE(categories.contains(PluralRules::Keyword::Other)); 168 } 169 170 // PluralRules should define the sort order of the keywords. 171 // ICU returns these keywords in alphabetical order, so our implementation 172 // should do the same. 173 // 174 // https://github.com/tc39/ecma402/issues/578 175 TEST(IntlPluralRules, CategoriesSortOrder) 176 { 177 PluralRulesOptions defaultOptions; 178 179 auto prResult = PluralRules::TryCreate("cy", defaultOptions); 180 ASSERT_TRUE(prResult.isOk()); 181 auto pr = prResult.unwrap(); 182 183 PluralRules::Keyword expected[] = { 184 PluralRules::Keyword::Few, PluralRules::Keyword::Many, 185 PluralRules::Keyword::One, PluralRules::Keyword::Other, 186 PluralRules::Keyword::Two, PluralRules::Keyword::Zero, 187 }; 188 189 // Categories() returns an EnumSet so we want to ensure it still iterates 190 // over elements in the expected sorted order. 191 size_t index = 0; 192 193 auto catResult = pr->Categories(); 194 ASSERT_TRUE(catResult.isOk()); 195 auto categories = catResult.unwrap(); 196 197 for (const PluralRules::Keyword keyword : categories) { 198 ASSERT_EQ(keyword, expected[index++]); 199 } 200 } 201 202 // en Cardinal Plural Rules 203 // one: i = 1 and v = 0 @integer 1 204 // other: @integer 0, 2~16, 100, 1000, 10000, 100000, 1000000, … 205 // @decimal 0.0~1.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, … 206 TEST(IntlPluralRules, SelectEnCardinal) 207 { 208 PluralRulesOptions defaultOptions; 209 210 auto prResult = PluralRules::TryCreate("en", defaultOptions); 211 ASSERT_TRUE(prResult.isOk()); 212 auto pr = prResult.unwrap(); 213 214 TEST_SELECT(pr->Select(0.00), PluralRules::Keyword::Other); 215 TEST_SELECT(pr->Select(1.00), PluralRules::Keyword::One); 216 TEST_SELECT(pr->Select(1.01), PluralRules::Keyword::Other); 217 TEST_SELECT(pr->Select(0.99), PluralRules::Keyword::Other); 218 } 219 220 // en Ordinal Plural Rules 221 // one: n % 10 = 1 and n % 100 != 11 222 // @integer 1, 21, 31, 41, 51, 61, 71, 81, 101, 1001, …, 223 // two: n % 10 = 2 and n % 100 != 12 224 // @integer 2, 22, 32, 42, 52, 62, 72, 82, 102, 1002, …, 225 // few: n % 10 = 3 and n % 100 != 13 226 // @integer 3, 23, 33, 43, 53, 63, 73, 83, 103, 1003, …, 227 // other: @integer 0, 4~18, 100, 1000, 10000, 100000, 1000000, … 228 TEST(IntlPluralRules, SelectEnOrdinal) 229 { 230 PluralRulesOptions options; 231 options.mPluralType = PluralRules::Type::Ordinal; 232 233 auto prResult = PluralRules::TryCreate("en", options); 234 ASSERT_TRUE(prResult.isOk()); 235 auto pr = prResult.unwrap(); 236 237 TEST_SELECT(pr->Select(01.00), PluralRules::Keyword::One); 238 TEST_SELECT(pr->Select(21.00), PluralRules::Keyword::One); 239 TEST_SELECT(pr->Select(31.00), PluralRules::Keyword::One); 240 TEST_SELECT(pr->Select(41.00), PluralRules::Keyword::One); 241 242 TEST_SELECT(pr->Select(02.00), PluralRules::Keyword::Two); 243 TEST_SELECT(pr->Select(22.00), PluralRules::Keyword::Two); 244 TEST_SELECT(pr->Select(32.00), PluralRules::Keyword::Two); 245 TEST_SELECT(pr->Select(42.00), PluralRules::Keyword::Two); 246 247 TEST_SELECT(pr->Select(03.00), PluralRules::Keyword::Few); 248 TEST_SELECT(pr->Select(23.00), PluralRules::Keyword::Few); 249 TEST_SELECT(pr->Select(33.00), PluralRules::Keyword::Few); 250 TEST_SELECT(pr->Select(43.00), PluralRules::Keyword::Few); 251 252 TEST_SELECT(pr->Select(00.00), PluralRules::Keyword::Other); 253 TEST_SELECT(pr->Select(11.00), PluralRules::Keyword::Other); 254 TEST_SELECT(pr->Select(12.00), PluralRules::Keyword::Other); 255 TEST_SELECT(pr->Select(13.00), PluralRules::Keyword::Other); 256 } 257 258 // cy Cardinal Plural Rules 259 // zero: n = 0 @integer 0 @decimal 0.0, 0.00, 0.000, 0.0000, 260 // one: n = 1 @integer 1 @decimal 1.0, 1.00, 1.000, 1.0000, 261 // two: n = 2 @integer 2 @decimal 2.0, 2.00, 2.000, 2.0000, 262 // few: n = 3 @integer 3 @decimal 3.0, 3.00, 3.000, 3.0000, 263 // many: n = 6 @integer 6 @decimal 6.0, 6.00, 6.000, 6.0000, 264 // other: @integer 4, 5, 7~20, 100, 1000, 10000, 100000, 1000000, … 265 // @decimal 0.1~0.9, 1.1~1.7, 10.0, 100.0, 1000.0, 10000.0, 100000.0, … 266 TEST(IntlPluralRules, SelectCyCardinal) 267 { 268 PluralRulesOptions defaultOptions; 269 270 auto prResult = PluralRules::TryCreate("cy", defaultOptions); 271 ASSERT_TRUE(prResult.isOk()); 272 auto pr = prResult.unwrap(); 273 274 TEST_SELECT(pr->Select(0.00), PluralRules::Keyword::Zero); 275 TEST_SELECT(pr->Select(1.00), PluralRules::Keyword::One); 276 TEST_SELECT(pr->Select(2.00), PluralRules::Keyword::Two); 277 TEST_SELECT(pr->Select(3.00), PluralRules::Keyword::Few); 278 TEST_SELECT(pr->Select(4.00), PluralRules::Keyword::Other); 279 TEST_SELECT(pr->Select(5.00), PluralRules::Keyword::Other); 280 TEST_SELECT(pr->Select(6.00), PluralRules::Keyword::Many); 281 TEST_SELECT(pr->Select(7.00), PluralRules::Keyword::Other); 282 } 283 284 // cy Ordinal Plural Rules 285 // zero: n = 0,7,8,9 @integer 0, 7~9, 286 // one: n = 1 @integer 1, 287 // two: n = 2 @integer 2, 288 // few: n = 3,4 @integer 3, 4, 289 // many: n = 5,6 @integer 5, 6, 290 // other: @integer 10~25, 100, 1000, 10000, 100000, 1000000, … 291 TEST(IntlPluralRules, SelectCyOrdinal) 292 { 293 PluralRulesOptions options; 294 options.mPluralType = PluralRules::Type::Ordinal; 295 296 auto prResult = PluralRules::TryCreate("cy", options); 297 ASSERT_TRUE(prResult.isOk()); 298 auto pr = prResult.unwrap(); 299 300 TEST_SELECT(pr->Select(0.00), PluralRules::Keyword::Zero); 301 TEST_SELECT(pr->Select(7.00), PluralRules::Keyword::Zero); 302 TEST_SELECT(pr->Select(8.00), PluralRules::Keyword::Zero); 303 TEST_SELECT(pr->Select(9.00), PluralRules::Keyword::Zero); 304 305 TEST_SELECT(pr->Select(1.00), PluralRules::Keyword::One); 306 307 TEST_SELECT(pr->Select(2.00), PluralRules::Keyword::Two); 308 309 TEST_SELECT(pr->Select(3.00), PluralRules::Keyword::Few); 310 TEST_SELECT(pr->Select(4.00), PluralRules::Keyword::Few); 311 312 TEST_SELECT(pr->Select(5.00), PluralRules::Keyword::Many); 313 TEST_SELECT(pr->Select(6.00), PluralRules::Keyword::Many); 314 315 TEST_SELECT(pr->Select(10.00), PluralRules::Keyword::Other); 316 TEST_SELECT(pr->Select(11.00), PluralRules::Keyword::Other); 317 } 318 319 // br Cardinal Plural Rules 320 // one: n % 10 = 1 and n % 100 != 11,71,91 321 // @integer 1, 21, 31, 41, 51, 61, 81, 101, 1001, … 322 // @decimal 1.0, 21.0, 31.0, 41.0, 51.0, 61.0, 81.0, 101.0, 1001.0, … 323 // two: n % 10 = 2 and n % 100 != 12,72,92 324 // @integer 2, 22, 32, 42, 52, 62, 82, 102, 1002, … 325 // @decimal 2.0, 22.0, 32.0, 42.0, 52.0, 62.0, 82.0, 102.0, 1002.0, … 326 // few: n % 10 = 3..4,9 and n % 100 != 10..19,70..79,90..99 327 // @integer 3, 4, 9, 23, 24, 29, 33, 34, 39, 43, 44, 49, 103, 1003, … 328 // @decimal 3.0, 4.0, 9.0, 23.0, 24.0, 29.0, 33.0, 34.0, 103.0, 1003.0, … 329 // many: n != 0 and n % 1000000 = 0 330 // @integer 1000000, … 331 // @decimal 1000000.0, 1000000.00, 1000000.000, 1000000.0000, … 332 // other: @integer 0, 5~8, 10~20, 100, 1000, 10000, 100000, … 333 // @decimal 0.0~0.9, 1.1~1.6, 10.0, 100.0, 1000.0, 10000.0, 100000.0, … 334 TEST(IntlPluralRules, SelectBrCardinal) 335 { 336 PluralRulesOptions defaultOptions; 337 338 auto prResult = PluralRules::TryCreate("br", defaultOptions); 339 ASSERT_TRUE(prResult.isOk()); 340 auto pr = prResult.unwrap(); 341 342 TEST_SELECT(pr->Select(00.00), PluralRules::Keyword::Other); 343 344 TEST_SELECT(pr->Select(01.00), PluralRules::Keyword::One); 345 TEST_SELECT(pr->Select(11.00), PluralRules::Keyword::Other); 346 TEST_SELECT(pr->Select(21.00), PluralRules::Keyword::One); 347 TEST_SELECT(pr->Select(31.00), PluralRules::Keyword::One); 348 349 TEST_SELECT(pr->Select(02.00), PluralRules::Keyword::Two); 350 TEST_SELECT(pr->Select(12.00), PluralRules::Keyword::Other); 351 TEST_SELECT(pr->Select(22.00), PluralRules::Keyword::Two); 352 TEST_SELECT(pr->Select(32.00), PluralRules::Keyword::Two); 353 354 TEST_SELECT(pr->Select(03.00), PluralRules::Keyword::Few); 355 TEST_SELECT(pr->Select(04.00), PluralRules::Keyword::Few); 356 TEST_SELECT(pr->Select(09.00), PluralRules::Keyword::Few); 357 TEST_SELECT(pr->Select(23.00), PluralRules::Keyword::Few); 358 359 TEST_SELECT(pr->Select(1000000), PluralRules::Keyword::Many); 360 361 TEST_SELECT(pr->Select(999999), PluralRules::Keyword::Other); 362 TEST_SELECT(pr->Select(1000005), PluralRules::Keyword::Other); 363 } 364 365 // br Ordinal Plural Rules 366 // br has no rules for Ordinal, so everything is PluralRules::Keyword::Other. 367 TEST(IntlPluralRules, SelectBrOrdinal) 368 { 369 PluralRulesOptions options; 370 options.mPluralType = PluralRules::Type::Ordinal; 371 372 auto prResult = PluralRules::TryCreate("br", options); 373 ASSERT_TRUE(prResult.isOk()); 374 auto pr = prResult.unwrap(); 375 376 TEST_SELECT(pr->Select(00.00), PluralRules::Keyword::Other); 377 378 TEST_SELECT(pr->Select(01.00), PluralRules::Keyword::Other); 379 TEST_SELECT(pr->Select(11.00), PluralRules::Keyword::Other); 380 TEST_SELECT(pr->Select(21.00), PluralRules::Keyword::Other); 381 TEST_SELECT(pr->Select(31.00), PluralRules::Keyword::Other); 382 383 TEST_SELECT(pr->Select(02.00), PluralRules::Keyword::Other); 384 TEST_SELECT(pr->Select(12.00), PluralRules::Keyword::Other); 385 TEST_SELECT(pr->Select(22.00), PluralRules::Keyword::Other); 386 TEST_SELECT(pr->Select(32.00), PluralRules::Keyword::Other); 387 388 TEST_SELECT(pr->Select(03.00), PluralRules::Keyword::Other); 389 TEST_SELECT(pr->Select(04.00), PluralRules::Keyword::Other); 390 TEST_SELECT(pr->Select(09.00), PluralRules::Keyword::Other); 391 TEST_SELECT(pr->Select(23.00), PluralRules::Keyword::Other); 392 393 TEST_SELECT(pr->Select(1000000), PluralRules::Keyword::Other); 394 395 TEST_SELECT(pr->Select(999999), PluralRules::Keyword::Other); 396 TEST_SELECT(pr->Select(1000005), PluralRules::Keyword::Other); 397 } 398 399 // hsb Cardinal Plural Rules 400 // one: v = 0 and i % 100 = 1 or f % 100 = 1 401 // @integer 1, 101, 201, 301, 401, 501, 601, 701, 1001, … 402 // @decimal 0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 10.1, 100.1, 1000.1, 403 // …, 404 // two: v = 0 and i % 100 = 2 or f % 100 = 2 405 // @integer 2, 102, 202, 302, 402, 502, 602, 702, 1002, … 406 // @decimal 0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2, 10.2, 100.2, 1000.2, 407 // …, 408 // few: v = 0 and i % 100 = 3..4 or f % 100 = 3..4 409 // @integer 3, 4, 103, 104, 203, 204, 303, 304, 403, 404, 503, 504, 603, 410 // 604, 703, 704, 1003, … 411 // @decimal 0.3, 412 // 0.4, 1.3, 1.4, 2.3, 2.4, 3.3, 3.4, 4.3, 4.4, 5.3, 5.4, 6.3, 6.4, 7.3, 7.4, 413 // 10.3, 100.3, 1000.3, …, 414 // other: @integer 0, 5~19, 100, 1000, 10000, 100000, 1000000, … 415 // @decimal 0.0, 0.5~1.0, 1.5~2.0, 2.5~2.7, 10.0, 100.0, 1000.0, 10000.0, 416 // 100000.0, 1000000.0, … 417 TEST(IntlPluralRules, SelectHsbCardinal) 418 { 419 PluralRulesOptions defaultOptions; 420 421 auto prResult = PluralRules::TryCreate("hsb", defaultOptions); 422 ASSERT_TRUE(prResult.isOk()); 423 auto pr = prResult.unwrap(); 424 425 TEST_SELECT(pr->Select(1.00), PluralRules::Keyword::One); 426 TEST_SELECT(pr->Select(101.00), PluralRules::Keyword::One); 427 428 TEST_SELECT(pr->Select(2.00), PluralRules::Keyword::Two); 429 TEST_SELECT(pr->Select(102.00), PluralRules::Keyword::Two); 430 431 TEST_SELECT(pr->Select(3.00), PluralRules::Keyword::Few); 432 TEST_SELECT(pr->Select(4.00), PluralRules::Keyword::Few); 433 TEST_SELECT(pr->Select(103.00), PluralRules::Keyword::Few); 434 435 TEST_SELECT(pr->Select(0.00), PluralRules::Keyword::Other); 436 TEST_SELECT(pr->Select(5.00), PluralRules::Keyword::Other); 437 TEST_SELECT(pr->Select(19.00), PluralRules::Keyword::Other); 438 TEST_SELECT(pr->Select(100.00), PluralRules::Keyword::Other); 439 } 440 441 // hsb Ordinal Plural Rules 442 // other: @integer 0~15, 100, 1000, 10000, 100000, 1000000, … 443 TEST(IntlPluralRules, SelectHsbOrdinal) 444 { 445 PluralRulesOptions options; 446 options.mPluralType = PluralRules::Type::Ordinal; 447 448 auto prResult = PluralRules::TryCreate("hsb", options); 449 ASSERT_TRUE(prResult.isOk()); 450 auto pr = prResult.unwrap(); 451 452 TEST_SELECT(pr->Select(00.00), PluralRules::Keyword::Other); 453 454 TEST_SELECT(pr->Select(01.00), PluralRules::Keyword::Other); 455 TEST_SELECT(pr->Select(11.00), PluralRules::Keyword::Other); 456 TEST_SELECT(pr->Select(21.00), PluralRules::Keyword::Other); 457 TEST_SELECT(pr->Select(31.00), PluralRules::Keyword::Other); 458 459 TEST_SELECT(pr->Select(02.00), PluralRules::Keyword::Other); 460 TEST_SELECT(pr->Select(12.00), PluralRules::Keyword::Other); 461 TEST_SELECT(pr->Select(22.00), PluralRules::Keyword::Other); 462 TEST_SELECT(pr->Select(32.00), PluralRules::Keyword::Other); 463 464 TEST_SELECT(pr->Select(03.00), PluralRules::Keyword::Other); 465 TEST_SELECT(pr->Select(04.00), PluralRules::Keyword::Other); 466 TEST_SELECT(pr->Select(09.00), PluralRules::Keyword::Other); 467 TEST_SELECT(pr->Select(23.00), PluralRules::Keyword::Other); 468 469 TEST_SELECT(pr->Select(1000000), PluralRules::Keyword::Other); 470 471 TEST_SELECT(pr->Select(999999), PluralRules::Keyword::Other); 472 TEST_SELECT(pr->Select(1000005), PluralRules::Keyword::Other); 473 } 474 475 TEST(IntlPluralRules, DefaultFractionDigits) 476 { 477 PluralRulesOptions defaultOptions; 478 479 auto prResult = PluralRules::TryCreate("en", defaultOptions); 480 ASSERT_TRUE(prResult.isOk()); 481 auto pr = prResult.unwrap(); 482 483 TEST_SELECT(pr->Select(1.000), PluralRules::Keyword::One); 484 485 TEST_SELECT(pr->Select(1.100), PluralRules::Keyword::Other); 486 TEST_SELECT(pr->Select(1.010), PluralRules::Keyword::Other); 487 TEST_SELECT(pr->Select(1.001), PluralRules::Keyword::Other); 488 489 TEST_SELECT(pr->Select(0.900), PluralRules::Keyword::Other); 490 TEST_SELECT(pr->Select(0.990), PluralRules::Keyword::Other); 491 TEST_SELECT(pr->Select(0.999), PluralRules::Keyword::Other); 492 } 493 494 TEST(IntlPluralRules, MaxFractionDigitsZero) 495 { 496 PluralRulesOptions options; 497 options.mFractionDigits = Some(std::pair<uint32_t, uint32_t>(0, 0)); 498 499 auto prResult = PluralRules::TryCreate("en", options); 500 ASSERT_TRUE(prResult.isOk()); 501 auto pr = prResult.unwrap(); 502 503 TEST_SELECT(pr->Select(1.000), PluralRules::Keyword::One); 504 TEST_SELECT(pr->Select(1.100), PluralRules::Keyword::One); 505 TEST_SELECT(pr->Select(1.010), PluralRules::Keyword::One); 506 TEST_SELECT(pr->Select(1.001), PluralRules::Keyword::One); 507 TEST_SELECT(pr->Select(0.900), PluralRules::Keyword::One); 508 TEST_SELECT(pr->Select(0.990), PluralRules::Keyword::One); 509 TEST_SELECT(pr->Select(0.999), PluralRules::Keyword::One); 510 } 511 512 TEST(IntlPluralRules, MaxFractionDigitsOne) 513 { 514 PluralRulesOptions options; 515 options.mFractionDigits = Some(std::pair<uint32_t, uint32_t>(0, 1)); 516 517 auto prResult = PluralRules::TryCreate("en", options); 518 ASSERT_TRUE(prResult.isOk()); 519 auto pr = prResult.unwrap(); 520 521 TEST_SELECT(pr->Select(1.000), PluralRules::Keyword::One); 522 TEST_SELECT(pr->Select(1.010), PluralRules::Keyword::One); 523 TEST_SELECT(pr->Select(1.001), PluralRules::Keyword::One); 524 TEST_SELECT(pr->Select(0.990), PluralRules::Keyword::One); 525 TEST_SELECT(pr->Select(0.999), PluralRules::Keyword::One); 526 527 TEST_SELECT(pr->Select(1.100), PluralRules::Keyword::Other); 528 TEST_SELECT(pr->Select(0.900), PluralRules::Keyword::Other); 529 } 530 531 TEST(IntlPluralRules, MaxSignificantDigitsOne) 532 { 533 PluralRulesOptions options; 534 options.mSignificantDigits = Some(std::pair<uint32_t, uint32_t>(1, 1)); 535 536 auto prResult = PluralRules::TryCreate("en", options); 537 ASSERT_TRUE(prResult.isOk()); 538 auto pr = prResult.unwrap(); 539 540 TEST_SELECT(pr->Select(1.000), PluralRules::Keyword::One); 541 TEST_SELECT(pr->Select(1.100), PluralRules::Keyword::One); 542 TEST_SELECT(pr->Select(1.010), PluralRules::Keyword::One); 543 TEST_SELECT(pr->Select(1.001), PluralRules::Keyword::One); 544 TEST_SELECT(pr->Select(0.990), PluralRules::Keyword::One); 545 TEST_SELECT(pr->Select(0.999), PluralRules::Keyword::One); 546 547 TEST_SELECT(pr->Select(0.900), PluralRules::Keyword::Other); 548 } 549 550 TEST(IntlPluralRules, MaxFractionDigitsTwo) 551 { 552 PluralRulesOptions options; 553 options.mFractionDigits = Some(std::pair<uint32_t, uint32_t>(0, 2)); 554 555 auto prResult = PluralRules::TryCreate("en", options); 556 ASSERT_TRUE(prResult.isOk()); 557 auto pr = prResult.unwrap(); 558 559 TEST_SELECT(pr->Select(1.000), PluralRules::Keyword::One); 560 TEST_SELECT(pr->Select(1.001), PluralRules::Keyword::One); 561 TEST_SELECT(pr->Select(0.999), PluralRules::Keyword::One); 562 563 TEST_SELECT(pr->Select(1.100), PluralRules::Keyword::Other); 564 TEST_SELECT(pr->Select(1.010), PluralRules::Keyword::Other); 565 TEST_SELECT(pr->Select(0.900), PluralRules::Keyword::Other); 566 TEST_SELECT(pr->Select(0.990), PluralRules::Keyword::Other); 567 } 568 569 TEST(IntlPluralRules, MaxSignificantDigitsTwo) 570 { 571 PluralRulesOptions options; 572 options.mSignificantDigits = Some(std::pair<uint32_t, uint32_t>(1, 2)); 573 574 auto prResult = PluralRules::TryCreate("en", options); 575 ASSERT_TRUE(prResult.isOk()); 576 auto pr = prResult.unwrap(); 577 578 TEST_SELECT(pr->Select(1.000), PluralRules::Keyword::One); 579 TEST_SELECT(pr->Select(1.010), PluralRules::Keyword::One); 580 TEST_SELECT(pr->Select(1.001), PluralRules::Keyword::One); 581 TEST_SELECT(pr->Select(0.999), PluralRules::Keyword::One); 582 583 TEST_SELECT(pr->Select(1.100), PluralRules::Keyword::Other); 584 TEST_SELECT(pr->Select(0.900), PluralRules::Keyword::Other); 585 TEST_SELECT(pr->Select(0.990), PluralRules::Keyword::Other); 586 } 587 588 // en Plural Range Rules 589 // other: one other 590 // other: other one 591 // other: other other 592 TEST(IntlPluralRules, SelectRangeEn) 593 { 594 for (auto type : {PluralRules::Type::Cardinal, PluralRules::Type::Ordinal}) { 595 PluralRulesOptions options; 596 options.mPluralType = type; 597 auto prResult = PluralRules::TryCreate("en", options); 598 ASSERT_TRUE(prResult.isOk()); 599 auto pr = prResult.unwrap(); 600 601 TEST_SELECT(pr->SelectRange(0, 0), PluralRules::Keyword::Other); 602 TEST_SELECT(pr->SelectRange(0, 1), PluralRules::Keyword::Other); 603 TEST_SELECT(pr->SelectRange(0, 2), PluralRules::Keyword::Other); 604 TEST_SELECT(pr->SelectRange(1, 1), PluralRules::Keyword::Other); 605 TEST_SELECT(pr->SelectRange(1, 2), PluralRules::Keyword::Other); 606 TEST_SELECT(pr->SelectRange(1, 10), PluralRules::Keyword::Other); 607 } 608 } 609 610 // fr Cardinal Plural Rules 611 // one: i = 0,1 612 // @integer 0, 1 613 // @decimal 0.0~1.5 614 // many: e = 0 and i != 0 and i % 1000000 = 0 and v = 0 or e != 0..5 615 // @integer 1000000, 1c6, 2c6, 3c6, 4c6, 5c6, 6c6, … 616 // @decimal 1.0000001c6, 1.1c6, 2.0000001c6, 2.1c6, 3.0000001c6, 3.1c6, 617 // … 618 // other: @integer 2~17, 100, 1000, 10000, 100000, 1c3, 2c3, 3c3, 4c3, 5c3, 619 // 6c3, … 620 // @decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 621 // 1000000.0, 1.0001c3, 1.1c3, 2.0001c3, 2.1c3, 3.0001c3, 3.1c3, … 622 // 623 // fr Plural Range Rules 624 // one: one one 625 // other: one other 626 // other: other other 627 TEST(IntlPluralRules, SelectRangeFrCardinal) 628 { 629 PluralRulesOptions options; 630 options.mPluralType = PluralRules::Type::Cardinal; 631 auto prResult = PluralRules::TryCreate("fr", options); 632 ASSERT_TRUE(prResult.isOk()); 633 auto pr = prResult.unwrap(); 634 635 TEST_SELECT(pr->SelectRange(0, 0), PluralRules::Keyword::One); 636 TEST_SELECT(pr->SelectRange(0, 1), PluralRules::Keyword::One); 637 TEST_SELECT(pr->SelectRange(0, 2), PluralRules::Keyword::Other); 638 TEST_SELECT(pr->SelectRange(1, 1), PluralRules::Keyword::One); 639 TEST_SELECT(pr->SelectRange(1, 2), PluralRules::Keyword::Other); 640 TEST_SELECT(pr->SelectRange(1, 10), PluralRules::Keyword::Other); 641 TEST_SELECT(pr->SelectRange(1, 1000000), PluralRules::Keyword::Other); 642 } 643 644 // fr Ordinal Plural Rules 645 // one: n = 1 @integer 1 646 // other: @integer 0, 2~16, 100, 1000, 10000, 100000, 1000000, … 647 // 648 // fr Plural Range Rules 649 // one: one one 650 // other: one other 651 // other: other other 652 TEST(IntlPluralRules, SelectRangeFrOrdinal) 653 { 654 PluralRulesOptions options; 655 options.mPluralType = PluralRules::Type::Ordinal; 656 auto prResult = PluralRules::TryCreate("fr", options); 657 ASSERT_TRUE(prResult.isOk()); 658 auto pr = prResult.unwrap(); 659 660 TEST_SELECT(pr->SelectRange(0, 0), PluralRules::Keyword::Other); 661 TEST_SELECT(pr->SelectRange(0, 1), PluralRules::Keyword::Other); 662 TEST_SELECT(pr->SelectRange(0, 2), PluralRules::Keyword::Other); 663 TEST_SELECT(pr->SelectRange(1, 1), PluralRules::Keyword::One); 664 TEST_SELECT(pr->SelectRange(1, 2), PluralRules::Keyword::Other); 665 TEST_SELECT(pr->SelectRange(1, 10), PluralRules::Keyword::Other); 666 TEST_SELECT(pr->SelectRange(1, 1000000), PluralRules::Keyword::Other); 667 } 668 669 } // namespace intl 670 } // namespace mozilla