From 1c7d203993e1febd9db6ca03827b9d8ba1cc43ed Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 7 Sep 2019 10:09:42 +0300 Subject: [PATCH 1/3] compiler: drop TODO comment, #294 was fixed a while ago --- pkg/vm/compiler/codegen.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/vm/compiler/codegen.go b/pkg/vm/compiler/codegen.go index 471ef70af..d0ae6290b 100644 --- a/pkg/vm/compiler/codegen.go +++ b/pkg/vm/compiler/codegen.go @@ -720,9 +720,6 @@ func (c *codegen) convertToken(tok token.Token) { case token.GEQ: emitOpcode(c.prog, vm.GTE) case token.EQL: - // TODO: this is wrong (and the next one also is), see issue #294 - // Changing it EQUAL is not that big of an improvement, so we're - // using NUMEQUAL for now emitOpcode(c.prog, vm.NUMEQUAL) case token.NEQ: emitOpcode(c.prog, vm.NUMNOTEQUAL) From 7e8dc9ad66b1e337d9614d34c6377cb60031f6f1 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 7 Sep 2019 10:10:11 +0300 Subject: [PATCH 2/3] vm: uncomment TestForLoopBigIter It works just fine. --- pkg/vm/tests/for_test.go | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/pkg/vm/tests/for_test.go b/pkg/vm/tests/for_test.go index 0cc443451..70b063ffe 100644 --- a/pkg/vm/tests/for_test.go +++ b/pkg/vm/tests/for_test.go @@ -357,18 +357,16 @@ func TestDec(t *testing.T) { eval(t, src, big.NewInt(1)) } -// TODO: This could be a nasty bug. Output of the VM is 65695. -// Only happens above 100000, could be binary read issue. -//func TestForLoopBigIter(t *testing.T) { -// src := ` -// package foo -// func Main() int { -// x := 0 -// for i := 0; i < 100000; i++ { -// x = i -// } -// return x -// } -// ` -// eval(t, src, big.NewInt(99999)) -//} +func TestForLoopBigIter(t *testing.T) { + src := ` + package foo + func Main() int { + x := 0 + for i := 0; i < 100000; i++ { + x = i + } + return x + } + ` + eval(t, src, big.NewInt(99999)) +} From 8c448ab8904615c9aba06a91e6fea5076bb187f9 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 7 Sep 2019 10:28:00 +0300 Subject: [PATCH 3/3] util: add some tests to make GetVarSize() fully covered --- pkg/util/size.go | 2 +- pkg/util/size_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/pkg/util/size.go b/pkg/util/size.go index dfc88c614..33b7b60fc 100644 --- a/pkg/util/size.go +++ b/pkg/util/size.go @@ -43,7 +43,7 @@ func GetVarStringSize(value string) int { // GetVarSize return the size om bytes of a variable. This implementation is not exactly like the C# // (reference: GetVarSize(this T[] value), https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs#L53) as in the C# variable -// like Uint160, Uint256 are not supported. @TODO: make sure to have full unit tests coverage. +// like Uint160, Uint256 are not supported. func GetVarSize(value interface{}) int { v := reflect.ValueOf(value) switch v.Kind() { diff --git a/pkg/util/size_test.go b/pkg/util/size_test.go index f4c57b475..15eb77523 100644 --- a/pkg/util/size_test.go +++ b/pkg/util/size_test.go @@ -2,11 +2,28 @@ package util import ( "fmt" + "io" "testing" "github.com/stretchr/testify/assert" ) +// Mock structure to test getting size of an array of serializable things +type smthSerializable struct { +} + +func (*smthSerializable) DecodeBinary(io.Reader) error { + return nil +} + +func (*smthSerializable) EncodeBinary(io.Writer) error { + return nil +} + +func (*smthSerializable) Size() int { + return 42 +} + func TestVarSize(t *testing.T) { testCases := []struct { variable interface{} @@ -38,6 +55,31 @@ func TestVarSize(t *testing.T) { "test_int_5", 5, }, + { + uint(252), + "test_uint_1", + 1, + }, + { + uint(253), + "test_uint_2", + 3, + }, + { + uint(65535), + "test_uint_3", + 3, + }, + { + uint(65536), + "test_uint_4", + 5, + }, + { + uint(4294967295), + "test_uint_5", + 5, + }, { []byte{1, 2, 4, 5, 6}, "test_[]byte_1", @@ -128,6 +170,10 @@ func TestVarSize(t *testing.T) { "test_string_3", 41, }, + {[]*smthSerializable{&smthSerializable{}, &smthSerializable{}}, + "test_Serializable", + 2 * 42 + 1, + }, } for _, tc := range testCases { @@ -137,3 +183,12 @@ func TestVarSize(t *testing.T) { }) } } + +func TestVarSizePanic(t *testing.T) { + defer func() { + r := recover() + assert.NotNil(t, r) + }() + + _ = GetVarSize(t) +}