Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more tests for adjustable types. #102

Merged
merged 10 commits into from
Sep 13, 2023
2 changes: 2 additions & 0 deletions deep_causality/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +13,8 @@ use super::*;

impl<T> Adjustable<T> for AdjustableSpace<T>
where
T: Default
T: Debug
+ Default
+ Add<T, Output = T>
+ Sub<T, Output = T>
+ Mul<T, Output = T>
Expand Down Expand Up @@ -57,40 +59,46 @@ where

fn adjust<const W: usize, const H: usize, const D: usize, const C: usize>(
&mut self,
_array_grid: &ArrayGrid<T, W, H, D, C>,
array_grid: &ArrayGrid<T, W, H, D, C>,
) -> 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(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct AdjustableSpace<T>
where
T: Default + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Copy,
{
#[getter(name = space_id)] // Rename ID getter to prevent conflict impl with identifiable
id: u64,
x: T,
y: T,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -20,15 +21,101 @@ where
{
fn update<const W: usize, const H: usize, const D: usize, const C: usize>(
&mut self,
_array_grid: &ArrayGrid<T, W, H, D, C>,
array_grid: &ArrayGrid<T, W, H, D, C>,
) -> 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<const W: usize, const H: usize, const D: usize, const C: usize>(
&mut self,
_array_grid: &ArrayGrid<T, W, H, D, C>,
array_grid: &ArrayGrid<T, W, H, D, C>,
) -> 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(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ where
T: Copy + Display + Default + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T>,
{
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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<T> Adjustable<T> for Data<T> 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);
Expand All @@ -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);
Expand Down
Loading
Loading