Skip to content

Commit

Permalink
nested functions | tests
Browse files Browse the repository at this point in the history
  • Loading branch information
a-givertzman committed Oct 20, 2023
1 parent 85dd835 commit cde5a97
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 81 deletions.
19 changes: 19 additions & 0 deletions src/core_/point/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,22 @@ impl<T: std::ops::BitOr<Output = T>> std::ops::BitOr for Point<T> {
}
}
}


impl<T: std::cmp::PartialOrd> std::cmp::PartialOrd for Point<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match self.name.partial_cmp(&other.name) {
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
match self.value.partial_cmp(&other.value) {
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
match self.status.partial_cmp(&other.status) {
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
self.timestamp.partial_cmp(&other.timestamp)
}
}
2 changes: 1 addition & 1 deletion src/core_/types/bool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Bool(pub bool);
impl std::ops::Add for Bool {
type Output = Bool;
Expand Down
6 changes: 4 additions & 2 deletions src/services/task/nested_function/fn_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ use super::fn_::{FnIn, FnOut, FnInOut};
pub struct FnInput {
pub id: String,
point: PointType,
initial: PointType,
}
///
///
impl FnInput {
pub fn new(id: &str, initial: PointType) -> Self {
Self {
id: id.into(),
point: initial
point: initial.clone(),
initial
}
}
}
Expand All @@ -42,7 +44,7 @@ impl FnOut for FnInput {
}
//
fn reset(&mut self) {
todo!()
self.point = self.initial.clone();
}
}
///
Expand Down
78 changes: 50 additions & 28 deletions src/services/task/nested_function/fn_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,74 @@
use log::trace;
use std::{cell::RefCell, rc::Rc, fmt::Debug};

use crate::core_::{point::{point_type::PointType, point::Point}, types::{type_of::DebugTypeOf, bool::Bool}};

use super::fn_::{FnInOut, FnIn, FnOut};


///
/// Returns true on input grater then setpoint
pub struct FnTripGe<TIn, TInput> where TInput: TraitFnOutput<TIn> + FnReset {
// input: Rc<RefCell<dyn FnOutput<TIn>>>,
input: Rc<RefCell<TInput>>,
setpoint: TIn,
trip: bool,
#[derive(Debug)]
pub struct FnTripGe {
id: String,
input: Rc<RefCell<Box<dyn FnInOut>>>,
setpoint: f64,
initial: bool,
}

impl<TIn, TInput: TraitFnOutput<TIn> + FnReset> FnTripGe<TIn, TInput> {
///
///
impl FnTripGe {
#[allow(dead_code)]
pub fn new(initial: bool, input: Rc<RefCell<TInput>>, setpoint: TIn) -> Self {
pub fn new(id: &str, initial: bool, input: Rc<RefCell<Box<dyn FnInOut>>>, setpoint: f64) -> Self {
Self {
id: id.into(),
input,
setpoint,
trip: initial,
initial: initial,
}
}
}


impl<T: PartialOrd + Debug, TInput: TraitFnOutput<T> + FnReset> TraitFnOutput<bool> for FnTripGe<T, TInput> {
///
fn out(&mut self) -> bool {
///
///
impl FnIn for FnTripGe {
fn add(&mut self, _: PointType) {
panic!("FnTimer.add | method is not used")
}
}
///
///
impl FnOut for FnTripGe {
//
//
fn out(&mut self) -> PointType {
// debug!("FnTrip.out | input: {:?}", self.input.print());
let value = self.input.borrow_mut().out();
let point = self.input.borrow_mut().out();
let value: bool = match &point {
PointType::Bool(point) => {
let value = if point.value.0 {1.0} else {0.0};
value > self.setpoint
},
PointType::Int(point) => {
point.value as f64 > self.setpoint
},
PointType::Float(point) => {
point.value > self.setpoint
},
_ => panic!("FnCount.out | {:?} type is not supported: {:?}", point.typeOf(), point),
};
trace!("FnTrip.out | input.out: {:?}", &value);
if self.trip {
if value < self.setpoint {
self.trip = false;
}
} else {
if value >= self.setpoint {
self.trip = true;
PointType::Bool(
Point::<Bool> {
name: String::from("FnTripGe"),
value: Bool(value),
status: point.status(),
timestamp: point.timestamp(),
}
}
self.trip
)
}
}

impl<T, TInput: TraitFnOutput<T> + FnReset> FnReset for FnTripGe<T, TInput> {
//
//
fn reset(&mut self) {
self.trip = self.initial;
self.input.borrow_mut().reset();
}
}
2 changes: 1 addition & 1 deletion src/services/task/nested_function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod fn_;
pub mod fn_sum;
pub mod fn_input;
pub mod fn_count;
// pub mod fn_trip;
pub mod fn_trip;
pub mod fn_timer;

pub mod fn_inputs;
Expand Down
16 changes: 6 additions & 10 deletions src/tests/unit/fn_count_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn test_single() {
DebugSession::init(LogLevel::Debug);
initOnce();
info!("test_single");
let mut input = initEach(PointType::Bool(Point::newBool("bool", false)));
let input = initEach(PointType::Bool(Point::newBool("bool", false)));
let mut fnCount = FnCount::new(
0,
input.clone(),
Expand Down Expand Up @@ -73,8 +73,7 @@ fn test_single() {
let state = fnCount.out();
// debug!("input: {:?}", &mut input);
debug!("value: {:?} | state: {:?}", value, state);
let targetState = PointType::Int(Point::newInt("tesr", targetState));
assert_eq!(state, targetState);
assert_eq!(state.asInt().value, targetState);
}
}
//
Expand All @@ -84,8 +83,7 @@ fn test_multiple() {
DebugSession::init(LogLevel::Debug);
initOnce();
info!("test_multiple");
// let (initial, switches) = initEach();
let mut input = initEach(PointType::Bool(Point::newBool("bool", false)));
let input = initEach(PointType::Bool(Point::newBool("bool", false)));
let mut fnCount = FnCount::new(
0,
input.clone(),
Expand Down Expand Up @@ -113,8 +111,7 @@ fn test_multiple() {
let state = fnCount.out();
// debug!("input: {:?}", &mut input);
debug!("value: {:?} | state: {:?}", value, state);
let targetState = PointType::Int(Point::newInt("tesr", targetState));
assert_eq!(state, targetState);
assert_eq!(state.asInt().value, targetState);
}
}

Expand All @@ -123,7 +120,7 @@ fn test_multiple_reset() {
DebugSession::init(LogLevel::Debug);
initOnce();
info!("test_multiple_reset");
let mut input = initEach(PointType::Bool(Point::newBool("bool", false)));
let input = initEach(PointType::Bool(Point::newBool("bool", false)));
let mut fnCount = FnCount::new(
0,
input.clone(),
Expand Down Expand Up @@ -154,7 +151,6 @@ fn test_multiple_reset() {
let state = fnCount.out();
// debug!("input: {:?}", &mut input);
debug!("value: {:?} | state: {:?}", value, state);
let targetState = PointType::Int(Point::newInt("tesr", targetState));
assert_eq!(state, targetState);
assert_eq!(state.asInt().value, targetState);
}
}
22 changes: 9 additions & 13 deletions src/tests/unit/fn_input_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{sync::Once, rc::Rc, cell::RefCell};
use crate::{
core_::{debug::debug_session::{DebugSession, LogLevel},
point::{point_type::PointType, point::Point}},
services::task::nested_function::{fn_::{FnIn, FnOut, FnInOut}, fn_input::FnInput},
services::task::nested_function::{fn_::FnInOut, fn_input::FnInput},
};

// Note this useful idiom: importing names from outer (for mod tests) scope.
Expand Down Expand Up @@ -44,7 +44,7 @@ fn test_int() {
DebugSession::init(LogLevel::Debug);
initOnce();
info!("test_int");
let mut input = initEach(PointType::Int(Point::newInt("int", 0)));
let input = initEach(PointType::Int(Point::newInt("int", 0)));
let testData = vec![
0,
1,
Expand All @@ -68,8 +68,7 @@ fn test_int() {
let state = input.borrow_mut().out();
// debug!("input: {:?}", &mut input);
debug!("value: {:?} | state: {:?}", value, state);
let targetState = PointType::Int(Point::newInt("tesr", value));
assert_eq!(state, targetState);
assert_eq!(state.asInt().value, value);
}
}

Expand All @@ -78,7 +77,7 @@ fn test_bool() {
DebugSession::init(LogLevel::Debug);
initOnce();
info!("test_bool");
let mut input = initEach(PointType::Bool(Point::newBool("bool", false)));
let input = initEach(PointType::Bool(Point::newBool("bool", false)));
let testData = vec![
false,
false,
Expand All @@ -104,8 +103,7 @@ fn test_bool() {
let state = input.borrow_mut().out();
// debug!("input: {:?}", &mut input);
debug!("value: {:?} | state: {:?}", value, state);
let targetState = PointType::Bool(Point::newBool("tesr", value));
assert_eq!(state, targetState);
assert_eq!(state.asBool().value.0, value);
}
}

Expand All @@ -114,7 +112,7 @@ fn test_float() {
DebugSession::init(LogLevel::Debug);
initOnce();
info!("test_float");
let mut input = initEach(PointType::Float(Point::newFloat("float", 0.0)));
let input = initEach(PointType::Float(Point::newFloat("float", 0.0)));
let testData = vec![
0.0,
1.0,
Expand All @@ -140,8 +138,7 @@ fn test_float() {
let state = input.borrow_mut().out();
// debug!("input: {:?}", &mut input);
debug!("value: {:?} | state: {:?}", value, state);
let targetState = PointType::Float(Point::newFloat("tesr", value));
assert_eq!(state, targetState);
assert_eq!(state.asFloat().value, value);
}
}

Expand All @@ -151,7 +148,7 @@ fn test_string() {
DebugSession::init(LogLevel::Debug);
initOnce();
info!("test_string");
let mut input = initEach(PointType::String(Point::newString("string", "0")));
let input = initEach(PointType::String(Point::newString("string", "0")));
let testData = vec![
"0",
"1",
Expand All @@ -175,7 +172,6 @@ fn test_string() {
let state = input.borrow_mut().out();
// debug!("input: {:?}", &mut input);
debug!("value: {:?} | state: {:?}", value, state);
let targetState = PointType::String(Point::newString("tesr", value));
assert_eq!(state, targetState);
assert_eq!(state.asString().value, value);
}
}
Loading

0 comments on commit cde5a97

Please sign in to comment.