1#![cfg_attr(not(feature = "std"), no_std)]
54extern crate alloc;
55
56pub mod collections;
57pub mod error;
58pub mod index;
59pub mod util;
60
61pub use collections::map::Map;
63pub use collections::set::Set;
64
65pub use error::Error;
67
68pub use index::external::Cached as CachedIndex;
70pub use index::external::OneLevel as OneLevelIndex;
71pub use index::external::Static as StaticIndex;
72
73pub use index::external::Cached;
75pub use index::external::OneLevel;
76pub use index::external::Static;
77#[cfg(feature = "std")]
78pub use index::owned::Dynamic;
79#[cfg(feature = "std")]
80pub use index::owned::Dynamic as DynamicIndex;
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85 use alloc::vec::Vec;
86
87 #[test]
88 fn test_integration_basic() {
89 let data: Vec<u64> = (0..10000).collect();
90 let index = index::Builder::new()
91 .epsilon(64)
92 .epsilon_recursive(4)
93 .build(&data)
94 .unwrap();
95
96 for i in (0..10000).step_by(100) {
97 let pos = index.lower_bound(&data, &i);
98 assert_eq!(pos, i as usize);
99 }
100 }
101
102 #[test]
103 fn test_integration_signed() {
104 let data: Vec<i64> = (-5000..5000).collect();
105 let index = index::external::Static::new(&data, 64, 4).unwrap();
106
107 for i in (-5000i64..5000).step_by(100) {
108 let pos = index.lower_bound(&data, &i);
109 let expected = (i + 5000) as usize;
110 assert_eq!(pos, expected, "Failed for key {}", i);
111 }
112
113 assert!(index.contains(&data, &-5000));
114 assert!(!index.contains(&data, &5000));
115 }
116
117 #[test]
118 fn test_integration_sparse() {
119 let data: Vec<u64> = (0..1000).map(|i| i * i).collect();
120 let index = index::external::Static::new(&data, 32, 4).unwrap();
121
122 for (i, &key) in data.iter().enumerate() {
123 let pos = index.lower_bound(&data, &key);
124 assert_eq!(pos, i, "Failed for key {} at index {}", key, i);
125 }
126 }
127
128 #[test]
129 fn test_missing_keys() {
130 let data: Vec<u64> = (0..100).map(|i| i * 2).collect();
131 let index = index::external::Static::new(&data, 8, 4).unwrap();
132
133 let pos = index.lower_bound(&data, &1);
134 assert_eq!(pos, 1);
135
136 let pos = index.lower_bound(&data, &199);
137 assert_eq!(pos, 100);
138 }
139
140 #[test]
141 fn test_set_api() {
142 let set: Set<u64> = (0..1000).collect();
143 assert!(set.contains(&500));
144 assert!(!set.contains(&1001));
145 }
146
147 #[test]
148 fn test_map_api() {
149 let map: Map<u64, i32> = (0..100).map(|i| (i, i as i32 * 2)).collect();
150 assert_eq!(map.get(&50), Some(&100));
151 }
152}