// Copyright 2020-2021 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 mod common; use bee_ternary::*; use common::{fuzz, gen_buf_balanced, gen_buf_unbalanced}; use rand::{thread_rng, Rng}; fn get_generic() { println!("{}", std::any::type_name::()); fuzz(100, || { let (a, a_i8) = gen_buf_balanced::(1..1000); fuzz(25, || { assert_eq!(a.get(a.len() + thread_rng().gen_range(0..20)), None); }); let mut sl = a.as_slice(); let mut sl_i8 = &a_i8[..]; for _ in 0..20 { if sl.is_empty() { break; } let i = thread_rng().gen_range(0..sl.len()); assert_eq!( sl.get(i), Some(::Trit::try_from(sl_i8[i]).unwrap_or_else(|_| unreachable!())), ); let idx = thread_rng().gen_range(0..sl.len()); let len = thread_rng().gen_range(0..sl.len() - idx); sl_i8 = &sl_i8[idx..idx + len]; sl = &sl[idx..idx + len]; } }); } fn get_generic_unbalanced() { fuzz(100, || { let (a, a_i8) = gen_buf_unbalanced::(1..1000); fuzz(25, || { assert_eq!(a.get(a.len() + thread_rng().gen_range(0..20)), None); }); let mut sl = a.as_slice(); let mut sl_i8 = &a_i8[..]; for _ in 0..20 { if sl.is_empty() { break; } let i = thread_rng().gen_range(0..sl.len()); assert_eq!( sl.get(i), Some(::Trit::try_from(sl_i8[i]).unwrap_or_else(|_| unreachable!())), ); let idx = thread_rng().gen_range(0..sl.len()); let len = thread_rng().gen_range(0..sl.len() - idx); sl_i8 = &sl_i8[idx..idx + len]; sl = &sl[idx..idx + len]; } }); } fn set_generic() { println!("{}", std::any::type_name::()); fuzz(100, || { let (mut a, mut a_i8) = gen_buf_balanced::(1..1000); fuzz(100, || { let mut sl = a.as_slice_mut(); let mut sl_i8 = &mut a_i8[..]; for _ in 0..10 { if sl.is_empty() { break; } let i = thread_rng().gen_range(0..sl.len()); let trit = thread_rng().gen_range(-1i8..2); sl.set(i, trit.try_into().unwrap_or_else(|_| unreachable!())); sl_i8[i] = trit; assert_eq!( sl.get(i), Some(::Trit::try_from(sl_i8[i]).unwrap_or_else(|_| unreachable!())), ); let idx = thread_rng().gen_range(0..sl.len()); let len = thread_rng().gen_range(0..sl.len() - idx); sl_i8 = &mut sl_i8[idx..idx + len]; sl = &mut sl[idx..idx + len]; } assert!( a.iter() .zip(a_i8.iter()) .all(|(a, b)| a == (*b).try_into().unwrap_or_else(|_| unreachable!())) ); assert_eq!(a.len(), a_i8.len()); }); }); } fn set_generic_unbalanced() { println!("{}", std::any::type_name::()); fuzz(100, || { let (mut a, mut a_i8) = gen_buf_unbalanced::(1..1000); fuzz(100, || { let mut sl = a.as_slice_mut(); let mut sl_i8 = &mut a_i8[..]; for _ in 0..10 { if sl.is_empty() { break; } let i = thread_rng().gen_range(0..sl.len()); let trit = thread_rng().gen_range(0i8..3); sl.set(i, trit.try_into().unwrap_or_else(|_| unreachable!())); sl_i8[i] = trit; assert_eq!( sl.get(i), Some(::Trit::try_from(sl_i8[i]).unwrap_or_else(|_| unreachable!())), ); let idx = thread_rng().gen_range(0..sl.len()); let len = thread_rng().gen_range(0..sl.len() - idx); sl_i8 = &mut sl_i8[idx..idx + len]; sl = &mut sl[idx..idx + len]; } assert!( a.iter() .zip(a_i8.iter()) .all(|(a, b)| a == (*b).try_into().unwrap_or_else(|_| unreachable!())) ); assert_eq!(a.len(), a_i8.len()); }); }); } fn chunks_generic() { fuzz(100, || { let (a, a_i8) = gen_buf_balanced::(2..1000); let chunk_len = thread_rng().gen_range(1..a.len()); for (a, a_i8) in a.chunks(chunk_len).zip(a_i8.chunks(chunk_len)) { assert_eq!(a.len(), a_i8.len()); assert!( a.iter() .zip(a_i8.iter()) .all(|(a, b)| a == (*b).try_into().unwrap_or_else(|_| unreachable!())) ); } }); } fn chunks_generic_unbalanced() { fuzz(100, || { let (a, a_i8) = gen_buf_unbalanced::(2..1000); let chunk_len = thread_rng().gen_range(1..a.len()); for (a, a_i8) in a.chunks(chunk_len).zip(a_i8.chunks(chunk_len)) { assert_eq!(a.len(), a_i8.len()); assert!( a.iter() .zip(a_i8.iter()) .all(|(a, b)| a == (*b).try_into().unwrap_or_else(|_| unreachable!())) ); } }); } fn set_panic_generic() { let mut a = gen_buf_balanced::(0..1000).0; let len = a.len(); a.set(len, ::Trit::zero()); } fn set_panic_generic_unbalanced() { let mut a = gen_buf_unbalanced::(0..1000).0; let len = a.len(); a.set(len, ::Trit::zero()); } #[test] fn get() { get_generic::>(); get_generic_unbalanced::>(); get_generic::(); get_generic::(); get_generic::(); get_generic::(); } #[test] fn set() { set_generic::>(); set_generic_unbalanced::>(); set_generic::(); set_generic::(); set_generic::(); set_generic::(); } #[test] #[should_panic] fn set_panic() { set_panic_generic::>(); set_panic_generic_unbalanced::>(); set_panic_generic::(); set_panic_generic::(); set_panic_generic::(); set_panic_generic::(); } #[test] fn chunks() { chunks_generic::>(); chunks_generic_unbalanced::>(); chunks_generic::(); chunks_generic::(); chunks_generic::(); chunks_generic::(); } #[test] fn chunks_mut() { fuzz(100, || { let (mut a, mut a_i8) = gen_buf_balanced::(2..1000); let chunk_len = thread_rng().gen_range(1..a.len()); for (a, a_i8) in a.chunks_mut(chunk_len).zip(a_i8.chunks_mut(chunk_len)) { assert_eq!(a.len(), a_i8.len()); assert!( a.iter() .zip(a_i8.iter()) .all(|(a, b)| a == (*b).try_into().unwrap_or_else(|_| unreachable!())) ); } }); }