Esempio n. 1
0
func TestPrimes(t *testing.T) {
	actual := utils.Take(10, primes.Primes())
	expected := []int{
		2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
	}
	i := 0
	for p := range actual {
		if p != expected[i] {
			t.Errorf("failed Primes[%d]: got %v, want %v", i, p, expected[i])
		}
		i = i + 1
	}
}
Esempio n. 2
0
func TestPrimes(t *testing.T) {
	expected := []int{
		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, 101, 103, 107, 109, 113,
	}
	actual := utils.Take(len(expected), primes.Primes())

	i := 0
	for p := range actual {
		if p != expected[i] {
			t.Errorf("failed Primes[%d]: got %v, want %v", i, p, expected[i])
		}
		i = i + 1
	}
}
Esempio n. 3
0
func TestFilteredPrimes(t *testing.T) {
	expected := []int{
		101, 103, 107, 109,
	}
	fn := func(n int) bool { return 100 < n && n < 110 }
	actual := utils.Filter(fn, utils.Take(100, primes.Primes()))

	i := 0
	for p := range actual {
		if p != expected[i] {
			t.Errorf("Primes[%d]: got %v, want %v", i, p, expected[i])
		}
		i = i + 1
	}

	if i != len(expected) {
		t.Errorf("length: got %d, want %d", i, len(expected))
	}
}
Esempio n. 4
0
func pe2() {
	var n uint64
	if len(os.Args) > 1 {
		var err error
		n, err = strconv.ParseUint(os.Args[1], 10, 64)
		if err != nil {
			fmt.Println("failed ParseUint: ", err)
		}
	} else {
		n = 4000000
	}

	for n := range utils.Take(20, pe.Fibonacci()) {
		fmt.Printf("%d ", n)
	}
	fmt.Println("...")

	ans := pe.Pe2(n)
	fmt.Println("pe2: ", ans)
}
Esempio n. 5
0
func BenchmarkPrimes(b *testing.B) {
	for i := 0; i < b.N; i++ {
		utils.Take(100, primes.Primes())
	}
}