From 8a0eb0e2668d4d14310224b20ee142c7541f486a Mon Sep 17 00:00:00 2001 From: Dennis Date: Sat, 13 Aug 2022 18:21:46 +0200 Subject: [PATCH] Add tests for op nodes --- node-graph/gcore/src/ops.rs | 59 ++++++++++++++++++++++++++++++----- node-graph/gcore/src/value.rs | 8 +++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/node-graph/gcore/src/ops.rs b/node-graph/gcore/src/ops.rs index 6036682f..672048df 100644 --- a/node-graph/gcore/src/ops.rs +++ b/node-graph/gcore/src/ops.rs @@ -26,6 +26,13 @@ impl<'n, T: 'n, U> Node<'n, (T, U)> for FstNode { a } } +impl<'n, T: 'n, U> Node<'n, &'n (T, U)> for FstNode { + type Output = &'n T; + fn eval(&'n self, input: &'n (T, U)) -> Self::Output { + let (a, _) = input; + a + } +} /// Destructures a Tuple of two values and returns the first one pub struct SndNode; @@ -37,6 +44,14 @@ impl<'n, T, U: 'n> Node<'n, (T, U)> for SndNode { } } +impl<'n, T, U: 'n> Node<'n, &'n (T, U)> for SndNode { + type Output = &'n U; + fn eval(&'n self, input: &'n (T, U)) -> Self::Output { + let (_, b) = input; + b + } +} + /// Return a tuple with two instances of the input argument pub struct DupNode; impl<'n, T: Clone> Node<'n, T> for DupNode { @@ -61,10 +76,42 @@ mod test { use crate::{generic::*, structural::*, value::*}; #[test] - pub fn foo() { - let value = ComposeNode::new(ValueNode(4u32), IdNode); - let value2 = ValueNode(5u32); + pub fn dup_node() { + let value = ValueNode(4u32); let dup = DupNode.after(value); + assert_eq!(dup.eval(()), (&4, &4)); + } + #[test] + pub fn id_node() { + let value = IdNode.after(ValueNode(4u32)); + assert_eq!(value.eval(()), &4); + } + #[test] + pub fn clone_node() { + let cloned = CloneNode.after(ValueNode(4u32)); + assert_eq!(cloned.eval(()), 4); + } + #[test] + pub fn fst_node() { + let fst = FstNode.after(ValueNode((4u32, "a")).clone()); + assert_eq!(fst.eval(()), 4); + } + #[test] + pub fn snd_node() { + let fst = SndNode.after(ValueNode((4u32, "a")).clone()); + assert_eq!(fst.eval(()), "a"); + } + #[test] + pub fn add_node() { + let a = ValueNode(42u32); + let b = ValueNode(6u32); + let cons_a = ConsNode(a); + let sum = AddNode.after(cons_a).after(b); + + assert_eq!(sum.eval(()), 48); + } + #[test] + pub fn foo() { fn int(_: (), state: &u32) -> &u32 { state } @@ -74,10 +121,6 @@ mod test { let fnn = FnNode::new(&swap); let fns = FnNodeWithState::new(int, 42u32); assert_eq!(fnn.eval((1u32, 2u32)), (2, 1)); - let _ = fns.eval(()); - let snd = SndNode.after(dup); - assert_eq!(snd.eval(()), &4u32); - let sum = AddNode.after(ConsNode(snd)).eval(value2.eval(())); - assert_eq!(sum, 9); + assert_eq!(fns.eval(()), &42); } } diff --git a/node-graph/gcore/src/value.rs b/node-graph/gcore/src/value.rs index f0a60939..d2855e0b 100644 --- a/node-graph/gcore/src/value.rs +++ b/node-graph/gcore/src/value.rs @@ -2,6 +2,8 @@ use core::marker::PhantomData; use core::mem::MaybeUninit; use core::sync::atomic::AtomicBool; +use crate::ops::CloneNode; +use crate::structural::ComposeNode; use crate::Node; pub struct IntNode; @@ -27,6 +29,12 @@ impl ValueNode { } } +impl<'n, T: Clone + 'n> ValueNode { + pub const fn clone(self) -> ComposeNode<'n, (), ValueNode, CloneNode> { + ComposeNode::new(self, CloneNode) + } +} + impl From for ValueNode { fn from(value: T) -> Self { ValueNode::new(value)