vm: use truncated division for Integers
When divisor is negative, the result is truncated towards zero.
This commit is contained in:
parent
a87f849115
commit
4b44190485
2 changed files with 16 additions and 6 deletions
|
@ -776,7 +776,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
|
||||||
a := v.estack.Pop().BigInt()
|
a := v.estack.Pop().BigInt()
|
||||||
v.checkBigIntSize(a)
|
v.checkBigIntSize(a)
|
||||||
|
|
||||||
v.estack.PushVal(new(big.Int).Div(a, b))
|
v.estack.PushVal(new(big.Int).Quo(a, b))
|
||||||
|
|
||||||
case opcode.MUL:
|
case opcode.MUL:
|
||||||
a := v.estack.Pop().BigInt()
|
a := v.estack.Pop().BigInt()
|
||||||
|
|
|
@ -832,11 +832,21 @@ func TestMULBigResult(t *testing.T) {
|
||||||
|
|
||||||
func TestDiv(t *testing.T) {
|
func TestDiv(t *testing.T) {
|
||||||
prog := makeProgram(opcode.DIV)
|
prog := makeProgram(opcode.DIV)
|
||||||
vm := load(prog)
|
runCase := func(p, q, result int64) func(t *testing.T) {
|
||||||
vm.estack.PushVal(4)
|
return func(t *testing.T) {
|
||||||
vm.estack.PushVal(2)
|
vm := load(prog)
|
||||||
runVM(t, vm)
|
vm.estack.PushVal(p)
|
||||||
assert.Equal(t, int64(2), vm.estack.Pop().BigInt().Int64())
|
vm.estack.PushVal(q)
|
||||||
|
runVM(t, vm)
|
||||||
|
assert.Equal(t, result, vm.estack.Pop().BigInt().Int64())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("positive/positive", runCase(5, 2, 2))
|
||||||
|
t.Run("positive/negative", runCase(5, -2, -2))
|
||||||
|
t.Run("negative/positive", runCase(-5, 2, -2))
|
||||||
|
t.Run("negative/negative", runCase(-5, -2, 2))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSub(t *testing.T) {
|
func TestSub(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue