diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crates/library/src/adaptive.rs | 8 | ||||
-rw-r--r-- | src/crates/library/src/proptest/strategies.rs | 17 | ||||
-rw-r--r-- | src/crates/library/src/sorted_unique_vector.rs | 40 | ||||
-rw-r--r-- | src/crates/library/src/sorted_vector.rs | 20 |
4 files changed, 41 insertions, 44 deletions
diff --git a/src/crates/library/src/adaptive.rs b/src/crates/library/src/adaptive.rs index 17be55b..3161970 100644 --- a/src/crates/library/src/adaptive.rs +++ b/src/crates/library/src/adaptive.rs @@ -125,11 +125,12 @@ where mod tests { use std::collections::HashSet; - use crate::{traits::Container, AdaptiveContainer, EagerUniqueVec}; + use crate::{traits::Container, AdaptiveContainer, SortedUniqueVec}; #[test] fn adaptive_container_lo_functionality() { - let mut c: AdaptiveContainer<10, EagerUniqueVec<_>, HashSet<_>, usize> = Default::default(); + let mut c: AdaptiveContainer<10, SortedUniqueVec<_>, HashSet<_>, usize> = + Default::default(); for i in 0..5 { c.insert(i); @@ -140,7 +141,8 @@ mod tests { #[test] fn adaptive_container_adapts() { - let mut c: AdaptiveContainer<10, EagerUniqueVec<_>, HashSet<_>, usize> = Default::default(); + let mut c: AdaptiveContainer<10, SortedUniqueVec<_>, HashSet<_>, usize> = + Default::default(); for i in 1..=9 { c.insert(i); diff --git a/src/crates/library/src/proptest/strategies.rs b/src/crates/library/src/proptest/strategies.rs index b866c67..e61daa4 100644 --- a/src/crates/library/src/proptest/strategies.rs +++ b/src/crates/library/src/proptest/strategies.rs @@ -2,27 +2,26 @@ use proptest::prelude::*; use std::ops::Range; -use crate::eager_sorted_vector::EagerSortedVec; -use crate::eager_unique_vector::EagerUniqueVec; +use crate::{SortedUniqueVec, SortedVec}; use proptest::collection::vec; -pub fn eager_unique_vec<T: Strategy + 'static>( +pub fn sorted_unique_vec<T: Strategy + 'static>( element: T, size: Range<usize>, -) -> impl Strategy<Value = EagerUniqueVec<T::Value>> +) -> impl Strategy<Value = SortedUniqueVec<T::Value>> where - <T as Strategy>::Value: PartialEq, + <T as Strategy>::Value: PartialEq + Ord, { - vec(element, size.clone()).prop_map(EagerUniqueVec::from_vec) + vec(element, size.clone()).prop_map(SortedUniqueVec::from_vec) } -pub fn eager_sorted_vec<T: Strategy + 'static>( +pub fn sorted_vec<T: Strategy + 'static>( element: T, size: Range<usize>, -) -> impl Strategy<Value = EagerSortedVec<T::Value>> +) -> impl Strategy<Value = SortedVec<T::Value>> where <T as Strategy>::Value: Ord, { - vec(element, size.clone()).prop_map(EagerSortedVec::from_vec) + vec(element, size.clone()).prop_map(SortedVec::from_vec) } diff --git a/src/crates/library/src/sorted_unique_vector.rs b/src/crates/library/src/sorted_unique_vector.rs index c5a36d8..cb11ad0 100644 --- a/src/crates/library/src/sorted_unique_vector.rs +++ b/src/crates/library/src/sorted_unique_vector.rs @@ -13,14 +13,10 @@ pub struct SortedUniqueVec<T> { } impl<T: PartialEq + std::cmp::Ord> SortedUniqueVec<T> { - pub fn from_vec(v: Vec<T>) -> SortedUniqueVec<T> { - let mut vec = Vec::<T>::new(); - for i in v { - if !vec.contains(&i) { - vec.push(i); - } - } - SortedUniqueVec { v: vec } + pub fn from_vec(mut v: Vec<T>) -> SortedUniqueVec<T> { + v.sort(); + v.dedup(); + SortedUniqueVec { v } } pub fn new() -> SortedUniqueVec<T> { @@ -241,13 +237,13 @@ impl<E: Ord> FromIterator<E> for SortedUniqueVec<E> { #[cfg(test)] mod tests { use super::*; - use crate::proptest::{strategies::eager_unique_vec, *}; + use crate::proptest::{strategies::sorted_unique_vec, *}; use im::conslist::ConsList; use proptest::prelude::*; fn abstraction<T>(v: SortedUniqueVec<T>) -> ConsList<T> where - T: PartialEq, + T: PartialEq + Ord, { let list: ConsList<T> = ConsList::from(v.to_vec()); list @@ -259,7 +255,7 @@ mod tests { })] #[test] - fn test_eager_unique_vec_len(ref mut v in eager_unique_vec(".*", 0..100)) { + fn test_sorted_unique_vec_len(ref mut v in sorted_unique_vec(".*", 0..100)) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, unique(&abs_list)); @@ -269,7 +265,7 @@ mod tests { } #[test] - fn test_eager_unique_vec_contains(ref mut v in eager_unique_vec(".*", 0..100), a in ".*") { + fn test_sorted_unique_vec_contains(ref mut v in sorted_unique_vec(".*", 0..100), a in ".*") { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, unique(&abs_list)); @@ -279,7 +275,7 @@ mod tests { } #[test] - fn test_eager_unique_vec_is_empty(ref mut v in eager_unique_vec(".*", 0..100)) { + fn test_sorted_unique_vec_is_empty(ref mut v in sorted_unique_vec(".*", 0..100)) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, unique(&abs_list)); @@ -289,18 +285,18 @@ mod tests { } #[test] - fn test_eager_unique_vec_insert(ref mut v in eager_unique_vec(".*", 0..100), a in ".*") { + fn test_sorted_unique_vec_insert(ref mut v in sorted_unique_vec(0_usize..100, 0..100), a in 0_usize..100) { let abs_list = abstraction(v.clone()); //pre - assert_eq!(abs_list, unique(&abs_list)); + assert_eq!(abs_list, unique(&abs_list).sort()); //post - let after_list = unique(&abs_list.append(conslist![a.clone()])); - Container::<String>::insert(v, a.clone()); + let after_list = unique(&abs_list.append(conslist![a.clone()])).sort(); + Container::<usize>::insert(v, a.clone()); assert_eq!(abstraction(v.clone()), after_list); } #[test] - fn test_eager_unique_vec_clear(ref mut v in eager_unique_vec(".*", 0..100)) { + fn test_sorted_unique_vec_clear(ref mut v in sorted_unique_vec(".*", 0..100)) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, unique(&abs_list)); @@ -311,7 +307,7 @@ mod tests { } #[test] - fn test_eager_unique_vec_remove(ref mut v in eager_unique_vec(".*", 0..100), a in ".*") { + fn test_sorted_unique_vec_remove(ref mut v in sorted_unique_vec(".*", 0..100), a in ".*") { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, unique(&abs_list)); @@ -323,7 +319,7 @@ mod tests { } #[test] - fn test_eager_unique_vec_first(ref mut v in eager_unique_vec(".*", 0..100)) { + fn test_sorted_unique_vec_first(ref mut v in sorted_unique_vec(".*", 0..100)) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, unique(&abs_list)); @@ -335,7 +331,7 @@ mod tests { } #[test] - fn test_eager_unique_vec_last(ref mut v in eager_unique_vec(".*", 0..100)) { + fn test_sorted_unique_vec_last(ref mut v in sorted_unique_vec(".*", 0..100)) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, unique(&abs_list)); @@ -347,7 +343,7 @@ mod tests { } #[test] - fn test_eager_unique_vec_nth(ref mut v in eager_unique_vec(".*", 0..100), n in 0usize..100) { + fn test_sorted_unique_vec_nth(ref mut v in sorted_unique_vec(".*", 0..100), n in 0usize..100) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, unique(&abs_list)); diff --git a/src/crates/library/src/sorted_vector.rs b/src/crates/library/src/sorted_vector.rs index 3d4ae1f..925e3ef 100644 --- a/src/crates/library/src/sorted_vector.rs +++ b/src/crates/library/src/sorted_vector.rs @@ -232,7 +232,7 @@ impl<E: Ord> FromIterator<E> for SortedVec<E> { #[cfg(test)] mod tests { use super::*; - use crate::proptest::{strategies::eager_sorted_vec, *}; + use crate::proptest::{strategies::sorted_vec, *}; use im::conslist::ConsList; use proptest::prelude::*; @@ -251,7 +251,7 @@ mod tests { })] #[test] - fn test_eager_sorted_vec_len(ref mut v in eager_sorted_vec(".*", 0..100)) { + fn test_sorted_vec_len(ref mut v in sorted_vec(".*", 0..100)) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, abs_list.sort()); @@ -261,7 +261,7 @@ mod tests { } #[test] - fn test_eager_sorted_vec_contains(ref mut v in eager_sorted_vec(".*", 0..100), a in ".*") { + fn test_sorted_vec_contains(ref mut v in sorted_vec(".*", 0..100), a in ".*") { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, abs_list.sort()); @@ -271,7 +271,7 @@ mod tests { } #[test] - fn test_eager_sorted_vec_is_empty(ref mut v in eager_sorted_vec(".*", 0..100)) { + fn test_sorted_vec_is_empty(ref mut v in sorted_vec(".*", 0..100)) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, abs_list.sort()); @@ -281,7 +281,7 @@ mod tests { } #[test] - fn test_eager_sorted_vec_insert(ref mut v in eager_sorted_vec(".*", 0..100), a in ".*") { + fn test_sorted_vec_insert(ref mut v in sorted_vec(".*", 0..100), a in ".*") { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, abs_list.sort()); @@ -292,7 +292,7 @@ mod tests { } #[test] - fn test_eager_sorted_vec_clear(ref mut v in eager_sorted_vec(".*", 0..100)) { + fn test_sorted_vec_clear(ref mut v in sorted_vec(".*", 0..100)) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, abs_list.sort()); @@ -303,7 +303,7 @@ mod tests { } #[test] - fn test_eager_sorted_vec_remove(ref mut v in eager_sorted_vec(".*", 0..100), a in ".*") { + fn test_sorted_vec_remove(ref mut v in sorted_vec(".*", 0..100), a in ".*") { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, abs_list.sort()); @@ -315,7 +315,7 @@ mod tests { } #[test] - fn test_eager_sorted_vec_first(ref mut v in eager_sorted_vec(".*", 0..100)) { + fn test_sorted_vec_first(ref mut v in sorted_vec(".*", 0..100)) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, abs_list.sort()); @@ -327,7 +327,7 @@ mod tests { } #[test] - fn test_eager_sorted_vec_last(ref mut v in eager_sorted_vec(".*", 0..100)) { + fn test_sorted_vec_last(ref mut v in sorted_vec(".*", 0..100)) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, abs_list.sort()); @@ -339,7 +339,7 @@ mod tests { } #[test] - fn test_eager_sorted_vec_nth(ref mut v in eager_sorted_vec(".*", 0..100), n in 0usize..100) { + fn test_sorted_vec_nth(ref mut v in sorted_vec(".*", 0..100), n in 0usize..100) { let abs_list = abstraction(v.clone()); //pre assert_eq!(abs_list, abs_list.sort()); |