From 84622bb7f8d891fad2ab9c1dfdb3b31f1e71d306 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 15 May 2023 18:57:23 -0400 Subject: [PATCH] Adds test and fixes for allocate bridges --- crates/lox-library/src/lib.rs | 4 ++-- crates/lox-library/src/tests.rs | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/lox-library/src/lib.rs b/crates/lox-library/src/lib.rs index 925fac2..bc493c3 100644 --- a/crates/lox-library/src/lib.rs +++ b/crates/lox-library/src/lib.rs @@ -339,8 +339,8 @@ impl BridgeAuth { distributor_bridges: &mut Vec, bdb: &mut BridgeDb, ) { - while let Some(bridge) = distributor_bridges.iter().next_back() { - self.bridge_table.unallocated_bridges.push(*bridge) + while let Some(bridge) = distributor_bridges.pop() { + self.bridge_table.unallocated_bridges.push(bridge); } while self.bridge_table.unallocated_bridges.len() >= MAX_BRIDGES_PER_BUCKET { let mut bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET]; diff --git a/crates/lox-library/src/tests.rs b/crates/lox-library/src/tests.rs index 8130b31..5627d9a 100644 --- a/crates/lox-library/src/tests.rs +++ b/crates/lox-library/src/tests.rs @@ -601,9 +601,27 @@ fn test_redeem_invite() { #[test] fn test_allocate_bridges() { let mut th = TestHarness::new(); - // Check that any bridges in distributor_bridges are first added to unallocated bridges - // and finally are added as openinv_buckets - // TODO + let distributor_bridges: &mut Vec = &mut Vec::new(); + let table_size = th.ba.bridge_table.buckets.len(); + for _ in 0..3 { + distributor_bridges.push(BridgeLine::random()); + } + assert!(!distributor_bridges.is_empty(), "No BridgeLines in distributor_bridges"); + th.ba.allocate_bridges(distributor_bridges, &mut th.bdb); + assert!(distributor_bridges.is_empty(), "BridgeLines in distributor_bridges were not allocated"); + assert!(th.ba.bridge_table.buckets.len() > table_size, "Size of bridge table did not increase"); + let table_size = th.ba.bridge_table.buckets.len(); + for _ in 0..2 { + distributor_bridges.push(BridgeLine::random()); + th.ba.bridge_table.unallocated_bridges.push(BridgeLine::random()); + } + assert!(!th.ba.bridge_table.unallocated_bridges.is_empty(), "No BridgeLines in unallocated bridges"); + assert!(!distributor_bridges.is_empty(), "No BridgeLines in distributor_bridges"); + th.ba.allocate_bridges(distributor_bridges, &mut th.bdb); + assert!(th.ba.bridge_table.unallocated_bridges.len() == 1, "Incorrect number of bridges remain unallocated"); + assert!(distributor_bridges.is_empty(), "BridgeLines in distributor_bridges were not allocated"); + assert!(th.ba.bridge_table.buckets.len() > table_size, "Size of bridge table did not increase"); + } #[test]