Skip to content

Commit 5114180

Browse files
committed
Add test for full numerical precision during Duration::total
1 parent 2b07fde commit 5114180

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

src/builtins/core/duration/tests.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,9 @@ fn negative_fields_to_string() {
159159

160160
#[test]
161161
fn preserve_precision_loss() {
162-
const MAX_SAFE_INT: i64 = 9_007_199_254_740_991;
163162
let duration = Duration::from_partial_duration(PartialDuration {
164-
milliseconds: Some(MAX_SAFE_INT),
165-
microseconds: Some(MAX_SAFE_INT as i128),
163+
milliseconds: Some(MAX_SAFE_INTEGER),
164+
microseconds: Some(MAX_SAFE_INTEGER as i128),
166165
..Default::default()
167166
})
168167
.unwrap();
@@ -198,7 +197,6 @@ fn duration_from_str() {
198197

199198
#[test]
200199
fn duration_max_safe() {
201-
const MAX_SAFE_INTEGER: i64 = 9007199254740991;
202200

203201
// From test262 built-ins/Temporal/Duration/prototype/subtract/result-out-of-range-3.js
204202
assert!(Duration::new(0, 0, 0, 0, 0, 0, 0, 0, 9_007_199_254_740_991_926_258, 0).is_err());
@@ -405,11 +403,11 @@ fn test_duration_compare() {
405403
}
406404
}
407405

408-
const MAX_SAFE_INT: i64 = 9_007_199_254_740_991;
406+
const MAX_SAFE_INTEGER: i64 = 9_007_199_254_740_991;
409407

410408
#[test]
411409
fn duration_round_out_of_range_norm_conversion() {
412-
let duration = Duration::new(0, 0, 0, 0, 0, 0, MAX_SAFE_INT, 0, 0, 999_999_999).unwrap();
410+
let duration = Duration::new(0, 0, 0, 0, 0, 0, MAX_SAFE_INTEGER, 0, 0, 999_999_999).unwrap();
413411
let err = duration.round_with_provider(
414412
RoundingOptions {
415413
largest_unit: Some(Unit::Nanosecond),
@@ -426,8 +424,8 @@ fn duration_round_out_of_range_norm_conversion() {
426424
#[cfg_attr(not(feature = "float64_representable_durations"), should_panic)]
427425
fn duration_float64_representable() {
428426
// built-ins/Temporal/Duration/prototype/add/float64-representable-integer
429-
let duration = Duration::new(0, 0, 0, 0, 0, 0, 0, 0, MAX_SAFE_INT as i128, 0).unwrap();
430-
let duration2 = Duration::new(0, 0, 0, 0, 0, 0, 0, 0, MAX_SAFE_INT as i128 - 1, 0).unwrap();
427+
let duration = Duration::new(0, 0, 0, 0, 0, 0, 0, 0, MAX_SAFE_INTEGER as i128, 0).unwrap();
428+
let duration2 = Duration::new(0, 0, 0, 0, 0, 0, 0, 0, MAX_SAFE_INTEGER as i128 - 1, 0).unwrap();
431429
let added = duration.add(&duration2).unwrap();
432430
assert_eq!(added.microseconds, 18014398509481980);
433431
assert_eq!(
@@ -441,3 +439,16 @@ fn duration_float64_representable() {
441439
"Should not internally use a more accurate representation when adding"
442440
);
443441
}
442+
443+
#[test]
444+
fn total_full_numeric_precision() {
445+
// Tests that Duration::total operates without any loss of precision
446+
447+
// built-ins/Temporal/Duration/prototype/total/precision-exact-mathematical-values-6
448+
let d = Duration::new(0, 0, 0, 0, 816, 0, 0, 0, 0, 2_049_187_497_660).unwrap();
449+
assert_eq!(d.total(Unit::Hour, None).unwrap(), 816.56921874935);
450+
451+
// built-ins/Temporal/Duration/prototype/total/precision-exact-mathematical-values-7
452+
let d = Duration::new(0, 0, 0, 0, 0, 0, 0, MAX_SAFE_INTEGER + 1, 1999, 0).unwrap();
453+
assert_eq!(d.total(Unit::Millisecond, None).unwrap(), 9007199254740994.);
454+
}

0 commit comments

Comments
 (0)