// Test the two generic access functions in prime.go, Primes() and // Iterator(). Also test that the generators agree on all primes under // a somewhat bigger limit. // // Use basic sieve as a reference, compare it to other generators. func TestGeneric(t *testing.T) { const limit = 1000 // exercise generic function Primes on sieve. reference := prime.Primes(sieve.New(limit), 0, limit) gloop: for _, gen := range []prime.Generator{ segment.New(limit), queue.PQueue{}, sprp.New(), } { // exercise generic function Iterator on other generators. ch := prime.Iterator(gen, 0, limit) for i, pRef := range reference { switch p, ok := <-ch; { case !ok: t.Errorf("%s.Iterator(0, %d) didn't find all primes.", reflect.TypeOf(gen), limit) continue gloop case p != pRef: t.Errorf("%dth prime? sieve says %d, %s says %d\n", i+1, pRef, reflect.TypeOf(gen), p) continue gloop } } if _, ok := <-ch; ok { t.Errorf("%s.Iterator(0, %d) returning too many primes.\n", reflect.TypeOf(gen), limit) } } }
func Benchmark1e8(b *testing.B) { for i := 0; i < b.N; i++ { segment.New(1e8).Iterate(1, 1e8, func(uint64) (terminate bool) { return }) } }
// Exercise Limit and Iterate methods of each implemented generator. func TestMethods(t *testing.T) { t100(t, segment.New(100)) t100(t, sieve.New(100)) t100(t, queue.PQueue{}) t100(t, sprp.New()) }