66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package primes
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestIsKnownPrime(t *testing.T) {
|
|
primes.Prime(100)
|
|
var known = []struct {
|
|
n int64
|
|
inRange bool
|
|
isPrime bool
|
|
}{
|
|
{2, true, true}, // prime
|
|
{3, true, true}, // prime
|
|
{4, true, false},
|
|
{5, true, true}, // prime
|
|
{52, true, false},
|
|
{53, true, true}, // prime
|
|
{54, true, false},
|
|
{58, true, false},
|
|
{59, true, true}, // prime
|
|
{60, true, false},
|
|
{1000000, false, false}, // not cached
|
|
}
|
|
for _, k := range known {
|
|
inRange, isPrime := primes.isKnownPrime(k.n)
|
|
assert.Equal(t, k.inRange, inRange)
|
|
assert.Equal(t, k.isPrime, isPrime)
|
|
}
|
|
}
|
|
|
|
func TestIsPrime(t *testing.T) {
|
|
assert.True(t, IsPrime(84017))
|
|
}
|
|
|
|
func TestPrimeN(t *testing.T) {
|
|
assert.Equal(t, int64(2), NewCache().Prime(1))
|
|
assert.Equal(t, int64(541), NewCache().Prime(100))
|
|
assert.Equal(t, int64(1987), NewCache().Prime(300))
|
|
assert.Equal(t, int64(7919), NewCache().Prime(1000))
|
|
assert.Equal(t, int64(84017), NewCache().Prime(8192))
|
|
assert.Equal(t, int64(104729), NewCache().Prime(10000))
|
|
}
|
|
|
|
func TestIterator(t *testing.T) {
|
|
nth := 1
|
|
reference := NewCache()
|
|
assert.Equal(t, int64(104729), reference.Prime(10000))
|
|
c := NewCache()
|
|
it := NewIteratorFromCache(c)
|
|
for it.Next() && nth < 10000 {
|
|
assert.Equal(t, reference.Prime(nth), it.Get())
|
|
nth++
|
|
}
|
|
}
|
|
|
|
func BenchmarkGenerateCache(b *testing.B) {
|
|
var c = &cache{}
|
|
for i := 0; i < b.N; i++ {
|
|
c.Prime(5000)
|
|
}
|
|
}
|