// 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, gen_trit_balanced, gen_trit_unbalanced}; use rand::{thread_rng, Rng}; fn create_generic() { assert!(TritBuf::::new().len() == 0); fuzz(100, || { let len = thread_rng().gen_range(0..100); let mut buf = TritBuf::::zeros(len); assert!(buf.len() == len); buf.clear(); assert!(buf.len() == 0); }); fuzz(100, || { let trits = gen_buf_balanced::(0..1000).1; let buf: TritBuf = trits .iter() .map(|t| ::Trit::try_from(*t).ok().unwrap()) .collect(); assert!(buf.len() == trits.len()); }); } fn create_unbalanced() { assert!(TritBuf::::new().len() == 0); fuzz(100, || { let len = thread_rng().gen_range(0..100); assert!(TritBuf::::zeros(len).len() == len); }); fuzz(100, || { let trits = gen_buf_unbalanced::(0..1000).1; let buf: TritBuf = trits .iter() .map(|t| ::Trit::try_from(*t).ok().unwrap()) .collect(); assert!(buf.len() == trits.len()); }); } fn push_pop_generic() { fuzz(100, || { let (mut a, mut b) = gen_buf_balanced::(0..100); for _ in 0..1000 { if thread_rng().gen() { let trit = gen_trit_balanced(); a.push(trit.try_into().unwrap_or_else(|_| unreachable!())); b.push(trit); } else { assert_eq!( a.pop(), b.pop().map(|x| x.try_into().unwrap_or_else(|_| unreachable!())) ); } } }); } fn push_pop_generic_unbalanced() { fuzz(100, || { let (mut a, mut b) = gen_buf_unbalanced::(0..100); for _ in 0..1000 { if thread_rng().gen() { let trit = gen_trit_unbalanced(); a.push(trit.try_into().unwrap_or_else(|_| unreachable!())); b.push(trit); } else { assert_eq!( a.pop(), b.pop().map(|x| x.try_into().unwrap_or_else(|_| unreachable!())) ); } } }); } fn eq_generic() { fuzz(100, || { let a = gen_buf_balanced::(0..1000).0; let b = a.clone(); assert_eq!(a, b); }); } fn eq_generic_unbalanced() { fuzz(100, || { let a = gen_buf_unbalanced::(0..1000).0; let b = a.clone(); assert_eq!(a, b); }); } fn encode_generic() where U::Slice: raw::RawEncoding::Trit>, { fuzz(100, || { let a = gen_buf_balanced::(0..100).0; let b = a.encode::(); assert_eq!(a, b); assert_eq!(a.len(), b.len()); let c = b.encode::(); assert_eq!(a, c); assert_eq!(a.len(), c.len()); }); } fn with_capacity_generic() { let cap = thread_rng().gen_range(1..1000); let mut buf = TritBuf::::with_capacity(cap); assert!(buf.capacity() < (cap + ::TRITS_PER_BYTE)); for _ in 0..cap { buf.push(::Trit::zero()); } assert!(buf.capacity() >= cap); assert!(buf.capacity() < (cap + ::TRITS_PER_BYTE)); } #[test] fn create() { create_generic::>(); create_unbalanced::>(); create_generic::(); create_generic::(); create_generic::(); create_generic::(); } #[test] fn push_pop() { push_pop_generic::>(); push_pop_generic_unbalanced::>(); push_pop_generic::(); push_pop_generic::(); push_pop_generic::(); push_pop_generic::(); } #[test] fn eq() { eq_generic::>(); eq_generic_unbalanced::>(); eq_generic::(); eq_generic::(); eq_generic::(); eq_generic::(); } #[test] fn encode() { encode_generic::, T2B1Buf>(); // encode_generic::, T2B1Buf>(); encode_generic::, T3B1Buf>(); // encode_generic::, T3B1Buf>(); encode_generic::, T4B1Buf>(); // encode_generic::, T4B1Buf>(); encode_generic::>(); // encode_generic::>(); encode_generic::>(); // encode_generic::>(); encode_generic::>(); // encode_generic::>(); encode_generic::>(); // encode_generic::>(); encode_generic::(); encode_generic::(); encode_generic::(); encode_generic::(); encode_generic::(); encode_generic::(); encode_generic::(); encode_generic::(); encode_generic::(); } #[test] fn with_capacity() { with_capacity_generic::>(); with_capacity_generic::>(); with_capacity_generic::(); with_capacity_generic::(); with_capacity_generic::(); with_capacity_generic::(); }