Beispiel #1
0
func t100(t *testing.T, pg prime.Generator) {
	if lim := pg.Limit(); lim < 100 {
		t.Errorf("%s Limit() returned %d.  result >= 100 expected.",
			reflect.TypeOf(pg), lim)
	}
	// Test boundary conditions and test results against known primes.
	k100 := []uint64{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
		53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
	ti(t, pg, 0, 100, k100) // Iterate should handle 0 and 1 as min
	ti(t, pg, 1, 100, k100)
	ti(t, pg, 2, 100, k100)     // min = first prime, but 2 is often special
	ti(t, pg, 3, 100, k100[1:]) // min = first odd prime, still often special
	ti(t, pg, 4, 97, k100[2:])  // min = first composite, max is prime
	ti(t, pg, 5, 97, k100[2:])  // min = typical prime
}
Beispiel #2
0
func ti(t *testing.T, pg prime.Generator, min, max uint64, known []uint64) {
	var i int
	pg.Iterate(min, max, func(prime uint64) (terminate bool) {
		if i == len(known) {
			t.Errorf("%s.Iterate(%d, %d) returning too many primes.",
				reflect.TypeOf(pg), min, max)
			return true
		}
		if prime != known[i] {
			t.Errorf("%s.Iterate(%d, %d) = %d for %dth prime.  %d expected.",
				reflect.TypeOf(pg), min, max, prime, i+1, known[i])
			return true
		}
		i++
		return
	})
	if i != len(known) {
		t.Errorf("%s.Iterate(%d, %d) didn't find all primes.",
			reflect.TypeOf(pg), min, max)
	}
}