use super::*;
impl<E: Environment, I: IntegerType> FromFields for Integer<E, I> {
type Field = Field<E>;
fn from_fields(fields: &[Self::Field]) -> Result<Self> {
ensure!(fields.len() == 1, "Integer must be recovered from a single field element");
Self::from_field(&fields[0])
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network_environment::Console;
type CurrentEnvironment = Console;
const ITERATIONS: u64 = 10_000;
fn check_from_fields<I: IntegerType>() -> Result<()> {
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let expected = Integer::<CurrentEnvironment, I>::rand(&mut rng);
let candidate = Integer::from_fields(&expected.to_fields()?)?;
assert_eq!(expected, candidate);
}
Ok(())
}
#[test]
fn test_u8_from_fields() -> Result<()> {
type I = u8;
check_from_fields::<I>()
}
#[test]
fn test_i8_from_fields() -> Result<()> {
type I = i8;
check_from_fields::<I>()
}
#[test]
fn test_u16_from_fields() -> Result<()> {
type I = u16;
check_from_fields::<I>()
}
#[test]
fn test_i16_from_fields() -> Result<()> {
type I = i16;
check_from_fields::<I>()
}
#[test]
fn test_u32_from_fields() -> Result<()> {
type I = u32;
check_from_fields::<I>()
}
#[test]
fn test_i32_from_fields() -> Result<()> {
type I = i32;
check_from_fields::<I>()
}
#[test]
fn test_u64_from_fields() -> Result<()> {
type I = u64;
check_from_fields::<I>()
}
#[test]
fn test_i64_from_fields() -> Result<()> {
type I = i64;
check_from_fields::<I>()
}
#[test]
fn test_u128_from_fields() -> Result<()> {
type I = u128;
check_from_fields::<I>()
}
#[test]
fn test_i128_from_fields() -> Result<()> {
type I = i128;
check_from_fields::<I>()
}
}