| 1 | #![cfg(target_arch = "wasm32")] |
| 2 | |
| 3 | // Tests use both `Decimal::from_str` (the standard `FromStr` trait) and `Decimal::from_string` |
| 4 | // (the wasm-bindgen binding). This is intentional: `from_string` tests exercise the wasm API |
| 5 | // surface, while `from_str` tests verify that core Decimal functionality works under wasm. |
| 6 | |
| 7 | use core::str::FromStr; |
| 8 | use rust_decimal::Decimal; |
| 9 | use wasm_bindgen_test::*; |
| 10 | |
| 11 | // from_number |
| 12 | |
| 13 | #[wasm_bindgen_test] |
| 14 | fn it_converts_positive_integer_from_number() { |
| 15 | let a = Decimal::from_number(42.0).unwrap(); |
| 16 | assert!(!a.is_sign_negative()); |
| 17 | assert_eq!("42", a.to_string()); |
| 18 | } |
| 19 | |
| 20 | #[wasm_bindgen_test] |
| 21 | fn it_converts_negative_integer_from_number() { |
| 22 | let a = Decimal::from_number(-123.0).unwrap(); |
| 23 | assert!(a.is_sign_negative()); |
| 24 | assert_eq!("-123", a.to_string()); |
| 25 | } |
| 26 | |
| 27 | #[wasm_bindgen_test] |
| 28 | fn it_converts_zero_from_number() { |
| 29 | let a = Decimal::from_number(0.0).unwrap(); |
| 30 | assert_eq!("0", a.to_string()); |
| 31 | } |
| 32 | |
| 33 | #[wasm_bindgen_test] |
| 34 | fn it_converts_positive_float_from_number() { |
| 35 | let a = Decimal::from_number(3.14).unwrap(); |
| 36 | assert_eq!("3.14", a.to_string()); |
| 37 | } |
| 38 | |
| 39 | #[wasm_bindgen_test] |
| 40 | fn it_converts_small_fractional_from_number() { |
| 41 | let a = Decimal::from_number(0.001).unwrap(); |
| 42 | assert_eq!("0.001", a.to_string()); |
| 43 | } |
| 44 | |
| 45 | #[wasm_bindgen_test] |
| 46 | fn it_converts_large_number_from_number() { |
| 47 | let a = Decimal::from_number(1_000_000_000.0).unwrap(); |
| 48 | assert_eq!("1000000000", a.to_string()); |
| 49 | } |
| 50 | |
| 51 | #[wasm_bindgen_test] |
| 52 | fn it_returns_none_for_nan() { |
| 53 | assert!(Decimal::from_number(f64::NAN).is_none()); |
| 54 | } |
| 55 | |
| 56 | #[wasm_bindgen_test] |
| 57 | fn it_returns_none_for_positive_infinity() { |
| 58 | assert!(Decimal::from_number(f64::INFINITY).is_none()); |
| 59 | } |
| 60 | |
| 61 | #[wasm_bindgen_test] |
| 62 | fn it_returns_none_for_negative_infinity() { |
| 63 | assert!(Decimal::from_number(f64::NEG_INFINITY).is_none()); |
| 64 | } |
| 65 | |
| 66 | // from_string |
| 67 | |
| 68 | #[wasm_bindgen_test] |
| 69 | fn it_converts_positive_float_from_string() { |
| 70 | let a = Decimal::from_string("233.323223").unwrap(); |
| 71 | assert!(!a.is_sign_negative()); |
| 72 | assert_eq!(a.scale(), 6); |
| 73 | assert_eq!("233.323223", a.to_string()); |
| 74 | } |
| 75 | |
| 76 | #[wasm_bindgen_test] |
| 77 | fn it_converts_negative_float_from_string() { |
| 78 | let a = Decimal::from_string("-233.43343").unwrap(); |
| 79 | assert!(a.is_sign_negative()); |
| 80 | assert_eq!("-233.43343", a.to_string()); |
| 81 | } |
| 82 | |
| 83 | #[wasm_bindgen_test] |
| 84 | fn it_converts_big_integer_from_string() { |
| 85 | let a = Decimal::from_string("79228162514264337593543950330").unwrap(); |
| 86 | assert_eq!("79228162514264337593543950330", a.to_string()); |
| 87 | } |
| 88 | |
| 89 | #[wasm_bindgen_test] |
| 90 | fn it_returns_none_for_empty_string() { |
| 91 | assert!(Decimal::from_string("").is_none()); |
| 92 | } |
| 93 | |
| 94 | #[wasm_bindgen_test] |
| 95 | fn it_returns_none_for_invalid_string() { |
| 96 | assert!(Decimal::from_string("not_a_number").is_none()); |
| 97 | } |
| 98 | |
| 99 | // to_string_js |
| 100 | |
| 101 | #[wasm_bindgen_test] |
| 102 | fn it_converts_decimal_to_string() { |
| 103 | let a = Decimal::from_string("12.3456789").unwrap(); |
| 104 | assert_eq!("12.3456789", a.to_string_js()); |
| 105 | } |
| 106 | |
| 107 | #[wasm_bindgen_test] |
| 108 | fn it_converts_negative_decimal_to_string() { |
| 109 | let a = Decimal::from_number(-42.0).unwrap(); |
| 110 | assert_eq!("-42", a.to_string_js()); |
| 111 | } |
| 112 | |
| 113 | // to_number |
| 114 | |
| 115 | #[wasm_bindgen_test] |
| 116 | fn it_converts_positive_decimal_to_number() { |
| 117 | let a = Decimal::from_str("233.323223").unwrap(); |
| 118 | let n = a.to_number(); |
| 119 | assert!((n - 233.323223).abs() < 1e-10); |
| 120 | } |
| 121 | |
| 122 | #[wasm_bindgen_test] |
| 123 | fn it_converts_negative_decimal_to_number() { |
| 124 | let a = Decimal::from_str("-233.43343").unwrap(); |
| 125 | let n = a.to_number(); |
| 126 | assert!((n - -233.43343).abs() < 1e-10); |
| 127 | } |
| 128 | |
| 129 | #[wasm_bindgen_test] |
| 130 | fn it_converts_zero_to_number() { |
| 131 | let a = Decimal::from_str("0").unwrap(); |
| 132 | assert_eq!(a.to_number(), 0.0); |
| 133 | } |
| 134 | |
| 135 | // Round-trip: from_number -> to_number |
| 136 | |
| 137 | #[wasm_bindgen_test] |
| 138 | fn it_round_trips_positive_float() { |
| 139 | let n = 12345.6789; |
| 140 | let a = Decimal::from_number(n).unwrap(); |
| 141 | assert!((a.to_number() - n).abs() < 1e-10); |
| 142 | } |
| 143 | |
| 144 | #[wasm_bindgen_test] |
| 145 | fn it_round_trips_negative_float() { |
| 146 | let n = -9876.54321; |
| 147 | let a = Decimal::from_number(n).unwrap(); |
| 148 | assert!((a.to_number() - n).abs() < 1e-10); |
| 149 | } |
| 150 | |
| 151 | // Round-trip: from_str -> to_number -> from_number -> to_string |
| 152 | |
| 153 | #[wasm_bindgen_test] |
| 154 | fn it_round_trips_through_string_and_number() { |
| 155 | let tests = ["1.5", "100", "0.001", "-42.42"]; |
| 156 | for test in &tests { |
| 157 | let a = Decimal::from_str(test).unwrap(); |
| 158 | let n = a.to_number(); |
| 159 | let b = Decimal::from_number(n).unwrap(); |
| 160 | assert_eq!(test.to_string(), b.to_string(), "Round-trip failed for {test}"); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Round-trip: from_string -> to_string_js |
| 165 | |
| 166 | #[wasm_bindgen_test] |
| 167 | fn it_round_trips_from_string_to_string_js() { |
| 168 | let tests = [ |
| 169 | "12.3456789", |
| 170 | "79228162514264337593543950330", |
| 171 | "-5233.9008808150288439427720175", |
| 172 | "0", |
| 173 | "0.001", |
| 174 | ]; |
| 175 | for test in &tests { |
| 176 | let a = Decimal::from_string(test).unwrap(); |
| 177 | assert_eq!(test.to_string(), a.to_string_js(), "Round-trip failed for {test}"); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Round-trip: from_string -> to_number |
| 182 | |
| 183 | #[wasm_bindgen_test] |
| 184 | fn it_round_trips_from_string_to_number() { |
| 185 | let tests = [("1.5", 1.5), ("100", 100.0), ("-42.42", -42.42), ("0.001", 0.001)]; |
| 186 | for (input, expected) in &tests { |
| 187 | let a = Decimal::from_string(input).unwrap(); |
| 188 | let n = a.to_number(); |
| 189 | assert!((n - expected).abs() < 1e-10, "Round-trip failed for {input}: got {n}"); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Arithmetic via wasm-constructed decimals |
| 194 | // |
| 195 | // Note: `from_number` converts via `f64`, which may introduce trailing scale |
| 196 | // (e.g. `1.5` has scale 1). Arithmetic preserves scale from the operands, so |
| 197 | // results may include trailing zeros (e.g. `1.5 + 2.5 = "4.0"` not `"4"`). |
| 198 | |
| 199 | #[wasm_bindgen_test] |
| 200 | fn it_can_add_wasm_decimals() { |
| 201 | let a = Decimal::from_number(1.5).unwrap(); |
| 202 | let b = Decimal::from_number(2.5).unwrap(); |
| 203 | assert_eq!("4.0", (a + b).to_string()); |
| 204 | } |
| 205 | |
| 206 | #[wasm_bindgen_test] |
| 207 | fn it_can_subtract_wasm_decimals() { |
| 208 | let a = Decimal::from_number(10.0).unwrap(); |
| 209 | let b = Decimal::from_number(3.5).unwrap(); |
| 210 | assert_eq!("6.5", (a - b).to_string()); |
| 211 | } |
| 212 | |
| 213 | #[wasm_bindgen_test] |
| 214 | fn it_can_multiply_wasm_decimals() { |
| 215 | // Multiplication of two integer-valued f64s (scale 0) produces scale 0 |
| 216 | let a = Decimal::from_number(6.0).unwrap(); |
| 217 | let b = Decimal::from_number(7.0).unwrap(); |
| 218 | assert_eq!("42", (a * b).to_string()); |
| 219 | } |
| 220 | |
| 221 | #[wasm_bindgen_test] |
| 222 | fn it_can_divide_wasm_decimals() { |
| 223 | // Division preserves scale from operands, so 10.0 / 4.0 yields "2.50" |
| 224 | let a = Decimal::from_number(10.0).unwrap(); |
| 225 | let b = Decimal::from_number(4.0).unwrap(); |
| 226 | assert_eq!("2.50", (a / b).to_string()); |
| 227 | } |
| 228 | |
| 229 | // Core functionality works under wasm |
| 230 | |
| 231 | #[wasm_bindgen_test] |
| 232 | fn it_can_parse_string_in_wasm() { |
| 233 | let a = Decimal::from_str("79228162514264337593543950330").unwrap(); |
| 234 | assert_eq!("79228162514264337593543950330", a.to_string()); |
| 235 | } |
| 236 | |
| 237 | #[wasm_bindgen_test] |
| 238 | fn it_can_serialize_deserialize_in_wasm() { |
| 239 | let tests = [ |
| 240 | "12.3456789", |
| 241 | "5233.9008808150288439427720175", |
| 242 | "-5233.9008808150288439427720175", |
| 243 | ]; |
| 244 | for test in &tests { |
| 245 | let a = Decimal::from_str(test).unwrap(); |
| 246 | let bytes = a.serialize(); |
| 247 | let b = Decimal::deserialize(bytes); |
| 248 | assert_eq!(test.to_string(), b.to_string()); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | #[wasm_bindgen_test] |
| 253 | fn it_can_compare_wasm_decimals() { |
| 254 | let a = Decimal::from_number(1.0).unwrap(); |
| 255 | let b = Decimal::from_number(2.0).unwrap(); |
| 256 | let c = Decimal::from_number(1.0).unwrap(); |
| 257 | assert!(a < b); |
| 258 | assert!(b > a); |
| 259 | assert_eq!(a, c); |
| 260 | } |
| 261 | |
| 262 | #[wasm_bindgen_test] |
| 263 | fn it_can_check_sign_of_wasm_decimal() { |
| 264 | let pos = Decimal::from_number(5.0).unwrap(); |
| 265 | let neg = Decimal::from_number(-5.0).unwrap(); |
| 266 | assert!(!pos.is_sign_negative()); |
| 267 | assert!(neg.is_sign_negative()); |
| 268 | } |
| 269 | |
| 270 | #[wasm_bindgen_test] |
| 271 | fn it_can_check_zero_of_wasm_decimal() { |
| 272 | let zero = Decimal::from_number(0.0).unwrap(); |
| 273 | let non_zero = Decimal::from_number(1.0).unwrap(); |
| 274 | assert!(zero.is_zero()); |
| 275 | assert!(!non_zero.is_zero()); |
| 276 | } |
| 277 | |
| 278 | #[wasm_bindgen_test] |
| 279 | fn it_can_round_wasm_decimal() { |
| 280 | let a = Decimal::from_str("2.567").unwrap(); |
| 281 | let rounded = a.round_dp(2); |
| 282 | assert_eq!("2.57", rounded.to_string()); |
| 283 | } |
| 284 | |
| 285 | #[wasm_bindgen_test] |
| 286 | fn it_can_floor_and_ceil_wasm_decimal() { |
| 287 | let a = Decimal::from_str("2.3").unwrap(); |
| 288 | assert_eq!("2", a.floor().to_string()); |
| 289 | assert_eq!("3", a.ceil().to_string()); |
| 290 | } |
| 291 | |
| 292 | #[wasm_bindgen_test] |
| 293 | fn it_can_extract_mantissa_and_scale_in_wasm() { |
| 294 | let a = Decimal::from_str("1.123456").unwrap(); |
| 295 | assert_eq!(a.mantissa(), 1123456i128); |
| 296 | assert_eq!(a.scale(), 6); |
| 297 | } |
| 298 | |
| 299 | #[wasm_bindgen_test] |
| 300 | fn it_can_access_constants_in_wasm() { |
| 301 | assert_eq!("0", Decimal::ZERO.to_string()); |
| 302 | assert_eq!("1", Decimal::ONE.to_string()); |
| 303 | assert_eq!("10", Decimal::TEN.to_string()); |
| 304 | } |