diff --git a/deep_causality/src/prelude.rs b/deep_causality/src/prelude.rs index c98c1a1..3a676f7 100644 --- a/deep_causality/src/prelude.rs +++ b/deep_causality/src/prelude.rs @@ -62,6 +62,8 @@ pub use crate::types::context_types::node_types::space_time::SpaceTime; pub use crate::types::context_types::node_types::time::Time; // Adjustable types pub use crate::types::context_types::node_types_adjustable::adjustable_data::*; +pub use crate::types::context_types::node_types_adjustable::adjustable_space::*; +pub use crate::types::context_types::node_types_adjustable::adjustable_space_time::*; pub use crate::types::context_types::node_types_adjustable::adjustable_time::*; pub use crate::types::context_types::relation_kind::*; pub use crate::types::context_types::time_scale::TimeScale; diff --git a/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space/adjustable.rs b/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space/adjustable.rs index b33484c..e0b1080 100644 --- a/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space/adjustable.rs +++ b/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space/adjustable.rs @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT // Copyright (c) "2023" . The DeepCausality Authors. All Rights Reserved. +use std::fmt::Debug; use std::ops::Add; use dcl_data_structures::grid_type::ArrayGrid; @@ -12,7 +13,8 @@ use super::*; impl Adjustable for AdjustableSpace where - T: Default + T: Debug + + Default + Add + Sub + Mul @@ -57,40 +59,46 @@ where fn adjust( &mut self, - _array_grid: &ArrayGrid, + array_grid: &ArrayGrid, ) -> Result<(), AdjustmentError> { - // // Create a 3D PointIndex for each of the updated x,y,z coordinates - // let p1 = PointIndex::new3d(0, 0, 0); - // let p2 = PointIndex::new3d(0, 0, 1); - // let p3 = PointIndex::new3d(0, 0, 2); - // - // // Get the data at the index position - // let new_x = array_grid.get(p1); - // let new_y = array_grid.get(p2); - // let new_z = array_grid.get(p3); - // - // // Calculate the adjusted data - // let adjusted_x = self.x + new_x; - // let adjusted_y = self.y + new_y; - // let adjusted_z = self.z + new_z; - // - // // Check if the adjusted data are okay to update - // if adjusted_x < T::default() { - // return Err(AdjustmentError("Adjustment failed, new X data is NEGATIVE".into())); - // } - // - // if adjusted_y < T::default() { - // return Err(AdjustmentError("Adjustment failed, new Y data is NEGATIVE".into())); - // } - // - // if adjusted_z < T::default() { - // return Err(AdjustmentError("Adjustment failed, new Z data is NEGATIVE".into())); - // } - // - // // Replace the internal data with the adjusted data - // self.x = adjusted_x; - // self.y = adjusted_y; - // self.z = adjusted_z; + // Create a 3D PointIndex for each of the updated x,y,z coordinates + let p1 = PointIndex::new3d(0, 0, 0); + let p2 = PointIndex::new3d(0, 0, 1); + let p3 = PointIndex::new3d(0, 0, 2); + + // Get the data at the index position + let new_x = array_grid.get(p1); + let new_y = array_grid.get(p2); + let new_z = array_grid.get(p3); + + // Calculate the adjusted data + let adjusted_x = self.x + new_x; + let adjusted_y = self.y + new_y; + let adjusted_z = self.z + new_z; + + // Check if the adjusted data are okay to update + if adjusted_x < T::default() { + return Err(AdjustmentError( + "Adjustment failed, new X data is NEGATIVE".into(), + )); + } + + if adjusted_y < T::default() { + return Err(AdjustmentError( + "Adjustment failed, new Y data is NEGATIVE".into(), + )); + } + + if adjusted_z < T::default() { + return Err(AdjustmentError( + "Adjustment failed, new Z data is NEGATIVE".into(), + )); + } + + // Replace the internal data with the adjusted data + self.x = adjusted_x; + self.y = adjusted_y; + self.z = adjusted_z; Ok(()) } diff --git a/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space/mod.rs b/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space/mod.rs index 99f7f8a..7790435 100644 --- a/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space/mod.rs +++ b/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space/mod.rs @@ -15,6 +15,7 @@ pub struct AdjustableSpace where T: Default + Add + Sub + Mul + Copy, { + #[getter(name = space_id)] // Rename ID getter to prevent conflict impl with identifiable id: u64, x: T, y: T, diff --git a/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space_time/adjustable.rs b/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space_time/adjustable.rs index 1cddc41..cae2376 100644 --- a/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space_time/adjustable.rs +++ b/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space_time/adjustable.rs @@ -3,6 +3,7 @@ use std::ops::Add; // SPDX-License-Identifier: MIT // Copyright (c) "2023" . The DeepCausality Authors. All Rights Reserved. use dcl_data_structures::grid_type::ArrayGrid; +use dcl_data_structures::prelude::PointIndex; use crate::prelude::{Adjustable, AdjustmentError, UpdateError}; @@ -20,15 +21,101 @@ where { fn update( &mut self, - _array_grid: &ArrayGrid, + array_grid: &ArrayGrid, ) -> Result<(), UpdateError> { + // Create a 4D PointIndex for each of the updated x,y,z coordinates plus time t + let p1 = PointIndex::new4d(0, 0, 0, 0); + let p2 = PointIndex::new4d(0, 0, 0, 1); + let p3 = PointIndex::new4d(0, 0, 0, 2); + let pt = PointIndex::new4d(0, 0, 0, 3); + + // get the data at the index position + let new_x = array_grid.get(p1); + let new_y = array_grid.get(p2); + let new_z = array_grid.get(p3); + let new_t = array_grid.get(pt); + + // Check if the new data are okay to update + if new_x == T::default() { + return Err(UpdateError("Update failed, new X data is ZERO".into())); + } + + if new_y == T::default() { + return Err(UpdateError("Update failed, new Y data is ZERO".into())); + } + + if new_z == T::default() { + return Err(UpdateError("Update failed, new Z data is ZERO".into())); + } + + if new_t < T::default() { + return Err(UpdateError( + "Update failed, new T (Time) is Negative".into(), + )); + } + + // Update the internal data + self.x = new_x; + self.y = new_y; + self.z = new_z; + self.time_unit = new_t; + Ok(()) } fn adjust( &mut self, - _array_grid: &ArrayGrid, + array_grid: &ArrayGrid, ) -> Result<(), AdjustmentError> { + // Create a 4D PointIndex for each of the updated x,y,z coordinates plus time t + let p1 = PointIndex::new4d(0, 0, 0, 0); + let p2 = PointIndex::new4d(0, 0, 0, 1); + let p3 = PointIndex::new4d(0, 0, 0, 2); + let pt = PointIndex::new4d(0, 0, 0, 3); + + // get the data at the index position + let new_x = array_grid.get(p1); + let new_y = array_grid.get(p2); + let new_z = array_grid.get(p3); + let new_t = array_grid.get(pt); + + // Calculate the adjusted data + let adjusted_x = self.x + new_x; + let adjusted_y = self.y + new_y; + let adjusted_z = self.z + new_z; + let adjusted_t = self.time_unit + new_t; + + // Check if the adjusted data are okay to update + if adjusted_x < T::default() { + return Err(AdjustmentError( + "Adjustment failed, new X data is NEGATIVE".into(), + )); + } + + if adjusted_y < T::default() { + return Err(AdjustmentError( + "Adjustment failed, new Y data is NEGATIVE".into(), + )); + } + + if adjusted_z < T::default() { + return Err(AdjustmentError( + "Adjustment failed, new Z data is NEGATIVE".into(), + )); + } + + if adjusted_t < T::default() { + return Err(AdjustmentError( + "Adjustment failed, new T (Time) is Negative".into(), + )); + } + + // Replace the internal data with the adjusted data + self.x = adjusted_x; + self.y = adjusted_y; + self.z = adjusted_z; + self.time_unit = adjusted_t; + Ok(()) } } diff --git a/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space_time/display.rs b/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space_time/display.rs index b91373a..8d16ad5 100644 --- a/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space_time/display.rs +++ b/deep_causality/src/types/context_types/node_types_adjustable/adjustable_space_time/display.rs @@ -9,7 +9,10 @@ where T: Copy + Display + Default + Add + Sub + Mul, { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "AdjustableSpaceTime<{}> {{ id: {}, time_scale: {}, time_unit: {}, x: {}, y: {}, z: {} }}", - T::default(), self.id, self.time_scale, self.time_unit, self.x, self.y, self.z) + write!( + f, + "AdjustableSpaceTime {{ id: {}, time_scale: {}, time_unit: {}, x: {}, y: {}, z: {} }}", + self.id, self.time_scale, self.time_unit, self.x, self.y, self.z + ) } } diff --git a/deep_causality/src/types/context_types/node_types_adjustable/mod.rs b/deep_causality/src/types/context_types/node_types_adjustable/mod.rs index c9ac09f..ad1f362 100644 --- a/deep_causality/src/types/context_types/node_types_adjustable/mod.rs +++ b/deep_causality/src/types/context_types/node_types_adjustable/mod.rs @@ -2,5 +2,5 @@ // Copyright (c) "2023" . The DeepCausality Authors. All Rights Reserved. pub mod adjustable_data; pub mod adjustable_space; -mod adjustable_space_time; +pub mod adjustable_space_time; pub mod adjustable_time; diff --git a/deep_causality/tests/types/context_types/adjustable/adjustable_data_tests.rs b/deep_causality/tests/types/context_types/adjustable/adjustable_data_tests.rs index 49a7c86..812a575 100644 --- a/deep_causality/tests/types/context_types/adjustable/adjustable_data_tests.rs +++ b/deep_causality/tests/types/context_types/adjustable/adjustable_data_tests.rs @@ -10,7 +10,7 @@ fn test_update() { let mut d = AdjustableData::new(0, 0); assert_eq!(d.data(), &0); - let array_grid = utils::get_array_grid(42); + let array_grid = utils::get_1d_array_grid(42); let res = d.update(&array_grid); assert!(res.is_ok()); @@ -23,7 +23,7 @@ fn test_update_err() { let mut d = AdjustableData::new(0, 42); assert_eq!(d.data(), &42); - let array_grid = utils::get_array_grid(0); + let array_grid = utils::get_1d_array_grid(0); // Update fails with UpdateError let res = d.update(&array_grid); @@ -38,7 +38,7 @@ fn test_adjust() { let mut d = AdjustableData::new(0, 21); assert_eq!(d.data(), &21); - let array_grid = utils::get_array_grid(21); + let array_grid = utils::get_1d_array_grid(21); let res = d.adjust(&array_grid); assert!(res.is_ok()); @@ -51,7 +51,7 @@ fn test_adjust_err() { let mut d = AdjustableData::new(0, 21); assert_eq!(d.data(), &21); - let array_grid = utils::get_array_grid(-23); + let array_grid = utils::get_1d_array_grid(-23); // adjustment fails with AdjustmentError let res = d.adjust(&array_grid); diff --git a/deep_causality/tests/types/context_types/adjustable/adjustable_default.rs b/deep_causality/tests/types/context_types/adjustable/adjustable_default_tests.rs similarity index 89% rename from deep_causality/tests/types/context_types/adjustable/adjustable_default.rs rename to deep_causality/tests/types/context_types/adjustable/adjustable_default_tests.rs index 737b19b..a087bfb 100644 --- a/deep_causality/tests/types/context_types/adjustable/adjustable_default.rs +++ b/deep_causality/tests/types/context_types/adjustable/adjustable_default_tests.rs @@ -21,7 +21,7 @@ impl Adjustable for Data where T: Copy + Default {} #[test] fn test_update() { let mut d = Data::new(32); - let array_grid = utils::get_array_grid(42); + let array_grid = utils::get_1d_array_grid(42); // Default implementation does nothing let res = d.update(&array_grid); @@ -31,7 +31,7 @@ fn test_update() { #[test] fn test_adjustment() { let mut d = Data::new(32); - let array_grid = utils::get_array_grid(42); + let array_grid = utils::get_1d_array_grid(42); // Default implementation does nothing let res = d.adjust(&array_grid); diff --git a/deep_causality/tests/types/context_types/adjustable/adjustable_space_tests.rs b/deep_causality/tests/types/context_types/adjustable/adjustable_space_tests.rs new file mode 100644 index 0000000..2333d07 --- /dev/null +++ b/deep_causality/tests/types/context_types/adjustable/adjustable_space_tests.rs @@ -0,0 +1,152 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) "2023" . The DeepCausality Authors. All Rights Reserved. + +use deep_causality::prelude::{Adjustable, AdjustableSpace, Identifiable}; + +use crate::types::context_types::adjustable::utils; + +#[test] +fn test_update() { + let mut d = AdjustableSpace::new(0, 1, 2, 3); + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); + + let array_grid = utils::get_3d_array_grid(42, 43, 44); + + let res = d.update(&array_grid); + assert!(res.is_ok()); + + assert_eq!(d.x(), &42); // 42 + + assert_eq!(d.y(), &43); // 43 + + assert_eq!(d.z(), &44); // 44 +} + +#[test] +fn test_update_err() { + let mut d = AdjustableSpace::new(0, 1, 2, 3); + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); + + let array_grid = utils::get_3d_array_grid(0, 1, 2); + let res = d.update(&array_grid); + assert!(res.is_err()); + + let array_grid = utils::get_3d_array_grid(1, 0, 2); + let res = d.update(&array_grid); + assert!(res.is_err()); + + let array_grid = utils::get_3d_array_grid(1, 9, 0); + let res = d.update(&array_grid); + assert!(res.is_err()); + + // Old values still in place, as before the failed update. + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); +} + +#[test] +fn test_adjust() { + let mut d = AdjustableSpace::new(0, 10, 20, 30); + assert_eq!(d.x(), &10); + assert_eq!(d.y(), &20); + assert_eq!(d.z(), &30); + + let array_grid = utils::get_3d_array_grid(42, 43, 44); + + let res = d.adjust(&array_grid); + assert!(res.is_ok()); + + assert_eq!(d.x(), &52); + assert_eq!(d.y(), &63); + assert_eq!(d.z(), &74); +} + +#[test] +fn test_adjust_err() { + let mut d = AdjustableSpace::new(0, 1, 2, 3); + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); + + let array_grid = utils::get_3d_array_grid(-10, 20, 30); + let res = d.adjust(&array_grid); + assert!(res.is_err()); + + let array_grid = utils::get_3d_array_grid(10, -10, 30); + let res = d.adjust(&array_grid); + assert!(res.is_err()); + + let array_grid = utils::get_3d_array_grid(10, 10, -10); + let res = d.adjust(&array_grid); + assert!(res.is_err()); + + // Old values still in place, as before the failed update. + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); +} + +#[test] +fn test_id() { + let id = 1; + + let d = AdjustableSpace::new(id, 1, 2, 3); + assert_eq!(d.id(), id); +} + +#[test] +fn test_x() { + let id = 1; + let x = 42; + + let d = AdjustableSpace::new(id, x, 2, 3); + assert_eq!(d.id(), id); + assert_eq!(d.x(), &x); +} + +#[test] +fn test_y() { + let id = 1; + let x = 42; + let y = 23; + + let d = AdjustableSpace::new(id, x, y, 3); + assert_eq!(d.id(), id); + assert_eq!(d.x(), &x); + assert_eq!(d.y(), &y); +} + +#[test] +fn test_z() { + let id = 1; + let x = 42; + let y = 23; + let z = 99; + + let d = AdjustableSpace::new(id, x, y, z); + assert_eq!(d.id(), id); + assert_eq!(d.x(), &x); + assert_eq!(d.y(), &y); + assert_eq!(d.z(), &z); +} + +#[test] +fn test_to_string() { + let id = 1; + let x = 42; + let y = 23; + let z = 99; + + let d = AdjustableSpace::new(id, x, y, z); + let exp = format!( + "AdjustableSpace {{ id={:?}, x={:?}, y={:?}, z={:?} }}", + id, x, y, z + ); + let act = d.to_string(); + assert_eq!(act, exp); +} diff --git a/deep_causality/tests/types/context_types/adjustable/adjustable_space_time_tests.rs b/deep_causality/tests/types/context_types/adjustable/adjustable_space_time_tests.rs new file mode 100644 index 0000000..5f4439c --- /dev/null +++ b/deep_causality/tests/types/context_types/adjustable/adjustable_space_time_tests.rs @@ -0,0 +1,204 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) "2023" . The DeepCausality Authors. All Rights Reserved. + +use deep_causality::prelude::{ + Adjustable, AdjustableSpaceTime, Identifiable, SpaceTemporal, TimeScale, +}; + +use crate::types::context_types::adjustable::utils; + +#[test] +fn test_update() { + let id = 1; + let mut d = AdjustableSpaceTime::new(id, TimeScale::Minute, 4, 1, 2, 3); + assert_eq!(d.id(), id); + assert_eq!(d.time_scale(), &TimeScale::Minute); + assert_eq!(d.time_unit(), &4); + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); + + // Shift coordinates and time + let array_grid = utils::get_4d_array_grid(2, 3, 4, 5); + let res = d.update(&array_grid); + assert!(res.is_ok()); + + assert_eq!(d.time_scale(), &TimeScale::Minute); + assert_eq!(d.x(), &2); + assert_eq!(d.y(), &3); + assert_eq!(d.z(), &4); + assert_eq!(d.time_unit(), &5); +} + +#[test] +fn test_update_err() { + let id = 1; + let mut d = AdjustableSpaceTime::new(id, TimeScale::Minute, 4, 1, 2, 3); + assert_eq!(d.id(), id); + assert_eq!(d.time_scale(), &TimeScale::Minute); + assert_eq!(d.time_unit(), &4); + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); + + let array_grid = utils::get_4d_array_grid(0, 3, 4, 5); + let res = d.update(&array_grid); + assert!(res.is_err()); + + let array_grid = utils::get_4d_array_grid(1, 0, 4, 5); + let res = d.update(&array_grid); + assert!(res.is_err()); + + let array_grid = utils::get_4d_array_grid(1, 2, 0, 5); + let res = d.update(&array_grid); + assert!(res.is_err()); + + let array_grid = utils::get_4d_array_grid(1, 2, 3, -5); + let res = d.update(&array_grid); + assert!(res.is_err()); + + assert_eq!(d.id(), id); + assert_eq!(d.time_scale(), &TimeScale::Minute); + assert_eq!(d.time_unit(), &4); + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); +} + +#[test] +fn test_adjust() { + let id = 1; + let mut d = AdjustableSpaceTime::new(id, TimeScale::Minute, 4, 1, 2, 3); + assert_eq!(d.id(), id); + assert_eq!(d.time_scale(), &TimeScale::Minute); + assert_eq!(d.time_unit(), &4); + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); + + // Shift coordinates and time + let array_grid = utils::get_4d_array_grid(10, 10, 10, 10); + + let res = d.adjust(&array_grid); + assert!(res.is_ok()); + + assert_eq!(d.time_scale(), &TimeScale::Minute); + + assert_eq!(d.x(), &11); + assert_eq!(d.y(), &12); + assert_eq!(d.z(), &13); + assert_eq!(d.time_unit(), &14); +} + +#[test] +fn test_adjust_err() { + let id = 1; + let mut d = AdjustableSpaceTime::new(id, TimeScale::Minute, 4, 1, 2, 3); + assert_eq!(d.id(), id); + assert_eq!(d.time_scale(), &TimeScale::Minute); + assert_eq!(d.time_unit(), &4); + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); + + let array_grid = utils::get_4d_array_grid(-10, 3, 4, 5); + let res = d.adjust(&array_grid); + assert!(res.is_err()); + + let array_grid = utils::get_4d_array_grid(1, -10, 4, 5); + let res = d.adjust(&array_grid); + assert!(res.is_err()); + + let array_grid = utils::get_4d_array_grid(1, 2, -10, 5); + let res = d.adjust(&array_grid); + assert!(res.is_err()); + + let array_grid = utils::get_4d_array_grid(1, 2, 3, -10); + let res = d.adjust(&array_grid); + assert!(res.is_err()); + + assert_eq!(d.id(), id); + assert_eq!(d.time_scale(), &TimeScale::Minute); + assert_eq!(d.time_unit(), &4); + assert_eq!(d.x(), &1); + assert_eq!(d.y(), &2); + assert_eq!(d.z(), &3); +} + +#[test] +fn test_id() { + let id = 1; + + let d = AdjustableSpaceTime::new(id, TimeScale::Minute, 4, 1, 2, 3); + assert_eq!(d.id(), id); +} + +#[test] +fn test_x() { + let id = 1; + let x = 42; + + let d = AdjustableSpaceTime::new(id, TimeScale::Minute, 4, x, 2, 3); + assert_eq!(d.id(), id); + assert_eq!(d.x(), &x); +} + +#[test] +fn test_y() { + let id = 1; + let x = 42; + let y = 23; + + let d = AdjustableSpaceTime::new(id, TimeScale::Minute, 4, x, y, 3); + assert_eq!(d.id(), id); + assert_eq!(d.x(), &x); + assert_eq!(d.y(), &y); +} + +#[test] +fn test_z() { + let id = 1; + let x = 42; + let y = 23; + let z = 99; + + let d = AdjustableSpaceTime::new(id, TimeScale::Minute, 4, x, y, z); + assert_eq!(d.id(), id); + assert_eq!(d.x(), &x); + assert_eq!(d.y(), &y); + assert_eq!(d.z(), &z); +} + +#[test] +fn test_t() { + let id = 1; + let x = 42; + let y = 23; + let z = 99; + let t = 5; + + let d = AdjustableSpaceTime::new(id, TimeScale::Minute, t, x, y, z); + assert_eq!(d.id(), id); + assert_eq!(d.x(), &x); + assert_eq!(d.y(), &y); + assert_eq!(d.z(), &z); + assert_eq!(d.t(), &t); +} + +#[test] +fn test_to_string() { + let id = 1; + let x = 42; + let y = 23; + let z = 99; + let t = 5; + + let d = AdjustableSpaceTime::new(id, TimeScale::Minute, t, x, y, z); + let exp = + format!( + "AdjustableSpaceTime {{ id: {}, time_scale: {:?}, time_unit: {}, x: {}, y: {}, z: {} }}", + id, TimeScale::Minute, t, x, y, z + ); + let act = d.to_string(); + assert_eq!(act, exp); +} diff --git a/deep_causality/tests/types/context_types/adjustable/adjustable_time_tests.rs b/deep_causality/tests/types/context_types/adjustable/adjustable_time_tests.rs index def58e4..ea0ee79 100644 --- a/deep_causality/tests/types/context_types/adjustable/adjustable_time_tests.rs +++ b/deep_causality/tests/types/context_types/adjustable/adjustable_time_tests.rs @@ -11,7 +11,7 @@ fn test_update() { assert_eq!(*d.time_unit(), 12); assert_eq!(*d.time_scale(), TimeScale::Minute); - let array_grid = utils::get_array_grid(42); + let array_grid = utils::get_1d_array_grid(42); let res = d.update(&array_grid); assert!(res.is_ok()); @@ -24,7 +24,7 @@ fn test_update_err() { assert_eq!(*d.time_unit(), 42); assert_eq!(*d.time_scale(), TimeScale::Minute); - let array_grid = utils::get_array_grid(0); + let array_grid = utils::get_1d_array_grid(0); // Update fails with UpdateError let res = d.update(&array_grid); @@ -40,7 +40,7 @@ fn test_adjust() { assert_eq!(*d.time_unit(), 42); assert_eq!(*d.time_scale(), TimeScale::Minute); - let array_grid = utils::get_array_grid(22); + let array_grid = utils::get_1d_array_grid(22); let res = d.adjust(&array_grid); assert!(res.is_ok()); @@ -53,7 +53,7 @@ fn test_adjust_err() { assert_eq!(*d.time_unit(), 21); assert_eq!(*d.time_scale(), TimeScale::Minute); - let array_grid = utils::get_array_grid(-23); + let array_grid = utils::get_1d_array_grid(-23); // adjustment fails with AdjustmentError let res = d.adjust(&array_grid); @@ -63,6 +63,25 @@ fn test_adjust_err() { assert_eq!(*d.time_unit(), 21); } +#[test] +fn test_time_scale() { + let id = 1; + + let d = AdjustableTime::new(1, TimeScale::Minute, 21); + assert_eq!(*d.time_id(), id); + assert_eq!(*d.time_scale(), TimeScale::Minute); +} + +#[test] +fn test_time_unit() { + let id = 1; + + let d = AdjustableTime::new(1, TimeScale::Minute, 21); + assert_eq!(*d.time_id(), id); + assert_eq!(*d.time_scale(), TimeScale::Minute); + assert_eq!(*d.time_unit(), 21); +} + #[test] fn test_time_id() { let id = 1; @@ -81,12 +100,15 @@ fn test_id() { #[test] fn test_to_string() { - // let id = 1; - // let time_unit = 21; - // let time_scale = TimeScale::Minute; - // - // let mut d = AdjustableTime::new(0, TimeScale::Minute, 21, 21); - // let exp = format!("AdjustableTime: id: {}, time_scale: {:?}, time_unit: {}", id, time_scale, time_unit, &time_unit); - // let act = d.to_string(); - // assert_eq!(act, exp); + let id = 1; + let time_unit = 21; + let time_scale = TimeScale::Minute; + + let d = AdjustableTime::new(id, TimeScale::Minute, 21); + let exp = format!( + "AdjustableTime: id: {}, time_scale: {:?}, time_unit: {}", + id, time_scale, time_unit + ); + let act = d.to_string(); + assert_eq!(act, exp); } diff --git a/deep_causality/tests/types/context_types/adjustable/mod.rs b/deep_causality/tests/types/context_types/adjustable/mod.rs index fd3c176..a044b29 100644 --- a/deep_causality/tests/types/context_types/adjustable/mod.rs +++ b/deep_causality/tests/types/context_types/adjustable/mod.rs @@ -3,7 +3,11 @@ #[cfg(test)] mod adjustable_data_tests; #[cfg(test)] -mod adjustable_default; +mod adjustable_default_tests; +#[cfg(test)] +mod adjustable_space_tests; +#[cfg(test)] +mod adjustable_space_time_tests; #[cfg(test)] mod adjustable_time_tests; mod utils; diff --git a/deep_causality/tests/types/context_types/adjustable/utils.rs b/deep_causality/tests/types/context_types/adjustable/utils.rs index 03c679b..b4e0886 100644 --- a/deep_causality/tests/types/context_types/adjustable/utils.rs +++ b/deep_causality/tests/types/context_types/adjustable/utils.rs @@ -3,16 +3,17 @@ use dcl_data_structures::grid_type::{ArrayGrid, ArrayType}; use dcl_data_structures::prelude::PointIndex; -pub const HEIGHT: usize = 1; +pub const HEIGHT: usize = 5; // set all unused dimensions to 0 to save some memory. -pub const WIDTH: usize = 0; -pub const DEPTH: usize = 0; -pub const TIME: usize = 0; +pub const WIDTH: usize = 5; +pub const DEPTH: usize = 5; +pub const TIME: usize = 5; pub type AdjustmentData = ArrayGrid; -pub fn get_array_grid(val: i32) -> AdjustmentData { - let ag: ArrayGrid = ArrayGrid::new(ArrayType::Array1D); +pub fn get_1d_array_grid(val: i32) -> AdjustmentData { + let array_type = ArrayType::Array1D; + let ag: ArrayGrid = ArrayGrid::new(array_type); // Create a 1D PointIndex let p = PointIndex::new1d(0); @@ -22,3 +23,39 @@ pub fn get_array_grid(val: i32) -> AdjustmentData { ag } + +pub fn get_3d_array_grid(v1: i32, v2: i32, v3: i32) -> AdjustmentData { + let array_type = ArrayType::Array3D; + let ag: ArrayGrid = ArrayGrid::new(array_type); + + // Create a 3D PointIndex for each of the updated x,y,z coordinates + let p1 = PointIndex::new3d(0, 0, 0); + let p2 = PointIndex::new3d(0, 0, 1); + let p3 = PointIndex::new3d(0, 0, 2); + + // Store an i32 with th position of the point index + ag.set(p1, v1); + ag.set(p2, v2); + ag.set(p3, v3); + + ag +} + +pub fn get_4d_array_grid(v1: i32, v2: i32, v3: i32, t: i32) -> AdjustmentData { + let array_type = ArrayType::Array4D; + let ag: ArrayGrid = ArrayGrid::new(array_type); + + // Create a 4D PointIndex for each of the updated x,y,z coordinates plus time t + let p1 = PointIndex::new4d(0, 0, 0, 0); + let p2 = PointIndex::new4d(0, 0, 0, 1); + let p3 = PointIndex::new4d(0, 0, 0, 2); + let pt = PointIndex::new4d(0, 0, 0, 3); + + // Store an i32 with th position of the point index + ag.set(p1, v1); + ag.set(p2, v2); + ag.set(p3, v3); + ag.set(pt, t); + + ag +} diff --git a/deep_causality_macros/src/collections.rs b/deep_causality_macros/src/collections.rs index 03f09b5..8656503 100644 --- a/deep_causality_macros/src/collections.rs +++ b/deep_causality_macros/src/collections.rs @@ -13,9 +13,8 @@ pub fn expand_make_len() -> TokenStream { } pub fn expand_make_is_empty() -> TokenStream { - "fn is_empty(&self) -> bool - { - self.is_empty()\ + "fn is_empty(&self) -> bool{ + self.is_empty() }" .parse() .unwrap()