@@ -5,10 +5,14 @@ import (
55 "math/big"
66 "testing"
77
8+ "github.com/multiversx/mx-chain-core-go/core"
9+ "github.com/multiversx/mx-chain-scenario-go/worldmock"
810 vmMath "github.com/multiversx/mx-chain-vm-go/math"
911 contextmock "github.com/multiversx/mx-chain-vm-go/mock/context"
1012 test "github.com/multiversx/mx-chain-vm-go/testcommon"
1113 "github.com/multiversx/mx-chain-vm-go/vmhost"
14+ "github.com/multiversx/mx-chain-vm-go/vmhost/vmhooks"
15+ "github.com/stretchr/testify/assert"
1216
1317 twoscomplement "github.com/multiversx/mx-components-big-int/twos-complement"
1418)
@@ -328,3 +332,123 @@ func TestManBuffers_StorageLoad(t *testing.T) {
328332 Storage (storage ... )
329333 })
330334}
335+
336+ func TestManBuffers_Ints_WithNoBarnardOpcodesFlag (t * testing.T ) {
337+ parentBalance := int64 (100000 )
338+ gasProvided := uint64 (100000 )
339+
340+ _ , err := test .BuildMockInstanceCallTest (t ).
341+ WithContracts (
342+ test .CreateMockContract (test .ParentAddress ).
343+ WithBalance (parentBalance ).
344+ WithMethods (func (instanceMock * contextmock.InstanceMock , config interface {}) {
345+ instanceMock .AddMockMethod ("test" , func () * contextmock.InstanceMock {
346+ host := instanceMock .Host
347+ instance := contextmock .GetMockInstance (host )
348+ managedTypes := host .ManagedTypes ()
349+ hooks := vmhooks .NewVMHooksImpl (host )
350+
351+ sourceBuffer := managedTypes .NewManagedBufferFromBytes ([]byte {0x00 , 0xff , 0x00 , 0xff , 0x00 , 0xff , 0x00 , 0xff })
352+
353+ bigIntDest := managedTypes .NewBigIntFromInt64 (0 )
354+ result := hooks .MBufferToBigIntUnsigned (sourceBuffer , bigIntDest )
355+ assert .True (t , result == 0 )
356+
357+ bigIntResult , err := managedTypes .GetBigInt (bigIntDest )
358+ assert .Nil (t , err )
359+ assert .True (t , bigIntResult .Cmp (big .NewInt (0x00_ff_00_ff_00_ff_00_ff )) == 0 )
360+
361+ result = hooks .MBufferToBigIntSigned (sourceBuffer , bigIntDest )
362+ assert .True (t , result == 0 )
363+
364+ bigIntResult , err = managedTypes .GetBigInt (bigIntDest )
365+ assert .Nil (t , err )
366+ assert .True (t , bigIntResult .Cmp (big .NewInt (0x00_ff_00_ff_00_ff_00_ff )) == 0 )
367+
368+ smallInt := hooks .MBufferToSmallIntSigned (sourceBuffer )
369+ assert .True (t , smallInt == 0x00_ff_00_ff_00_ff_00_ff )
370+
371+ smallInt = hooks .MBufferToSmallIntUnsigned (sourceBuffer )
372+ assert .True (t , smallInt == 0x00_ff_00_ff_00_ff_00_ff )
373+
374+ return instance
375+ })
376+ })).
377+ WithInput (test .CreateTestContractCallInputBuilder ().
378+ WithRecipientAddr (test .ParentAddress ).
379+ WithGasProvided (gasProvided ).
380+ WithFunction ("test" ).
381+ Build ()).
382+ WithSetup (func (host vmhost.VMHost , world * worldmock.MockWorld ) {
383+ enableEpochsHandler := host .EnableEpochsHandler ().(* worldmock.EnableEpochsHandlerStub )
384+ enableEpochsHandler .IsFlagEnabledCalled = func (flag core.EnableEpochFlag ) bool {
385+ return flag != vmhost .BarnardOpcodesFlag
386+ }
387+ }).
388+ AndAssertResults (func (world * worldmock.MockWorld , verify * test.VMOutputVerifier ) {
389+ verify .Ok ().
390+ GasRemaining (gasProvided - 37 ).
391+ GasUsed (test .ParentAddress , 37 )
392+ })
393+ assert .Nil (t , err )
394+ }
395+
396+ func TestManBuffers_Ints_WithBarnardOpcodesFlag (t * testing.T ) {
397+ parentBalance := int64 (100000 )
398+ gasProvided := uint64 (100000 )
399+
400+ _ , err := test .BuildMockInstanceCallTest (t ).
401+ WithContracts (
402+ test .CreateMockContract (test .ParentAddress ).
403+ WithBalance (parentBalance ).
404+ WithMethods (func (instanceMock * contextmock.InstanceMock , config interface {}) {
405+ instanceMock .AddMockMethod ("test" , func () * contextmock.InstanceMock {
406+ host := instanceMock .Host
407+ instance := contextmock .GetMockInstance (host )
408+ managedTypes := host .ManagedTypes ()
409+ hooks := vmhooks .NewVMHooksImpl (host )
410+
411+ sourceBuffer := managedTypes .NewManagedBufferFromBytes ([]byte {0x00 , 0xff , 0x00 , 0xff , 0x00 , 0xff , 0x00 , 0xff })
412+
413+ bigIntDest := managedTypes .NewBigIntFromInt64 (0 )
414+ result := hooks .MBufferToBigIntUnsigned (sourceBuffer , bigIntDest )
415+ assert .True (t , result == 0 )
416+
417+ bigIntResult , err := managedTypes .GetBigInt (bigIntDest )
418+ assert .Nil (t , err )
419+ assert .True (t , bigIntResult .Cmp (big .NewInt (0x00_ff_00_ff_00_ff_00_ff )) == 0 )
420+
421+ result = hooks .MBufferToBigIntSigned (sourceBuffer , bigIntDest )
422+ assert .True (t , result == 0 )
423+
424+ bigIntResult , err = managedTypes .GetBigInt (bigIntDest )
425+ assert .Nil (t , err )
426+ assert .True (t , bigIntResult .Cmp (big .NewInt (0x00_ff_00_ff_00_ff_00_ff )) == 0 )
427+
428+ smallInt := hooks .MBufferToSmallIntSigned (sourceBuffer )
429+ assert .True (t , smallInt == 0x00_ff_00_ff_00_ff_00_ff )
430+
431+ smallInt = hooks .MBufferToSmallIntUnsigned (sourceBuffer )
432+ assert .True (t , smallInt == 0x00_ff_00_ff_00_ff_00_ff )
433+
434+ return instance
435+ })
436+ })).
437+ WithInput (test .CreateTestContractCallInputBuilder ().
438+ WithRecipientAddr (test .ParentAddress ).
439+ WithGasProvided (gasProvided ).
440+ WithFunction ("test" ).
441+ Build ()).
442+ WithSetup (func (host vmhost.VMHost , world * worldmock.MockWorld ) {
443+ enableEpochsHandler := host .EnableEpochsHandler ().(* worldmock.EnableEpochsHandlerStub )
444+ enableEpochsHandler .IsFlagEnabledCalled = func (flag core.EnableEpochFlag ) bool {
445+ return true
446+ }
447+ }).
448+ AndAssertResults (func (world * worldmock.MockWorld , verify * test.VMOutputVerifier ) {
449+ verify .Ok ().
450+ GasRemaining (gasProvided - 69 ).
451+ GasUsed (test .ParentAddress , 69 )
452+ })
453+ assert .Nil (t , err )
454+ }
0 commit comments