From d9e26aa6de48d6ffed4324c3d24785d47dd095e4 Mon Sep 17 00:00:00 2001 From: Evgenii Date: Mon, 24 Jun 2019 10:56:15 +0300 Subject: [PATCH 1/2] Use testify/require for testing --- gf127/gf127_test.go | 30 ++++++++++++------------------ gf127/gf127x2_test.go | 25 ++++++++++++------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/gf127/gf127_test.go b/gf127/gf127_test.go index bc7e902..bab2dac 100644 --- a/gf127/gf127_test.go +++ b/gf127/gf127_test.go @@ -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,6 @@ 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) } } diff --git a/gf127/gf127x2_test.go b/gf127/gf127x2_test.go index 64370c1..9baa2d1 100644 --- a/gf127/gf127x2_test.go +++ b/gf127/gf127x2_test.go @@ -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) } } From 9a258e8741a0e1aa0fe9317d4274e89ecd52809c Mon Sep 17 00:00:00 2001 From: Evgenii Date: Mon, 24 Jun 2019 11:02:42 +0300 Subject: [PATCH 2/2] Add test for marshalling/unmarshalling --- gf127/gf127_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/gf127/gf127_test.go b/gf127/gf127_test.go index bab2dac..e24533d 100644 --- a/gf127/gf127_test.go +++ b/gf127/gf127_test.go @@ -89,3 +89,22 @@ func TestInv(t *testing.T) { 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) +}