mod common;
use common::*;
use flow_value::{BigInt, DIVISION_DIGITS, Decimal, Fault, Int, Value};
#[test]
fn an_integer_literal_denotes_the_exact_number_its_digits_spell() {
assert_eq!(int("42").to_string(), "42");
assert_eq!(int("-42").to_string(), "-42");
assert_eq!(int("007").to_string(), "7");
assert_eq!(int("-0").to_string(), "0");
let huge = "123456789012345678901234567890123456789012345678901234567890";
assert_eq!(int(huge).to_string(), huge);
}
#[test]
fn only_the_decoded_literal_spelling_parses_as_an_integer() {
for refused in [
"", "-", "+5", "1_000", " 5", "5 ", "0x10", "1.5", "--5", "5-",
] {
assert_eq!(Int::parse(refused), None, "{refused:?} should be refused");
}
}
#[test]
fn integer_arithmetic_is_exact_with_no_upper_or_lower_bound() {
let max = int("9223372036854775807");
assert_eq!(max.add(&Int::from(1)), int("9223372036854775808"));
assert_eq!(
int("-9223372036854775808").sub(&Int::from(1)),
int("-9223372036854775809")
);
assert_eq!(max.mul(&max), int("85070591730234615847396907784232501249"));
assert_eq!(Int::from(40).add(&Int::from(2)), Int::from(42));
assert_eq!(Int::from(40).sub(&Int::from(42)), Int::from(-2));
assert_eq!(Int::from(6).mul(&Int::from(-7)), Int::from(-42));
assert_eq!(Int::from(42).neg(), Int::from(-42));
assert_eq!(Int::from(-42).neg(), Int::from(42));
}
#[test]
fn integer_division_rounds_toward_negative_infinity() {
let div = |a: i64, b: i64| Int::from(a).div(&Int::from(b)).expect("nonzero divisor");
assert_eq!(div(7, 2), Int::from(3));
assert_eq!(div(-7, 2), Int::from(-4));
assert_eq!(div(7, -2), Int::from(-4));
assert_eq!(div(-7, -2), Int::from(3));
assert_eq!(div(6, 3), Int::from(2));
assert_eq!(div(-6, 3), Int::from(-2));
}
#[test]
fn the_remainder_is_zero_or_carries_the_divisors_sign() {
let rem = |a: i64, b: i64| Int::from(a).rem(&Int::from(b)).expect("nonzero divisor");
assert_eq!(rem(7, 2), Int::from(1));
assert_eq!(rem(-7, 2), Int::from(1));
assert_eq!(rem(7, -2), Int::from(-1));
assert_eq!(rem(-7, -2), Int::from(-1));
assert_eq!(rem(6, -3), Int::from(0));
}
#[test]
fn a_zero_divisor_faults_for_div_and_remainder_alike() {
assert_eq!(Int::from(7).div(&Int::from(0)), Err(Fault::DivisionByZero));
assert_eq!(Int::from(7).rem(&Int::from(0)), Err(Fault::DivisionByZero));
assert_eq!(Int::from(0).div(&Int::from(0)), Err(Fault::DivisionByZero));
}
#[test]
fn integers_order_by_value() {
assert!(Int::from(-3) < Int::from(2));
assert!(int("100000000000000000000") > int("99999999999999999999"));
assert!(int("-100000000000000000000") < int("-99999999999999999999"));
}
#[test]
fn an_integer_reports_its_sign_and_zero() {
assert!(Int::from(0).is_zero());
assert!(!Int::from(1).is_zero());
assert!(Int::from(-1).is_negative());
assert!(!Int::from(0).is_negative());
assert!(!Int::from(1).is_negative());
}
#[test]
fn an_integer_is_a_position_only_when_it_is_non_negative_and_reachable() {
assert_eq!(Int::from(0).to_index(), Some(0));
assert_eq!(Int::from(5).to_index(), Some(5));
assert_eq!(Int::from(-1).to_index(), None);
assert_eq!(int("1000000000000000000000000").to_index(), None);
}
#[test]
fn an_integer_exposes_its_exact_number_to_an_encoder() {
assert_eq!(int("-12").as_bigint(), &BigInt::from(-12));
assert_eq!(Int::from(BigInt::from(9)), Int::from(9));
assert_eq!(Int::from_count(3), Int::from(3));
assert_eq!(format!("{:?}", Int::from(-3)), "-3");
}
#[test]
fn a_decimal_literal_denotes_exactly_the_number_its_digits_spell() {
assert_eq!(dec("4.2").to_string(), "4.2");
assert_eq!(dec("-4.2").to_string(), "-4.2");
assert_eq!(dec("0.001").to_string(), "0.001");
assert_eq!(dec("12").to_string(), "12");
assert_eq!(dec("12.0").to_string(), "12");
let long = "3.1415926535897932384626433832795028841971693993751";
assert_eq!(dec(long).to_string(), long);
}
#[test]
fn only_the_decoded_literal_spelling_parses_as_a_decimal() {
for refused in [
"", ".5", "5.", "-.5", "+1.5", "1e5", "1.5e2", "1_0.5", "1.2.3", "1.-5", "1. 5", "-", "abc",
] {
assert_eq!(
Decimal::parse(refused),
None,
"{refused:?} should be refused"
);
}
}
#[test]
fn decimals_are_normalized_so_a_value_is_its_number_not_its_digits() {
assert_eq!(dec("1.50"), dec("1.5"));
assert_eq!(dec("1.50").to_string(), "1.5");
assert_eq!(dec("100").coefficient(), &BigInt::from(1));
assert_eq!(dec("100").exponent(), 2);
assert_eq!(dec("1.50").coefficient(), &BigInt::from(15));
assert_eq!(dec("1.50").exponent(), -1);
assert!(same(&decimal_value("1.50"), &decimal_value("1.5")));
}
#[test]
fn zero_has_one_value_with_no_sign() {
let zero = dec("0");
assert_eq!(dec("-0.000"), zero);
assert_eq!(dec("0.0").exponent(), 0);
assert_eq!(dec("5").sub(&dec("5")).unwrap(), zero);
assert_eq!(zero.neg(), zero);
assert_eq!(zero.neg().to_string(), "0");
assert!(zero.is_zero());
assert!(!dec("0.1").is_zero());
assert_eq!(Decimal::new(BigInt::from(0), 7).unwrap().exponent(), 0);
}
#[test]
fn decimal_addition_subtraction_and_multiplication_are_exact() {
assert_eq!(dec("0.1").add(&dec("0.2")).unwrap(), dec("0.3"));
assert_eq!(dec("19.99").mul(&dec("3.0")).unwrap(), dec("59.97"));
assert_eq!(dec("1.5").sub(&dec("0.25")).unwrap(), dec("1.25"));
assert_eq!(dec("100").add(&dec("0.001")).unwrap(), dec("100.001"));
assert_eq!(dec("0.001").add(&dec("100")).unwrap(), dec("100.001"));
assert_eq!(dec("2.5").mul(&dec("0.4")).unwrap(), dec("1"));
assert_eq!(dec("-1.5").mul(&dec("2")).unwrap(), dec("-3"));
assert_eq!(dec("1.5").neg(), dec("-1.5"));
}
#[test]
fn a_quotient_with_a_finite_expansion_is_exact_however_long() {
assert_eq!(dec("1.0").div(&dec("2.0")).unwrap(), dec("0.5"));
assert_eq!(dec("1").div(&dec("8")).unwrap(), dec("0.125"));
assert_eq!(dec("3").div(&dec("0.5")).unwrap(), dec("6"));
assert_eq!(dec("1").div(&dec("0.04")).unwrap(), dec("25"));
assert_eq!(dec("12").div(&dec("3")).unwrap(), dec("4"));
assert_eq!(dec("-7").div(&dec("2")).unwrap(), dec("-3.5"));
assert_eq!(dec("7").div(&dec("-2")).unwrap(), dec("-3.5"));
assert_eq!(dec("-7").div(&dec("-2")).unwrap(), dec("3.5"));
assert_eq!(dec("0").div(&dec("3")).unwrap(), dec("0"));
assert_eq!(dec("6").div(&dec("15")).unwrap(), dec("0.4"));
let power = Decimal::new(BigInt::from(2).pow(120), 0).unwrap();
let expected = Decimal::new(BigInt::from(5).pow(120), -120).unwrap();
assert_eq!(dec("1").div(&power).unwrap(), expected);
let five = Decimal::new(BigInt::from(5).pow(60), 0).unwrap();
let expected = Decimal::new(BigInt::from(2).pow(60), -60).unwrap();
assert_eq!(dec("1").div(&five).unwrap(), expected);
}
#[test]
fn any_other_quotient_rounds_to_thirty_four_significant_digits() {
assert_eq!(DIVISION_DIGITS, 34);
assert_eq!(
dec("1.0").div(&dec("3.0")).unwrap().to_string(),
"0.3333333333333333333333333333333333"
);
assert_eq!(
dec("2").div(&dec("3")).unwrap().to_string(),
"0.6666666666666666666666666666666667"
);
assert_eq!(
dec("-2").div(&dec("3")).unwrap().to_string(),
"-0.6666666666666666666666666666666667"
);
assert_eq!(
dec("1").div(&dec("7")).unwrap().to_string(),
"0.1428571428571428571428571428571429"
);
assert_eq!(
dec("10").div(&dec("3")).unwrap().to_string(),
"3.333333333333333333333333333333333"
);
assert_eq!(
dec("100000").div(&dec("3")).unwrap().to_string(),
"33333.33333333333333333333333333333"
);
assert_eq!(
dec("1").div(&dec("30000")).unwrap().to_string(),
"0.00003333333333333333333333333333333333"
);
assert_eq!(
dec("7").div(&dec("3")).unwrap().to_string(),
"2.333333333333333333333333333333333"
);
assert_eq!(
dec("8").div(&dec("9")).unwrap().to_string(),
"0.8888888888888888888888888888888889"
);
assert_eq!(
dec("10000000000000000000000000000000000000001")
.div(&dec("3"))
.unwrap()
.to_string(),
"3333333333333333333333333333333333000000"
);
assert_eq!(
dec("-10000000000000000000000000000000000000001")
.div(&dec("7"))
.unwrap()
.to_string(),
"-1428571428571428571428571428571429000000"
);
assert_eq!(
dec("1").div(&dec("9")).unwrap().to_string(),
"0.1111111111111111111111111111111111"
);
}
#[test]
fn rounding_up_carries_into_a_new_leading_digit_and_normalizes() {
let almost = Decimal::new(BigInt::from(10).pow(34) - 1, 0).unwrap();
let above = almost
.add(&dec("0.000000000000000000000000000000000001"))
.unwrap();
assert_eq!(almost.div(&above).unwrap(), dec("1"));
assert_eq!(almost.div(&above).unwrap().exponent(), 0);
}
#[test]
fn a_quotient_whose_first_thirty_five_digits_are_a_power_of_ten_keeps_thirty_four() {
let numerator = Decimal::new(BigInt::from(3) * BigInt::from(10).pow(34) + 2, 0).unwrap();
let denominator = Decimal::new(BigInt::from(3), 34).unwrap();
assert_eq!(numerator.div(&denominator).unwrap(), dec("1"));
}
#[test]
fn a_zero_divisor_faults_for_decimal_division() {
assert_eq!(dec("1").div(&dec("0")), Err(Fault::DivisionByZero));
assert_eq!(dec("0").div(&dec("0.0")), Err(Fault::DivisionByZero));
}
#[test]
fn a_scale_beyond_a_sixty_four_bit_exponent_is_past_the_limit() {
let top = Decimal::new(BigInt::from(1), i64::MAX).unwrap();
let bottom = Decimal::new(BigInt::from(1), i64::MIN).unwrap();
assert_eq!(Decimal::new(BigInt::from(10), i64::MAX), Err(Fault::Limit));
assert_eq!(top.mul(&dec("10")), Err(Fault::Limit));
assert_eq!(bottom.mul(&dec("0.1")), Err(Fault::Limit));
let five_top = Decimal::new(BigInt::from(5), i64::MAX).unwrap();
assert_eq!(five_top.add(&five_top), Err(Fault::Limit));
assert_eq!(five_top.sub(&five_top.neg()), Err(Fault::Limit));
assert_eq!(bottom.div(&dec("10")), Err(Fault::Limit));
assert_eq!(bottom.div(&dec("2")), Err(Fault::Limit));
assert_eq!(bottom.div(&dec("3")), Err(Fault::Limit));
assert_eq!(top.div(&dec("0.1")), Err(Fault::Limit));
}
#[test]
fn decimals_order_by_value() {
let ascending = [
"-1000", "-2.5", "-2.25", "-1", "-0.001", "0", "0.001", "0.01", "0.5", "1", "1.25", "1.5",
"2", "10", "11", "100.5", "1000",
];
for (at, left) in ascending.iter().enumerate() {
for (other, right) in ascending.iter().enumerate() {
assert_eq!(
dec(left).cmp(&dec(right)),
at.cmp(&other),
"{left} against {right}"
);
assert_eq!(
dec(left).partial_cmp(&dec(right)),
Some(at.cmp(&other)),
"{left} against {right}"
);
}
}
}
#[test]
fn ordering_far_apart_decimals_never_builds_the_power_of_ten_between_them() {
let huge = Decimal::new(BigInt::from(1), i64::MAX - 1).unwrap();
let tiny = Decimal::new(BigInt::from(1), i64::MIN + 1).unwrap();
assert!(huge > tiny);
assert!(huge.neg() < tiny.neg());
assert!(tiny > dec("0"));
}
#[test]
fn floor_rounds_toward_negative_infinity_to_an_int() {
assert_eq!(dec("2.5").floor(), Int::from(2));
assert_eq!(dec("-2.5").floor(), Int::from(-3));
assert_eq!(dec("3").floor(), Int::from(3));
assert_eq!(dec("-3").floor(), Int::from(-3));
assert_eq!(dec("1200").floor(), Int::from(1200));
assert_eq!(dec("12.75").floor(), Int::from(12));
assert_eq!(dec("-12.75").floor(), Int::from(-13));
assert_eq!(dec("0.5").floor(), Int::from(0));
assert_eq!(dec("-0.5").floor(), Int::from(-1));
assert_eq!(dec("0.05").floor(), Int::from(0));
assert_eq!(dec("-0.05").floor(), Int::from(-1));
assert_eq!(dec("0").floor(), Int::from(0));
let tiny = Decimal::new(BigInt::from(-7), -1000).unwrap();
assert_eq!(tiny.floor(), Int::from(-1));
let tiny = Decimal::new(BigInt::from(7), -1000).unwrap();
assert_eq!(tiny.floor(), Int::from(0));
}
#[test]
fn an_integer_counts_the_bits_of_its_magnitude() {
assert_eq!(Int::from(0).bits(), 0);
assert_eq!(Int::from(1).bits(), 1);
assert_eq!(Int::from(-1).bits(), 1);
assert_eq!(Int::from(255).bits(), 8);
assert_eq!(Int::from(-256).bits(), 9);
assert_eq!(int(&"9".repeat(40)).bits(), 133);
}
#[test]
fn a_floor_within_a_bit_bound_is_the_floor_and_beyond_it_is_none() {
assert_eq!(dec("255.9").floor_within(8), Some(Int::from(255)));
assert_eq!(dec("256").floor_within(8), None);
assert_eq!(dec("-255.5").floor_within(8), None, "the floor is -256");
assert_eq!(dec("-254.5").floor_within(8), Some(Int::from(-255)));
assert_eq!(dec("0.5").floor_within(0), Some(Int::from(0)));
assert_eq!(dec("-0.5").floor_within(0), None);
assert_eq!(dec("999").floor_within(10), Some(Int::from(999)));
assert_eq!(dec("1000").floor_within(9), None);
assert_eq!(dec("1000").floor_within(10), Some(Int::from(1000)));
let vast = Decimal::new(BigInt::from(3), i64::MAX).unwrap();
assert_eq!(vast.floor_within(4096), None);
assert_eq!(
Decimal::new(BigInt::from(1), 4).unwrap().floor_within(4),
None
);
assert_eq!(
Decimal::new(BigInt::from(1), 4).unwrap().floor_within(14),
Some(Int::from(10_000))
);
}
#[test]
fn an_integer_converts_to_the_same_number_as_a_decimal() {
assert_eq!(Decimal::from(&Int::from(1500)), dec("1500"));
assert_eq!(Decimal::from(&Int::from(1500)).exponent(), 2);
assert_eq!(Decimal::from(&Int::from(-7)), dec("-7"));
assert_eq!(Decimal::from(&Int::from(0)), dec("0"));
}
#[test]
fn a_decimals_text_is_plain_notation_with_no_exponent() {
assert_eq!(
Decimal::new(BigInt::from(15), 3).unwrap().to_string(),
"15000"
);
assert_eq!(
Decimal::new(BigInt::from(-15), -1).unwrap().to_string(),
"-1.5"
);
assert_eq!(
Decimal::new(BigInt::from(15), -2).unwrap().to_string(),
"0.15"
);
assert_eq!(
Decimal::new(BigInt::from(15), -4).unwrap().to_string(),
"0.0015"
);
assert_eq!(
Decimal::new(BigInt::from(-15), -4).unwrap().to_string(),
"-0.0015"
);
assert_eq!(
Decimal::new(BigInt::from(123), -1).unwrap().to_string(),
"12.3"
);
assert_eq!(format!("{:?}", dec("2.50")), "2.5");
}
#[test]
fn adding_zero_never_builds_the_power_of_ten_between_the_two_scales() {
let tiny = Decimal::new(BigInt::from(-7), i64::MIN + 1).unwrap();
let huge = Decimal::new(BigInt::from(3), i64::MAX - 1).unwrap();
for far in [&tiny, &huge] {
assert_eq!(&dec("0").add(far).unwrap(), far);
assert_eq!(&far.add(&dec("0")).unwrap(), far);
assert_eq!(dec("0").sub(far).unwrap(), far.neg());
assert_eq!(&far.sub(&dec("0")).unwrap(), far);
}
}
#[test]
fn a_decimal_measures_its_text_without_writing_it() {
for spelling in [
"0", "1", "-1", "15000", "-1.5", "0.15", "0.0015", "-0.0015", "12.3",
] {
let number = dec(spelling);
assert_eq!(
number.text_len(),
u128::try_from(spelling.len()).unwrap(),
"{spelling}"
);
}
let number = |coefficient: i64, exponent: i64| {
Decimal::new(BigInt::from(coefficient), exponent).unwrap()
};
for (coefficient, exponent) in [(15, 3), (-15, -1), (15, -2), (123, -1), (-123, -3), (7, 0)] {
let it = number(coefficient, exponent);
assert_eq!(
it.text_len(),
u128::try_from(it.to_string().len()).unwrap(),
"{it}"
);
}
assert_eq!(
number(1, i64::MAX).text_len(),
1 + u128::from(i64::MAX.unsigned_abs())
);
assert_eq!(
number(-1, i64::MIN).text_len(),
3 + u128::from(i64::MIN.unsigned_abs())
);
}
#[test]
fn int_and_decimal_values_never_compare_with_each_other() {
assert_eq!(
flow_value::compare(&Value::int(1), &decimal_value("1")),
Err(flow_value::DataError::Mismatch)
);
}
#[test]
fn faults_describe_themselves() {
assert_eq!(Fault::DivisionByZero.to_string(), "division by zero");
assert_eq!(
Fault::Limit.to_string(),
"the result's exponent does not fit in 64 bits"
);
}
#[test]
fn decimals_with_long_coefficients_order_and_measure_by_value() {
let long = |digits: &str, exponent: i64| {
Decimal::new(digits.parse::<BigInt>().expect("digits"), exponent).expect("a decimal")
};
let ascending = [
long("-123456789012345678901234567891", 0),
long("-123456789012345678901234567890001", -3),
long("-99999999999999999999999", 0),
long("12345678901234567890123456789", -40),
long("1000000000000000000000001", -2),
long("99999999999999999999999", 0),
long("123456789012345678901234567890000001", -6),
long("1234567890123456789012345678901", -1),
long("12345678901234567890123456789013", -2),
long("1", 30),
];
for (at, left) in ascending.iter().enumerate() {
for (other, right) in ascending.iter().enumerate() {
assert_eq!(left.cmp(right), at.cmp(&other), "{left} against {right}");
}
assert_eq!(
left.text_len(),
u128::try_from(left.to_string().len()).expect("a short text"),
"{left}"
);
}
let zeros = long(&format!("7{}", "0".repeat(45)), -5);
assert_eq!(zeros.coefficient(), &BigInt::from(7));
assert_eq!(zeros.exponent(), 40);
for places in 15..25_u32 {
let power = Decimal::new(BigInt::from(10).pow(places), 0).expect("a power of ten");
assert_eq!(power.text_len(), u128::from(places) + 1, "10^{places}");
let below = Decimal::new(BigInt::from(10).pow(places) - 1, 0).expect("nines");
assert_eq!(below.text_len(), u128::from(places), "10^{places} - 1");
}
}