extern crate alloc;
use alloc::boxed::Box;
use core::convert::Infallible;
use core::error::Error;
use core::num::{ParseFloatError, ParseIntError};
use crate::{
PrimitiveError, PrimitiveFloat, PrimitiveInteger, PrimitiveNumber, PrimitiveSigned,
PrimitiveUnsigned,
};
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 parse_float() {
fn check<T: PrimitiveFloat>(s: &str, ok: T) {
let r: Result<T, ParseFloatError> = s.parse();
assert_eq!(r, Ok(ok));
}
check("0", 0f32);
}
#[test]
fn parse_int() {
fn check<T: PrimitiveInteger>(s: &str, ok: T) {
let r: Result<T, ParseIntError> = s.parse();
assert_eq!(r, Ok(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_from_signed() {
fn check<T: PrimitiveSigned>(x: i8, ok: T) {
let r: Result<T, Infallible> = T::try_from(x);
assert_eq!(r, Ok(ok));
}
check(0i8, 0i32);
}
#[test]
fn try_from_unsigned() {
fn check<T: PrimitiveUnsigned>(x: u8, ok: T) {
let r: Result<T, Infallible> = T::try_from(x);
assert_eq!(r, Ok(ok));
}
check(0u8, 0u32);
}
#[test]
fn try_into() {
fn check<T: PrimitiveInteger>(x: T, ok: u32) {
check_result(x.try_into(), ok);
}
check(0i32, 0u32);
}