//! Regression: every wide tier's `cbrt_strict` used to overflow the
//! work integer inside the generic-wide cbrt kernel when SCALE
//! approached `MAX_SCALE`.
//!
//! At SCALE = MAX_SCALE the kernel needs `mag * 10^(2*SCALE)`, which
//! peaks near `10^(3*SCALE)` bits. The original `$CbrtWide` for each
//! tier was sized at 2× the storage width — large enough for most
//! SCALEs but not for MAX_SCALE. The work-integer widths were bumped
//! one step further per tier so the kernel is correct at any SCALE up
//! to that tier's `MAX_SCALE`.
//!
//! Companion to `tests/d57_max_scale_cbrt_panic.rs`, which covers the
//! D57 reproducer in detail.
//!
//! Note: at MAX_SCALE the representable range is bounded by
//! `Storage::MAX / 10^MAX_SCALE`, so test inputs must be small. Each
//! tier asserts `cbrt(1) == 1` — a value that always fits since
//! `Storage::MAX >= 10^MAX_SCALE` by construction of MAX_SCALE.

#![cfg(not(any(
    feature = "rounding-half-away-from-zero",
    feature = "rounding-half-toward-zero",
    feature = "rounding-trunc",
    feature = "rounding-floor",
    feature = "rounding-ceiling",
)))]

/// Build `D::<MAX_SCALE>` representing `1.0` via `from_bits(10^MAX_SCALE)`
/// then assert `cbrt(1) == 1`. The raw `10^MAX_SCALE` is generated by
/// `pow` on the storage type so the literal can't drift away from the
/// SCALE.
macro_rules! cbrt_of_one_test {
    ($fn_name:ident, $tier:ident, $scale:literal, $Storage:ident) => {
        #[test]
        fn $fn_name() {
            let ten = decimal_scaled::$Storage::from_str_radix("10", 10)
                .expect("10 literal");
            let raw = ten.pow($scale as u32);
            let one = decimal_scaled::$tier::<$scale>::from_bits(raw);
            let r = one.cbrt_strict();
            assert_eq!(
                r,
                one,
                concat!(
                    "cbrt(1) at ",
                    stringify!($tier),
                    "<",
                    stringify!($scale),
                    "> should equal 1",
                ),
            );
        }
    };
}

#[cfg(feature = "wide")]
cbrt_of_one_test!(d76_max_scale_cbrt_of_one, D76, 76, Int256);

#[cfg(feature = "wide")]
cbrt_of_one_test!(d115_max_scale_cbrt_of_one, D115, 115, Int384);

#[cfg(feature = "wide")]
cbrt_of_one_test!(d153_max_scale_cbrt_of_one, D153, 153, Int512);

#[cfg(feature = "wide")]
cbrt_of_one_test!(d230_max_scale_cbrt_of_one, D230, 230, Int768);

#[cfg(any(feature = "wide", feature = "x-wide"))]
cbrt_of_one_test!(d307_max_scale_cbrt_of_one, D307, 307, Int1024);

#[cfg(feature = "x-wide")]
cbrt_of_one_test!(d462_max_scale_cbrt_of_one, D462, 461, Int1536);

#[cfg(feature = "x-wide")]
cbrt_of_one_test!(d616_max_scale_cbrt_of_one, D616, 615, Int2048);

#[cfg(feature = "xx-wide")]
cbrt_of_one_test!(d924_max_scale_cbrt_of_one, D924, 923, Int3072);

#[cfg(feature = "xx-wide")]
cbrt_of_one_test!(d1232_max_scale_cbrt_of_one, D1232, 1231, Int4096);
