use bubbletree::BubbleTree; use rand::{rngs::StdRng, Rng, SeedableRng}; #[test] pub fn test_random() { // Generate random data let mut rng = StdRng::seed_from_u64(0); let mut data = Vec::new(); let n = 10000; for _ in 0..n { let x = rng.gen_range(0.0..100.0); let y = rng.gen_range(0.0..100.0); data.push([x, y]); } // Configure the tree let fanout = 10; let compression_factor = 100; let mut tree = BubbleTree::new(fanout, compression_factor).expect("Failed to create tree"); // Insert and delete points randomly let mut point_ids = Vec::new(); let deletion_probability = 0.2; // 20% while !data.is_empty() { let random = rng.gen::(); if random <= deletion_probability && !point_ids.is_empty() { let random_id = rng.gen_range(0..point_ids.len()); let point_id = point_ids.swap_remove(random_id); tree.delete(point_id); } else { let point = data.pop().unwrap(); let point_id = tree.insert(point); point_ids.push(point_id); } let expected_num_leaves = (point_ids.len() as f64 / compression_factor as f64).ceil() as usize; // Check the number of leaves which should be: // num_leaves = num_points / compression_factor assert!( tree.num_leaves == expected_num_leaves, "Num leaves: {}, but expected: {expected_num_leaves}", tree.num_leaves ); } }