void check_random (int argc, char *argv[]) { mpz_t x, s0, s1, s2, s3, m; mp_size_t xsize; int i; int reps = 100000; int bit0, bit1, bit2, bit3; unsigned long int bitindex; const char *s = ""; if (argc == 2) reps = atoi (argv[1]); mpz_init (x); mpz_init (s0); mpz_init (s1); mpz_init (s2); mpz_init (s3); mpz_init (m); for (i = 0; i < reps; i++) { xsize = urandom () % (2 * SIZE) - SIZE; mpz_random2 (x, xsize); bitindex = urandom () % SIZE; mpz_set (s0, x); bit0 = mpz_tstbit (x, bitindex); mpz_setbit (x, bitindex); MPZ_CHECK_FORMAT (x); mpz_set (s1, x); bit1 = mpz_tstbit (x, bitindex); mpz_clrbit (x, bitindex); MPZ_CHECK_FORMAT (x); mpz_set (s2, x); bit2 = mpz_tstbit (x, bitindex); mpz_setbit (x, bitindex); MPZ_CHECK_FORMAT (x); mpz_set (s3, x); bit3 = mpz_tstbit (x, bitindex); #define FAIL(str) do { s = str; goto fail; } while (0) if (bit1 != 1) FAIL ("bit1 != 1"); if (bit2 != 0) FAIL ("bit2 != 0"); if (bit3 != 1) FAIL ("bit3 != 1"); if (bit0 == 0) { if (mpz_cmp (s0, s1) == 0 || mpz_cmp (s0, s2) != 0 || mpz_cmp (s0, s3) == 0) abort (); } else { if (mpz_cmp (s0, s1) != 0 || mpz_cmp (s0, s2) == 0 || mpz_cmp (s0, s3) != 0) abort (); } if (mpz_cmp (s1, s2) == 0 || mpz_cmp (s1, s3) != 0) abort (); if (mpz_cmp (s2, s3) == 0) abort (); mpz_ui_pow_ui (m, 2L, bitindex); MPZ_CHECK_FORMAT (m); mpz_ior (x, s2, m); MPZ_CHECK_FORMAT (x); if (mpz_cmp (x, s3) != 0) abort (); mpz_com (m, m); MPZ_CHECK_FORMAT (m); mpz_and (x, s1, m); MPZ_CHECK_FORMAT (x); if (mpz_cmp (x, s2) != 0) abort (); } mpz_clear (x); mpz_clear (s0); mpz_clear (s1); mpz_clear (s2); mpz_clear (s3); mpz_clear (m); return; fail: printf ("%s\n", s); printf ("bitindex = %lu\n", bitindex); printf ("x = "); mpz_out_str (stdout, -16, x); printf (" hex\n"); exit (1); }
void check_monotonic (int argc, char **argv) { mpq_t a; mp_size_t size; int reps = 100; int i, j; double last_d, new_d; mpq_t qlast_d, qnew_d; mpq_t eps; if (argc == 2) reps = atoi (argv[1]); /* The idea here is to test the monotonousness of mpq_get_d by adding numbers to the numerator and denominator. */ mpq_init (a); mpq_init (eps); mpq_init (qlast_d); mpq_init (qnew_d); for (i = 0; i < reps; i++) { size = urandom () % SIZE - SIZE/2; mpz_random2 (mpq_numref (a), size); do { size = urandom () % SIZE - SIZE/2; mpz_random2 (mpq_denref (a), size); } while (mpz_cmp_ui (mpq_denref (a), 0) == 0); mpq_canonicalize (a); last_d = mpq_get_d (a); mpq_set_d (qlast_d, last_d); for (j = 0; j < 10; j++) { size = urandom () % EPSIZE + 1; mpz_random2 (mpq_numref (eps), size); size = urandom () % EPSIZE + 1; mpz_random2 (mpq_denref (eps), size); mpq_canonicalize (eps); mpq_add (a, a, eps); mpq_canonicalize (a); new_d = mpq_get_d (a); if (last_d > new_d) { printf ("\nERROR (test %d/%d): bad mpq_get_d results\n", i, j); printf ("last: %.16g\n", last_d); printf (" new: %.16g\n", new_d); dump (a); abort (); } mpq_set_d (qnew_d, new_d); MPQ_CHECK_FORMAT (qnew_d); if (mpq_cmp (qlast_d, qnew_d) > 0) { printf ("ERROR (test %d/%d): bad mpq_set_d results\n", i, j); printf ("last: %.16g\n", last_d); dump (qlast_d); printf (" new: %.16g\n", new_d); dump (qnew_d); abort (); } last_d = new_d; mpq_set (qlast_d, qnew_d); } } mpq_clear (a); mpq_clear (eps); mpq_clear (qlast_d); mpq_clear (qnew_d); }
void check_ref (void) { static const int offset[] = { -2, -1, 0, 1, 2, 3 }; mpz_t z; int test, neg, sought, oindex, o; mp_size_t size, isize; unsigned long start, got, want; mpz_init (z); for (test = 0; test < 5; test++) { for (size = 0; size < 5; size++) { mpz_random2 (z, size); for (neg = 0; neg <= 1; neg++) { if (neg) mpz_neg (z, z); for (isize = 0; isize <= size; isize++) { for (oindex = 0; oindex < numberof (offset); oindex++) { o = offset[oindex]; if ((int) isize*GMP_NUMB_BITS < -o) continue; /* start would be negative */ start = isize*GMP_NUMB_BITS + o; for (sought = 0; sought <= 1; sought++) { if (sought == 0) { got = mpz_scan0 (z, start); want = refmpz_scan0 (z, start); } else { got = mpz_scan1 (z, start); want = refmpz_scan1 (z, start); } if (got != want) { printf ("wrong at test=%d, size=%ld, neg=%d, start=%lu, sought=%d\n", test, size, neg, start, sought); printf (" z 0x"); mpz_out_str (stdout, -16, z); printf ("\n"); printf (" got=%lu, want=%lu\n", got, want); exit (1); } } } } } } } mpz_clear (z); }