Skip to content

Commit

Permalink
Add unit test cases of federate_info.rs
Browse files Browse the repository at this point in the history
- 17 unit test cases are added.
  • Loading branch information
chanijjani committed Jan 30, 2024
1 parent 1e3089e commit 0e0c45a
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 6 deletions.
147 changes: 143 additions & 4 deletions rust/rti/src/federate_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ impl FederateInfo {
self.clock_synchronization_enabled
}

pub fn in_transit_message_tags(&mut self) -> &mut InTransitMessageRecordQueue {
&mut self.in_transit_message_tags
}

pub fn server_hostname(&self) -> String {
self.server_hostname.clone()
}

pub fn server_port(&self) -> i32 {
self.server_port
}

pub fn set_requested_stop(&mut self, requested_stop: bool) {
self.requested_stop = requested_stop;
}
Expand All @@ -93,10 +105,6 @@ impl FederateInfo {
self.clock_synchronization_enabled = clock_synchronization_enabled;
}

pub fn in_transit_message_tags(&mut self) -> &mut InTransitMessageRecordQueue {
&mut self.in_transit_message_tags
}

pub fn set_server_hostname(&mut self, server_hostname: String) {
self.server_hostname = server_hostname;
}
Expand All @@ -105,3 +113,134 @@ impl FederateInfo {
self.server_port = server_port;
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::net::TcpStream;

#[test]
fn test_new_positive() {
let federate_info = FederateInfo::new();
// TODO: Check federate_info
assert!(true);
}

#[test]
fn test_e_positive() {
let federate_info = FederateInfo::new();
let e = federate_info.e();
assert!(e == &SchedulingNode::new());
}

#[test]
fn test_enclave_positive() {
let mut federate_info = FederateInfo::new();
let enclave = federate_info.enclave();
assert!(enclave == &SchedulingNode::new());
}

#[test]
fn test_requested_stop_positive() {
let federate_info = FederateInfo::new();
let is_requested_stop = federate_info.requested_stop();
assert!(is_requested_stop == false);
}

#[test]
fn test_initial_stream_positive() {
let federate_info = FederateInfo::new();
let initial_stream = federate_info.stream();
// TODO: Check initial_stream
assert!(true);
}

#[test]
fn test_clock_synchronization_enabled_positive() {
let federate_info = FederateInfo::new();
let is_clock_synchronization_enabled = federate_info.clock_synchronization_enabled();
assert!(is_clock_synchronization_enabled == true);
}

#[test]
fn test_in_transit_message_tags_positive() {
let mut federate_info = FederateInfo::new();
let in_transit_message_tags = federate_info.in_transit_message_tags();
assert!(in_transit_message_tags == &mut InTransitMessageRecordQueue::new())
}

#[test]
fn test_set_requested_stop_as_true_positive() {
let mut federate_info = FederateInfo::new();
federate_info.set_requested_stop(true);
assert!(federate_info.requested_stop() == true);
}

#[test]
fn test_set_requested_stop_as_false_positive() {
let mut federate_info = FederateInfo::new();
federate_info.set_requested_stop(false);
assert!(federate_info.requested_stop() == false);
}

#[test]
fn test_set_stream_with_valid_stream_positive() {
let mut federate_info = FederateInfo::new();
// TODO: Enable below
// match TcpStream::connect("127.0.0.1:8080") {
// Ok(valid_stream) => {
// federate_info.set_stream(valid_stream);
// assert!(federate_info.stream() == valid_stream);
// }
// Err(_e) => {
// assert!(false);
// }
// };
}

#[test]
fn test_set_clock_synchronization_enabled_with_true_positive() {
let mut federate_info = FederateInfo::new();
federate_info.set_clock_synchronization_enabled(true);
assert!(federate_info.clock_synchronization_enabled() == true);
}

#[test]
fn test_set_clock_synchronization_enabled_with_false_positive() {
let mut federate_info = FederateInfo::new();
federate_info.set_clock_synchronization_enabled(false);
assert!(federate_info.clock_synchronization_enabled() == false);
}

#[test]
fn test_set_server_hostname_with_valid_name_positive() {
let mut federate_info = FederateInfo::new();
let server_name = String::from("test_rust_rti_server");
federate_info.set_server_hostname(server_name.clone());
assert!(federate_info.server_hostname() == server_name);
}

#[test]
fn test_set_server_hostname_with_invalid_name_negative() {
let mut federate_info = FederateInfo::new();
let server_name = String::from("");
federate_info.set_server_hostname(server_name.clone());
assert!(federate_info.server_hostname().len() == 0);
}

#[test]
fn test_set_server_port_with_valid_port_positive() {
let mut federate_info = FederateInfo::new();
let server_port = 8080;
federate_info.set_server_port(server_port);
assert!(federate_info.server_port() == server_port);
}

#[test]
fn test_set_server_port_with_invalid_port_negative() {
let mut federate_info = FederateInfo::new();
let server_port = -1;
federate_info.set_server_port(server_port);
assert!(federate_info.server_port() < 0);
}
}
1 change: 1 addition & 0 deletions rust/rti/src/message_record/message_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::tag::{Instant, Tag};
* @brief Queue to keep a record of in-transit messages.
*
*/
#[derive(PartialEq)]
pub struct InTransitMessageRecordQueue {
main_queue: PriorityQueue<Tag, usize>, // The primary queue.
transfer_queue: PriorityQueue<Tag, usize>, // Queue used for housekeeping.
Expand Down
3 changes: 3 additions & 0 deletions rust/rti/src/rti_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const IS_IN_ZERO_DELAY_CYCLE: i32 = 1;
const IS_IN_CYCLE: i32 = 2;

/** Mode of execution of a federate. */
#[derive(PartialEq)]
enum ExecutionMode {
FAST,
REALTIME,
Expand All @@ -41,6 +42,7 @@ pub enum SchedulingNodeState {
}

/** Struct for minimum delays from upstream nodes. */
#[derive(PartialEq)]
pub struct MinimumDelay {
id: i32, // ID of the upstream node.
min_delay: Tag, // Minimum delay from upstream.
Expand Down Expand Up @@ -69,6 +71,7 @@ impl MinimumDelay {
* denoted with ~>) because those connections do not impose
* any scheduling constraints.
*/
#[derive(PartialEq)]
pub struct SchedulingNode {
id: u16, // ID of this scheduling node.
completed: Tag, // The largest logical tag completed by the federate (or NEVER if no LTC has been received).
Expand Down
4 changes: 2 additions & 2 deletions rust/rti/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl Tag {
// );
result
}

pub fn lf_tag_add(a: &Tag, b: &Tag) -> Tag {
if a.time() == NEVER || b.time() == NEVER {
return Tag::never_tag();
Expand Down Expand Up @@ -205,4 +205,4 @@ mod tests {
let t2 = Tag::new(0, 0);
assert_eq!(0, Tag::lf_tag_compare(&t1, &t2))
}
}
}

0 comments on commit 0e0c45a

Please sign in to comment.