@@ -404,3 +404,54 @@ func TestTracker_ConcurrentCloneRace(t *testing.T) {
404404 }
405405 wg .Wait ()
406406}
407+
408+ type zeroAllocEstimator struct {
409+ cost uint64
410+ }
411+
412+ func (e * zeroAllocEstimator ) CallCost (function , overloadID string , args []ref.Val , result ref.Val ) * uint64 {
413+ return & e .cost
414+ }
415+
416+ func TestTracker_EvalUnaryBinaryAllocs (t * testing.T ) {
417+ estimator := & zeroAllocEstimator {cost : 3 }
418+ tracker , err := cost .NewTracker (estimator )
419+ if err != nil {
420+ t .Fatalf ("NewTracker() failed: %v" , err )
421+ }
422+ unmodeledTracker , err := cost .NewTracker (nil )
423+ if err != nil {
424+ t .Fatalf ("NewTracker(nil) failed: %v" , err )
425+ }
426+
427+ unaryCall := & testCall {function : "!" , overloadID : overloads .LogicalNot }
428+ binaryCall := & testCall {function : "_+_" , overloadID : overloads .AddInt64 }
429+ arg1 := types .Int (1 )
430+ arg2 := types .Int (2 )
431+ res := types .Int (3 )
432+
433+ if allocs := testing .AllocsPerRun (100 , func () {
434+ unmodeledTracker .EvalUnary (nil , 1 , unaryCall , types .True , types .False )
435+ }); allocs != 0 {
436+ t .Errorf ("EvalUnary (unmodeled) allocated %v times, want 0" , allocs )
437+ }
438+
439+ if allocs := testing .AllocsPerRun (100 , func () {
440+ unmodeledTracker .EvalBinary (nil , 2 , binaryCall , arg1 , arg2 , res )
441+ }); allocs != 0 {
442+ t .Errorf ("EvalBinary (unmodeled) allocated %v times, want 0" , allocs )
443+ }
444+
445+ if allocs := testing .AllocsPerRun (100 , func () {
446+ tracker .EvalUnary (nil , 1 , unaryCall , types .True , types .False )
447+ }); allocs != 0 {
448+ t .Errorf ("EvalUnary (with estimator) allocated %v times, want 0" , allocs )
449+ }
450+
451+ if allocs := testing .AllocsPerRun (100 , func () {
452+ tracker .EvalBinary (nil , 2 , binaryCall , arg1 , arg2 , res )
453+ }); allocs != 0 {
454+ t .Errorf ("EvalBinary (with estimator) allocated %v times, want 0" , allocs )
455+ }
456+ }
457+
0 commit comments