use core::fmt::Debug; use elrond_wasm::{ api::ManagedTypeApi, types::{ BigInt, BigUint, ManagedAddress, ManagedBuffer, ManagedByteArray, ManagedOption, ManagedType, TokenIdentifier, }, }; use elrond_wasm_debug::DebugApi; fn test_some_for_value(f: F) where M: ManagedTypeApi, T: ManagedType + Clone + Debug + Eq, F: Fn() -> T, { assert!(ManagedOption::some(f()).is_some()); assert_eq!(ManagedOption::some(f()), ManagedOption::some(f())); assert_eq!(ManagedOption::some(f()), ManagedOption::some(f())); assert_eq!(ManagedOption::from(Some(f())), ManagedOption::some(f())); assert_eq!(ManagedOption::some(f()).into_option(), Some(f())); assert_ne!(ManagedOption::some(f()), ManagedOption::::none()); } fn test_none_for_type() where M: ManagedTypeApi, T: ManagedType + Clone + Debug + Eq, { assert!(ManagedOption::::none().is_none()); assert_eq!(ManagedOption::::none(), ManagedOption::::none()); assert_eq!(ManagedOption::::none(), ManagedOption::::none()); assert_eq!(ManagedOption::from(None), ManagedOption::::none()); assert_eq!(ManagedOption::::none().into_option(), None); } #[test] fn test_some() { let _ = DebugApi::dummy(); test_some_for_value(|| BigUint::::from(1u32)); test_some_for_value(|| BigInt::::from(2i32)); test_some_for_value(|| ManagedBuffer::::from(&b"3abc"[..])); test_some_for_value(|| ManagedByteArray::::from(&[4u8; 4])); test_some_for_value(|| ManagedAddress::::from(&[5u8; 32])); test_some_for_value(|| TokenIdentifier::::from(&b"TOKEN-000006"[..])); } #[test] fn test_none() { let _ = DebugApi::dummy(); test_none_for_type::>(); test_none_for_type::>(); test_none_for_type::>(); test_none_for_type::>(); test_none_for_type::>(); test_none_for_type::>(); } #[test] fn test_unwrap() { let _ = DebugApi::dummy(); assert_eq!( ManagedOption::some(BigUint::::from(1u32)) .unwrap_or_else(BigUint::::zero), BigUint::::from(1u32) ); assert_eq!( ManagedOption::none().unwrap_or_else(BigUint::::zero), BigUint::::zero() ); } #[test] fn test_map() { let _ = DebugApi::dummy(); // example BigInt -> BigUint assert_eq!( ManagedOption::some(BigUint::::from(1u32)).map(BigInt::::from), ManagedOption::some(BigInt::::from(1i32)) ); assert_eq!( ManagedOption::>::none().map(BigInt::::from), ManagedOption::none() ); // example BigUint -> BigInt (magnitude) assert_eq!( ManagedOption::some(BigInt::::from(-1i32)).map(|x| x.magnitude()), ManagedOption::some(BigUint::::from(1u32)) ); assert_eq!( ManagedOption::none().map(|x: BigInt| x.magnitude()), ManagedOption::none() ); // BigInt::into_big_uint is actually related assert_eq!( BigInt::::from(1i32).into_big_uint(), ManagedOption::some(BigUint::::from(1u32)) ); assert_eq!( BigInt::::from(-1i32).into_big_uint(), ManagedOption::none() ); }