stat/card: make tests parallel

This commit is contained in:
Egon Elbre
2020-03-14 13:42:37 +02:00
parent 619455fee7
commit a760dc5d65

View File

@@ -73,47 +73,21 @@ func mustCounter(c counter, err error) counter {
}
func TestCounters(t *testing.T) {
var dst []byte
for _, test := range counterTests {
rnd := rand.New(rand.NewSource(1))
c := test.counter()
for i := 0; i < int(test.count); i++ {
dst = strconv.AppendUint(dst[:0], rnd.Uint64(), 16)
dst = append(dst, '-')
dst = strconv.AppendUint(dst, uint64(i), 16)
n, err := c.Write(dst)
if n != len(dst) {
t.Errorf("unexpected number of bytes written for %s: got:%d want:%d",
test.name, n, len(dst))
break
}
if err != nil {
t.Errorf("unexpected error for %s: %v", test.name, err)
break
}
}
t.Parallel()
if got := c.Count(); !floats.EqualWithinRel(got, test.count, test.tol) {
t.Errorf("unexpected count for %s: got:%.0f want:%.0f", test.name, got, test.count)
}
}
}
func TestUnion(t *testing.T) {
var dst []byte
for _, test := range counterTests {
if strings.HasPrefix(test.name, "exact") {
continue
}
rnd := rand.New(rand.NewSource(1))
var cs [2]counter
for j := range cs {
cs[j] = test.counter()
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
rnd := rand.New(rand.NewSource(1))
var dst []byte
c := test.counter()
for i := 0; i < int(test.count); i++ {
dst = strconv.AppendUint(dst[:0], rnd.Uint64(), 16)
dst = append(dst, '-')
dst = strconv.AppendUint(dst, uint64(i), 16)
n, err := cs[j].Write(dst)
n, err := c.Write(dst)
if n != len(dst) {
t.Errorf("unexpected number of bytes written for %s: got:%d want:%d",
test.name, n, len(dst))
@@ -124,22 +98,61 @@ func TestUnion(t *testing.T) {
break
}
}
}
u := test.counter()
var err error
switch u := u.(type) {
case *HyperLogLog32:
err = u.Union(cs[0].(*HyperLogLog32), cs[1].(*HyperLogLog32))
case *HyperLogLog64:
err = u.Union(cs[0].(*HyperLogLog64), cs[1].(*HyperLogLog64))
}
if err != nil {
t.Errorf("unexpected error from Union call: %v", err)
}
if got := u.Count(); !floats.EqualWithinRel(got, 2*test.count, 2*test.tol) {
t.Errorf("unexpected count for %s: got:%.0f want:%.0f", test.name, got, 2*test.count)
if got := c.Count(); !floats.EqualWithinRel(got, test.count, test.tol) {
t.Errorf("unexpected count for %s: got:%.0f want:%.0f", test.name, got, test.count)
}
})
}
}
func TestUnion(t *testing.T) {
t.Parallel()
for _, test := range counterTests {
if strings.HasPrefix(test.name, "exact") {
continue
}
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
rnd := rand.New(rand.NewSource(1))
var dst []byte
var cs [2]counter
for j := range cs {
cs[j] = test.counter()
for i := 0; i < int(test.count); i++ {
dst = strconv.AppendUint(dst[:0], rnd.Uint64(), 16)
dst = append(dst, '-')
dst = strconv.AppendUint(dst, uint64(i), 16)
n, err := cs[j].Write(dst)
if n != len(dst) {
t.Errorf("unexpected number of bytes written for %s: got:%d want:%d",
test.name, n, len(dst))
break
}
if err != nil {
t.Errorf("unexpected error for %s: %v", test.name, err)
break
}
}
}
u := test.counter()
var err error
switch u := u.(type) {
case *HyperLogLog32:
err = u.Union(cs[0].(*HyperLogLog32), cs[1].(*HyperLogLog32))
case *HyperLogLog64:
err = u.Union(cs[0].(*HyperLogLog64), cs[1].(*HyperLogLog64))
}
if err != nil {
t.Errorf("unexpected error from Union call: %v", err)
}
if got := u.Count(); !floats.EqualWithinRel(got, 2*test.count, 2*test.tol) {
t.Errorf("unexpected count for %s: got:%.0f want:%.0f", test.name, got, 2*test.count)
}
})
}
}
@@ -167,6 +180,8 @@ func mustResetCounter(c resetCounter, err error) resetCounter {
}
func TestResetCounters(t *testing.T) {
t.Parallel()
var dst []byte
for _, test := range counterResetTests {
c := test.resetCounter()
@@ -255,6 +270,8 @@ func mustCounterEncoder(c counterEncoder, err error) counterEncoder {
}
func TestBinaryEncoding(t *testing.T) {
t.Parallel()
RegisterHash(fnv.New32a)
RegisterHash(fnv.New64a)
defer func() {
@@ -324,6 +341,8 @@ var invalidRegisterTests = []struct {
}
func TestRegisterInvalid(t *testing.T) {
t.Parallel()
for _, test := range invalidRegisterTests {
var r interface{}
func() {
@@ -356,6 +375,8 @@ var rhoQTests = []struct {
}
func TestRhoQ(t *testing.T) {
t.Parallel()
for _, test := range rhoQTests {
got := rho32q(uint32(test.bits), test.q)
if got != test.want {