Added better error checking and tests for appropriate failures.
This commit is contained in:
parent
600fd0c276
commit
fbf1a9214b
5 changed files with 444 additions and 143 deletions
|
@ -1,9 +1,30 @@
|
|||
use crate::prelude::*;
|
||||
use crate::rail_segment;
|
||||
use crate::rail_segment::RailSegment;
|
||||
use crate::rail_connector::RailConnector;
|
||||
//use crate::graph_coord::GraphCoord;
|
||||
//use std::collections::HashMap;
|
||||
use std::vec::Vec;
|
||||
//use std::string::String;
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
IndexError(usize),
|
||||
SegmentError(rail_segment::Error),
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::IndexError(index) => write!(f, "Nonexistent segment index: {index}."),
|
||||
Self::SegmentError(err) => write!(f, "Segment error: {err}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for Error {}
|
||||
|
||||
pub struct RailGraph {
|
||||
segments: Vec<Option<Box<dyn RailSegment>>>,
|
||||
|
@ -21,10 +42,12 @@ impl RailGraph {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_segment(&self, index: usize) -> Option<&Box<dyn RailSegment>> {
|
||||
// definition
|
||||
|
||||
pub fn get_segment(&self, index: usize) -> Result<&Box<dyn RailSegment>, Error> {
|
||||
match &self.segments.get(index) {
|
||||
Some(ret) => ret.as_ref(),
|
||||
None => None,
|
||||
Some(Some(ret)) => Ok(ret),
|
||||
_ => Err(Error::IndexError(index)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,58 +63,110 @@ impl RailGraph {
|
|||
ret
|
||||
}
|
||||
|
||||
pub fn remove_segment(&mut self, index: usize) {
|
||||
pub fn remove_segment(&mut self, index: usize) -> Result<bool, Error> {
|
||||
if index >= self.segments.len() {
|
||||
return Err(Error::IndexError(index));
|
||||
}
|
||||
|
||||
self.isolate_segment(index);
|
||||
self.segments[index] = Option::None;
|
||||
if index < self.min_available_index {
|
||||
self.min_available_index = index;
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn connect_endpoints(&mut self, index1: usize, endpoint1: usize, index2: usize, endpoint2: usize) {
|
||||
pub fn connect_endpoints(&mut self, index1: usize, endpoint1: usize, index2: usize, endpoint2: usize) -> Result<bool, Error> {
|
||||
// validate input first
|
||||
if !self.validate_index(index1) {
|
||||
return Err(Error::IndexError(index1));
|
||||
} else if !self.validate_index(index2) {
|
||||
return Err(Error::IndexError(index2));
|
||||
} else if !self.validate_endpoint(index1, endpoint1) {
|
||||
return Err(Error::SegmentError(rail_segment::Error::EndpointError(endpoint1)));
|
||||
} else if !self.validate_endpoint(index2, endpoint2) {
|
||||
return Err(Error::SegmentError(rail_segment::Error::EndpointError(endpoint2)));
|
||||
}
|
||||
|
||||
// Needs improvement: silently fails with invalid endpoints.
|
||||
let connector1 = RailConnector::new(index2, endpoint2);
|
||||
let connector2 = RailConnector::new(index1, endpoint1);
|
||||
// Before we continue, make sure that anything that these endpoints are already connected to are disconnected.
|
||||
if let Some(segment1) = &self.segments[index1] {
|
||||
if let Ok(old_connection) = segment1.borrow_connection(endpoint1) {
|
||||
let old_index = old_connection.segment_index;
|
||||
let old_endpoint = old_connection.endpoint_index;
|
||||
if let Some(old_segment) = &mut self.segments[old_index] {
|
||||
let _ = old_segment.remove_connection(old_endpoint);
|
||||
let old_connection1 = self.get_connection_from_index(index1, endpoint1);
|
||||
let old_connection2 = self.get_connection_from_index(index2, endpoint2);
|
||||
if let Ok(old_connection) = old_connection1 {
|
||||
match &mut self.segments[old_connection.segment_index] {
|
||||
Some(old_segment) => {
|
||||
if let Err(e) = old_segment.remove_connection(old_connection.endpoint_index) {
|
||||
return Err(Error::SegmentError(e));
|
||||
}
|
||||
},
|
||||
None => {
|
||||
return Err(Error::IndexError(old_connection.segment_index));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(segment2) = &self.segments[index2] {
|
||||
if let Ok(old_connection) = segment2.borrow_connection(endpoint2) {
|
||||
let old_index = old_connection.segment_index;
|
||||
let old_endpoint = old_connection.endpoint_index;
|
||||
if let Some(old_segment) = &mut self.segments[old_index] {
|
||||
let _ = old_segment.remove_connection(old_endpoint);
|
||||
if let Ok(old_connection) = old_connection2 {
|
||||
match &mut self.segments[old_connection.segment_index] {
|
||||
Some(old_segment) => {
|
||||
if let Err(e) = old_segment.remove_connection(old_connection.endpoint_index) {
|
||||
return Err(Error::SegmentError(e));
|
||||
}
|
||||
},
|
||||
None => {
|
||||
return Err(Error::IndexError(old_connection.segment_index));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Connect them now
|
||||
if let Some(segment1) = &mut self.segments[index1] {
|
||||
let _ = segment1.add_connection(endpoint1, connector1);
|
||||
}
|
||||
if let Some(segment2) = &mut self.segments[index2] {
|
||||
let _ = segment2.add_connection(endpoint2, connector2);
|
||||
}
|
||||
let segment1 = &mut self.segments[index1].as_mut().unwrap();
|
||||
let _ = segment1.add_connection(endpoint1, connector1);
|
||||
let segment2 = &mut self.segments[index2].as_mut().unwrap();
|
||||
let _ = segment2.add_connection(endpoint2, connector2);
|
||||
|
||||
//if let Some(segment1) = &mut self.segments[index1] {
|
||||
// if let Err(e) = segment1.add_connection(endpoint1, connector1) {
|
||||
// return Err(Error::SegmentError(e));
|
||||
// }
|
||||
//}
|
||||
//if let Some(segment2) = &mut self.segments[index2] {
|
||||
// if let Err(e) = segment2.add_connection(endpoint2, connector2) {
|
||||
// return Err(Error::SegmentError(e));
|
||||
// }
|
||||
//}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn disconnect_endpoint(&mut self, index: usize, endpoint: usize) {
|
||||
if let Some(segment) = &self.segments[index] {
|
||||
if let Ok(old_connection) = segment.borrow_connection(endpoint) {
|
||||
let old_index = old_connection.segment_index;
|
||||
let old_endpoint = old_connection.endpoint_index;
|
||||
if let Some(old_segment) = &mut self.segments[old_index] {
|
||||
let _ = old_segment.remove_connection(old_endpoint);
|
||||
}
|
||||
}
|
||||
pub fn disconnect_endpoint(&mut self, index: usize, endpoint: usize) -> Result<bool, Error> {
|
||||
if index >= self.segments.len() {
|
||||
return Err(Error::IndexError(index));
|
||||
}
|
||||
|
||||
match self.get_connection_from_index(index, endpoint) {
|
||||
Ok(old_connection) => {
|
||||
match &mut self.segments[old_connection.segment_index] {
|
||||
Some(old_segment) => {
|
||||
if let Err(e) = old_segment.remove_connection(old_connection.endpoint_index) {
|
||||
return Err(Error::SegmentError(e));
|
||||
}
|
||||
},
|
||||
None => {
|
||||
return Err(Error::IndexError(old_connection.segment_index));
|
||||
},
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
}
|
||||
|
||||
if let Some(segment) = &mut self.segments[index] {
|
||||
let _ = segment.remove_connection(endpoint);
|
||||
match segment.remove_connection(endpoint) {
|
||||
Ok(_) => Ok(true),
|
||||
Err(e) => Err(Error::SegmentError(e)),
|
||||
}
|
||||
} else {
|
||||
Err(Error::IndexError(index)) // should never happen since this is already tested in the big match block.
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -99,9 +174,50 @@ impl RailGraph {
|
|||
new_segment: Box<dyn RailSegment>,
|
||||
new_segment_endpoint: usize,
|
||||
onto_segment: usize,
|
||||
ont_segment_endpoint: usize) {
|
||||
ont_segment_endpoint: usize) -> Result<bool, Error> {
|
||||
let index = self.add_segment(new_segment);
|
||||
self.connect_endpoints(index, new_segment_endpoint, onto_segment, ont_segment_endpoint);
|
||||
self.connect_endpoints(index, new_segment_endpoint, onto_segment, ont_segment_endpoint)
|
||||
}
|
||||
|
||||
// querying and traversal
|
||||
|
||||
//pub fn transform_of_coord(&self, coord: &GraphCoord) -> Transform3D {
|
||||
// if let Some(segment) = self.
|
||||
//}
|
||||
|
||||
// private
|
||||
|
||||
fn validate_index(&self, index: usize) -> bool {
|
||||
match &self.segments.get(index) {
|
||||
Some(Some(_)) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_endpoint(&self, index: usize, endpoint: usize) -> bool {
|
||||
match self.get_segment(index) {
|
||||
Ok(segment) => {
|
||||
match segment.check_connection(endpoint) {
|
||||
Err(rail_segment::Error::EndpointError(endpoint)) => false,
|
||||
_ => true,
|
||||
}
|
||||
},
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_connection_from_segment(segment: &Box<dyn RailSegment>, endpoint: usize) -> Result<RailConnector, Error> {
|
||||
match segment.get_connection(endpoint) {
|
||||
Ok(connection) => Ok(connection),
|
||||
Err(e) => Err(Error::SegmentError(e)),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_connection_from_index(&self, index: usize, endpoint: usize) -> Result<RailConnector, Error> {
|
||||
match self.get_segment(index) {
|
||||
Ok(segment) => Self::get_connection_from_segment(segment, endpoint),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
fn isolate_segment(&mut self, index: usize) {
|
||||
|
@ -165,22 +281,29 @@ mod tests {
|
|||
|
||||
let mut test_graph = RailGraph::new();
|
||||
test_graph.add_segment(test_segment1);
|
||||
test_graph.append_segment(test_segment2, 0, 0, 1);
|
||||
test_graph.append_segment(test_segment3, 0, 1, 1);
|
||||
let _ = test_graph.append_segment(test_segment2, 0, 0, 1);
|
||||
let _ = test_graph.append_segment(test_segment3, 0, 1, 1);
|
||||
return test_graph;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_segment() {
|
||||
let test_graph = create_test_graph();
|
||||
assert!(match test_graph.get_segment(0) { Option::Some(_)=>true, _=>false },
|
||||
let mut test_graph = create_test_graph();
|
||||
|
||||
// test successes
|
||||
assert!(match test_graph.get_segment(0) { Ok(_)=>true, Err(_)=>false, },
|
||||
"Segment 0 could not be retrieved.");
|
||||
assert!(match test_graph.get_segment(1) { Option::Some(_)=>true, _=>false },
|
||||
assert!(match test_graph.get_segment(1) { Ok(_)=>true, _=>false, },
|
||||
"Segment 1 could not be retrieved.");
|
||||
assert!(match test_graph.get_segment(2) { Option::Some(_)=>true, _=>false },
|
||||
assert!(match test_graph.get_segment(2) { Ok(_)=>true, _=>false, },
|
||||
"Segment 2 could not be retrieved.");
|
||||
assert!(match test_graph.get_segment(3) { Option::None=>true, _=>false },
|
||||
"Segment that didn't exist was retrieved.");
|
||||
|
||||
// test failures
|
||||
assert!(match test_graph.get_segment(3) { Err(Error::IndexError(3)) =>true, _=>false, },
|
||||
"Segment was retrieved: index out of bounds.");
|
||||
_ = test_graph.remove_segment(1);
|
||||
assert!(match test_graph.get_segment(1) { Err(Error::IndexError(1)) =>true, _=>false, },
|
||||
"Segment was retrieved: deleted.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -198,21 +321,57 @@ mod tests {
|
|||
#[test]
|
||||
fn test_remove_segment() {
|
||||
let mut test_graph = create_test_graph();
|
||||
test_graph.remove_segment(1);
|
||||
|
||||
// test failures
|
||||
assert!(match test_graph.remove_segment(3) { Err(Error::IndexError(3))=>true, _=>false, },
|
||||
"Non-existent segment was removed.");
|
||||
|
||||
// test successes
|
||||
assert!(match test_graph.remove_segment(1) { Ok(true)=>true, _=>false, },
|
||||
"Segment was not removed.");
|
||||
assert_eq!(test_graph.segments.len(), 3, "Graph has incorrect number of segments.");
|
||||
assert_eq!(test_graph.min_available_index, 1, "Incorrect min_available_index.");
|
||||
let first_segment = test_graph.get_segment(0).unwrap();
|
||||
assert!(!first_segment.check_connection(1).unwrap(), "Connection 1 wasn't broken.");
|
||||
let second_segment = test_graph.get_segment(2).unwrap();
|
||||
assert!(!second_segment.check_connection(0).unwrap(), "Connection 2 wasn't broken.");
|
||||
assert!(match test_graph.get_segment(1) { Option::None=>true, _=>false },
|
||||
assert!(match test_graph.get_segment(1) { Err(Error::IndexError(1))=>true, _=>false, },
|
||||
"Segment that didn't exist was retrieved.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_connect_endpoints() {
|
||||
let mut test_graph = create_test_graph();
|
||||
test_graph.connect_endpoints(0, 0, 2, 1);
|
||||
|
||||
// test failures
|
||||
assert!(match test_graph.connect_endpoints(3, 0, 2, 1) { Err(Error::IndexError(3))=>true, _=> false, },
|
||||
"Connection(3, 0) to (2, 1) was made.");
|
||||
assert!(match test_graph.get_segment(2).unwrap().borrow_connection(1) { Err(rail_segment::Error::ConnectionError(1))=>true, _=>false, },
|
||||
"Invalid connection at (2, 1) #1.");
|
||||
assert!(match test_graph.connect_endpoints(0, 2, 2, 1) { Err(Error::SegmentError(rail_segment::Error::EndpointError(2)))=>true, _=> false, },
|
||||
"Connection(0, 2) to (2, 1) was made.");
|
||||
assert!(match test_graph.get_segment(2).unwrap().borrow_connection(1) { Err(rail_segment::Error::ConnectionError(1))=>true, _=>false, },
|
||||
"Invalid connection at (2, 1) #2.");
|
||||
assert!(match test_graph.connect_endpoints(0, 0, 3, 1) { Err(Error::IndexError(3))=>true, _=> false, },
|
||||
"Connection(0, 0) to (3, 1) was made.");
|
||||
assert!(match test_graph.get_segment(0).unwrap().borrow_connection(0) { Err(rail_segment::Error::ConnectionError(0))=>true, _=>false, },
|
||||
"Invalid connection at (0, 0) #1.");
|
||||
assert!(match test_graph.connect_endpoints(0, 0, 2, 2) { Err(Error::SegmentError(rail_segment::Error::EndpointError(2)))=>true, _=> false, },
|
||||
"Connection(0, 0) to (2, 2) was made.");
|
||||
assert!(match test_graph.get_segment(0).unwrap().borrow_connection(0) { Err(rail_segment::Error::ConnectionError(0))=>true, _=>false, },
|
||||
"Invalid connection at (0, 0) #2.");
|
||||
assert!(match test_graph.connect_endpoints(0, 1, 3, 1) { Err(Error::IndexError(3))=>true, _=> false, },
|
||||
"Connection(0, 1) to (3, 1) was made.");
|
||||
assert!(match test_graph.get_segment(0).unwrap().borrow_connection(1) { Ok(_)=>true, _=>false, },
|
||||
"Removed connection at (0, 1) #1.");
|
||||
assert!(match test_graph.connect_endpoints(0, 1, 2, 2) { Err(Error::SegmentError(rail_segment::Error::EndpointError(2)))=>true, _=> false, },
|
||||
"Connection(0, 1) to (2, 2) was made.");
|
||||
assert!(match test_graph.get_segment(0).unwrap().borrow_connection(1) { Ok(_)=>true, _=>false, },
|
||||
"Removed connection at (0, 1) #2.");
|
||||
|
||||
// test successes
|
||||
assert!(match test_graph.connect_endpoints(0, 0, 2, 1) { Ok(true)=>true, _=> false, },
|
||||
"Connection(0, 0) to (2, 1) was not made.");
|
||||
let first_segment = test_graph.get_segment(0).unwrap();
|
||||
let first_connection = first_segment.borrow_connection(0).unwrap();
|
||||
assert_eq!(first_connection.segment_index, 2, "Connection(0, 0) had the incorrect index.");
|
||||
|
@ -221,7 +380,8 @@ mod tests {
|
|||
let second_connection = second_segment.borrow_connection(1).unwrap();
|
||||
assert_eq!(second_connection.segment_index, 0, "Connection(2, 1) had the incorrect index.");
|
||||
assert_eq!(second_connection.endpoint_index, 0, "Connection(2, 1) had the incorrect endpoint.");
|
||||
test_graph.connect_endpoints(0, 1, 2, 0);
|
||||
assert!(match test_graph.connect_endpoints(0, 1, 2, 0) { Ok(true)=>true, _=> false, },
|
||||
"Connection(0, 1) to (2, 0) was not made.");
|
||||
let first_segment = test_graph.get_segment(0).unwrap();
|
||||
let first_connection = first_segment.borrow_connection(1).unwrap();
|
||||
assert_eq!(first_connection.segment_index, 2, "Connection(0, 1) had the incorrect index.");
|
||||
|
@ -242,7 +402,18 @@ mod tests {
|
|||
#[test]
|
||||
fn test_disconnect_endpoint() {
|
||||
let mut test_graph = create_test_graph();
|
||||
test_graph.disconnect_endpoint(0, 1);
|
||||
|
||||
// test failures
|
||||
assert!(match test_graph.disconnect_endpoint(3, 1) { Err(Error::IndexError(3))=>true, _=>false },
|
||||
"Invalid segment accessed.");
|
||||
assert!(match test_graph.disconnect_endpoint(1, 2) { Err(Error::SegmentError(rail_segment::Error::EndpointError(2)))=>true, _=>false },
|
||||
"Invalid endpoint disconnected.");
|
||||
assert!(match test_graph.disconnect_endpoint(0, 0) { Err(Error::SegmentError(rail_segment::Error::ConnectionError(0)))=>true, _=>false },
|
||||
"Unconnected endpoint disconnected.");
|
||||
|
||||
// test successes
|
||||
assert!(match test_graph.disconnect_endpoint(0, 1) { Ok(true)=>true, _=>false },
|
||||
"Endpoint failed to disconnect.");
|
||||
let first_segment = test_graph.get_segment(0).unwrap();
|
||||
let first_connection = first_segment.borrow_connection(1);
|
||||
assert!(match first_connection { Result::Err(_)=>true, _=>false },
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue