diff --git a/src/drone.rs b/src/drone.rs index 659f9dd..877a968 100644 --- a/src/drone.rs +++ b/src/drone.rs @@ -6,12 +6,12 @@ //! //! Author: Rustastic (Andrea Carzeri, Alessandro Busola, Andrea Denina, Giulio Bosio) +use crate::packet_buffer; use colored::Colorize; use crossbeam_channel::{select_biased, Receiver, Sender}; use log::{error, info, warn}; use rand::Rng; use std::collections::{HashMap, HashSet}; - use wg_2024::{ controller::{DroneCommand, DroneEvent}, drone::Drone, @@ -19,7 +19,8 @@ use wg_2024::{ packet::{FloodRequest, FloodResponse, Fragment, Nack, NackType, NodeType, Packet, PacketType}, }; -use crate::packet_buffer; +#[cfg(test)] +mod tests; #[allow(clippy::module_name_repetitions)] #[derive(Debug, Clone)] diff --git a/src/drone/tests/mod.rs b/src/drone/tests/mod.rs new file mode 100644 index 0000000..ef94ab0 --- /dev/null +++ b/src/drone/tests/mod.rs @@ -0,0 +1,3 @@ +mod test_flooding; +mod test_fragment_ack_nack; +mod test_handle_command; diff --git a/tests/test_flooding.rs b/src/drone/tests/test_flooding.rs similarity index 99% rename from tests/test_flooding.rs rename to src/drone/tests/test_flooding.rs index d59490c..af133c7 100644 --- a/tests/test_flooding.rs +++ b/src/drone/tests/test_flooding.rs @@ -1,3 +1,4 @@ +use crate::RustasticDrone; use crossbeam_channel::{select_biased, unbounded, Receiver, Sender}; use std::{ collections::{HashMap, HashSet}, @@ -12,8 +13,6 @@ use wg_2024::{ packet::{FloodRequest, FloodResponse, NodeType, Packet, PacketType}, }; -use rustastic_drone::RustasticDrone; - struct Host { id: NodeId, controller_send: Sender, diff --git a/tests/test_fragment_ack_nack.rs b/src/drone/tests/test_fragment_ack_nack.rs similarity index 99% rename from tests/test_fragment_ack_nack.rs rename to src/drone/tests/test_fragment_ack_nack.rs index 2b7754e..0afc31a 100644 --- a/tests/test_fragment_ack_nack.rs +++ b/src/drone/tests/test_fragment_ack_nack.rs @@ -1,5 +1,4 @@ -use rustastic_drone::RustasticDrone; - +use crate::RustasticDrone; use crossbeam_channel::{unbounded, Receiver, Sender}; use std::{ collections::HashMap, diff --git a/tests/test_handle_command.rs b/src/drone/tests/test_handle_command.rs similarity index 80% rename from tests/test_handle_command.rs rename to src/drone/tests/test_handle_command.rs index 2ac5ee8..e197328 100644 --- a/tests/test_handle_command.rs +++ b/src/drone/tests/test_handle_command.rs @@ -1,5 +1,4 @@ -use rustastic_drone::RustasticDrone; - +use crate::RustasticDrone; use crossbeam_channel::unbounded; use std::{ collections::HashMap, @@ -22,7 +21,7 @@ fn test_add_sender() { HashMap::new(), 0f32, ))); - println!("{drone_thread:?}"); + // println!("{drone_thread:?}"); let drone = drone_thread.clone(); let handler = thread::spawn(move || drone_thread.lock().unwrap().run()); @@ -35,9 +34,8 @@ fn test_add_sender() { controller_to_drone.send(DroneCommand::Crash).unwrap(); handler.join().unwrap(); let drone = drone.lock().unwrap(); - println!("{drone:?}"); - //TODO cannot access private field because test is not a submodule of drone - // assert_ne!(drone.packet_send.iter().last().0, 15); + // println!("{drone:?}"); + assert_eq!(*drone.packet_send.iter().last().unwrap().0, 15u8); } #[test] @@ -53,7 +51,7 @@ fn test_set_pdr() { HashMap::new(), 0f32, ))); - println!("{drone_thread:?}"); + // println!("{drone_thread:?}"); let drone = drone_thread.clone(); let handler = thread::spawn(move || drone_thread.lock().unwrap().run()); @@ -67,15 +65,18 @@ fn test_set_pdr() { handler.join().unwrap(); let drone = drone.lock().unwrap(); - println!("{drone:?}"); - //TODO cannot access private field because test is not a submodule of drone - // assert_ne!(drone.pdr, 0.05); + // println!("{drone:?}"); + #[allow(clippy::float_cmp)] + { + assert_eq!(drone.pdr, 0.05); + } } #[test] fn test_remove_sender() { let (drone_to_controller, _controller_from_drone) = unbounded(); let (controller_to_drone, drone_from_controller) = unbounded(); let mut sender = HashMap::new(); + sender.insert(3, unbounded().0); sender.insert(2, unbounded().0); let drone_thread = Arc::new(Mutex::new(RustasticDrone::new( 1, @@ -85,7 +86,7 @@ fn test_remove_sender() { sender, 0f32, ))); - println!("{drone_thread:?}"); + // println!("{drone_thread:?}"); let drone = drone_thread.clone(); let handler = thread::spawn(move || drone_thread.lock().unwrap().run()); @@ -98,7 +99,6 @@ fn test_remove_sender() { handler.join().unwrap(); let drone = drone.lock().unwrap(); - println!("{drone:?}"); - //TODO cannot access private field because test is not a submodule of drone - // assert_eq!( drone.packet_send.iter().last().1, 2 ) ; + // println!("{drone:?}"); + assert_ne!(*drone.packet_send.iter().last().unwrap().0, 2); }