From 84c19a9a1ecc94d7854cad0983ff808bd5bfb624 Mon Sep 17 00:00:00 2001 From: wandalen Date: Wed, 27 Mar 2024 15:38:02 +0200 Subject: [PATCH] experimenting --- .../former_tests/container_former_common.rs | 290 +++++++++++++++++ .../former_tests/container_former_hashset.rs | 63 +++- .../inc/former_tests/container_former_vec.rs | 307 +----------------- module/core/former/tests/inc/mod.rs | 2 + 4 files changed, 364 insertions(+), 298 deletions(-) create mode 100644 module/core/former/tests/inc/former_tests/container_former_common.rs diff --git a/module/core/former/tests/inc/former_tests/container_former_common.rs b/module/core/former/tests/inc/former_tests/container_former_common.rs new file mode 100644 index 0000000000..a251cef936 --- /dev/null +++ b/module/core/former/tests/inc/former_tests/container_former_common.rs @@ -0,0 +1,290 @@ +// #![ allow( dead_code ) ] + +use super::*; +#[ allow( unused_imports ) ] +use collection_tools::Vec; + +// + +#[ test ] +fn definitions() +{ + + pub fn f1< Definition >( _x : Definition ) + where + Definition : former::FormerDefinitionTypes, + { + } + + pub fn f2< Definition >( _x : Definition ) + where + Definition : former::FormerDefinition, + { + } + + pub fn f3< Definition, End >( _x : End ) + where + Definition : former::FormerDefinitionTypes, + End : former::FormingEnd< Definition >, + { + } + + // f1( former::VectorDefinition::< String, () >::default() ); + f2( former::VectorDefinition::< String, (), Vec< String >, the_module::NoEnd >::default() ); + f3::< former::VectorDefinition< String, (), Vec< String >, the_module::NoEnd >, the_module::ReturnStorage >( the_module::ReturnStorage ); + f3::< < former::VectorDefinition< String, (), Vec< String >, the_module::NoEnd > as the_module::FormerDefinition >::Types, the_module::ReturnStorage >( the_module::ReturnStorage ); + + // assert_eq!( 0, 1 ); + + let vec : Vec< String > = vec![ "a".into(), "b".into(), "c".into() ]; + +} + +// + +#[ test ] +fn begin_and_custom_end() +{ + + // xxx : make example with that + + // basic case + + fn return_13( _storage : Vec< String >, _context : Option< () > ) -> f32 + { + 13.1 + } + let got = the_module::VectorSubformer::begin( None, None, return_13 ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13.1; + a_id!( got, exp ); + + let got = the_module::VectorSubformer::new( return_13 ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13.1; + a_id!( got, exp ); + + // with a context + + fn context_plus_13( _storage : Vec< String >, context : Option< f32 > ) -> f32 + { + if let Some( context ) = context + { + 13.1 + context + } + else + { + 13.1 + } + } + let got = the_module::VectorSubformer::begin( None, Some( 10.0 ), context_plus_13 ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 23.1; + a_id!( got, exp ); + + // + +} + +// + +#[ test ] +fn custom_definition() +{ + + // xxx : make example of that + + struct Return13; + impl former::FormerDefinitionTypes for Return13 + { + type Storage = Vec< String >; + type Formed = i32; + type Context = (); + } + + impl former::FormerDefinition for Return13 + { + type Types = Return13; + type End = Return13; + } + + // - + + impl the_module::FormingEnd< Return13 > + for Return13 + { + fn call + ( + &self, + _storage : < Return13 as the_module::FormerDefinitionTypes >::Storage, + _context : Option< < Return13 as the_module::FormerDefinitionTypes >::Context > + ) -> < Return13 as the_module::FormerDefinitionTypes >::Formed + { + 13 + } + } + + // + + let got = the_module::ContainerSubformer::< String, Return13 >::begin( None, None, Return13 ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13; + a_id!( got, exp ); + + let got = the_module::ContainerSubformer::< String, Return13 >::new( Return13 ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13; + a_id!( got, exp ); + + // + +} + +// + +#[ test ] +fn custom_definition_parametrized() +{ + + // xxx : make example of that + + struct Return13< E >( ::core::marker::PhantomData< E > ); + + impl< E > Return13< E > + { + pub fn new() -> Self + { + Self ( ::core::marker::PhantomData ) + } + } + + impl< E > former::FormerDefinitionTypes for Return13< E > + { + type Storage = Vec< E >; + type Formed = i32; + type Context = (); + } + + impl< E > former::FormerDefinition for Return13< E > + { + type Types = Return13< E >; + type End = Return13< E >; + } + + // - + + impl< E > the_module::FormingEnd< Return13< E > > + for Return13< E > + { + fn call + ( + &self, + _storage : < Return13< E > as the_module::FormerDefinitionTypes >::Storage, + _context : Option< < Return13< E > as the_module::FormerDefinitionTypes >::Context > + ) -> < Return13< E > as the_module::FormerDefinitionTypes >::Formed + { + 13 + } + } + + // + + let got = the_module::ContainerSubformer::< String, Return13< String > >::begin( None, None, Return13::new() ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13; + a_id!( got, exp ); + + let got = the_module::ContainerSubformer::< String, Return13< String > >::new( Return13::new() ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13; + a_id!( got, exp ); + + // + + type MyContainer< E > = the_module::ContainerSubformer::< E, Return13< E > >; + + let got = MyContainer::< String >::begin( None, None, Return13::new() ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13; + a_id!( got, exp ); + + let got = MyContainer::< String >::new( Return13::new() ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13; + a_id!( got, exp ); + + // + +} + +// + +#[ test ] +fn custom_definition_custom_end() +{ + + struct Return13; + impl former::FormerDefinitionTypes for Return13 + { + type Storage = Vec< String >; + type Formed = i32; + type Context = (); + } + impl former::FormerDefinition for Return13 + { + type Types = Return13; + type End = former::FormingEndWrapper< < Self as former::FormerDefinition >::Types >; + } + + // + + fn return_13( _storage : Vec< String >, _context : Option< () > ) -> i32 + { + 13 + } + + let end_wrapper : the_module::FormingEndWrapper< Return13 > = the_module::FormingEndWrapper::new( return_13 ); + let got = the_module::ContainerSubformer::< String, Return13 >::new( end_wrapper ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13; + a_id!( got, exp ); + + let got = the_module::ContainerSubformer::< String, Return13 >::new( return_13.into() ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13; + a_id!( got, exp ); + + let got = the_module::ContainerSubformer::< String, Return13 >::new_with( return_13 ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = 13; + a_id!( got, exp ); + + // + +} + +// diff --git a/module/core/former/tests/inc/former_tests/container_former_hashset.rs b/module/core/former/tests/inc/former_tests/container_former_hashset.rs index eb80142068..a84a716f45 100644 --- a/module/core/former/tests/inc/former_tests/container_former_hashset.rs +++ b/module/core/former/tests/inc/former_tests/container_former_hashset.rs @@ -8,9 +8,41 @@ use collection_tools::HashSet; // qqq : zzz : remove #[ cfg( not( feature = "use_alloc" ) ) ] #[ cfg( not( feature = "use_alloc" ) ) ] #[ test ] -fn push() +fn add() { + // expliccit with ContainerSubformer + + let got : HashSet< String > = the_module + ::ContainerSubformer + ::< String, former::HashSetDefinition< String, (), HashSet< String >, the_module::ReturnStorage > > + ::new( former::ReturnStorage ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = hset! + [ + "a".to_string(), + "b".to_string(), + ]; + a_id!( got, exp ); + + // expliccit with HashSetSubformer + + let got : HashSet< String > = the_module::HashSetSubformer::< String, (), HashSet< String >, the_module::ReturnStorage > + ::new( former::ReturnStorage ) + .add( "a" ) + .add( "b" ) + .form(); + let exp = hset! + [ + "a".to_string(), + "b".to_string(), + ]; + a_id!( got, exp ); + + // compact with HashSetSubformer + let got : HashSet< String > = the_module::HashSetSubformer::new( former::ReturnStorage ) .add( "a" ) .add( "b" ) @@ -22,6 +54,35 @@ fn push() ]; a_id!( got, exp ); + // with begin + + let got : HashSet< String > = the_module::HashSetSubformer + ::begin( Some( hset![ "a".to_string() ] ), Some( () ), former::ReturnStorage ) + .add( "b" ) + .form(); + let exp = hset! + [ + "a".to_string(), + "b".to_string(), + ]; + a_id!( got, exp ); + + // with help of ext + + use the_module::HashSetExt; + let got : HashSet< String > = HashSet::former() + .add( "a" ) + .add( "b" ) + .form(); + let exp = hset! + [ + "a".to_string(), + "b".to_string(), + ]; + a_id!( got, exp ); + + // + } // qqq : zzz : remove #[ cfg( not( feature = "use_alloc" ) ) ] diff --git a/module/core/former/tests/inc/former_tests/container_former_vec.rs b/module/core/former/tests/inc/former_tests/container_former_vec.rs index 7bb3ad4644..8d4aea60fe 100644 --- a/module/core/former/tests/inc/former_tests/container_former_vec.rs +++ b/module/core/former/tests/inc/former_tests/container_former_vec.rs @@ -6,47 +6,11 @@ use collection_tools::Vec; // -#[ test ] -fn definitions() -{ - - pub fn f1< Definition >( _x : Definition ) - where - Definition : former::FormerDefinitionTypes, - { - } - - pub fn f2< Definition >( _x : Definition ) - where - Definition : former::FormerDefinition, - { - } - - pub fn f3< Definition, End >( _x : End ) - where - Definition : former::FormerDefinitionTypes, - End : former::FormingEnd< Definition >, - { - } - - // f1( former::VectorDefinition::< String, () >::default() ); - f2( former::VectorDefinition::< String, (), Vec< String >, the_module::NoEnd >::default() ); - f3::< former::VectorDefinition< String, (), Vec< String >, the_module::NoEnd >, the_module::ReturnStorage >( the_module::ReturnStorage ); - f3::< < former::VectorDefinition< String, (), Vec< String >, the_module::NoEnd > as the_module::FormerDefinition >::Types, the_module::ReturnStorage >( the_module::ReturnStorage ); - - // assert_eq!( 0, 1 ); - - let vec : Vec< String > = vec![ "a".into(), "b".into(), "c".into() ]; - -} - -// - #[ test ] fn add() { - // + // expliccit with ContainerSubformer let got : Vec< String > = the_module ::ContainerSubformer @@ -62,13 +26,10 @@ fn add() ]; a_id!( got, exp ); - // + // expliccit with VectorSubformer - let got : Vec< String > = the_module::ContainerSubformer:: - < - String, - former::VectorDefinition< String, (), Vec< String >, the_module::ReturnStorage >, - >::new( former::ReturnStorage ) + let got : Vec< String > = the_module::VectorSubformer::< String, (), Vec< String >, the_module::ReturnStorage > + ::new( former::ReturnStorage ) .add( "a" ) .add( "b" ) .form(); @@ -79,9 +40,9 @@ fn add() ]; a_id!( got, exp ); - // + // compact with VectorSubformer - let got : Vec< String > = the_module::VectorSubformer::< String, (), Vec< String >, the_module::ReturnStorage >::new( former::ReturnStorage ) + let got : Vec< String > = the_module::VectorSubformer::new( former::ReturnStorage ) .add( "a" ) .add( "b" ) .form(); @@ -92,10 +53,10 @@ fn add() ]; a_id!( got, exp ); - // + // with begin - let got : Vec< String > = the_module::VectorSubformer::new( former::ReturnStorage ) - .add( "a" ) + let got : Vec< String > = the_module::VectorSubformer + ::begin( Some( vec![ "a".to_string() ] ), Some( () ), former::ReturnStorage ) .add( "b" ) .form(); let exp = vec! @@ -105,7 +66,7 @@ fn add() ]; a_id!( got, exp ); - // + // with help of ext use the_module::VecExt; let got : Vec< String > = Vec::former() @@ -143,251 +104,3 @@ fn replace() } // - -#[ test ] -fn begin_and_custom_end() -{ - - // xxx : make example with that - - // basic case - - fn return_13( _storage : Vec< String >, _context : Option< () > ) -> f32 - { - 13.1 - } - let got = the_module::VectorSubformer::begin( None, None, return_13 ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13.1; - a_id!( got, exp ); - - let got = the_module::VectorSubformer::new( return_13 ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13.1; - a_id!( got, exp ); - - // with a context - - fn context_plus_13( _storage : Vec< String >, context : Option< f32 > ) -> f32 - { - if let Some( context ) = context - { - 13.1 + context - } - else - { - 13.1 - } - } - let got = the_module::VectorSubformer::begin( None, Some( 10.0 ), context_plus_13 ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 23.1; - a_id!( got, exp ); - - // - -} - -// - -#[ test ] -fn custom_definition() -{ - - // xxx : make example of that - - struct Return13; - impl former::FormerDefinitionTypes for Return13 - { - type Storage = Vec< String >; - type Formed = i32; - type Context = (); - } - - impl former::FormerDefinition for Return13 - { - type Types = Return13; - type End = Return13; - } - - // - - - impl the_module::FormingEnd< Return13 > - for Return13 - { - fn call - ( - &self, - _storage : < Return13 as the_module::FormerDefinitionTypes >::Storage, - _context : Option< < Return13 as the_module::FormerDefinitionTypes >::Context > - ) -> < Return13 as the_module::FormerDefinitionTypes >::Formed - { - 13 - } - } - - // - - let got = the_module::ContainerSubformer::< String, Return13 >::begin( None, None, Return13 ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13; - a_id!( got, exp ); - - let got = the_module::ContainerSubformer::< String, Return13 >::new( Return13 ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13; - a_id!( got, exp ); - - // - -} - -// - -#[ test ] -fn custom_definition_parametrized() -{ - - // xxx : make example of that - - // xxx2 : continue - struct Return13< E >( ::core::marker::PhantomData< E > ); - - impl< E > Return13< E > - { - pub fn new() -> Self - { - Self ( ::core::marker::PhantomData ) - } - } - - impl< E > former::FormerDefinitionTypes for Return13< E > - { - type Storage = Vec< E >; - type Formed = i32; - type Context = (); - } - - impl< E > former::FormerDefinition for Return13< E > - { - type Types = Return13< E >; - type End = Return13< E >; - } - - // - - - impl< E > the_module::FormingEnd< Return13< E > > - for Return13< E > - { - fn call - ( - &self, - _storage : < Return13< E > as the_module::FormerDefinitionTypes >::Storage, - _context : Option< < Return13< E > as the_module::FormerDefinitionTypes >::Context > - ) -> < Return13< E > as the_module::FormerDefinitionTypes >::Formed - { - 13 - } - } - - // - - let got = the_module::ContainerSubformer::< String, Return13< String > >::begin( None, None, Return13::new() ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13; - a_id!( got, exp ); - - let got = the_module::ContainerSubformer::< String, Return13< String > >::new( Return13::new() ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13; - a_id!( got, exp ); - - // - - type MyContainer< E > = the_module::ContainerSubformer::< E, Return13< E > >; - - let got = MyContainer::< String >::begin( None, None, Return13::new() ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13; - a_id!( got, exp ); - - let got = MyContainer::< String >::new( Return13::new() ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13; - a_id!( got, exp ); - - // - -} - -// - -#[ test ] -fn custom_definition_custom_end() -{ - - struct Return13; - impl former::FormerDefinitionTypes for Return13 - { - type Storage = Vec< String >; - type Formed = i32; - type Context = (); - } - impl former::FormerDefinition for Return13 - { - type Types = Return13; - type End = former::FormingEndWrapper< < Self as former::FormerDefinition >::Types >; - } - - // - - fn return_13( _storage : Vec< String >, _context : Option< () > ) -> i32 - { - 13 - } - - let end_wrapper : the_module::FormingEndWrapper< Return13 > = the_module::FormingEndWrapper::new( return_13 ); - let got = the_module::ContainerSubformer::< String, Return13 >::new( end_wrapper ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13; - a_id!( got, exp ); - - let got = the_module::ContainerSubformer::< String, Return13 >::new( return_13.into() ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13; - a_id!( got, exp ); - - let got = the_module::ContainerSubformer::< String, Return13 >::new_with( return_13 ) - .add( "a" ) - .add( "b" ) - .form(); - let exp = 13; - a_id!( got, exp ); - - // - -} - -// diff --git a/module/core/former/tests/inc/mod.rs b/module/core/former/tests/inc/mod.rs index ab7c1c47c9..e8b2ed3c47 100644 --- a/module/core/former/tests/inc/mod.rs +++ b/module/core/former/tests/inc/mod.rs @@ -7,6 +7,8 @@ mod former_tests #[ allow( unused_imports ) ] use super::*; + #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] + mod container_former_common; #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] mod container_former_vec; #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ]