@@ -388,6 +388,55 @@ func TestSaturatingIncrementIntoClampsOnOverflow(t *testing.T) {
388388 }
389389}
390390
391+ func TestSaturatingDecrement (t * testing.T ) {
392+ // normal decrement
393+ gas := ComputationGas (10 )
394+ newGas := gas .SaturatingDecrement (ResourceKindComputation , 5 )
395+ if got , want := newGas .Get (ResourceKindComputation ), uint64 (5 ); got != want {
396+ t .Errorf ("unexpected computation gas: got %v, want %v" , got , want )
397+ }
398+ if got , want := newGas .SingleGas (), uint64 (5 ); got != want {
399+ t .Errorf ("unexpected single gas: got %v, want %v" , got , want )
400+ }
401+
402+ // saturating decrement on kind
403+ gas = MultiGasFromPairs (
404+ Pair {ResourceKindComputation , 10 },
405+ Pair {ResourceKindStorageAccess , 10 },
406+ )
407+
408+ newGas = gas .SaturatingDecrement (ResourceKindComputation , 20 )
409+ if got , want := newGas .Get (ResourceKindComputation ), uint64 (0 ); got != want {
410+ t .Errorf ("unexpected comp gas: got %v, want %v" , got , want )
411+ }
412+ if got , want := newGas .Get (ResourceKindStorageAccess ), uint64 (10 ); got != want {
413+ t .Errorf ("unexpected storage access gas: got %v, want %v" , got , want )
414+ }
415+ if got , want := newGas .SingleGas (), uint64 (10 ); got != want {
416+ t .Errorf ("unexpected total (should drop by 10 only): got %v, want %v" , got , want )
417+ }
418+
419+ if got , want := newGas .SingleGas (),
420+ newGas .Get (ResourceKindComputation )+ newGas .Get (ResourceKindStorageAccess ); got != want {
421+ t .Errorf ("total/sum mismatch: total=%v sum=%v" , got , want )
422+ }
423+
424+ // total-only decrement case
425+ gas = MultiGasFromPairs (
426+ Pair {ResourceKindComputation , math .MaxUint64 - 1 },
427+ Pair {ResourceKindHistoryGrowth , 1 },
428+ )
429+
430+ newGas = gas .SaturatingDecrement (ResourceKindHistoryGrowth , 1 )
431+ if got , want := newGas .Get (ResourceKindHistoryGrowth ), uint64 (0 ); got != want {
432+ t .Errorf ("unexpected history growth gas: got %v, want %v" , got , want )
433+ }
434+
435+ if got , want := newGas .SingleGas (), uint64 (math .MaxUint64 - 1 ); got != want {
436+ t .Errorf ("unexpected total gas: got %v, want %v" , got , want )
437+ }
438+ }
439+
391440func TestMultiGasSingleGasTracking (t * testing.T ) {
392441 g := ZeroGas ()
393442 if got := g .SingleGas (); got != 0 {
0 commit comments