1- using CareBoo . Burst . Delegates ;
1+ using CareBoo . Burst . Delegates ;
22using NUnit . Framework ;
33using Unity . Burst ;
44using Unity . Collections ;
88internal class BurstDelegateTests
99{
1010 [ BurstCompile ]
11- public struct CallFuncJob < TImpl > : IJob
12- where TImpl : struct , IFunc < int , int >
11+ public struct CallFuncJob < TFunc > : IJob
12+ where TFunc : struct , IFunc < int , int >
1313 {
14- public ValueFunc < int , int > . Impl < TImpl > Func ;
14+ public ValueFunc < int , int > . Impl < TFunc > Func ;
1515 public NativeArray < int > In ;
1616 public NativeArray < int > Out ;
1717
@@ -21,29 +21,39 @@ public void Execute()
2121 }
2222 }
2323
24+ public static CallFuncJob < TFunc > NewCallFuncJob < TFunc > (
25+ ValueFunc < int , int > . Impl < TFunc > func ,
26+ NativeArray < int > input ,
27+ NativeArray < int > output
28+ ) where TFunc : struct , IFunc < int , int >
29+ {
30+ return new CallFuncJob < TFunc > { Func = func , In = input , Out = output } ;
31+ }
32+
2433 [ BurstCompile ]
25- public static int Add4 ( int val ) => val + 4 ;
34+ public static int Add ( int x , int y ) => x + y ;
35+
36+ public static readonly ValueFunc < int , int , int > . Impl < BurstFunc < int , int , int > > AddFunc = ValueFunc < int , int , int > . Compile ( Add ) ;
2637
27- public static readonly BurstFunc < int , int > Add4Func = BurstFunc < int , int > . Compile ( Add4 ) ;
2838
2939 [ Test ]
3040 public void TestCompiledPointerCanInvokeInsideOfBurstedCode ( )
3141 {
3242 using var input = new NativeArray < int > ( new int [ 1 ] , Allocator . Persistent ) ;
3343 using var output = new NativeArray < int > ( new int [ 1 ] , Allocator . Persistent ) ;
34- var expected = Add4 ( input [ 0 ] ) ;
35- new CallFuncJob < BurstFunc < int , int > > { Func = ValueFunc < int , int > . New ( Add4Func ) , In = input , Out = output } . Run ( ) ;
44+ var expected = Add ( 4 , input [ 0 ] ) ;
45+ NewCallFuncJob ( AddFunc . Apply ( 4 ) , input , output ) . Run ( ) ;
3646 var actual = output [ 0 ] ;
3747 Assert . AreEqual ( expected , actual ) ;
3848 }
3949
4050 [ Test ]
41- public void TestCanInvokeOutsideOfBurstedCode ( )
51+ public void TestCompiledPointerCanInvokeOutsideOfBurstedCode ( )
4252 {
4353 using var input = new NativeArray < int > ( new int [ 1 ] , Allocator . Persistent ) ;
4454 using var output = new NativeArray < int > ( new int [ 1 ] , Allocator . Persistent ) ;
45- var expected = Add4 ( input [ 0 ] ) ;
46- var actual = Add4Func . Invoke ( input [ 0 ] ) ;
55+ var expected = Add ( 4 , input [ 0 ] ) ;
56+ var actual = AddFunc . Apply ( 4 ) . Invoke ( input [ 0 ] ) ;
4757 Assert . AreEqual ( expected , actual ) ;
4858 }
4959}
0 commit comments