Commit 9eb8c4c6 authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kamil Kisiel

decoder: Cleanup decodeLong tests (#43)

Instead of copy-pasting full test driver for each value, make it to be
only one test with many values in table each tested by common driver.

TestZeroLengthData was checking output.BitLen() == 0 which is another
way to test for equality with big.Int(0) according to

	https://golang.org/pkg/math/big/#Int.BitLen

So join it too to the rest of decodeLong tests.

While at decodeLong topic, rename BenchmarkSpeed -> BenchmarkDecodeLong
as this benchmark checks speed of decodeLong only, nothing else.
parent 2a0ddd42
...@@ -173,96 +173,35 @@ func TestDecodeMultiple(t *testing.T) { ...@@ -173,96 +173,35 @@ func TestDecodeMultiple(t *testing.T) {
} }
} }
func TestZeroLengthData(t *testing.T) { func TestDecodeLong(t *testing.T) {
data := "" var testv = []struct {
output, err := decodeLong(data) data string
if err != nil { value int64 // converted to big.Int by test driver
t.Errorf("Error from decodeLong - %v\n", err) }{
} {"", 0},
if output.BitLen() > 0 { {"\xff\x00", 255},
t.Fail() {"\xff\x7f", 32767},
} {"\x00\xff", -256},
} {"\x00\x80", -32768},
{"\x80", -128},
func TestValue1(t *testing.T) { {"\x7f", 127},
data := "\xff\x00"
output, err := decodeLong(data)
if err != nil {
t.Errorf("Error from decodeLong - %v\n", err)
}
target := big.NewInt(255)
if target.Cmp(output) != 0 {
t.Fail()
}
}
func TestValue2(t *testing.T) {
data := "\xff\x7f"
output, err := decodeLong(data)
if err != nil {
t.Errorf("Error from decodeLong - %v\n", err)
}
target := big.NewInt(32767)
if target.Cmp(output) != 0 {
t.Fail()
}
}
func TestValue3(t *testing.T) {
data := "\x00\xff"
output, err := decodeLong(data)
if err != nil {
t.Errorf("Error from decodeLong - %v\n", err)
}
target := big.NewInt(256)
target.Neg(target)
if target.Cmp(output) != 0 {
t.Logf("\nGot %v\nExpecting %v\n", output, target)
t.Fail()
}
}
func TestValue4(t *testing.T) {
data := "\x00\x80"
output, err := decodeLong(data)
if err != nil {
t.Errorf("Error from decodeLong - %v\n", err)
}
target := big.NewInt(32768)
target.Neg(target)
if target.Cmp(output) != 0 {
t.Logf("\nGot %v\nExpecting %v\n", output, target)
t.Fail()
} }
}
func TestValue5(t *testing.T) { for _, tt := range testv {
data := "\x80" value, err := decodeLong(tt.data)
output, err := decodeLong(data) if err != nil {
if err != nil { t.Errorf("data %q: %s", tt.data, err)
t.Errorf("Error from decodeLong - %v\n", err) continue
} }
target := big.NewInt(128)
target.Neg(target)
if target.Cmp(output) != 0 {
t.Logf("\nGot %v\nExpecting %v\n", output, target)
t.Fail()
}
}
func TestValue6(t *testing.T) { valueOk := big.NewInt(tt.value)
data := "\x7f" if valueOk.Cmp(value) != 0 {
output, err := decodeLong(data) t.Errorf("data %q: ->long: got %s ; want %s", tt.data, value, valueOk)
if err != nil { }
t.Errorf("Error from decodeLong - %v\n", err)
}
target := big.NewInt(127)
if target.Cmp(output) != 0 {
t.Fail()
} }
} }
func BenchmarkSpeed(b *testing.B) { func BenchmarkDecodeLong(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
data := "\x00\x80" data := "\x00\x80"
_, err := decodeLong(data) _, err := decodeLong(data)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment