Skip to content

Commit

Permalink
Fix examples and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoledoux committed Jun 25, 2024
1 parent a7332c4 commit 3a8c6f6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
11 changes: 7 additions & 4 deletions examples/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ struct MyAttribute {
fn main() {
let mut dt = startin::Triangulation::new();
dt.set_duplicates_handling(startin::DuplicateHandling::Highest);
let _ = dt.add_attribute_map("intensity".to_string(), "f64".to_string());
let _ = dt.add_attribute_map("classification".to_string(), "u64".to_string());
let _ = dt.add_attribute_map("visited".to_string(), "bool".to_string());
let att_schema: Vec<(String, String)> = vec![
("intensity".to_string(), "f64".to_string()),
("classification".to_string(), "u64".to_string()),
("visited".to_string(), "bool".to_string()),
];
let _ = dt.set_attributes_schema(att_schema);

let a = MyAttribute {
intensity: 44.0,
Expand All @@ -36,7 +39,7 @@ fn main() {
let _ = dt.add_vertex_attributes(vi.unwrap(), json!({"classification": 1, "visited": true}));

println!("{}", dt);
println!("{:?}\n", dt.list_all_attributes());
println!("{:?}\n", dt.all_attributes());
println!("{:?}", dt.all_attributes());

println!("{:?}", dt.get_vertex_attributes(4));
Expand Down
2 changes: 1 addition & 1 deletion examples/example2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
let re = dt.insert_one_pt(22.2, 33.3, 4.4);
match re {
Ok(_v) => println!("Inserted new point"),
Err(v) => println!("Duplicate of vertex #{}, not inserted", v),
Err((v, _b)) => println!("Duplicate of vertex #{}, not inserted", v),
}
//-- remove it
let re = dt.remove(6);
Expand Down
2 changes: 1 addition & 1 deletion examples/readme_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
"Inserted new point, now the DT has {} vertices",
dt.number_of_vertices()
),
Err(v) => println!("Duplicate of vertex #{}, not inserted", v),
Err((v, _b)) => println!("Duplicate of vertex #{}, not inserted", v),
}
//-- remove it
match dt.remove(6) {
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
//! "Inserted new point, now the DT has {} vertices",
//! dt.number_of_vertices()
//! ),
//! Err(v) => println!("Duplicate of vertex #{}, not inserted", v),
//! Err((v, _b)) => println!("Duplicate of vertex #{}, not inserted", v),
//! }
//! //-- remove it
//! match dt.remove(6) {
Expand Down Expand Up @@ -540,8 +540,9 @@ impl Triangulation {

/// Insert the point (`px`, `py`, `pz`) in the triangulation.
/// Returns the vertex ID of the point if the vertex didn't exist.
/// If there was a vertex at that location, an Error is thrown with the already
/// existing vertex ID.
/// If there was a vertex at that location, an Error is thrown with a tuple
/// indicating 1) the vertex ID of the existing vertex; 2) true/false whether the
/// z-value and attributes were updated.
pub fn insert_one_pt(&mut self, px: f64, py: f64, pz: f64) -> Result<usize, (usize, bool)> {
if !self.is_init {
return self.insert_one_pt_init_phase(px, py, pz);
Expand Down
18 changes: 9 additions & 9 deletions tests/duplicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,33 @@ fn duplicate_alltests() {

dt.set_duplicates_handling(startin::DuplicateHandling::First);
let re = dt.insert_one_pt(5.0, 5.0, 20.0);
assert_eq!(Err(5), re);
assert_eq!(Err((5, false)), re);
match re {
Ok(_) => (),
Err(i) => assert_eq!(dt.get_point(i).unwrap()[2], 10.0),
Err((i, _b)) => assert_eq!(dt.get_point(i).unwrap()[2], 10.0),
}

dt.set_duplicates_handling(startin::DuplicateHandling::Last);
let re = dt.insert_one_pt(5.0, 5.0, 20.0);
assert_eq!(Err(5), re);
assert_eq!(Err((5, true)), re);
match re {
Ok(_) => (),
Err(i) => assert_eq!(dt.get_point(i).unwrap()[2], 20.0),
Err((i, _b)) => assert_eq!(dt.get_point(i).unwrap()[2], 20.0),
}

dt.set_duplicates_handling(startin::DuplicateHandling::Highest);
let re = dt.insert_one_pt(5.0, 5.0, 20.0);
assert_eq!(Err(5), re);
let re = dt.insert_one_pt(5.0, 5.0, 21.0);
assert_eq!(Err((5, true)), re);
match re {
Ok(_) => (),
Err(i) => assert_eq!(dt.get_point(i).unwrap()[2], 20.0),
Err((i, _b)) => assert_eq!(dt.get_point(i).unwrap()[2], 21.0),
}

dt.set_duplicates_handling(startin::DuplicateHandling::Lowest);
let re = dt.insert_one_pt(5.0, 5.0, 5.0);
assert_eq!(Err(5), re);
assert_eq!(Err((5, true)), re);
match re {
Ok(_) => (),
Err(i) => assert_eq!(dt.get_point(i).unwrap()[2], 5.0),
Err((i, _b)) => assert_eq!(dt.get_point(i).unwrap()[2], 5.0),
}
}
2 changes: 1 addition & 1 deletion tests/init_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn duplicates() {
let _re = dt.insert_one_pt(2.2, 2.3, 4.5);
let re2 = dt.insert_one_pt(2.2, 2.3, 4.5);
assert!(re2.is_err());
assert_eq!(2, re2.unwrap_err());
assert_eq!((2, false), re2.unwrap_err());
assert_eq!(2, dt.number_of_vertices());
assert_eq!(0, dt.number_of_triangles());
}
Expand Down

0 comments on commit 3a8c6f6

Please sign in to comment.