void check_limb (void) { int i; mp_limb_t limb; mpz_t z; char *s; check_one ("0", "%Md", CNST_LIMB(0)); check_one ("1", "%Md", CNST_LIMB(1)); /* "i" many 1 bits, tested against mpz_get_str in decimal and hex */ limb = 1; mpz_init_set_ui (z, 1L); for (i = 1; i <= GMP_LIMB_BITS; i++) { s = mpz_get_str (NULL, 10, z); check_one (s, "%Mu", limb); (*__gmp_free_func) (s, strlen (s) + 1); s = mpz_get_str (NULL, 16, z); check_one (s, "%Mx", limb); (*__gmp_free_func) (s, strlen (s) + 1); s = mpz_get_str (NULL, -16, z); check_one (s, "%MX", limb); (*__gmp_free_func) (s, strlen (s) + 1); limb = 2*limb + 1; mpz_mul_2exp (z, z, 1L); mpz_add_ui (z, z, 1L); } mpz_clear (z); }
void check_all (mpz_ptr a, unsigned long d) { check_one (a, d); mpz_neg (a, a); check_one (a, d); }
/* Comparing 1 and 1+2^-n. "y" is volatile to make gcc store and fetch it, which forces it to a 64-bit double, whereas on x86 it would otherwise remain on the float stack as an 80-bit long double. */ void check_one_2exp (void) { double e; mpz_t x; volatile double y; int i; mpz_init (x); e = 1.0; for (i = 0; i < 128; i++) { e /= 2.0; y = 1.0 + e; if (y == 1.0) break; mpz_set_ui (x, 1L); check_one ("check_one_2exp", x, y, -1, -1); check_one ("check_one_2exp", x, -y, 1, -1); mpz_set_si (x, -1L); check_one ("check_one_2exp", x, y, -1, -1); check_one ("check_one_2exp", x, -y, 1, -1); } mpz_clear (x); }
static void check_pn (mpz_ptr n, int want) { check_one (n, want); mpz_neg (n, n); check_one (n, want); }
void test_all(expected_results const& results) { double const d2r = bg::math::d2r<double>(); double lon1r = results.p1.lon * d2r; double lat1r = results.p1.lat * d2r; double lon2r = results.p2.lon * d2r; double lat2r = results.p2.lat * d2r; if(lon1r > lon2r) { std::swap(lon1r, lon2r); std::swap(lat1r, lat2r); } double res_an = test_vrt_lon_geo<bg::formula::andoyer_inverse> (lon1r, lat1r, lon2r, lat2r); double res_th = test_vrt_lon_geo<bg::formula::thomas_inverse> (lon1r, lat1r, lon2r, lat2r); double res_vi = test_vrt_lon_geo<bg::formula::vincenty_inverse> (lon1r, lat1r, lon2r, lat2r); double res_sh = test_vrt_lon_sph(lon1r, lat1r, lon2r, lat2r); bg::math::normalize_longitude<bg::radian, double>(res_an); bg::math::normalize_longitude<bg::radian, double>(res_th); bg::math::normalize_longitude<bg::radian, double>(res_vi); bg::math::normalize_longitude<bg::radian, double>(res_sh); check_one(res_an, results.andoyer * d2r, res_vi, 0.001); check_one(res_th, results.thomas * d2r, res_vi, 0.01);//in some tests thomas gives low accuracy check_one(res_vi, results.vincenty * d2r, res_vi, 0.0000001); check_one(res_sh, results.spherical * d2r, res_vi, 1); }
/* Equality of integers with up to 53 bits */ void check_onebits (void) { mpz_t x, x2; double y; int i; mpz_init_set_ui (x, 0L); mpz_init (x2); for (i = 0; i < 512; i++) { mpz_mul_2exp (x, x, 1); mpz_add_ui (x, x, 1L); y = mpz_get_d (x); mpz_set_d (x2, y); /* stop if any truncation is occurring */ if (mpz_cmp (x, x2) != 0) break; check_one ("check_onebits", x, y, 0, 0); check_one ("check_onebits", x, -y, 1, 0); mpz_neg (x, x); check_one ("check_onebits", x, y, -1, 0); check_one ("check_onebits", x, -y, 0, 0); mpz_neg (x, x); } mpz_clear (x); mpz_clear (x2); }
void check_rand (void) { unsigned long min_prec = __GMPF_BITS_TO_PREC (1); gmp_randstate_t rands; mpf_t got, u; unsigned long prec, v; int i; /* The nails code in mpf_mul_ui currently isn't exact, so suppress these tests for now. */ if (BITS_PER_UI > GMP_NUMB_BITS) return; mpf_init (got); mpf_init (u); gmp_randinit_default(rands); for (i = 0; i < 200; i++) { /* got precision */ prec = min_prec + gmp_urandomm_ui (rands, 15L); refmpf_set_prec_limbs (got, prec); /* u precision */ prec = min_prec + gmp_urandomm_ui (rands, 15L); refmpf_set_prec_limbs (u, prec); /* u, possibly negative */ mpf_rrandomb (u, rands, PREC(u), (mp_exp_t) 20); if (gmp_urandomb_ui (rands, 1L)) mpf_neg (u, u); /* v, 0 to BITS_PER_ULONG bits (inclusive) */ prec = gmp_urandomm_ui (rands, BITS_PER_ULONG+1); v = gmp_urandomb_ui (rands, prec); if ((i % 2) == 0) { /* separate */ mpf_mul_ui (got, u, v); check_one ("separate", got, u, v); } else { /* overlap */ prec = refmpf_set_overlap (got, u); mpf_mul_ui (got, got, v); check_one ("overlap src==dst", got, u, v); mpf_set_prec_raw (got, prec); } } mpf_clear (got); mpf_clear (u); gmp_randclear(rands); }
void check_random (int argc, char *argv[]) { gmp_randstate_ptr rands = RANDS; mpz_t a, c, d, ra, rc; int i; int want; int reps = 50000; if (argc >= 2) reps = atoi (argv[1]); mpz_init (a); mpz_init (c); mpz_init (d); mpz_init (ra); mpz_init (rc); for (i = 0; i < reps; i++) { mpz_errandomb (a, rands, 8*BITS_PER_MP_LIMB); MPZ_CHECK_FORMAT (a); mpz_errandomb (c, rands, 8*BITS_PER_MP_LIMB); MPZ_CHECK_FORMAT (c); mpz_errandomb_nonzero (d, rands, 8*BITS_PER_MP_LIMB); mpz_negrandom (a, rands); MPZ_CHECK_FORMAT (a); mpz_negrandom (c, rands); MPZ_CHECK_FORMAT (c); mpz_negrandom (d, rands); mpz_fdiv_r (ra, a, d); mpz_fdiv_r (rc, c, d); want = (mpz_cmp (ra, rc) == 0); check_one (a, c, d, want); mpz_sub (ra, ra, rc); mpz_sub (a, a, ra); MPZ_CHECK_FORMAT (a); check_one (a, c, d, 1); if (! mpz_pow2abs_p (d)) { refmpz_combit (a, urandom() % (8*BITS_PER_MP_LIMB)); check_one (a, c, d, 0); } } mpz_clear (a); mpz_clear (c); mpz_clear (d); mpz_clear (ra); mpz_clear (rc); }
void check_all (mpq_srcptr q, int base, const char *want) { char *s; check_one (q, base, want); s = __gmp_allocate_strdup (want); strtoupper (s); check_one (q, -base, s); (*__gmp_free_func) (s, strlen(s)+1); }
void check_all (mpq_ptr x, mpq_ptr y, int want) { check_one (x, y, want); check_one (y, x, want); mpq_neg (x, x); mpq_neg (y, y); check_one (x, y, want); check_one (y, x, want); }
void check_all (mpz_ptr x, mpz_ptr y, int want_cmp, int want_cmpabs) { check_one (x, y, want_cmp, want_cmpabs); check_one (y, x, -want_cmp, -want_cmpabs); mpz_neg (x, x); mpz_neg (y, y); want_cmp = -want_cmp; check_one (x, y, want_cmp, want_cmpabs); check_one (y, x, -want_cmp, -want_cmpabs); }
static void check_primes (void) { static const char * const primes[] = { "2", "17", "65537", /* diffie-hellman-group1-sha1, also "Well known group 2" in RFC 2412, 2^1024 - 2^960 - 1 + 2^64 * { [2^894 pi] + 129093 } */ "0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" "FFFFFFFFFFFFFFFF", NULL }; mpz_t n; int i; mpz_init (n); for (i = 0; primes[i]; i++) { mpz_set_str_or_abort (n, primes[i], 0); check_one (n, 1); } mpz_clear (n); }
void check_all (mpz_ptr w, mpz_ptr x, mpz_ptr y) { int swap, wneg, xneg, yneg; MPZ_CHECK_FORMAT (w); MPZ_CHECK_FORMAT (x); MPZ_CHECK_FORMAT (y); for (swap = 0; swap < 2; swap++) { for (wneg = 0; wneg < 2; wneg++) { for (xneg = 0; xneg < 2; xneg++) { for (yneg = 0; yneg < 2; yneg++) { check_one (w, x, y); if (mpz_fits_ulong_p (y)) check_one_ui (w, x, mpz_get_ui (y)); mpz_neg (y, y); } mpz_neg (x, x); } mpz_neg (w, w); } mpz_swap (x, y); } }
void check_various (void) { mpf_t got, u, v; mpf_init (got); mpf_init (u); mpf_init (v); /* 100/4 == 25 */ mpf_set_prec (got, 20L); mpf_set_ui (u, 100L); mpf_set_ui (v, 4L); mpf_div (got, u, v); MPF_CHECK_FORMAT (got); ASSERT_ALWAYS (mpf_cmp_ui (got, 25L) == 0); /* 1/(2^n+1), a case where truncating the divisor would be wrong */ mpf_set_prec (got, 500L); mpf_set_prec (v, 900L); mpf_set_ui (v, 1L); mpf_mul_2exp (v, v, 800L); mpf_add_ui (v, v, 1L); mpf_div (got, u, v); check_one ("1/2^n+1, separate", got, u, v); mpf_clear (got); mpf_clear (u); mpf_clear (v); }
void check_various (void) { mpf_t got; mpq_t q; mpf_init (got); mpq_init (q); /* 1/1 == 1 */ mpf_set_prec (got, 20L); mpq_set_ui (q, 1L, 1L); mpf_set_q (got, q); MPF_CHECK_FORMAT (got); ASSERT_ALWAYS (mpf_cmp_ui (got, 1L) == 0); /* 1/(2^n+1), a case where truncating the divisor would be wrong */ mpf_set_prec (got, 500L); mpq_set_ui (q, 1L, 1L); mpz_mul_2exp (mpq_denref(q), mpq_denref(q), 800L); mpz_add_ui (mpq_denref(q), mpq_denref(q), 1L); check_one (got, q); mpf_clear (got); mpq_clear (q); }
void check_all (mpf_ptr src, mpf_ptr trunc, mpf_ptr ceil, mpf_ptr floor) { /* some of these values are generated with direct field assignments */ MPF_CHECK_FORMAT (src); MPF_CHECK_FORMAT (trunc); MPF_CHECK_FORMAT (ceil); MPF_CHECK_FORMAT (floor); check_one (src, trunc, ceil, floor); mpf_neg (src, src); mpf_neg (trunc, trunc); mpf_neg (ceil, ceil); mpf_neg (floor, floor); check_one (src, trunc, floor, ceil); }
void check_data (void) { static const struct { const char *a; const char *c; const char *d; int want; } data[] = { /* strict equality mod 0 */ { "0", "0", "0", 1 }, { "11", "11", "0", 1 }, { "3", "11", "0", 0 }, /* anything congruent mod 1 */ { "0", "0", "1", 1 }, { "1", "0", "1", 1 }, { "0", "1", "1", 1 }, { "123", "456", "1", 1 }, { "0x123456789123456789", "0x987654321987654321", "1", 1 }, /* csize==1, dsize==2 changing to 1 after stripping 2s */ { "0x3333333333333333", "0x33333333", "0x180000000", 1 }, { "0x33333333333333333333333333333333", "0x3333333333333333", "0x18000000000000000", 1 }, /* another dsize==2 becoming 1, with opposite signs this time */ { "0x444444441", "-0x22222221F", "0x333333330", 1 }, { "0x44444444444444441", "-0x2222222222222221F", "0x33333333333333330", 1 }, }; mpz_t a, c, d; int i; mpz_init (a); mpz_init (c); mpz_init (d); for (i = 0; i < numberof (data); i++) { mpz_set_str_or_abort (a, data[i].a, 0); mpz_set_str_or_abort (c, data[i].c, 0); mpz_set_str_or_abort (d, data[i].d, 0); check_one (a, c, d, data[i].want); } mpz_clear (a); mpz_clear (c); mpz_clear (d); }
int main (int argc, char **argv) { mpz_t x2; mpz_t root1; mp_size_t x2_size; int i; int reps = 5000; unsigned long nth; gmp_randstate_ptr rands; mpz_t bs; unsigned long bsi, size_range; tests_start (); rands = RANDS; mpz_init (bs); if (argc == 2) reps = atoi (argv[1]); mpz_init (x2); mpz_init (root1); /* This triggers a gcc 4.3.2 bug */ mpz_set_str (x2, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000000000000002", 16); mpz_root (root1, x2, 2); check_one (root1, x2, 2, -1); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 12 + 2; mpz_urandomb (bs, rands, size_range); x2_size = mpz_get_ui (bs) + 10; mpz_rrandomb (x2, rands, x2_size); mpz_urandomb (bs, rands, 15); nth = mpz_getlimbn (bs, 0) % mpz_sizeinbase (x2, 2) + 2; mpz_root (root1, x2, nth); mpz_urandomb (bs, rands, 4); bsi = mpz_get_ui (bs); if ((bsi & 1) != 0) { /* With 50% probability, set x2 near a perfect power. */ mpz_pow_ui (x2, root1, nth); if ((bsi & 2) != 0) { mpz_sub_ui (x2, x2, bsi >> 2); mpz_abs (x2, x2); } else
t_fillist *check_and_set(char *buf, t_fillist *all, t_fillist *lst) { if (check_one(buf)) { lst = new_fillist_elem(lst, buf); if (check_two(all, lst)) return (lst); } error(); return (NULL); }
static void check (void) { mpz_t z; mpz_init (z); mpz_set_ui (z, 0L); check_one (z); mpz_set_si (z, 123L); check_one (z); mpz_rrandomb (z, RANDS, 2*BITS_PER_MP_LIMB); check_one (z); mpz_rrandomb (z, RANDS, 5*BITS_PER_MP_LIMB); check_one (z); mpz_clear (z); }
static void check (void) { mpz_t z; mpz_init (z); mpz_set_ui (z, 0L); check_one (z); mpz_set_si (z, 123L); check_one (z); mpz_rrandomb (z, RANDS, 2*GMP_NUMB_BITS); check_one (z); mpz_rrandomb (z, RANDS, 5*GMP_NUMB_BITS); check_one (z); mpz_clear (z); }
void check_infinity (void) { mpz_t x; double y = HUGE_VAL; if (y != 2*y) return; mpz_init (x); /* 0 cmp inf */ mpz_set_ui (x, 0L); check_one ("check_infinity", x, y, -1, -1); check_one ("check_infinity", x, -y, 1, -1); /* 123 cmp inf */ mpz_set_ui (x, 123L); check_one ("check_infinity", x, y, -1, -1); check_one ("check_infinity", x, -y, 1, -1); /* -123 cmp inf */ mpz_set_si (x, -123L); check_one ("check_infinity", x, y, -1, -1); check_one ("check_infinity", x, -y, 1, -1); /* 2^5000 cmp inf */ mpz_set_ui (x, 1L); mpz_mul_2exp (x, x, 5000L); check_one ("check_infinity", x, y, -1, -1); check_one ("check_infinity", x, -y, 1, -1); /* -2^5000 cmp inf */ mpz_neg (x, x); check_one ("check_infinity", x, y, -1, -1); check_one ("check_infinity", x, -y, 1, -1); mpz_clear (x); }
void check_random (int reps) { gmp_randstate_t rands; mpz_t a, d, r; int i; int want; gmp_randinit_default(rands); mpz_init (a); mpz_init (d); mpz_init (r); for (i = 0; i < reps; i++) { mpz_erandomb (a, rands, 512); mpz_erandomb_nonzero (d, rands, 512); mpz_fdiv_r (r, a, d); want = (mpz_sgn (r) == 0); check_one (a, d, want); mpz_sub (a, a, r); check_one (a, d, 1); if (mpz_cmpabs_ui (d, 1L) == 0) continue; mpz_add_ui (a, a, 1L); check_one (a, d, 0); } mpz_clear (a); mpz_clear (d); mpz_clear (r); gmp_randclear(rands); }
void check_infinity (void) { mpf_t x; double y = tests_infinity_d (); if (y == 0.0) return; mpf_init (x); /* 0 cmp inf */ mpf_set_ui (x, 0L); check_one ("check_infinity", x, y, -1); check_one ("check_infinity", x, -y, 1); /* 123 cmp inf */ mpf_set_ui (x, 123L); check_one ("check_infinity", x, y, -1); check_one ("check_infinity", x, -y, 1); /* -123 cmp inf */ mpf_set_si (x, -123L); check_one ("check_infinity", x, y, -1); check_one ("check_infinity", x, -y, 1); /* 2^5000 cmp inf */ mpf_set_ui (x, 1L); mpf_mul_2exp (x, x, 5000L); check_one ("check_infinity", x, y, -1); check_one ("check_infinity", x, -y, 1); /* -2^5000 cmp inf */ mpf_neg (x, x); check_one ("check_infinity", x, y, -1); check_one ("check_infinity", x, -y, 1); mpf_clear (x); }
void check_various (void) {gmp_randstate_t rands; mpz_t z; mpz_init (z); gmp_randinit_default(rands); mpz_set_ui (z, 0L); check_one (z); mpz_set_si (z, 123L); check_one (z); mpz_rrandomb (z, rands, 2*BITS_PER_MP_LIMB); check_one (z); mpz_rrandomb (z, rands, 5*BITS_PER_MP_LIMB); check_one (z); mpz_clear (z); gmp_randclear(rands); }
void check_data (void) { static const struct { const char *a; const char *d; int want; } data[] = { { "0", "0", 1 }, { "17", "0", 0 }, { "0", "1", 1 }, { "123", "1", 1 }, { "-123", "1", 1 }, { "0", "2", 1 }, { "1", "2", 0 }, { "2", "2", 1 }, { "-2", "2", 1 }, { "0x100000000000000000000000000000000", "2", 1 }, { "0x100000000000000000000000000000001", "2", 0 }, { "0x3333333333333333", "3", 1 }, { "0x3333333333333332", "3", 0 }, { "0x33333333333333333333333333333333", "3", 1 }, { "0x33333333333333333333333333333332", "3", 0 }, /* divisor changes from 2 to 1 limb after stripping 2s */ { "0x3333333300000000", "0x180000000", 1 }, { "0x33333333333333330000000000000000", "0x18000000000000000", 1 }, { "0x133333333333333330000000000000000", "0x18000000000000000", 0 }, }; mpz_t a, d; int i; mpz_init (a); mpz_init (d); for (i = 0; i < numberof (data); i++) { mpz_set_str_or_abort (a, data[i].a, 0); mpz_set_str_or_abort (d, data[i].d, 0); check_one (a, d, data[i].want); } mpz_clear (a); mpz_clear (d); }
void check_random (int reps) { gmp_randstate_ptr rands = RANDS; mpz_t a, d, r; int i; int want; mpz_init (a); mpz_init (d); mpz_init (r); for (i = 0; i < reps; i++) { mpz_erandomb (a, rands, 1 << 19); mpz_erandomb_nonzero (d, rands, 1 << 18); mpz_fdiv_r (r, a, d); want = (mpz_sgn (r) == 0); check_one (a, d, want); mpz_sub (a, a, r); check_one (a, d, 1); if (mpz_cmpabs_ui (d, 1L) == 0) continue; mpz_add_ui (a, a, 1L); check_one (a, d, 0); } mpz_clear (a); mpz_clear (d); mpz_clear (r); }
int main() { Local location = create_l(N); check(location); printf("\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); check_one(location); printf("\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); delete_step(2, location ); check(location); printf("\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); }
int chess_vic(void) { int i = 0; int j = 0; int res = 0; for(j = 0; j < BOARD_Y; j++){ for(i = 0; i < BOARD_X; i++){ res = check_one(i, j); if(res != 0){ return res; } } } return 0; }
void check_base (mpz_srcptr base) { unsigned long exp; mpz_t want; mpz_init (want); mpz_set_ui (want, 1L); for (exp = 0; exp < 20; exp++) { check_one (want, base, exp); mpz_mul (want, want, base); } mpz_clear (want); }