Debian Codegraph @

debian/rust-vendor/icu_plurals/tests/operands.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 mod fixtures;
6
7 use fixed_decimal::Decimal;
8 use icu_plurals::PluralOperands;
9 #[cfg(feature = "unstable")]
10 use icu_plurals::RawPluralOperands;
11
12 #[test]
13 fn test_parsing_operands() {
14 let test_set: fixtures::OperandsTestSet =
15 serde_json::from_str(include_str!("fixtures/operands.json"))
16 .expect("Failed to read a fixture");
17
18 for test in test_set.string {
19 let operands: PluralOperands = test.input.parse().expect("Failed to parse to operands.");
20 assert_eq!(operands, test.output.into());
21 }
22
23 for test in test_set.int {
24 let operands: PluralOperands = test.input.into();
25 assert_eq!(operands, test.output.clone().into());
26
27 if test.input.is_positive() {
28 let x = test.input as usize;
29 let operands: PluralOperands = x.into();
30 assert_eq!(operands, test.output.into());
31 }
32 }
33
34 #[cfg(feature = "unstable")]
35 for test in test_set.floats {
36 let t = test.clone();
37 let operands: PluralOperands = t.output.into();
38 let raw_operands = RawPluralOperands::from(operands);
39 let expected: f64 = t.input.abs();
40 let fraction = raw_operands.t as f64 / 10_f64.powi(raw_operands.v as i32);
41 let n = raw_operands.i as f64 + fraction;
42 let actual = (n - expected).abs();
43 assert!(
44 actual < f64::EPSILON,
45 "actual: {}, for test: {:?}",
46 actual,
47 test
48 );
49 }
50 }
51
52 #[test]
53 fn test_parsing_operand_errors() {
54 let operands: Result<PluralOperands, _> = "".parse();
55 assert!(operands.is_err());
56 }
57
58 #[test]
59 fn test_from_fixed_decimals() {
60 let test_set: fixtures::OperandsTestSet =
61 serde_json::from_str(include_str!("fixtures/operands.json"))
62 .expect("Failed to read a fixture");
63 for test in test_set.from_test {
64 let input: Decimal = Decimal::from(&test.input);
65 let actual: PluralOperands = PluralOperands::from(&input);
66 let expected: PluralOperands = PluralOperands::from(test.expected.clone());
67 assert_eq!(
68 expected, actual,
69 "\n\t(expected==left; actual==right)\n\t\t{:?}",
70 test
71 );
72 }
73 }