Merge pull request #6 from nspcc-dev/fix/tests

Use testify/require for testing
This commit is contained in:
fyrchik 2019-06-24 11:58:28 +03:00 committed by GitHub
commit 62a3dafe71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 31 deletions

View file

@ -2,6 +2,8 @@ package gf127
import (
"testing"
"github.com/stretchr/testify/require"
)
const maxUint64 = ^uint64(0)
@ -14,9 +16,7 @@ func TestAdd(t *testing.T) {
c = new(GF127)
)
c.Add(a, b)
if e[0] != c[0] || e[1] != c[1] {
t.Errorf("expected (%s), got (%s)", e.String(), c.String())
}
require.Equal(t, e, c)
}
var testCasesMul = [][3]*GF127{
@ -33,9 +33,8 @@ var testCasesMul = [][3]*GF127{
func TestMul(t *testing.T) {
c := new(GF127)
for _, tc := range testCasesMul {
if Mul(tc[0], tc[1], c); !c.Equals(tc[2]) {
t.Errorf("expected (%s), got (%s)", c.String(), tc[2].String())
}
Mul(tc[0], tc[1], c)
require.Equal(t, tc[2], c)
}
}
@ -48,9 +47,8 @@ var testCasesMul10 = [][2]*GF127{
func TestMul10(t *testing.T) {
c := new(GF127)
for _, tc := range testCasesMul10 {
if Mul10(tc[0], c); !c.Equals(tc[1]) {
t.Errorf("expected (%s), got (%s)", tc[1].String(), c.String())
}
Mul10(tc[0], c)
require.Equal(t, tc[1], c)
}
}
@ -63,9 +61,8 @@ var testCasesMul11 = [][2]*GF127{
func TestMul11(t *testing.T) {
c := new(GF127)
for _, tc := range testCasesMul11 {
if Mul11(tc[0], c); !c.Equals(tc[1]) {
t.Errorf("expected (%s), got (%s)", tc[1].String(), c.String())
}
Mul11(tc[0], c)
require.Equal(t, tc[1], c)
}
}
@ -78,9 +75,8 @@ var testCasesInv = [][2]*GF127{
func TestInv(t *testing.T) {
var a, b, c = new(GF127), new(GF127), new(GF127)
for _, tc := range testCasesInv {
if Inv(tc[0], c); !c.Equals(tc[1]) {
t.Errorf("expected (%s), got (%s)", tc[1].String(), c.String())
}
Inv(tc[0], c)
require.Equal(t, tc[1], c)
}
for i := 0; i < 3; i++ {
@ -90,8 +86,25 @@ func TestInv(t *testing.T) {
}
Inv(a, b)
Mul(a, b, c)
if !c.Equals(&GF127{1, 0}) {
t.Errorf("expected inverse of (%s), got (%s)", a.String(), b.String())
require.Equal(t, &GF127{1, 0}, c)
}
}
func TestGF127_MarshalBinary(t *testing.T) {
a := New(0xFF, 0xEE)
data, err := a.MarshalBinary()
require.NoError(t, err)
require.Equal(t, data, []byte{0, 0, 0, 0, 0, 0, 0, 0xEE, 0, 0, 0, 0, 0, 0, 0, 0xFF})
a = Random()
data, err = a.MarshalBinary()
require.NoError(t, err)
b := new(GF127)
err = b.UnmarshalBinary(data)
require.NoError(t, err)
require.Equal(t, a, b)
err = b.UnmarshalBinary([]byte{0, 1, 2, 3})
require.Error(t, err)
}

View file

@ -1,6 +1,10 @@
package gf127
import "testing"
import (
"testing"
"github.com/stretchr/testify/require"
)
var testCasesSplit = []struct {
num *GF127x2
@ -14,9 +18,8 @@ var testCasesSplit = []struct {
func TestSplit(t *testing.T) {
for _, tc := range testCasesSplit {
a, b := Split(tc.num)
if !a.Equals(tc.h1) || !b.Equals(tc.h2) {
t.Errorf("expected (%s,%s), got (%s,%s)", tc.h1, tc.h2, a, b)
}
require.Equal(t, tc.h1, a)
require.Equal(t, tc.h2, b)
}
}
@ -24,9 +27,7 @@ func TestCombineTo(t *testing.T) {
c := new(GF127x2)
for _, tc := range testCasesSplit {
CombineTo(tc.h1, tc.h2, c)
if !c.Equal(tc.num) {
t.Errorf("expected (%s), got (%s)", tc.num, c)
}
require.Equal(t, tc.num, c)
}
}
@ -39,9 +40,8 @@ var testCasesMul10x2 = [][2]*GF127x2{
func TestMul10x2(t *testing.T) {
c := new(GF127x2)
for _, tc := range testCasesMul10x2 {
if Mul10x2(tc[0], c); !c.Equal(tc[1]) {
t.Errorf("expected (%s), got (%s)", tc[1], c)
}
Mul10x2(tc[0], c)
require.Equal(t, tc[1], c)
}
}
@ -54,8 +54,7 @@ var testCasesMul11x2 = [][2]*GF127x2{
func TestMul11x2(t *testing.T) {
c := new(GF127x2)
for _, tc := range testCasesMul11x2 {
if Mul11x2(tc[0], c); !c.Equal(tc[1]) {
t.Errorf("expected (%s), got (%s)", tc[1], c)
}
Mul11x2(tc[0], c)
require.Equal(t, tc[1], c)
}
}