Ejemplo n.º 1
0
func Benchmark1e7(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sprp.New().Iterate(1, 1e7, func(uint64) (terminate bool) {
			return
		})
	}
}
Ejemplo n.º 2
0
// 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)
		}
	}
}
Ejemplo n.º 3
0
func TestBoundaryCases(t *testing.T) {
	// source: prime pages
	b12 := []uint64{5309, 5323, 5333, 5347}                        // 1-2 bases
	b23 := []uint64{316349261, 316349279, 316349329, 316349333}    // 2-3 bases
	b32 := []uint64{1<<32 - 99, 1<<32 - 65, 1<<32 - 17, 1<<32 - 5} // near the top

	s := sprp.New()
	for _, p := range [][]uint64{b12, b23, b32} {
		i := 0
		s.Iterate(p[0], p[len(p)-1], func(prime uint64) bool {
			if prime != p[i] {
				t.Fatal("Incorrect prime.  Expected", p[i], "got", prime)
			}
			i++
			return false
		})
	}
}
Ejemplo n.º 4
0
func TestLimit(t *testing.T) {
	if l := sprp.New().Limit(); l != 1<<32-1 {
		t.Errorf("Limit() returned %d. 1<<32-1 expected.", l)
	}
}
Ejemplo n.º 5
0
// 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())
}