void check_one (mpz_srcptr want, int fail, int base, const char *str) { mpz_t got; MPZ_CHECK_FORMAT (want); mp_trace_base = (base == 0 ? 16 : base); mpz_init (got); if (mpz_set_str (got, str, base) != fail) { printf ("mpz_set_str unexpectedly failed\n"); printf (" base %d\n", base); printf (" str \"%s\"\n", str); abort (); } MPZ_CHECK_FORMAT (got); if (fail == 0 && mpz_cmp (got, want) != 0) { printf ("mpz_set_str wrong\n"); printf (" base %d\n", base); printf (" str \"%s\"\n", str); mpz_trace ("got ", got); mpz_trace ("want", want); abort (); } mpz_clear (got); }
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_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_sequence (int argc, char *argv[]) { unsigned long n; unsigned long limit = 100 * BITS_PER_MP_LIMB; mpz_t want_ln, want_ln1, got_ln, got_ln1; if (argc > 1 && argv[1][0] == 'x') limit = ULONG_MAX; else if (argc > 1) limit = atoi (argv[1]); /* start at n==0 */ mpz_init_set_si (want_ln1, -1); /* L[-1] */ mpz_init_set_ui (want_ln, 2); /* L[0] */ mpz_init (got_ln); mpz_init (got_ln1); for (n = 0; n < limit; n++) { mpz_lucnum2_ui (got_ln, got_ln1, n); MPZ_CHECK_FORMAT (got_ln); MPZ_CHECK_FORMAT (got_ln1); if (mpz_cmp (got_ln, want_ln) != 0 || mpz_cmp (got_ln1, want_ln1) != 0) { printf ("mpz_lucnum2_ui(%lu) wrong\n", n); mpz_trace ("want ln ", want_ln); mpz_trace ("got ln ", got_ln); mpz_trace ("want ln1", want_ln1); mpz_trace ("got ln1", got_ln1); abort (); } mpz_lucnum_ui (got_ln, n); MPZ_CHECK_FORMAT (got_ln); if (mpz_cmp (got_ln, want_ln) != 0) { printf ("mpz_lucnum_ui(%lu) wrong\n", n); mpz_trace ("want ln", want_ln); mpz_trace ("got ln", got_ln); abort (); } mpz_add (want_ln1, want_ln1, want_ln); /* L[n+1] = L[n] + L[n-1] */ mpz_swap (want_ln1, want_ln); } mpz_clear (want_ln); mpz_clear (want_ln1); mpz_clear (got_ln); mpz_clear (got_ln1); }
void check_data (void) { static const struct { double d; mp_size_t want_size; mp_limb_t want_data[2]; } data[] = { { 0.0, 0 }, { 1.0, 1, { 1 } }, { -1.0, -1, { 1 } }, { 123.0, 1, { 123 } }, { -123.0, -1, { 123 } }, }; mpz_t z; int i; for (i = 0; i < numberof (data); i++) { mpz_init (z); mpz_set_d (z, data[i].d); MPZ_CHECK_FORMAT (z); if (z->_mp_size != data[i].want_size || refmpn_cmp_allowzero (z->_mp_d, data[i].want_data, ABS (data[i].want_size)) != 0) { printf ("mpz_set_d wrong on data[%d]\n", i); bad: d_trace (" d ", data[i].d); printf (" got size %ld\n", (long) z->_mp_size); printf (" want size %ld\n", (long) data[i].want_size); mpn_trace (" got z", z->_mp_d, z->_mp_size); mpn_trace (" want z", data[i].want_data, data[i].want_size); abort(); } mpz_clear (z); mpz_init_set_d (z, data[i].d); MPZ_CHECK_FORMAT (z); if (z->_mp_size != data[i].want_size || refmpn_cmp_allowzero (z->_mp_d, data[i].want_data, ABS (data[i].want_size)) != 0) { printf ("mpz_init_set_d wrong on data[%d]\n", i); goto bad; } mpz_clear (z); } }
void check_single (void) { mpz_t x; int limb, offset, initial; unsigned long bit; mpz_init (x); for (limb = 0; limb < 4; limb++) { for (offset = (limb==0 ? 0 : -2); offset <= 2; offset++) { for (initial = 0; initial >= -1; initial--) { mpz_set_si (x, (long) initial); bit = (unsigned long) limb*BITS_PER_MP_LIMB + offset; mpz_clrbit (x, bit); MPZ_CHECK_FORMAT (x); if (mpz_tstbit (x, bit) != 0) { printf ("check_single(): expected 0\n"); abort (); } mpz_setbit (x, bit); MPZ_CHECK_FORMAT (x); if (mpz_tstbit (x, bit) != 1) { printf ("check_single(): expected 0\n"); abort (); } mpz_clrbit (x, bit); MPZ_CHECK_FORMAT (x); if (mpz_tstbit (x, bit) != 0) { printf ("check_single(): expected 0\n"); abort (); } } } } mpz_clear (x); }
void check_one_ui (mpz_ptr w, mpz_ptr x, unsigned long y) { mpz_t want, got; mpz_init (want); mpz_init (got); mpz_mul_ui (want, x, (unsigned long) y); mpz_add (want, w, want); mpz_set (got, w); mpz_addmul_ui (got, x, (unsigned long) y); MPZ_CHECK_FORMAT (got); if (mpz_cmp (want, got) != 0) { printf ("mpz_addmul_ui fail\n"); fail: mpz_trace ("w", w); mpz_trace ("x", x); printf ("y=0x%lX %lu\n", y, y); mpz_trace ("want", want); mpz_trace ("got ", got); abort (); } mpz_mul_ui (want, x, y); mpz_sub (want, w, want); mpz_set (got, w); mpz_submul_ui (got, x, y); MPZ_CHECK_FORMAT (got); if (mpz_cmp (want, got) != 0) { printf ("mpz_submul_ui fail\n"); goto fail; } mpz_clear (want); mpz_clear (got); }
void check_all_inplace (mpz_ptr w, mpz_ptr y) { int wneg, yneg; MPZ_CHECK_FORMAT (w); MPZ_CHECK_FORMAT (y); for (wneg = 0; wneg < 2; wneg++) { for (yneg = 0; yneg < 2; yneg++) { check_one_inplace (w, y); if (mpz_fits_ulong_p (y)) check_one_ui_inplace (w, mpz_get_ui (y)); mpz_neg (y, y); } mpz_neg (w, w); } }
void check_one (mpz_srcptr w, mpz_srcptr x, mpz_srcptr y) { mpz_t want, got; mpz_init (want); mpz_init (got); mpz_mul (want, x, y); mpz_add (want, w, want); mpz_set (got, w); mpz_addmul (got, x, y); MPZ_CHECK_FORMAT (got); if (mpz_cmp (want, got) != 0) { printf ("mpz_addmul fail\n"); fail: mpz_trace ("w", w); mpz_trace ("x", x); mpz_trace ("y", y); mpz_trace ("want", want); mpz_trace ("got ", got); abort (); } mpz_mul (want, x, y); mpz_sub (want, w, want); mpz_set (got, w); mpz_submul (got, x, y); MPZ_CHECK_FORMAT (got); if (mpz_cmp (want, got) != 0) { printf ("mpz_submul fail\n"); goto fail; } mpz_clear (want); mpz_clear (got); }
/* exercise the case where mpz_clrbit or mpz_combit ends up extending a value like -2^(k*GMP_NUMB_BITS-1) when clearing bit k*GMP_NUMB_BITS-1. */ void check_clr_extend (void) { mpz_t got, want; unsigned long i; int f; mpz_init (got); mpz_init (want); for (i = 1; i < 5; i++) { for (f = 0; f <= 1; f++) { /* lots of 1 bits in _mp_d */ mpz_set_ui (got, 1L); mpz_mul_2exp (got, got, 10*GMP_NUMB_BITS); mpz_sub_ui (got, got, 1L); /* value -2^(n-1) representing ..11100..00 */ mpz_set_si (got, -1L); mpz_mul_2exp (got, got, i*GMP_NUMB_BITS-1); /* complement bit n, giving ..11000..00 which is -2^n */ if (f == 0) mpz_clrbit (got, i*GMP_NUMB_BITS-1); else mpz_combit (got, i*GMP_NUMB_BITS-1); MPZ_CHECK_FORMAT (got); mpz_set_si (want, -1L); mpz_mul_2exp (want, want, i*GMP_NUMB_BITS); if (mpz_cmp (got, want) != 0) { if (f == 0) printf ("mpz_clrbit: "); else printf ("mpz_combit: "); printf ("wrong after extension\n"); mpz_trace ("got ", got); mpz_trace ("want", want); abort (); } } } mpz_clear (got); mpz_clear (want); }
void check_one (mpz_srcptr want, mpz_srcptr base, unsigned long exp) { mpz_t got; mpz_init (got); MPZ_CHECK_FORMAT (want); mpz_pow_ui (got, base, exp); if (mpz_cmp (got, want)) { printf ("mpz_pow_ui wrong\n"); mpz_trace (" base", base); printf (" exp = %lu (0x%lX)\n", exp, exp); mpz_trace (" got ", got); mpz_trace (" want", want); abort (); } mpz_set (got, base); mpz_pow_ui (got, got, exp); if (mpz_cmp (got, want)) { printf ("mpz_pow_ui wrong\n"); mpz_trace (" base", base); printf (" exp = %lu (0x%lX)\n", exp, exp); mpz_trace (" got ", got); mpz_trace (" want", want); abort (); } if (mpz_fits_ulong_p (base)) { unsigned long base_u = mpz_get_ui (base); mpz_ui_pow_ui (got, base_u, exp); if (mpz_cmp (got, want)) { printf ("mpz_ui_pow_ui wrong\n"); printf (" base=%lu (0x%lX)\n", base_u, base_u); printf (" exp = %lu (0x%lX)\n", exp, exp); mpz_trace (" got ", got); mpz_trace (" want", want); abort (); } } mpz_clear (got); }
int main (int argc, char *argv[]) { unsigned long n; unsigned long limit = 2222; mpz_t f, r; tests_start (); if (argc > 1 && argv[1][0] == 'x') limit = ULONG_MAX; else if (argc > 1) limit = atoi (argv[1]); /* for small limb testing */ limit = MIN (limit, MP_LIMB_T_MAX); mpz_init_set_ui (f, 1); /* 0# = 1 */ mpz_init (r); for (n = 0; n < limit; n++) { mpz_primorial_ui (r, n); MPZ_CHECK_FORMAT (r); if (mpz_cmp (f, r) != 0) { printf ("mpz_primorial_ui(%lu) wrong\n", n); printf (" got "); mpz_out_str (stdout, 10, r); printf("\n"); printf (" want "); mpz_out_str (stdout, 10, f); printf("\n"); abort (); } if (isprime (n+1)) mpz_mul_ui (f, f, n+1); /* p# = (p-1)# * (p) */ } mpz_clear (f); mpz_clear (r); tests_end (); exit (0); }
void check_data (void) { static const struct { const char *a; const char *b; const char *want; } data[] = { /* This tickled a bug in gmp 4.1.2 mpn/x86/k6/gcd_finda.asm. */ { "0x3FFC000007FFFFFFFFFF00000000003F83FFFFFFFFFFFFFFF80000000000000001", "0x1FFE0007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC000000000000000000000001", "5" } }; mpz_t a, b, got, want; int i; mpz_init (a); mpz_init (b); mpz_init (got); mpz_init (want); for (i = 0; i < numberof (data); i++) { mpz_set_str_or_abort (a, data[i].a, 0); mpz_set_str_or_abort (b, data[i].b, 0); mpz_set_str_or_abort (want, data[i].want, 0); mpz_gcd (got, a, b); MPZ_CHECK_FORMAT (got); if (mpz_cmp (got, want) != 0) { printf ("mpz_gcd wrong on data[%d]\n", i); printf (" a %s\n", data[i].a); printf (" b %s\n", data[i].b); mpz_trace (" a", a); mpz_trace (" b", b); mpz_trace (" want", want); mpz_trace (" got ", got); abort (); } } mpz_clear (a); mpz_clear (b); mpz_clear (got); mpz_clear (want); }
void check_random (int argc, char *argv[]) { gmp_randstate_ptr rands = RANDS; int reps = 5000; mpz_t a, q, got; int i, qneg; unsigned long d; if (argc == 2) reps = atoi (argv[1]); mpz_init (a); mpz_init (q); mpz_init (got); for (i = 0; i < reps; i++) { d = (unsigned long) urandom(); mpz_erandomb (q, rands, 512); mpz_mul_ui (a, q, d); for (qneg = 0; qneg <= 1; qneg++) { mpz_divexact_ui (got, a, d); MPZ_CHECK_FORMAT (got); if (mpz_cmp (got, q) != 0) { printf ("mpz_divexact_ui wrong\n"); mpz_trace (" a", a); printf (" d=%lu\n", d); mpz_trace (" q", q); mpz_trace (" got", got); abort (); } mpz_neg (q, q); mpz_neg (a, a); } } mpz_clear (a); mpz_clear (q); mpz_clear (got); }
/* Try mpz_set_d on values 2^i+1, while such a value fits a double. */ void check_2n_plus_1 (void) { volatile double p, d, diff; mpz_t want, got; int i; mpz_init (want); mpz_init (got); p = 1.0; mpz_set_ui (want, 2L); /* gives 3 on first step */ for (i = 1; i < 500; i++) { mpz_mul_2exp (want, want, 1L); mpz_sub_ui (want, want, 1L); /* want = 2^i+1 */ p *= 2.0; /* p = 2^i */ d = p + 1.0; diff = d - p; if (diff != 1.0) break; /* rounding occurred, stop now */ mpz_set_d (got, d); MPZ_CHECK_FORMAT (got); if (mpz_cmp (got, want) != 0) { printf ("mpz_set_d wrong on 2^%d+1\n", i); d_trace (" d ", d); mpz_trace (" got ", got); mpz_trace (" want ", want); abort (); } } mpz_clear (want); mpz_clear (got); }
void check_one (mpz_srcptr z) { static const int shift[] = { 0, 1, BITS_PER_MP_LIMB, 2*BITS_PER_MP_LIMB, 5*BITS_PER_MP_LIMB }; int sh, shneg, neg; mpf_t f; mpz_t got, want; mpf_init2 (f, mpz_sizeinbase(z,2)); mpz_init (got); mpz_init (want); for (sh = 0; sh < numberof(shift); sh++) { for (shneg = 0; shneg <= 1; shneg++) { for (neg = 0; neg <= 1; neg++) { mpf_set_z (f, z); mpz_set (want, z); if (neg) { mpf_neg (f, f); mpz_neg (want, want); } if (shneg) { mpz_tdiv_q_2exp (want, want, shift[sh]); mpf_div_2exp (f, f, shift[sh]); } else { mpz_mul_2exp (want, want, shift[sh]); mpf_mul_2exp (f, f, shift[sh]); } mpz_set_f (got, f); MPZ_CHECK_FORMAT (got); if (mpz_cmp (got, want) != 0) { printf ("wrong result\n"); printf (" shift %d\n", shneg ? -shift[sh] : shift[sh]); printf (" neg %d\n", neg); mpf_trace (" f", f); mpz_trace (" got", got); mpz_trace (" want", want); abort (); } } } } mpf_clear (f); mpz_clear (got); mpz_clear (want); }
void check_one (mpz_t root1, mpz_t x2, unsigned long nth, int i) { mpz_t temp, temp2; mpz_t root2, rem2; mpz_init (root2); mpz_init (rem2); mpz_init (temp); mpz_init (temp2); MPZ_CHECK_FORMAT (root1); mpz_rootrem (root2, rem2, x2, nth); MPZ_CHECK_FORMAT (root2); MPZ_CHECK_FORMAT (rem2); mpz_pow_ui (temp, root1, nth); MPZ_CHECK_FORMAT (temp); mpz_add (temp2, temp, rem2); /* Is power of result > argument? */ if (mpz_cmp (root1, root2) != 0 || mpz_cmp (x2, temp2) != 0 || mpz_cmpabs (temp, x2) > 0) { fprintf (stderr, "ERROR after test %d\n", i); debug_mp (x2, 10); debug_mp (root1, 10); debug_mp (root2, 10); fprintf (stderr, "nth: %lu\n", nth); abort (); } if (nth > 1 && mpz_cmp_ui (temp, 1L) > 0 && ! mpz_perfect_power_p (temp)) { fprintf (stderr, "ERROR in mpz_perfect_power_p after test %d\n", i); debug_mp (temp, 10); debug_mp (root1, 10); fprintf (stderr, "nth: %lu\n", nth); abort (); } if (nth <= 10000 && mpz_sgn(x2) > 0) /* skip too expensive test */ { mpz_add_ui (temp2, root1, 1L); mpz_pow_ui (temp2, temp2, nth); MPZ_CHECK_FORMAT (temp2); /* Is square of (result + 1) <= argument? */ if (mpz_cmp (temp2, x2) <= 0) { fprintf (stderr, "ERROR after test %d\n", i); debug_mp (x2, 10); debug_mp (root1, 10); fprintf (stderr, "nth: %lu\n", nth); abort (); } } mpz_clear (root2); mpz_clear (rem2); mpz_clear (temp); mpz_clear (temp2); }
void check_data (void) { static const struct { const char *want; size_t count; int order; size_t size; int endian; int nail; char src[64]; } data[] = { { "0", 0,1, 1,1, 0 }, { "0", 1,1, 0,1, 0 }, { "0x12345678", 4,1, 1,1, 0, { '\22', '\64', '\126', '\170' } }, { "0x12345678", 1,1, 4,1, 0, { '\22', '\64', '\126', '\170' } }, { "0x12345678", 1,-1, 4,1, 0, { '\22', '\64', '\126', '\170' } }, { "0x12345678", 4,-1, 1,-1, 0, { '\170', '\126', '\064', '\22' } }, { "0x12345678", 1,1, 4,-1, 0, { '\170', '\126', '\064', '\22' } }, { "0x12345678", 1,-1, 4,-1, 0, { '\170', '\126', '\064', '\22' } }, { "0", 5,1, 1,1, 7, { '\376', '\376', '\376', '\376', '\376' } }, { "0", 5,-1, 1,1, 7, { '\376', '\376', '\376', '\376', '\376' } }, { "0x15", 5,1, 1,1, 7, { '\377', '\376', '\377', '\376', '\377' } }, { "0", 3,1, 2,1, 1, { '\200','\000', '\200','\000', '\200','\000' }}, { "0", 3,1, 2,-1, 1, { '\000','\200', '\000','\200', '\000','\200' }}, { "0", 3,1, 2,1, 15, { '\377','\376', '\377','\376', '\377','\376' }}, { "0x2A", 3,1, 2,1, 14, { '\377','\376', '\377','\376', '\377','\376' } }, { "0x06", 3,1, 2,1, 14, { '\377','\374', '\377','\375', '\377','\376' } }, { "0x24", 3,-1, 2,1, 14, { '\377','\374', '\377','\375', '\377','\376' } }, { "0x123456789ABC", 3,1, 2,1, 0, { '\022','\064', '\126','\170', '\232','\274' } }, { "0x123456789ABC", 3,-1, 2,1, 0, { '\232','\274', '\126','\170', '\022','\064' } }, { "0x123456789ABC", 3,1, 2,-1, 0, { '\064','\022', '\170','\126', '\274','\232' } }, { "0x123456789ABC", 3,-1, 2,-1, 0, { '\274','\232', '\170','\126', '\064','\022' } }, { "0x112233445566778899AABBCC", 3,1, 4,1, 0, { '\021','\042','\063','\104', '\125','\146','\167','\210', '\231','\252','\273','\314' } }, { "0x112233445566778899AABBCC", 3,-1, 4,1, 0, { '\231','\252','\273','\314', '\125','\146','\167','\210', '\021','\042','\063','\104' } }, { "0x112233445566778899AABBCC", 3,1, 4,-1, 0, { '\104','\063','\042','\021', '\210','\167','\146','\125', '\314','\273','\252','\231' } }, { "0x112233445566778899AABBCC", 3,-1, 4,-1, 0, { '\314','\273','\252','\231', '\210','\167','\146','\125', '\104','\063','\042','\021' } }, { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,1, 0, { '\020','\001','\040','\002','\060','\003','\100','\004', '\120','\005','\140','\006','\160','\007','\200','\010', '\220','\011','\240','\012','\260','\013','\300','\014' } }, { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,1, 0, { '\220','\011','\240','\012','\260','\013','\300','\014', '\120','\005','\140','\006','\160','\007','\200','\010', '\020','\001','\040','\002','\060','\003','\100','\004' } }, { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,-1, 0, { '\004','\100','\003','\060','\002','\040','\001','\020', '\010','\200','\007','\160','\006','\140','\005','\120', '\014','\300','\013','\260','\012','\240','\011','\220' } }, { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,-1, 0, { '\014','\300','\013','\260','\012','\240','\011','\220', '\010','\200','\007','\160','\006','\140','\005','\120', '\004','\100','\003','\060','\002','\040','\001','\020' } }, { "0x155555555555555555555555", 3,1, 4,1, 1, { '\325','\125','\125','\125', '\252','\252','\252','\252', '\325','\125','\125','\125' } }, { "0x155555555555555555555555", 3,-1, 4,1, 1, { '\325','\125','\125','\125', '\252','\252','\252','\252', '\325','\125','\125','\125' } }, { "0x155555555555555555555555", 3,1, 4,-1, 1, { '\125','\125','\125','\325', '\252','\252','\252','\252', '\125','\125','\125','\325' } }, { "0x155555555555555555555555", 3,-1, 4,-1, 1, { '\125','\125','\125','\325', '\252','\252','\252','\252', '\125','\125','\125','\325' } }, }; char buf[sizeof(data[0].src) + sizeof (mp_limb_t)]; char *src; size_t align; int i; mpz_t got, want; mpz_init (got); mpz_init (want); for (i = 0; i < numberof (data); i++) { for (align = 0; align < sizeof (mp_limb_t); align++) { mpz_set_str_or_abort (want, data[i].want, 0); src = buf + align; memcpy (src, data[i].src, data[i].count * data[i].size); mpz_set_ui (got, 0L); mpz_import (got, data[i].count, data[i].order, data[i].size, data[i].endian, data[i].nail, src); MPZ_CHECK_FORMAT (got); if (mpz_cmp (got, want) != 0) { printf ("wrong at data[%d]\n", i); printf (" count=%lu order=%d size=%lu endian=%d nail=%u align=%lu\n", (unsigned long) data[i].count, data[i].order, (unsigned long) data[i].size, data[i].endian, data[i].nail, (unsigned long) align); mpz_trace (" got ", got); mpz_trace (" want", want); abort (); } } } mpz_clear (got); mpz_clear (want); }
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 = 10000; mpz_t bs; unsigned long size_range, size; if (argc >= 2) reps = atoi (argv[1]); mpz_init (bs); mpz_init (a); mpz_init (c); mpz_init (d); mpz_init (ra); mpz_init (rc); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 16 + 1; /* 0..65536 bit operands */ mpz_urandomb (bs, rands, size_range); size = mpz_get_ui (bs); mpz_rrandomb (a, rands, size); mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 16 + 1; /* 0..65536 bit operands */ mpz_urandomb (bs, rands, size_range); size = mpz_get_ui (bs); mpz_rrandomb (c, rands, size); do { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 16 + 1; /* 0..65536 bit operands */ mpz_urandomb (bs, rands, size_range); size = mpz_get_ui (bs); mpz_rrandomb (d, rands, size); } while (SIZ(d) == 0); 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*GMP_LIMB_BITS)); check_one (a, c, d, 0); } } mpz_clear (bs); mpz_clear (a); mpz_clear (c); mpz_clear (d); mpz_clear (ra); mpz_clear (rc); }
int main (int argc, char **argv) { mpz_t base, exp, mod; mpz_t r1, r2, base2; mp_size_t base_size, exp_size, mod_size; unsigned long int exp2; int i; int reps = 100; 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 (base); mpz_init (exp); mpz_init (mod); mpz_init (r1); mpz_init (r2); mpz_init (base2); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 18 + 2; do /* Loop until mathematically well-defined. */ { mpz_urandomb (bs, rands, size_range); base_size = mpz_get_ui (bs); mpz_rrandomb (base, rands, base_size); mpz_urandomb (bs, rands, 6L); exp_size = mpz_get_ui (bs); mpz_rrandomb (exp, rands, exp_size); exp2 = mpz_getlimbn (exp, (mp_size_t) 0); } while (mpz_cmp_ui (base, 0) == 0 && exp2 == 0); do { mpz_urandomb (bs, rands, size_range); mod_size = mpz_get_ui (bs); mpz_rrandomb (mod, rands, mod_size); } while (mpz_cmp_ui (mod, 0) == 0); mpz_urandomb (bs, rands, 2); bsi = mpz_get_ui (bs); if ((bsi & 1) != 0) mpz_neg (base, base); /* printf ("%ld %ld\n", SIZ (base), SIZ (mod)); */ #if 0 putc ('\n', stderr); debug_mp (base, -16); debug_mp (mod, -16); #endif mpz_powm_ui (r1, base, exp2, mod); MPZ_CHECK_FORMAT (r1); mpz_set_ui (r2, 1); mpz_set (base2, base); mpz_mod (r2, r2, mod); /* needed when exp==0 and mod==1 */ while (exp2 != 0) { if (exp2 % 2 != 0) { mpz_mul (r2, r2, base2); mpz_mod (r2, r2, mod); } mpz_mul (base2, base2, base2); mpz_mod (base2, base2, mod); exp2 = exp2 / 2; } #if 0 debug_mp (r1, -16); debug_mp (r2, -16); #endif if (mpz_cmp (r1, r2) != 0) { fprintf (stderr, "\ntest %d: Incorrect results for operands:\n", i); debug_mp (base, -16); debug_mp (exp, -16); debug_mp (mod, -16); fprintf (stderr, "mpz_powm_ui result:\n"); debug_mp (r1, -16); fprintf (stderr, "reference result:\n"); debug_mp (r2, -16); abort (); } } mpz_clear (bs); mpz_clear (base); mpz_clear (exp); mpz_clear (mod); mpz_clear (r1); mpz_clear (r2); mpz_clear (base2); tests_end (); exit (0); }
void one_test (mpz_t op1, mpz_t op2, mpz_t ref, int i) { /* printf ("%d %d %d\n", SIZ (op1), SIZ (op2), ref != NULL ? SIZ (ref) : 0); fflush (stdout); */ /* fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); */ mpz_gcdext (gcd1, s, NULL, op1, op2); MPZ_CHECK_FORMAT (gcd1); MPZ_CHECK_FORMAT (s); if (ref && mpz_cmp (ref, gcd1) != 0) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpz_gcdext returned incorrect result\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "expected result:\n"); debug_mp (ref, -16); fprintf (stderr, "mpz_gcdext returns:\n");debug_mp (gcd1, -16); abort (); } if (!gcdext_valid_p(op1, op2, gcd1, s)) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpz_gcdext returned invalid result\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "mpz_gcdext returns:\n");debug_mp (gcd1, -16); fprintf (stderr, "s="); debug_mp (s, -16); abort (); } mpz_gcd (gcd2, op1, op2); MPZ_CHECK_FORMAT (gcd2); if (mpz_cmp (gcd2, gcd1) != 0) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpz_gcd returned incorrect result\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "expected result:\n"); debug_mp (gcd1, -16); fprintf (stderr, "mpz_gcd returns:\n"); debug_mp (gcd2, -16); abort (); } /* This should probably move to t-gcd_ui.c */ if (mpz_fits_ulong_p (op1) || mpz_fits_ulong_p (op2)) { if (mpz_fits_ulong_p (op1)) mpz_gcd_ui (gcd2, op2, mpz_get_ui (op1)); else mpz_gcd_ui (gcd2, op1, mpz_get_ui (op2)); if (mpz_cmp (gcd2, gcd1)) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpz_gcd_ui returned incorrect result\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "expected result:\n"); debug_mp (gcd1, -16); fprintf (stderr, "mpz_gcd_ui returns:\n"); debug_mp (gcd2, -16); abort (); } } mpz_gcdext (gcd2, temp1, temp2, op1, op2); MPZ_CHECK_FORMAT (gcd2); MPZ_CHECK_FORMAT (temp1); MPZ_CHECK_FORMAT (temp2); mpz_mul (temp1, temp1, op1); mpz_mul (temp2, temp2, op2); mpz_add (temp1, temp1, temp2); if (mpz_cmp (gcd1, gcd2) != 0 || mpz_cmp (gcd2, temp1) != 0) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpz_gcdext returned incorrect result\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "expected result:\n"); debug_mp (gcd1, -16); fprintf (stderr, "mpz_gcdext returns:\n");debug_mp (gcd2, -16); abort (); } }
void check_n (void) { { int n = -1; check_one ("blah", "%nblah", &n); ASSERT_ALWAYS (n == 0); } { int n = -1; check_one ("hello ", "hello %n", &n); ASSERT_ALWAYS (n == 6); } { int n = -1; check_one ("hello world", "hello %n world", &n); ASSERT_ALWAYS (n == 6); } #define CHECK_N(type, string) \ do { \ type x[2]; \ char fmt[128]; \ \ x[0] = ~ (type) 0; \ x[1] = ~ (type) 0; \ sprintf (fmt, "%%d%%%sn%%d", string); \ check_one ("123456", fmt, 123, &x[0], 456); \ \ /* should write whole of x[0] and none of x[1] */ \ ASSERT_ALWAYS (x[0] == 3); \ ASSERT_ALWAYS (x[1] == (type) ~ (type) 0); \ \ } while (0) CHECK_N (mp_limb_t, "M"); CHECK_N (char, "hh"); CHECK_N (long, "l"); #if HAVE_LONG_LONG CHECK_N (long long, "L"); #endif #if HAVE_INTMAX_T CHECK_N (intmax_t, "j"); #endif #if HAVE_PTRDIFF_T CHECK_N (ptrdiff_t, "t"); #endif CHECK_N (short, "h"); CHECK_N (size_t, "z"); { mpz_t x[2]; mpz_init_set_si (x[0], -987L); mpz_init_set_si (x[1], 654L); check_one ("123456", "%d%Zn%d", 123, x[0], 456); MPZ_CHECK_FORMAT (x[0]); MPZ_CHECK_FORMAT (x[1]); ASSERT_ALWAYS (mpz_cmp_ui (x[0], 3L) == 0); ASSERT_ALWAYS (mpz_cmp_ui (x[1], 654L) == 0); mpz_clear (x[0]); mpz_clear (x[1]); } { mpq_t x[2]; mpq_init (x[0]); mpq_init (x[1]); mpq_set_ui (x[0], -987L, 654L); mpq_set_ui (x[1], 4115L, 226L); check_one ("123456", "%d%Qn%d", 123, x[0], 456); MPQ_CHECK_FORMAT (x[0]); MPQ_CHECK_FORMAT (x[1]); ASSERT_ALWAYS (mpq_cmp_ui (x[0], 3L, 1L) == 0); ASSERT_ALWAYS (mpq_cmp_ui (x[1], 4115L, 226L) == 0); mpq_clear (x[0]); mpq_clear (x[1]); } { mpf_t x[2]; mpf_init (x[0]); mpf_init (x[1]); mpf_set_ui (x[0], -987L); mpf_set_ui (x[1], 654L); check_one ("123456", "%d%Fn%d", 123, x[0], 456); MPF_CHECK_FORMAT (x[0]); MPF_CHECK_FORMAT (x[1]); ASSERT_ALWAYS (mpf_cmp_ui (x[0], 3L) == 0); ASSERT_ALWAYS (mpf_cmp_ui (x[1], 654L) == 0); mpf_clear (x[0]); mpf_clear (x[1]); } { mp_limb_t a[5]; mp_limb_t a_want[numberof(a)]; mp_size_t i; a[0] = 123; check_one ("blah", "bl%Nnah", a, (mp_size_t) 0); ASSERT_ALWAYS (a[0] == 123); MPN_ZERO (a_want, numberof (a_want)); for (i = 1; i < numberof (a); i++) { check_one ("blah", "bl%Nnah", a, i); a_want[0] = 2; ASSERT_ALWAYS (mpn_cmp (a, a_want, i) == 0); } } }
void check_q (void) { static const struct { const char *fmt; const char *input; const char *want; int ret; long ftell; } data[] = { { "%Qd", "0", "0", 1, -1 }, { "%Qd", "1", "1", 1, -1 }, { "%Qd", "123", "123", 1, -1 }, { "%Qd", "+0", "0", 1, -1 }, { "%Qd", "+1", "1", 1, -1 }, { "%Qd", "+123", "123", 1, -1 }, { "%Qd", "-0", "0", 1, -1 }, { "%Qd", "-1", "-1", 1, -1 }, { "%Qd", "-123", "-123", 1, -1 }, { "%Qo", "0", "0", 1, -1 }, { "%Qo", "173", "123", 1, -1 }, { "%Qo", "+0", "0", 1, -1 }, { "%Qo", "+173", "123", 1, -1 }, { "%Qo", "-0", "0", 1, -1 }, { "%Qo", "-173", "-123", 1, -1 }, { "%Qx", "0", "0", 1, -1 }, { "%Qx", "7b", "123", 1, -1 }, { "%Qx", "7b", "123", 1, -1 }, { "%Qx", "+0", "0", 1, -1 }, { "%Qx", "+7b", "123", 1, -1 }, { "%Qx", "+7b", "123", 1, -1 }, { "%Qx", "-0", "-0", 1, -1 }, { "%Qx", "-7b", "-123", 1, -1 }, { "%Qx", "-7b", "-123", 1, -1 }, { "%QX", "0", "0", 1, -1 }, { "%QX", "7b", "123", 1, -1 }, { "%QX", "7b", "123", 1, -1 }, { "%QX", "+0", "0", 1, -1 }, { "%QX", "+7b", "123", 1, -1 }, { "%QX", "+7b", "123", 1, -1 }, { "%QX", "-0", "-0", 1, -1 }, { "%QX", "-7b", "-123", 1, -1 }, { "%QX", "-7b", "-123", 1, -1 }, { "%Qx", "0", "0", 1, -1 }, { "%Qx", "7B", "123", 1, -1 }, { "%Qx", "7B", "123", 1, -1 }, { "%Qx", "+0", "0", 1, -1 }, { "%Qx", "+7B", "123", 1, -1 }, { "%Qx", "+7B", "123", 1, -1 }, { "%Qx", "-0", "-0", 1, -1 }, { "%Qx", "-7B", "-123", 1, -1 }, { "%Qx", "-7B", "-123", 1, -1 }, { "%QX", "0", "0", 1, -1 }, { "%QX", "7B", "123", 1, -1 }, { "%QX", "7B", "123", 1, -1 }, { "%QX", "+0", "0", 1, -1 }, { "%QX", "+7B", "123", 1, -1 }, { "%QX", "+7B", "123", 1, -1 }, { "%QX", "-0", "-0", 1, -1 }, { "%QX", "-7B", "-123", 1, -1 }, { "%QX", "-7B", "-123", 1, -1 }, { "%Qi", "0", "0", 1, -1 }, { "%Qi", "1", "1", 1, -1 }, { "%Qi", "123", "123", 1, -1 }, { "%Qi", "+0", "0", 1, -1 }, { "%Qi", "+1", "1", 1, -1 }, { "%Qi", "+123", "123", 1, -1 }, { "%Qi", "-0", "0", 1, -1 }, { "%Qi", "-1", "-1", 1, -1 }, { "%Qi", "-123", "-123", 1, -1 }, { "%Qi", "00", "0", 1, -1 }, { "%Qi", "0173", "123", 1, -1 }, { "%Qi", "+00", "0", 1, -1 }, { "%Qi", "+0173", "123", 1, -1 }, { "%Qi", "-00", "0", 1, -1 }, { "%Qi", "-0173", "-123", 1, -1 }, { "%Qi", "0x0", "0", 1, -1 }, { "%Qi", "0x7b", "123", 1, -1 }, { "%Qi", "0x7b", "123", 1, -1 }, { "%Qi", "+0x0", "0", 1, -1 }, { "%Qi", "+0x7b", "123", 1, -1 }, { "%Qi", "+0x7b", "123", 1, -1 }, { "%Qi", "-0x0", "-0", 1, -1 }, { "%Qi", "-0x7b", "-123", 1, -1 }, { "%Qi", "-0x7b", "-123", 1, -1 }, { "%Qi", "0X0", "0", 1, -1 }, { "%Qi", "0X7b", "123", 1, -1 }, { "%Qi", "0X7b", "123", 1, -1 }, { "%Qi", "+0X0", "0", 1, -1 }, { "%Qi", "+0X7b", "123", 1, -1 }, { "%Qi", "+0X7b", "123", 1, -1 }, { "%Qi", "-0X0", "-0", 1, -1 }, { "%Qi", "-0X7b", "-123", 1, -1 }, { "%Qi", "-0X7b", "-123", 1, -1 }, { "%Qi", "0x0", "0", 1, -1 }, { "%Qi", "0x7B", "123", 1, -1 }, { "%Qi", "0x7B", "123", 1, -1 }, { "%Qi", "+0x0", "0", 1, -1 }, { "%Qi", "+0x7B", "123", 1, -1 }, { "%Qi", "+0x7B", "123", 1, -1 }, { "%Qi", "-0x0", "-0", 1, -1 }, { "%Qi", "-0x7B", "-123", 1, -1 }, { "%Qi", "-0x7B", "-123", 1, -1 }, { "%Qi", "0X0", "0", 1, -1 }, { "%Qi", "0X7B", "123", 1, -1 }, { "%Qi", "0X7B", "123", 1, -1 }, { "%Qi", "+0X0", "0", 1, -1 }, { "%Qi", "+0X7B", "123", 1, -1 }, { "%Qi", "+0X7B", "123", 1, -1 }, { "%Qi", "-0X0", "-0", 1, -1 }, { "%Qi", "-0X7B", "-123", 1, -1 }, { "%Qi", "-0X7B", "-123", 1, -1 }, { "%Qd", " 0", "0", 1, -1 }, { "%Qd", " 0", "0", 1, -1 }, { "%Qd", " 0", "0", 1, -1 }, { "%Qd", "\t0", "0", 1, -1 }, { "%Qd", "\t\t0", "0", 1, -1 }, { "%Qd", "3/2", "3/2", 1, -1 }, { "%Qd", "+3/2", "3/2", 1, -1 }, { "%Qd", "-3/2", "-3/2", 1, -1 }, { "%Qx", "f/10", "15/16", 1, -1 }, { "%Qx", "F/10", "15/16", 1, -1 }, { "%QX", "f/10", "15/16", 1, -1 }, { "%QX", "F/10", "15/16", 1, -1 }, { "%Qo", "20/21", "16/17", 1, -1 }, { "%Qo", "-20/21", "-16/17", 1, -1 }, { "%Qi", "10/11", "10/11", 1, -1 }, { "%Qi", "+10/11", "10/11", 1, -1 }, { "%Qi", "-10/11", "-10/11", 1, -1 }, { "%Qi", "010/11", "8/11", 1, -1 }, { "%Qi", "+010/11", "8/11", 1, -1 }, { "%Qi", "-010/11", "-8/11", 1, -1 }, { "%Qi", "0x10/11", "16/11", 1, -1 }, { "%Qi", "+0x10/11", "16/11", 1, -1 }, { "%Qi", "-0x10/11", "-16/11", 1, -1 }, { "%Qi", "10/011", "10/9", 1, -1 }, { "%Qi", "+10/011", "10/9", 1, -1 }, { "%Qi", "-10/011", "-10/9", 1, -1 }, { "%Qi", "010/011", "8/9", 1, -1 }, { "%Qi", "+010/011", "8/9", 1, -1 }, { "%Qi", "-010/011", "-8/9", 1, -1 }, { "%Qi", "0x10/011", "16/9", 1, -1 }, { "%Qi", "+0x10/011", "16/9", 1, -1 }, { "%Qi", "-0x10/011", "-16/9", 1, -1 }, { "%Qi", "10/0x11", "10/17", 1, -1 }, { "%Qi", "+10/0x11", "10/17", 1, -1 }, { "%Qi", "-10/0x11", "-10/17", 1, -1 }, { "%Qi", "010/0x11", "8/17", 1, -1 }, { "%Qi", "+010/0x11", "8/17", 1, -1 }, { "%Qi", "-010/0x11", "-8/17", 1, -1 }, { "%Qi", "0x10/0x11", "16/17", 1, -1 }, { "%Qi", "+0x10/0x11", "16/17", 1, -1 }, { "%Qi", "-0x10/0x11", "-16/17", 1, -1 }, { "hello%Qd", "hello0", "0", 1, -1 }, { "hello%Qd", "hello 0", "0", 1, -1 }, { "hello%Qd", "hello \t0", "0", 1, -1 }, { "hello%Qdworld", "hello 0world", "0", 1, -1 }, { "hello%Qd", "hello3/2", "3/2", 1, -1 }, { "hello%*Qd", "hello0", "-999/121", 0, -1 }, { "hello%*Qd", "hello 0", "-999/121", 0, -1 }, { "hello%*Qd", "hello \t0", "-999/121", 0, -1 }, { "hello%*Qdworld", "hello 0world", "-999/121", 0, -1 }, { "hello%*Qdworld", "hello3/2world", "-999/121", 0, -1 }, { "%Qd", "", "-999/121", -1, -1 }, { "%Qd", " ", "-999/121", -1, -1 }, { " %Qd", "", "-999/121", -1, -1 }, { "xyz%Qd", "", "-999/121", -1, -1 }, { "%*Qd", "", "-999/121", -1, -1 }, { " %*Qd", "", "-999/121", -1, -1 }, { "xyz%*Qd", "", "-999/121", -1, -1 }, /* match something, but invalid */ { "%Qd", "-", "-999/121", 0, 1 }, { "%Qd", "+", "-999/121", 0, 1 }, { "%Qd", "/-", "-999/121", 0, 1 }, { "%Qd", "/+", "-999/121", 0, 1 }, { "%Qd", "-/", "-999/121", 0, 1 }, { "%Qd", "+/", "-999/121", 0, 1 }, { "%Qd", "-/-", "-999/121", 0, 1 }, { "%Qd", "-/+", "-999/121", 0, 1 }, { "%Qd", "+/+", "-999/121", 0, 1 }, { "%Qd", "/123", "-999/121", 0, 1 }, { "%Qd", "-/123", "-999/121", 0, 1 }, { "%Qd", "+/123", "-999/121", 0, 1 }, { "%Qd", "123/", "-999/121", 0, 1 }, { "%Qd", "123/-", "-999/121", 0, 1 }, { "%Qd", "123/+", "-999/121", 0, 1 }, { "xyz%Qd", "xyz-", "-999/121", 0, 4 }, { "xyz%Qd", "xyz+", "-999/121", 0, 4 }, { "%1Qi", "12/57", "1", 1, 1 }, { "%2Qi", "12/57", "12", 1, 2 }, { "%3Qi", "12/57", "-999/121", 0, -1 }, { "%4Qi", "12/57", "12/5", 1, 4 }, { "%5Qi", "12/57", "12/57", 1, 5 }, { "%6Qi", "12/57", "12/57", 1, 5 }, { "%7Qi", "12/57", "12/57", 1, 5 }, { "%1Qi", "012/057", "0", 1, 1 }, { "%2Qi", "012/057", "01", 1, 2 }, { "%3Qi", "012/057", "012", 1, 3 }, { "%4Qi", "012/057", "-999/121", 0, -1 }, { "%5Qi", "012/057", "012/0", 1, 5 }, { "%6Qi", "012/057", "012/5", 1, 6 }, { "%7Qi", "012/057", "012/057", 1, 7 }, { "%8Qi", "012/057", "012/057", 1, 7 }, { "%9Qi", "012/057", "012/057", 1, 7 }, { "%1Qi", "0x12/0x57", "0", 1, 1 }, { "%2Qi", "0x12/0x57", "-999", 0, 2 }, { "%3Qi", "0x12/0x57", "0x1", 1, 3 }, { "%4Qi", "0x12/0x57", "0x12", 1, 4 }, { "%5Qi", "0x12/0x57", "-999/121", 0, 5 }, { "%6Qi", "0x12/0x57", "0x12/0", 1, 6 }, { "%7Qi", "0x12/0x57", "-999/121", 0, 7 }, { "%8Qi", "0x12/0x57", "0x12/0x5", 1, 8 }, { "%9Qi", "0x12/0x57", "0x12/0x57", 1, 9 }, { "%10Qi", "0x12/0x57", "0x12/0x57", 1, 9 }, { "%11Qi", "0x12/0x57", "0x12/0x57", 1, 9 }, { "%Qd", "xyz", "0", 0, 0 }, }; int i, j, ignore, got_ret, want_ret, got_upto, want_upto; mpq_t got, want; long got_l, want_ftell; int error = 0; fun_t fun; const char *name; char fmt[128]; mpq_init (got); mpq_init (want); for (i = 0; i < numberof (data); i++) { mpq_set_str_or_abort (want, data[i].want, 0); ASSERT_ALWAYS (strlen (data[i].fmt) + 2 < sizeof (fmt)); strcpy (fmt, data[i].fmt); strcat (fmt, "%n"); ignore = (strchr (fmt, '*') != NULL); for (j = 0; j <= 3; j++) { want_ret = data[i].ret; want_ftell = data[i].ftell; if (want_ftell == -1) want_ftell = strlen (data[i].input); want_upto = want_ftell; if (want_ret == -1 || (want_ret == 0 && ! ignore)) { want_ftell = -1; want_upto = -555; } switch (j) { case 0: name = "gmp_sscanf"; fun = fun_gmp_sscanf; break; case 1: name = "gmp_fscanf"; fun = fun_gmp_fscanf; break; case 2: if (strchr (data[i].input, '/') != NULL) continue; if (! libc_scanf_convert (fmt)) continue; name = "standard sscanf"; fun = fun_sscanf; break; case 3: if (strchr (data[i].input, '/') != NULL) continue; if (! libc_scanf_convert (fmt)) continue; name = "standard fscanf"; fun = fun_fscanf; break; default: ASSERT_ALWAYS (0); break; } got_upto = -555; got_ftell = -1; switch (j) { case 0: case 1: mpq_set_si (got, -999L, 121L); if (ignore) got_ret = (*fun) (data[i].input, fmt, &got_upto, NULL); else got_ret = (*fun) (data[i].input, fmt, got, &got_upto); break; case 2: case 3: got_l = -999L; if (ignore) got_ret = (*fun) (data[i].input, fmt, &got_upto, NULL); else got_ret = (*fun) (data[i].input, fmt, &got_l, &got_upto); mpq_set_si (got, got_l, (got_l == -999L ? 121L : 1L)); break; default: ASSERT_ALWAYS (0); break; } MPZ_CHECK_FORMAT (mpq_numref (got)); MPZ_CHECK_FORMAT (mpq_denref (got)); if (got_ret != want_ret) { printf ("%s wrong return value\n", name); error = 1; } /* use direct mpz compares, since some of the test data is non-canonical and can trip ASSERTs in mpq_equal */ if (want_ret == 1 && ! (mpz_cmp (mpq_numref(want), mpq_numref(got)) == 0 && mpz_cmp (mpq_denref(want), mpq_denref(got)) == 0)) { printf ("%s wrong result\n", name); error = 1; } if (got_upto != want_upto) { printf ("%s wrong upto\n", name); error = 1; } if (got_ftell != -1 && want_ftell != -1 && got_ftell != want_ftell) { printf ("%s wrong ftell\n", name); error = 1; } if (error) { printf (" fmt \"%s\"\n", data[i].fmt); printf (" input \"%s\"\n", data[i].input); printf (" ret want=%d\n", want_ret); printf (" got =%d\n", got_ret); mpq_trace (" value want", want); mpq_trace (" got ", got); printf (" upto want=%d\n", want_upto); printf (" got =%d\n", got_upto); if (got_ftell != -1) { printf (" ftell want =%ld\n", want_ftell); printf (" got =%ld\n", got_ftell); } abort (); } } } mpq_clear (got); mpq_clear (want); }
void check_n (void) { int ret; /* %n suppressed */ { int n = 123; gmp_sscanf (" ", " %*n", &n); ASSERT_ALWAYS (n == 123); } { int n = 123; fromstring_gmp_fscanf (" ", " %*n", &n); ASSERT_ALWAYS (n == 123); } #define CHECK_N(type, string) \ do { \ type x[2]; \ char fmt[128]; \ int ret; \ \ x[0] = ~ (type) 0; \ x[1] = ~ (type) 0; \ sprintf (fmt, "abc%%%sn", string); \ ret = gmp_sscanf ("abc", fmt, &x[0]); \ \ ASSERT_ALWAYS (ret == 0); \ \ /* should write whole of x[0] and none of x[1] */ \ ASSERT_ALWAYS (x[0] == 3); \ ASSERT_ALWAYS (x[1] == (type) ~ (type) 0); \ \ } while (0) CHECK_N (char, "hh"); CHECK_N (long, "l"); #if HAVE_LONG_LONG CHECK_N (long long, "L"); #endif #if HAVE_INTMAX_T CHECK_N (intmax_t, "j"); #endif #if HAVE_PTRDIFF_T CHECK_N (ptrdiff_t, "t"); #endif CHECK_N (short, "h"); CHECK_N (size_t, "z"); /* %Zn */ { mpz_t x[2]; mpz_init_set_si (x[0], -987L); mpz_init_set_si (x[1], 654L); ret = gmp_sscanf ("xyz ", "xyz%Zn", x[0]); MPZ_CHECK_FORMAT (x[0]); MPZ_CHECK_FORMAT (x[1]); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpz_cmp_ui (x[0], 3L) == 0); ASSERT_ALWAYS (mpz_cmp_ui (x[1], 654L) == 0); mpz_clear (x[0]); mpz_clear (x[1]); } { mpz_t x; mpz_init (x); ret = fromstring_gmp_fscanf ("xyz ", "xyz%Zn", x); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpz_cmp_ui (x, 3L) == 0); mpz_clear (x); } /* %Qn */ { mpq_t x[2]; mpq_init (x[0]); mpq_init (x[1]); mpq_set_ui (x[0], -987L, 654L); mpq_set_ui (x[1], 4115L, 226L); ret = gmp_sscanf ("xyz ", "xyz%Qn", x[0]); MPQ_CHECK_FORMAT (x[0]); MPQ_CHECK_FORMAT (x[1]); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpq_cmp_ui (x[0], 3L, 1L) == 0); ASSERT_ALWAYS (mpq_cmp_ui (x[1], 4115L, 226L) == 0); mpq_clear (x[0]); mpq_clear (x[1]); } { mpq_t x; mpq_init (x); ret = fromstring_gmp_fscanf ("xyz ", "xyz%Qn", x); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpq_cmp_ui (x, 3L, 1L) == 0); mpq_clear (x); } /* %Fn */ { mpf_t x[2]; mpf_init (x[0]); mpf_init (x[1]); mpf_set_ui (x[0], -987L); mpf_set_ui (x[1], 654L); ret = gmp_sscanf ("xyz ", "xyz%Fn", x[0]); MPF_CHECK_FORMAT (x[0]); MPF_CHECK_FORMAT (x[1]); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpf_cmp_ui (x[0], 3L) == 0); ASSERT_ALWAYS (mpf_cmp_ui (x[1], 654L) == 0); mpf_clear (x[0]); mpf_clear (x[1]); } { mpf_t x; mpf_init (x); ret = fromstring_gmp_fscanf ("xyz ", "xyz%Fn", x); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpf_cmp_ui (x, 3L) == 0); mpf_clear (x); } }
void check_data (void) { static const struct { const char *inp; int base; const char *want; int want_nread; } data[] = { { "0", 10, "0", 1 }, { "abc", 10, "0", 0 }, { "0xf", 10, "0", 1 }, { "ghi", 16, "0", 0 }, { "100", 90, "0", 0 }, { "ff", 16, "255", 2 }, { "-ff", 16, "-255", 3 }, { "FF", 16, "255", 2 }, { "-FF", 16, "-255", 3 }, { "z", 36, "35", 1 }, { "Z", 36, "35", 1 }, { "1B", 59, "70", 2 }, { "a", 60, "36", 1 }, { "A", 61, "10", 1 }, { "0x0", 0, "0", 3 }, { "0X10", 0, "16", 4 }, { "-0X0", 0, "0", 4 }, { "-0x10", 0, "-16", 5 }, { "0b0", 0, "0", 3 }, { "0B10", 0, "2", 4 }, { "-0B0", 0, "0", 4 }, { "-0b10", 0, "-2", 5 }, { "00", 0, "0", 2 }, { "010", 0, "8", 3 }, { "-00", 0, "0", 3 }, { "-010", 0, "-8", 4 }, { "0x", 0, "0", 2 }, { "0", 0, "0", 1 }, { " 030", 10, "30", 4 }, }; mpz_t got, want; long ftell_nread; int i, pre, post, j, got_nread, want_nread; FILE *fp; mpz_init (got); mpz_init (want); for (i = 0; i < numberof (data); i++) { for (pre = 0; pre <= 3; pre++) { for (post = 0; post <= 2; post++) { mpz_set_str_or_abort (want, data[i].want, 0); MPZ_CHECK_FORMAT (want); /* create the file new each time to ensure its length is what we want */ fp = fopen (FILENAME, "w+"); ASSERT_ALWAYS (fp != NULL); for (j = 0; j < pre; j++) putc (' ', fp); fputs (data[i].inp, fp); for (j = 0; j < post; j++) putc (' ', fp); fflush (fp); ASSERT_ALWAYS (! ferror(fp)); rewind (fp); got_nread = mpz_inp_str (got, fp, data[i].base); if (got_nread != 0) { ftell_nread = ftell (fp); if (got_nread != ftell_nread) { printf ("mpz_inp_str nread wrong\n"); printf (" inp \"%s\"\n", data[i].inp); printf (" base %d\n", data[i].base); printf (" pre %d\n", pre); printf (" post %d\n", post); printf (" got_nread %d\n", got_nread); printf (" ftell_nread %ld\n", ftell_nread); abort (); } } /* if data[i].inp is a whole string to read and there's no post whitespace then expect to have EOF */ if (post == 0 && data[i].want_nread == strlen(data[i].inp)) { int c = getc(fp); if (c != EOF) { printf ("mpz_inp_str didn't read to EOF\n"); printf (" inp \"%s\"\n", data[i].inp); printf (" base %d\n", data[i].base); printf (" pre %d\n", pre); printf (" post %d\n", post); printf (" c '%c' %#x\n", c, c); abort (); } } /* only expect "pre" included in the count when non-zero */ want_nread = data[i].want_nread; if (want_nread != 0) want_nread += pre; if (got_nread != want_nread) { printf ("mpz_inp_str nread wrong\n"); printf (" inp \"%s\"\n", data[i].inp); printf (" base %d\n", data[i].base); printf (" pre %d\n", pre); printf (" post %d\n", post); printf (" got_nread %d\n", got_nread); printf (" want_nread %d\n", want_nread); abort (); } MPZ_CHECK_FORMAT (got); if (mpz_cmp (got, want) != 0) { printf ("mpz_inp_str wrong result\n"); printf (" inp \"%s\"\n", data[i].inp); printf (" base %d\n", data[i].base); mpz_trace (" got ", got); mpz_trace (" want", want); abort (); } ASSERT_ALWAYS (fclose (fp) == 0); } } } mpz_clear (got); mpz_clear (want); }
void check_all (mpz_ptr want, mpz_srcptr x_orig, mpz_srcptr y_orig) { mpz_t got, x, y; int negx, negy, swap, inplace; mpz_init (got); mpz_init_set (x, x_orig); mpz_init_set (y, y_orig); for (swap = 0; swap < 2; swap++) { mpz_swap (x, y); for (negx = 0; negx < 2; negx++) { mpz_neg (x, x); for (negy = 0; negy < 2; negy++) { mpz_neg (y, y); for (inplace = 0; inplace <= 1; inplace++) { if (inplace) { mpz_set (got, x); mpz_lcm (got, got, y); } else mpz_lcm (got, x, y); MPZ_CHECK_FORMAT (got); if (mpz_cmp (got, want) != 0) { printf ("mpz_lcm wrong, inplace=%d\n", inplace); fail: mpz_trace ("x", x); mpz_trace ("y", y); mpz_trace ("got", got); mpz_trace ("want", want); abort (); } if (mpz_fits_ulong_p (y)) { unsigned long yu = mpz_get_ui (y); if (inplace) { mpz_set (got, x); mpz_lcm_ui (got, got, yu); } else mpz_lcm_ui (got, x, yu); if (mpz_cmp (got, want) != 0) { printf ("mpz_lcm_ui wrong, inplace=%d\n", inplace); printf ("yu=%lu\n", yu); goto fail; } } } } } } mpz_clear (got); mpz_clear (x); mpz_clear (y); }
void check_z (void) { static const struct { const char *fmt; const char *input; const char *want; int want_ret; long want_ftell; int want_upto; int not_glibc; } data[] = { { "%Zd", "0", "0", 1, -1, -1 }, { "%Zd", "1", "1", 1, -1, -1 }, { "%Zd", "123", "123", 1, -1, -1 }, { "%Zd", "+0", "0", 1, -1, -1 }, { "%Zd", "+1", "1", 1, -1, -1 }, { "%Zd", "+123", "123", 1, -1, -1 }, { "%Zd", "-0", "0", 1, -1, -1 }, { "%Zd", "-1", "-1", 1, -1, -1 }, { "%Zd", "-123", "-123", 1, -1, -1 }, { "%Zo", "0", "0", 1, -1, -1 }, { "%Zo", "173", "123", 1, -1, -1 }, { "%Zo", "+0", "0", 1, -1, -1 }, { "%Zo", "+173", "123", 1, -1, -1 }, { "%Zo", "-0", "0", 1, -1, -1 }, { "%Zo", "-173", "-123", 1, -1, -1 }, { "%Zx", "0", "0", 1, -1, -1 }, { "%Zx", "7b", "123", 1, -1, -1 }, { "%Zx", "7b", "123", 1, -1, -1 }, { "%Zx", "+0", "0", 1, -1, -1 }, { "%Zx", "+7b", "123", 1, -1, -1 }, { "%Zx", "+7b", "123", 1, -1, -1 }, { "%Zx", "-0", "-0", 1, -1, -1 }, { "%Zx", "-7b", "-123", 1, -1, -1 }, { "%Zx", "-7b", "-123", 1, -1, -1 }, { "%ZX", "0", "0", 1, -1, -1 }, { "%ZX", "7b", "123", 1, -1, -1 }, { "%ZX", "7b", "123", 1, -1, -1 }, { "%ZX", "+0", "0", 1, -1, -1 }, { "%ZX", "+7b", "123", 1, -1, -1 }, { "%ZX", "+7b", "123", 1, -1, -1 }, { "%ZX", "-0", "-0", 1, -1, -1 }, { "%ZX", "-7b", "-123", 1, -1, -1 }, { "%ZX", "-7b", "-123", 1, -1, -1 }, { "%Zx", "0", "0", 1, -1, -1 }, { "%Zx", "7B", "123", 1, -1, -1 }, { "%Zx", "7B", "123", 1, -1, -1 }, { "%Zx", "+0", "0", 1, -1, -1 }, { "%Zx", "+7B", "123", 1, -1, -1 }, { "%Zx", "+7B", "123", 1, -1, -1 }, { "%Zx", "-0", "-0", 1, -1, -1 }, { "%Zx", "-7B", "-123", 1, -1, -1 }, { "%Zx", "-7B", "-123", 1, -1, -1 }, { "%ZX", "0", "0", 1, -1, -1 }, { "%ZX", "7B", "123", 1, -1, -1 }, { "%ZX", "7B", "123", 1, -1, -1 }, { "%ZX", "+0", "0", 1, -1, -1 }, { "%ZX", "+7B", "123", 1, -1, -1 }, { "%ZX", "+7B", "123", 1, -1, -1 }, { "%ZX", "-0", "-0", 1, -1, -1 }, { "%ZX", "-7B", "-123", 1, -1, -1 }, { "%ZX", "-7B", "-123", 1, -1, -1 }, { "%Zi", "0", "0", 1, -1, -1 }, { "%Zi", "1", "1", 1, -1, -1 }, { "%Zi", "123", "123", 1, -1, -1 }, { "%Zi", "+0", "0", 1, -1, -1 }, { "%Zi", "+1", "1", 1, -1, -1 }, { "%Zi", "+123", "123", 1, -1, -1 }, { "%Zi", "-0", "0", 1, -1, -1 }, { "%Zi", "-1", "-1", 1, -1, -1 }, { "%Zi", "-123", "-123", 1, -1, -1 }, { "%Zi", "00", "0", 1, -1, -1 }, { "%Zi", "0173", "123", 1, -1, -1 }, { "%Zi", "+00", "0", 1, -1, -1 }, { "%Zi", "+0173", "123", 1, -1, -1 }, { "%Zi", "-00", "0", 1, -1, -1 }, { "%Zi", "-0173", "-123", 1, -1, -1 }, { "%Zi", "0x0", "0", 1, -1, -1 }, { "%Zi", "0x7b", "123", 1, -1, -1 }, { "%Zi", "0x7b", "123", 1, -1, -1 }, { "%Zi", "+0x0", "0", 1, -1, -1 }, { "%Zi", "+0x7b", "123", 1, -1, -1 }, { "%Zi", "+0x7b", "123", 1, -1, -1 }, { "%Zi", "-0x0", "-0", 1, -1, -1 }, { "%Zi", "-0x7b", "-123", 1, -1, -1 }, { "%Zi", "-0x7b", "-123", 1, -1, -1 }, { "%Zi", "0X0", "0", 1, -1, -1 }, { "%Zi", "0X7b", "123", 1, -1, -1 }, { "%Zi", "0X7b", "123", 1, -1, -1 }, { "%Zi", "+0X0", "0", 1, -1, -1 }, { "%Zi", "+0X7b", "123", 1, -1, -1 }, { "%Zi", "+0X7b", "123", 1, -1, -1 }, { "%Zi", "-0X0", "-0", 1, -1, -1 }, { "%Zi", "-0X7b", "-123", 1, -1, -1 }, { "%Zi", "-0X7b", "-123", 1, -1, -1 }, { "%Zi", "0x0", "0", 1, -1, -1 }, { "%Zi", "0x7B", "123", 1, -1, -1 }, { "%Zi", "0x7B", "123", 1, -1, -1 }, { "%Zi", "+0x0", "0", 1, -1, -1 }, { "%Zi", "+0x7B", "123", 1, -1, -1 }, { "%Zi", "+0x7B", "123", 1, -1, -1 }, { "%Zi", "-0x0", "-0", 1, -1, -1 }, { "%Zi", "-0x7B", "-123", 1, -1, -1 }, { "%Zi", "-0x7B", "-123", 1, -1, -1 }, { "%Zi", "0X0", "0", 1, -1, -1 }, { "%Zi", "0X7B", "123", 1, -1, -1 }, { "%Zi", "0X7B", "123", 1, -1, -1 }, { "%Zi", "+0X0", "0", 1, -1, -1 }, { "%Zi", "+0X7B", "123", 1, -1, -1 }, { "%Zi", "+0X7B", "123", 1, -1, -1 }, { "%Zi", "-0X0", "-0", 1, -1, -1 }, { "%Zi", "-0X7B", "-123", 1, -1, -1 }, { "%Zi", "-0X7B", "-123", 1, -1, -1 }, { "%Zd", " 0", "0", 1, -1, -1 }, { "%Zd", " 0", "0", 1, -1, -1 }, { "%Zd", " 0", "0", 1, -1, -1 }, { "%Zd", "\t0", "0", 1, -1, -1 }, { "%Zd", "\t\t0", "0", 1, -1, -1 }, { "hello%Zd", "hello0", "0", 1, -1, -1 }, { "hello%Zd", "hello 0", "0", 1, -1, -1 }, { "hello%Zd", "hello \t0", "0", 1, -1, -1 }, { "hello%Zdworld", "hello 0world", "0", 1, -1, -1 }, { "hello%*Zd", "hello0", "-999", 0, -1, -1 }, { "hello%*Zd", "hello 0", "-999", 0, -1, -1 }, { "hello%*Zd", "hello \t0", "-999", 0, -1, -1 }, { "hello%*Zdworld", "hello 0world", "-999", 0, -1, -1 }, { "%Zd", "", "-999", -1, -1, -555 }, { "%Zd", " ", "-999", -1, -1, -555 }, { " %Zd", "", "-999", -1, -1, -555 }, { "xyz%Zd", "", "-999", -1, -1, -555 }, { "%*Zd", "", "-999", -1, -1, -555 }, { " %*Zd", "", "-999", -1, -1, -555 }, { "xyz%*Zd", "", "-999", -1, -1, -555 }, { "%Zd", "xyz", "0", 0, 0, -555 }, /* match something, but invalid */ { "%Zd", "-", "-999", 0, 1, -555 }, { "%Zd", "+", "-999", 0, 1, -555 }, { "xyz%Zd", "xyz-", "-999", 0, 4, -555 }, { "xyz%Zd", "xyz+", "-999", 0, 4, -555 }, { "%Zi", "0x", "-999", 0, 2, -555 }, { "%Zi", "0X", "-999", 0, 2, -555 }, { "%Zi", "0x-", "-999", 0, 2, -555 }, { "%Zi", "0X+", "-999", 0, 2, -555 }, { "%Zi", "-0x", "-999", 0, 3, -555 }, { "%Zi", "-0X", "-999", 0, 3, -555 }, { "%Zi", "+0x", "-999", 0, 3, -555 }, { "%Zi", "+0X", "-999", 0, 3, -555 }, { "%1Zi", "1234", "1", 1, 1, 1 }, { "%2Zi", "1234", "12", 1, 2, 2 }, { "%3Zi", "1234", "123", 1, 3, 3 }, { "%4Zi", "1234", "1234", 1, 4, 4 }, { "%5Zi", "1234", "1234", 1, 4, 4 }, { "%6Zi", "1234", "1234", 1, 4, 4 }, { "%1Zi", "01234", "0", 1, 1, 1 }, { "%2Zi", "01234", "01", 1, 2, 2 }, { "%3Zi", "01234", "012", 1, 3, 3 }, { "%4Zi", "01234", "0123", 1, 4, 4 }, { "%5Zi", "01234", "01234", 1, 5, 5 }, { "%6Zi", "01234", "01234", 1, 5, 5 }, { "%7Zi", "01234", "01234", 1, 5, 5 }, { "%1Zi", "0x1234", "0", 1, 1, 1 }, { "%2Zi", "0x1234", "-999", 0, 2, -555 }, { "%3Zi", "0x1234", "0x1", 1, 3, 3 }, { "%4Zi", "0x1234", "0x12", 1, 4, 4 }, { "%5Zi", "0x1234", "0x123", 1, 5, 5 }, { "%6Zi", "0x1234", "0x1234", 1, 6, 6 }, { "%7Zi", "0x1234", "0x1234", 1, 6, 6 }, { "%8Zi", "0x1234", "0x1234", 1, 6, 6 }, { "%%xyz%Zd", "%xyz123", "123", 1, -1, -1 }, { "12%%34%Zd", "12%34567", "567", 1, -1, -1 }, { "%%%%%Zd", "%%123", "123", 1, -1, -1 }, /* various subtle EOF cases */ { "x", "", "-999", EOF, 0, -555 }, { " x", "", "-999", EOF, 0, -555 }, { "xyz", "", "-999", EOF, 0, -555 }, { " ", "", "-999", 0, 0, 0 }, { " ", " ", "-999", 0, 1, 1 }, { "%*Zd%Zd", "", "-999", EOF, 0, -555 }, { "%*Zd%Zd", "123", "-999", EOF, 3, -555 }, { "x", "x", "-999", 0, 1, 1 }, { "xyz", "x", "-999", EOF, 1, -555 }, { "xyz", "xy", "-999", EOF, 2, -555 }, { "xyz", "xyz", "-999", 0, 3, 3 }, { "%Zn", "", "0", 0, 0, 0 }, { " %Zn", "", "0", 0, 0, 0 }, { " x%Zn", "", "-999", EOF, 0, -555 }, { "xyz%Zn", "", "-999", EOF, 0, -555 }, { " x%Zn", "", "-999", EOF, 0, -555 }, { " %Zn x", " ", "-999", EOF, 1, -555 }, /* these seem to tickle a bug in glibc 2.2.4 */ { " x", " ", "-999", EOF, 1, -555, 1 }, { " xyz", " ", "-999", EOF, 1, -555, 1 }, { " x%Zn", " ", "-999", EOF, 1, -555, 1 }, }; int i, j, ignore; int got_ret, want_ret, got_upto, want_upto; mpz_t got, want; long got_l, want_ftell; int error = 0; fun_t fun; const char *name; char fmt[128]; mpz_init (got); mpz_init (want); for (i = 0; i < numberof (data); i++) { mpz_set_str_or_abort (want, data[i].want, 0); ASSERT_ALWAYS (strlen (data[i].fmt) + 2 < sizeof (fmt)); strcpy (fmt, data[i].fmt); strcat (fmt, "%n"); ignore = fmt_allignore (fmt); for (j = 0; j <= 3; j++) { want_ret = data[i].want_ret; want_ftell = data[i].want_ftell; if (want_ftell == -1) want_ftell = strlen (data[i].input); want_upto = data[i].want_upto; if (want_upto == -1) want_upto = strlen (data[i].input); switch (j) { case 0: name = "gmp_sscanf"; fun = fun_gmp_sscanf; break; case 1: name = "gmp_fscanf"; fun = fun_gmp_fscanf; break; case 2: #ifdef __GLIBC__ if (data[i].not_glibc) continue; #endif if (! libc_scanf_convert (fmt)) continue; name = "standard sscanf"; fun = fun_sscanf; break; case 3: #ifdef __GLIBC__ if (data[i].not_glibc) continue; #endif if (! libc_scanf_convert (fmt)) continue; name = "standard fscanf"; fun = fun_fscanf; break; default: ASSERT_ALWAYS (0); break; } got_upto = -555; got_ftell = -1L; switch (j) { case 0: case 1: mpz_set_si (got, -999L); if (ignore) got_ret = (*fun) (data[i].input, fmt, &got_upto, NULL); else got_ret = (*fun) (data[i].input, fmt, got, &got_upto); break; case 2: case 3: got_l = -999L; if (ignore) got_ret = (*fun) (data[i].input, fmt, &got_upto, NULL); else got_ret = (*fun) (data[i].input, fmt, &got_l, &got_upto); mpz_set_si (got, got_l); break; default: ASSERT_ALWAYS (0); break; } MPZ_CHECK_FORMAT (got); if (got_ret != want_ret) { printf ("%s wrong return value\n", name); error = 1; } if (want_ret == 1 && mpz_cmp (want, got) != 0) { printf ("%s wrong result\n", name); error = 1; } if (got_upto != want_upto) { printf ("%s wrong upto\n", name); error = 1; } if (got_ftell != -1 && want_ftell != -1 && got_ftell != want_ftell) { printf ("%s wrong ftell\n", name); error = 1; } if (error) { printf (" fmt \"%s\"\n", data[i].fmt); printf (" input \"%s\"\n", data[i].input); printf (" ignore %d\n", ignore); printf (" ret want=%d\n", want_ret); printf (" got =%d\n", got_ret); mpz_trace (" value want", want); mpz_trace (" got ", got); printf (" upto want =%d\n", want_upto); printf (" got =%d\n", got_upto); if (got_ftell != -1) { printf (" ftell want =%ld\n", want_ftell); printf (" got =%ld\n", got_ftell); } abort (); } } } mpz_clear (got); mpz_clear (want); }
int main (int argc, char **argv) { mpz_t op1, op2; mpz_t prod, quot; mp_size_t size; int i; int reps = 20000; gmp_randstate_ptr rands; mpz_t bs; unsigned long bsi, size_range; tests_start (); rands = RANDS; mp_trace_base = -16; mpz_init (bs); if (argc == 2) reps = atoi (argv[1]); mpz_init (op1); mpz_init (op2); mpz_init (prod); mpz_init (quot); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 10 + 2; /* 0..2047 bit operands */ mpz_urandomb (bs, rands, size_range); size = mpz_get_ui (bs); mpz_rrandomb (op1, rands, size); do { mpz_urandomb (bs, rands, size_range); size = mpz_get_ui (bs); mpz_rrandomb (op2, rands, size); } while (mpz_sgn (op2) == 0); mpz_urandomb (bs, rands, 2); bsi = mpz_get_ui (bs); if ((bsi & 1) != 0) mpz_neg (op1, op1); if ((bsi & 2) != 0) mpz_neg (op2, op2); mpz_mul (prod, op1, op2); mpz_divexact (quot, prod, op2); MPZ_CHECK_FORMAT (quot); if (mpz_cmp (quot, op1) != 0) { printf ("Wrong results:\n"); mpz_trace (" got ", quot); mpz_trace (" want ", op1); mpz_trace (" dividend", prod); mpz_trace (" divisor ", op2); abort (); } } mpz_clear (bs); mpz_clear (op1); mpz_clear (op2); mpz_clear (prod); mpz_clear (quot); tests_end (); exit (0); }
int main (int argc, char *argv[]) { mpz_t ref[MULTIFAC_WHEEL], ref2[MULTIFAC_WHEEL2], res; unsigned long n, j, m, m2; unsigned long limit = 2222, step = 1; tests_start (); if (argc > 1 && argv[1][0] == 'x') limit = ULONG_MAX; else if (argc > 1) limit = atoi (argv[1]); /* for small limb testing */ limit = MIN (limit, MP_LIMB_T_MAX); for (m = 0; m < MULTIFAC_WHEEL; m++) mpz_init_set_ui(ref [m],1); for (m2 = 0; m2 < MULTIFAC_WHEEL2; m2++) mpz_init_set_ui(ref2 [m2],1); mpz_init (res); m = 0; m2 = 0; for (n = 0; n <= limit;) { mpz_mfac_uiui (res, n, MULTIFAC_WHEEL); MPZ_CHECK_FORMAT (res); if (mpz_cmp (ref[m], res) != 0) { printf ("mpz_mfac_uiui(%lu,%d) wrong\n", n, MULTIFAC_WHEEL); printf (" got "); mpz_out_str (stdout, 10, res); printf("\n"); printf (" want "); mpz_out_str (stdout, 10, ref[m]); printf("\n"); abort (); } mpz_mfac_uiui (res, n, MULTIFAC_WHEEL2); MPZ_CHECK_FORMAT (res); if (mpz_cmp (ref2[m2], res) != 0) { printf ("mpz_mfac_uiui(%lu,%d) wrong\n", n, MULTIFAC_WHEEL2); printf (" got "); mpz_out_str (stdout, 10, res); printf("\n"); printf (" want "); mpz_out_str (stdout, 10, ref2[m2]); printf("\n"); abort (); } if (n + step <= limit) for (j = 0; j < step; j++) { n++; m++; m2++; if (m >= MULTIFAC_WHEEL) m -= MULTIFAC_WHEEL; if (m2 >= MULTIFAC_WHEEL2) m2 -= MULTIFAC_WHEEL2; mpz_mul_ui (ref[m], ref[m], n); /* Compute a reference, with current library */ mpz_mul_ui (ref2[m2], ref2[m2], n); /* Compute a reference, with current library */ } else n += step; } mpz_fac_ui (ref[0], n); mpz_mfac_uiui (res, n, 1); MPZ_CHECK_FORMAT (res); if (mpz_cmp (ref[0], res) != 0) { printf ("mpz_mfac_uiui(%lu,1) wrong\n", n); printf (" got "); mpz_out_str (stdout, 10, res); printf("\n"); printf (" want "); mpz_out_str (stdout, 10, ref[0]); printf("\n"); abort (); } mpz_2fac_ui (ref[0], n); mpz_mfac_uiui (res, n, 2); MPZ_CHECK_FORMAT (res); if (mpz_cmp (ref[0], res) != 0) { printf ("mpz_mfac_uiui(%lu,1) wrong\n", n); printf (" got "); mpz_out_str (stdout, 10, res); printf("\n"); printf (" want "); mpz_out_str (stdout, 10, ref[0]); printf("\n"); abort (); } n++; mpz_2fac_ui (ref[0], n); mpz_mfac_uiui (res, n, 2); MPZ_CHECK_FORMAT (res); if (mpz_cmp (ref[0], res) != 0) { printf ("mpz_mfac_uiui(%lu,2) wrong\n", n); printf (" got "); mpz_out_str (stdout, 10, res); printf("\n"); printf (" want "); mpz_out_str (stdout, 10, ref[0]); printf("\n"); abort (); } for (m = 0; m < MULTIFAC_WHEEL; m++) mpz_clear (ref[m]); for (m2 = 0; m2 < MULTIFAC_WHEEL2; m2++) mpz_clear (ref2[m2]); mpz_clear (res); tests_end (); exit (0); }
int main (int argc, char **argv) { mpz_t x2; mpz_t x, rem; mpz_t temp, temp2; mp_size_t x2_size; int i; int reps = 1000; gmp_randstate_ptr rands; mpz_t bs; unsigned long size_range; tests_start (); TESTS_REPS (reps, argv, argc); rands = RANDS; mpz_init (bs); mpz_init (x2); mpz_init (x); mpz_init (rem); mpz_init (temp); mpz_init (temp2); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 17 + 2; /* 0..262144 bit operands */ mpz_urandomb (bs, rands, size_range); x2_size = mpz_get_ui (bs); mpz_rrandomb (x2, rands, x2_size); /* printf ("%ld\n", SIZ (x2)); */ mpz_sqrtrem (x, rem, x2); MPZ_CHECK_FORMAT (x); MPZ_CHECK_FORMAT (rem); mpz_mul (temp, x, x); /* Is square of result > argument? */ if (mpz_cmp (temp, x2) > 0) dump_abort (x2, x, rem); mpz_add_ui (temp2, x, 1); mpz_mul (temp2, temp2, temp2); /* Is square of (result + 1) <= argument? */ if (mpz_cmp (temp2, x2) <= 0) dump_abort (x2, x, rem); mpz_add (temp2, temp, rem); /* Is the remainder wrong? */ if (mpz_cmp (x2, temp2) != 0) dump_abort (x2, x, rem); } mpz_clear (bs); mpz_clear (x2); mpz_clear (x); mpz_clear (rem); mpz_clear (temp); mpz_clear (temp2); tests_end (); exit (0); }