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