Skip to content

Commit cb0dbe7

Browse files
committed
Added further bounds tests
1 parent ee46aab commit cb0dbe7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/str.rs

+44
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ fn dispatch_next<const POINT: bool, const NEG: bool, const HAS: bool, const BIG:
213213
}
214214
}
215215

216+
/// Dispatch the next non-digit byte:
217+
///
218+
/// * POINT - a decimal point has been seen
219+
/// * NEG - we've encountered a `-` and the number is negative
220+
/// * HAS - a digit has been encountered (when HAS is false it's invalid)
221+
/// * BIG - a number that uses 96 bits instead of only 64 bits
222+
/// * FIRST - true if it is the first byte in the string
223+
/// * ROUND - attempt to round underflow
216224
#[inline(never)]
217225
fn non_digit_dispatch_u64<
218226
const POINT: bool,
@@ -347,6 +355,7 @@ fn handle_full_128<const POINT: bool, const NEG: bool, const ROUND: bool>(
347355
let next = *next;
348356
if POINT && scale >= 28 {
349357
if ROUND {
358+
// This is the call site
350359
maybe_round(data, next, scale, POINT, NEG)
351360
} else {
352361
Err(Error::Underflow)
@@ -699,6 +708,7 @@ mod test {
699708
use crate::Decimal;
700709
use arrayvec::ArrayString;
701710
use core::{fmt::Write, str::FromStr};
711+
use futures::StreamExt;
702712

703713
#[test]
704714
fn display_does_not_overflow_max_capacity() {
@@ -955,6 +965,40 @@ mod test {
955965
);
956966
}
957967

968+
#[test]
969+
fn character_at_rounding_position() {
970+
let tests = [
971+
// 6 is at the rounding position, so we round up
972+
(
973+
"1.000_000_000_000_000_000_000_000_000_06",
974+
Ok(Decimal::from_i128_with_scale(
975+
1_000_000_000_000_000_000_000_000_000_1,
976+
28,
977+
)),
978+
),
979+
// Decimal point is at the rounding position
980+
(
981+
"1_000_000_000_000_000_000_000_000_000_0.6",
982+
Ok(Decimal::from_i128_with_scale(
983+
1_000_000_000_000_000_000_000_000_000_1,
984+
0,
985+
)),
986+
),
987+
// Placeholder is at the rounding position
988+
(
989+
"1.000_000_000_000_000_000_000_000_000_0_6",
990+
Ok(Decimal::from_i128_with_scale(
991+
1_000_000_000_000_000_000_000_000_000_1,
992+
28,
993+
)),
994+
),
995+
];
996+
997+
for (index, (input, expected)) in tests.iter().enumerate() {
998+
assert_eq!(parse_str_radix_10(input), *expected, "Test Index {}", index);
999+
}
1000+
}
1001+
9581002
#[test]
9591003
fn from_str_edge_cases_1() {
9601004
assert_eq!(parse_str_radix_10(""), Err(Error::from("Invalid decimal: empty")));

0 commit comments

Comments
 (0)