extern crate alloc;
use alloc::boxed::Box;
use core::error::Error;
use crate::{PrimitiveError, PrimitiveFloatToInt, PrimitiveInteger, PrimitiveNumber};
fn check_result<'a, T: PrimitiveNumber, E: PrimitiveError>(r: Result<T, E>, ok: T) {
assert_eq!(r.clone(), Ok(ok));
assert_eq!(r.clone().unwrap(), ok);
let result: Result<T, Box<dyn Error + 'a>> = (|| Ok(r.clone()?))();
assert_eq!(result.unwrap(), ok);
let result: Result<T, Box<dyn Error + Send + Sync + 'a>> = (|| Ok(r.clone()?))();
assert_eq!(result.unwrap(), ok);
}
#[test]
fn parse() {
fn check<T: PrimitiveNumber>(s: &str, ok: T) {
check_result(s.parse(), ok);
}
check("0", 0u32);
}
#[test]
fn try_from() {
fn check<T: PrimitiveInteger>(x: i32, ok: T) {
check_result(T::try_from(x), ok);
}
check(0i32, 0u32);
}
#[test]
fn try_into() {
fn check<T: PrimitiveInteger>(x: T, ok: u32) {
check_result(x.try_into(), ok);
}
check(0i32, 0u32);
}
#[test]
fn to_int_unchecked() {
fn pi<T: PrimitiveFloatToInt<Int>, Int>() -> Int {
unsafe { T::PI.to_int_unchecked() }
}
assert_eq!(pi::<f32, i64>(), 3i64);
assert_eq!(pi::<f64, u8>(), 3u8);
}