Use package "restic/test"

This commit is contained in:
Alexander Neumann
2015-04-09 21:15:48 +02:00
parent a2514425a3
commit 4f4f3c421a
24 changed files with 248 additions and 403 deletions

View File

@@ -15,6 +15,7 @@ import (
"time"
"github.com/restic/restic/chunker"
. "github.com/restic/restic/test"
)
var benchmarkFile = flag.String("bench.file", "", "read from this file for benchmark")
@@ -189,7 +190,7 @@ func TestChunkerWithRandomPolynomial(t *testing.T) {
// generate a new random polynomial
start := time.Now()
p, err := chunker.RandomPolynomial()
ok(t, err)
OK(t, err)
t.Logf("generating random polynomial took %v", time.Since(start))
start = time.Now()
@@ -199,11 +200,11 @@ func TestChunkerWithRandomPolynomial(t *testing.T) {
// make sure that first chunk is different
c, err := ch.Next()
assert(t, c.Cut != chunks1[0].CutFP,
Assert(t, c.Cut != chunks1[0].CutFP,
"Cut point is the same")
assert(t, c.Length != chunks1[0].Length,
Assert(t, c.Length != chunks1[0].Length,
"Length is the same")
assert(t, !bytes.Equal(c.Digest, chunks1[0].Digest),
Assert(t, !bytes.Equal(c.Digest, chunks1[0].Digest),
"Digest is the same")
}
@@ -327,7 +328,7 @@ func BenchmarkChunker(b *testing.B) {
func BenchmarkNewChunker(b *testing.B) {
p, err := chunker.RandomPolynomial()
ok(b, err)
OK(b, err)
b.ResetTimer()

View File

@@ -1,36 +0,0 @@
package chunker_test
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"testing"
)
// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
if !condition {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
}
}
// ok fails the test if an err is not nil.
func ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
}
}
// equals fails the test if exp is not equal to act.
func equals(tb testing.TB, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
}
}

View File

@@ -5,6 +5,7 @@ import (
"testing"
"github.com/restic/restic/chunker"
. "github.com/restic/restic/test"
)
var polAddTests = []struct {
@@ -18,8 +19,8 @@ var polAddTests = []struct {
func TestPolAdd(t *testing.T) {
for _, test := range polAddTests {
equals(t, test.sum, test.x.Add(test.y))
equals(t, test.sum, test.y.Add(test.x))
Equals(t, test.sum, test.x.Add(test.y))
Equals(t, test.sum, test.y.Add(test.x))
}
}
@@ -77,11 +78,11 @@ var polMulTests = []struct {
func TestPolMul(t *testing.T) {
for i, test := range polMulTests {
m := test.x.Mul(test.y)
assert(t, test.res == m,
Assert(t, test.res == m,
"TestPolMul failed for test %d: %v * %v: want %v, got %v",
i, test.x, test.y, test.res, m)
m = test.y.Mul(test.x)
assert(t, test.res == test.y.Mul(test.x),
Assert(t, test.res == test.y.Mul(test.x),
"TestPolMul failed for %d: %v * %v: want %v, got %v",
i, test.x, test.y, test.res, m)
}
@@ -138,7 +139,7 @@ var polDivTests = []struct {
func TestPolDiv(t *testing.T) {
for i, test := range polDivTests {
m := test.x.Div(test.y)
assert(t, test.res == m,
Assert(t, test.res == m,
"TestPolDiv failed for test %d: %v * %v: want %v, got %v",
i, test.x, test.y, test.res, m)
}
@@ -175,7 +176,7 @@ var polModTests = []struct {
func TestPolModt(t *testing.T) {
for _, test := range polModTests {
equals(t, test.res, test.x.Mod(test.y))
Equals(t, test.res, test.x.Mod(test.y))
}
}
@@ -221,20 +222,20 @@ func BenchmarkPolDeg(t *testing.B) {
func TestRandomPolynomial(t *testing.T) {
_, err := chunker.RandomPolynomial()
ok(t, err)
OK(t, err)
}
func BenchmarkRandomPolynomial(t *testing.B) {
for i := 0; i < t.N; i++ {
_, err := chunker.RandomPolynomial()
ok(t, err)
OK(t, err)
}
}
func TestExpandPolynomial(t *testing.T) {
pol := chunker.Pol(0x3DA3358B4DC173)
s := pol.Expand()
equals(t, "x^53+x^52+x^51+x^50+x^48+x^47+x^45+x^41+x^40+x^37+x^36+x^34+x^32+x^31+x^27+x^25+x^24+x^22+x^19+x^18+x^16+x^15+x^14+x^8+x^6+x^5+x^4+x+1", s)
Equals(t, "x^53+x^52+x^51+x^50+x^48+x^47+x^45+x^41+x^40+x^37+x^36+x^34+x^32+x^31+x^27+x^25+x^24+x^22+x^19+x^18+x^16+x^15+x^14+x^8+x^6+x^5+x^4+x+1", s)
}
var polIrredTests = []struct {
@@ -269,7 +270,7 @@ var polIrredTests = []struct {
func TestPolIrreducible(t *testing.T) {
for _, test := range polIrredTests {
assert(t, test.f.Irreducible() == test.irred,
Assert(t, test.f.Irreducible() == test.irred,
"Irreducibility test for Polynomial %v failed: got %v, wanted %v",
test.f, test.f.Irreducible(), test.irred)
}
@@ -328,11 +329,11 @@ var polGCDTests = []struct {
func TestPolGCD(t *testing.T) {
for i, test := range polGCDTests {
gcd := test.f1.GCD(test.f2)
assert(t, test.gcd == gcd,
Assert(t, test.gcd == gcd,
"GCD test %d (%+v) failed: got %v, wanted %v",
i, test, gcd, test.gcd)
gcd = test.f2.GCD(test.f1)
assert(t, test.gcd == gcd,
Assert(t, test.gcd == gcd,
"GCD test %d (%+v) failed: got %v, wanted %v",
i, test, gcd, test.gcd)
}
@@ -361,7 +362,7 @@ var polMulModTests = []struct {
func TestPolMulMod(t *testing.T) {
for i, test := range polMulModTests {
mod := test.f1.MulMod(test.f2, test.g)
assert(t, mod == test.mod,
Assert(t, mod == test.mod,
"MulMod test %d (%+v) failed: got %v, wanted %v",
i, test, mod, test.mod)
}