示例#1
0
TEST(GeneratorTest, Comparision) {
    primewheel pwg, pws; siever svr; sieve segment;
    new_primewheel(&pwg, 3);
    new_primewheel(&pws, 4);

    new_sieve(&segment, DEF_BUFSIZE, 0);

    new_siever(&svr, &pws, 1000);

    prime* ps; size_t pcount;
    segmented_list_primes(&svr, &segment, 0, GEN_LIM, &ps, &pcount);

    prime_generator pg;
    new_prime_generator(&pg, &pwg, 100);
    size_t pgcount = 0;
    for (prime p = prime_generator_next(&pg, &segment);
         p < GEN_LIM;
         p = prime_generator_next(&pg, &segment)) {
        if (pgcount < pcount) {
            ASSERT_EQ(ps[pgcount], p);
        }
        pgcount++;
    }

    ASSERT_EQ(pcount, pgcount);

    free_primewheel(&pwg);
    free_primewheel(&pws);
    free_prime_generator(&pg);
    free_sieve(&segment);
    free_siever(&svr);

    free(ps);
}
示例#2
0
static void test_sieve(){
	const lint_t n = 250000;
	prime_sieve_t * sieve = alloc_sieve(n);
	lint_t i=2;
	for ( ; i<=n ; ++i){
		assert( slow_verify(i) == is_prime(sieve,i) );
	}
	free_sieve(sieve);
}
示例#3
0
TEST(SegmentedSieveTest, BasicCount) {
    primewheel pw;
    new_primewheel(&pw, 4);

    siever svr;
    new_siever(&svr, &pw, 1e9);

    sieve segment;
    new_sieve(&segment, DEF_BUFSIZE, 0);

    for (size_t i = 0; i < sizeof(P_COUNTS) / sizeof(P_COUNTS[0]); i++) {
        ASSERT_EQ(P_COUNTS[i],
                  segmented_count_primes(&svr, &segment, 0, P_LIMITS[i]));
    }

    free_sieve(&segment);
    free_siever(&svr);
    free_primewheel(&pw);
}
示例#4
0
TEST(SegmentedSieveTest, List) {
    primewheel pw;
    new_primewheel(&pw, 4);

    siever svr;
    new_siever(&svr, &pw, 1e9);

    sieve segment;
    new_sieve(&segment, DEF_BUFSIZE, 0);

    prime* primes;
    size_t nprimes = 1;

    segmented_list_primes(&svr, &segment, 0, P_LIMITS[7], &primes, &nprimes);
    ASSERT_EQ(P_COUNTS[7], nprimes);
    ASSERT_EQ(17329489, primes[1111110]);

    free(primes);
    free_sieve(&segment);
    free_siever(&svr);
    free_primewheel(&pw);
}
示例#5
0
TEST(SegmentedSieveTest, SubtractCount) {
    primewheel pw; new_primewheel(&pw, 4);
    siever svr; new_siever(&svr, &pw, 1e9);
    sieve segment; new_sieve(&segment, DEF_BUFSIZE, 0);

    size_t ncounts = sizeof(P_COUNTS) / sizeof(P_COUNTS[0]);
    size_t large_count = segmented_count_primes(&svr,
                                                &segment,
                                                0,
                                                P_LIMITS[ncounts - 1]);

    for (size_t i = 0; i < ncounts - 1; i++) {
        ASSERT_EQ(large_count - P_COUNTS[i],
                  segmented_count_primes(&svr, &segment,
                                         P_LIMITS[i], P_LIMITS[ncounts - 1]))
            << "failed limit: P_LIMITS[" << i << "] = "
            << std::scientific << (double)P_LIMITS[i];
    }

    free_sieve(&segment);
    free_siever(&svr);
    free_primewheel(&pw);
}
示例#6
0
TEST(SegmentedSieveTest, TrivialCasese) {
    primewheel pw; new_primewheel(&pw, 4);
    siever siever; new_siever(&siever, &pw, 1e7);
    sieve segment; new_sieve(&segment, DEF_BUFSIZE, 0);

    ASSERT_EQ(0, segmented_count_primes(&siever, &segment, 0, 0));
    ASSERT_EQ(0, segmented_count_primes(&siever, &segment, 0, 1));
    ASSERT_EQ(0, segmented_count_primes(&siever, &segment, 100, 99));

    prime *primes; size_t pcount;
    segmented_list_primes(&siever, &segment, 0, 0, &primes, &pcount);
    ASSERT_EQ(0, pcount);
    free(primes);
    segmented_list_primes(&siever, &segment, 0, 1, &primes, &pcount);
    ASSERT_EQ(0, pcount);
    free(primes);
    segmented_list_primes(&siever, &segment, 100, 99, &primes, &pcount);
    ASSERT_EQ(0, pcount);
    free(primes);

    free_siever(&siever);
    free_primewheel(&pw);
    free_sieve(&segment);
}