bounds.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use mini_avl::Set;
  2. use std::ops::{Add, Sub};
  3. #[test]
  4. fn test_count() {
  5. let mut set = Set::new();
  6. for i in 1..11 {
  7. assert!(set.insert(i));
  8. assert_eq!(set.count(), i);
  9. }
  10. for i in 1..11 {
  11. assert_eq!(set.count_lt(&i), i - 1);
  12. assert_eq!(set.count_le(&i), i);
  13. assert_eq!(set.count_ge(&i), 11 - i);
  14. assert_eq!(set.count_gt(&i), 10 - i);
  15. }
  16. }
  17. #[test]
  18. fn test_bounds_simple() {
  19. let mut set = Set::new();
  20. set.insert(1);
  21. set.insert(5);
  22. set.insert(9);
  23. assert_eq!(set.lower_bound(&-1), Some(&1));
  24. assert_eq!(set.lower_bound(&1), Some(&1));
  25. assert_eq!(set.lower_bound(&3), Some(&5));
  26. assert_eq!(set.lower_bound(&5), Some(&5));
  27. assert_eq!(set.lower_bound(&7), Some(&9));
  28. assert_eq!(set.lower_bound(&9), Some(&9));
  29. assert_eq!(set.lower_bound(&11), None);
  30. }
  31. #[test]
  32. fn test_bounds() {
  33. let mut values = vec![5, 3, 7, 1, 4, 6, 9];
  34. let mut set = Set::new();
  35. for v in values.iter() {
  36. set.insert(*v);
  37. }
  38. values.sort();
  39. let mut iter = values.iter();
  40. let mut lb = iter.next();
  41. for i in values.first().unwrap().sub(1)..values.last().unwrap().add(2) {
  42. while lb.map_or(false, |v| *v < i) {
  43. lb = iter.next();
  44. }
  45. assert_eq!(set.lower_bound(&i), lb);
  46. }
  47. }