Skip to content

Commit 77492ed

Browse files
committed
add tests
1 parent 366bf2b commit 77492ed

File tree

2 files changed

+226
-0
lines changed

2 files changed

+226
-0
lines changed

vmhost/vmhookstest/bigFloats_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
contextmock "github.com/multiversx/mx-chain-vm-go/mock/context"
1111
test "github.com/multiversx/mx-chain-vm-go/testcommon"
1212
"github.com/multiversx/mx-chain-vm-go/vmhost"
13+
"github.com/multiversx/mx-chain-vm-go/vmhost/vmhooks"
1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
1516
)
@@ -730,3 +731,104 @@ func TestBigFloats_GetConstE(t *testing.T) {
730731
ReturnData(encodedFloat)
731732
})
732733
}
734+
func TestBigFloats_Pow_WithNoBarnardOpcodesFlag(t *testing.T) {
735+
parentBalance := int64(100000)
736+
gasProvided := uint64(100000)
737+
738+
_, err := test.BuildMockInstanceCallTest(t).
739+
WithContracts(
740+
test.CreateMockContract(test.ParentAddress).
741+
WithBalance(parentBalance).
742+
WithMethods(func(instanceMock *contextmock.InstanceMock, config interface{}) {
743+
instanceMock.AddMockMethod("test", func() *contextmock.InstanceMock {
744+
host := instanceMock.Host
745+
instance := contextmock.GetMockInstance(host)
746+
managedTypes := host.ManagedTypes()
747+
hooks := vmhooks.NewVMHooksImpl(host)
748+
749+
destination, err := managedTypes.PutBigFloat(big.NewFloat(0))
750+
assert.Nil(t, err)
751+
arg, err := managedTypes.PutBigFloat(big.NewFloat(10))
752+
assert.Nil(t, err)
753+
754+
hooks.BigFloatPow(destination, arg, 5)
755+
756+
result, err := managedTypes.GetBigFloat(destination)
757+
assert.Nil(t, err)
758+
759+
println(result.String())
760+
761+
assert.True(t, result.Cmp(big.NewFloat(100000)) == 0)
762+
763+
return instance
764+
})
765+
})).
766+
WithInput(test.CreateTestContractCallInputBuilder().
767+
WithRecipientAddr(test.ParentAddress).
768+
WithGasProvided(gasProvided).
769+
WithFunction("test").
770+
Build()).
771+
WithSetup(func(host vmhost.VMHost, world *worldmock.MockWorld) {
772+
enableEpochsHandler := host.EnableEpochsHandler().(*worldmock.EnableEpochsHandlerStub)
773+
enableEpochsHandler.IsFlagEnabledCalled = func(flag core.EnableEpochFlag) bool {
774+
return flag != vmhost.BarnardOpcodesFlag
775+
}
776+
}).
777+
AndAssertResults(func(world *worldmock.MockWorld, verify *test.VMOutputVerifier) {
778+
verify.Ok().
779+
GasRemaining(gasProvided-36).
780+
GasUsed(test.ParentAddress, 36)
781+
})
782+
assert.Nil(t, err)
783+
}
784+
785+
func TestBigFloats_Pow_WithBarnardOpcodesFlag(t *testing.T) {
786+
parentBalance := int64(100000)
787+
gasProvided := uint64(100000)
788+
789+
_, err := test.BuildMockInstanceCallTest(t).
790+
WithContracts(
791+
test.CreateMockContract(test.ParentAddress).
792+
WithBalance(parentBalance).
793+
WithMethods(func(instanceMock *contextmock.InstanceMock, config interface{}) {
794+
instanceMock.AddMockMethod("test", func() *contextmock.InstanceMock {
795+
host := instanceMock.Host
796+
instance := contextmock.GetMockInstance(host)
797+
managedTypes := host.ManagedTypes()
798+
hooks := vmhooks.NewVMHooksImpl(host)
799+
800+
destination, err := managedTypes.PutBigFloat(big.NewFloat(0))
801+
assert.Nil(t, err)
802+
arg, err := managedTypes.PutBigFloat(big.NewFloat(10))
803+
assert.Nil(t, err)
804+
805+
hooks.BigFloatPow(destination, arg, 5)
806+
807+
result, err := managedTypes.GetBigFloat(destination)
808+
assert.Nil(t, err)
809+
810+
println(result.String())
811+
812+
assert.True(t, result.Cmp(big.NewFloat(100000)) == 0)
813+
814+
return instance
815+
})
816+
})).
817+
WithInput(test.CreateTestContractCallInputBuilder().
818+
WithRecipientAddr(test.ParentAddress).
819+
WithGasProvided(gasProvided).
820+
WithFunction("test").
821+
Build()).
822+
WithSetup(func(host vmhost.VMHost, world *worldmock.MockWorld) {
823+
enableEpochsHandler := host.EnableEpochsHandler().(*worldmock.EnableEpochsHandlerStub)
824+
enableEpochsHandler.IsFlagEnabledCalled = func(flag core.EnableEpochFlag) bool {
825+
return true
826+
}
827+
}).
828+
AndAssertResults(func(world *worldmock.MockWorld, verify *test.VMOutputVerifier) {
829+
verify.Ok().
830+
GasRemaining(gasProvided-41).
831+
GasUsed(test.ParentAddress, 41)
832+
})
833+
assert.Nil(t, err)
834+
}

vmhost/vmhookstest/manBuffers_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)