int main(int argc, char **argv) { int v, failures; v = (argc > 1) ? atoi(argv[1]) : 0; failures = 0; failures += test_div(v, "20000", "100", "200", "0"); failures += test_div(v, "20001", "100", "200", "1"); failures += test_div(v, "20013", "200", "100", "13"); failures += test_div(v, "287713", "571", "503", "500"); failures += test_div(v, "-13", "6", "-2", "1"); failures += test_div(v, "20", "-7", "-2", "6"); failures += test_div(v, "-100", "-9", "11", "1"); /* lldiv_t result = lldiv(5088824049625,33554393); result.quot: 151658, result.remainder: 31916031 */ failures += test_div(v, "5088824049625", "33554393", "151658", "31916031"); failures += test_div_by_zero(v); if (failures) { Test_log_error2("%d failures in %s\n", failures, __FILE__); } return cap_failures(failures); }
static void check_convergence (void) { mpfr_t x, y; int i, j; mpfr_init2(x, 130); mpfr_set_str_binary(x, "0.1011111101011010101000001010011111101000011100011101010011111011000011001010000000111100100111110011001010110100100001001000111001E6944"); mpfr_init2(y, 130); mpfr_set_ui(y, 5, MPFR_RNDN); test_div(x, x, y, MPFR_RNDD); /* exact division */ mpfr_set_prec(x, 64); mpfr_set_prec(y, 64); mpfr_set_str_binary(x, "0.10010010011011010100101001010111100000101110010010101E55"); mpfr_set_str_binary(y, "0.1E585"); test_div(x, x, y, MPFR_RNDN); mpfr_set_str_binary(y, "0.10010010011011010100101001010111100000101110010010101E-529"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_div for prec=64, rnd=MPFR_RNDN\n"); printf ("got "); mpfr_print_binary(x); puts (""); printf ("instead of "); mpfr_print_binary(y); puts (""); exit(1); } for (i=32; i<=64; i+=32) { mpfr_set_prec(x, i); mpfr_set_prec(y, i); mpfr_set_ui(x, 1, MPFR_RNDN); RND_LOOP(j) { mpfr_set_ui (y, 1, MPFR_RNDN); test_div (y, x, y, (mpfr_rnd_t) j); if (mpfr_cmp_ui (y, 1)) { printf ("mpfr_div failed for x=1.0, y=1.0, prec=%d rnd=%s\n", i, mpfr_print_rnd_mode ((mpfr_rnd_t) j)); printf ("got "); mpfr_print_binary(y); puts (""); exit (1); } } } mpfr_clear (x); mpfr_clear (y); }
static void check24 (const char *Ns, const char *Ds, mpfr_rnd_t rnd_mode, const char *Qs) { mpfr_t q, n, d; mpfr_inits2 (24, q, n, d, (mpfr_ptr) 0); mpfr_set_str1 (n, Ns); mpfr_set_str1 (d, Ds); test_div(q, n, d, rnd_mode); if (mpfr_cmp_str1 (q, Qs) ) { printf ("mpfr_div failed for n=%s, d=%s, prec=24, rnd_mode=%s\n", Ns, Ds, mpfr_print_rnd_mode(rnd_mode)); printf ("expected quotient is %s, got ", Qs); mpfr_out_str(stdout,10,0,q, MPFR_RNDN); putchar('\n'); exit (1); } mpfr_clears (q, n, d, (mpfr_ptr) 0); }
int test_main(int, char*[]) { BOOST_CHECK(test_mul(2, 3, 5, 7)); BOOST_CHECK(test_mul(2, 3, -5, 7)); BOOST_CHECK(test_mul(2, 3, -7, -5)); BOOST_CHECK(test_mul(-2, 3, 5, 7)); BOOST_CHECK(test_mul(-2, 3, -5, 7)); BOOST_CHECK(test_mul(-2, 3, -7, -5)); BOOST_CHECK(test_mul(-3, -2, 5, 7)); BOOST_CHECK(test_mul(-3, -2, -5, 7)); BOOST_CHECK(test_mul(-3, -2, -7, -5)); BOOST_CHECK(test_mul1(3, 5, 7)); BOOST_CHECK(test_mul1(3, -5, 7)); BOOST_CHECK(test_mul1(3, -7, -5)); BOOST_CHECK(test_mul1(-3, 5, 7)); BOOST_CHECK(test_mul1(-3, -5, 7)); BOOST_CHECK(test_mul1(-3, -7, -5)); BOOST_CHECK(test_div(30, 42, 2, 3)); BOOST_CHECK(test_div(30, 42, -3, -2)); BOOST_CHECK(test_div(-30, 42, 2, 3)); BOOST_CHECK(test_div(-30, 42, -3, -2)); BOOST_CHECK(test_div(-42, -30, 2, 3)); BOOST_CHECK(test_div(-42, -30, -3, -2)); BOOST_CHECK(test_div1(30, 42, 3)); BOOST_CHECK(test_div1(30, 42, -3)); BOOST_CHECK(test_div1(-30, 42, 3)); BOOST_CHECK(test_div1(-30, 42, -3)); BOOST_CHECK(test_div1(-42, -30, 3)); BOOST_CHECK(test_div1(-42, -30, -3)); BOOST_CHECK(test_div2(30, 2, 3)); BOOST_CHECK(test_div2(30, -3, -2)); BOOST_CHECK(test_div2(-30, 2, 3)); BOOST_CHECK(test_div2(-30, -3, -2)); BOOST_CHECK(test_square(2, 3)); BOOST_CHECK(test_square(-2, 3)); BOOST_CHECK(test_square(-3, 2)); BOOST_CHECK(test_sqrt(2, 3)); BOOST_CHECK(test_sqrt(5, 7)); BOOST_CHECK(test_sqrt(-1, 2)); return 0; }
static void check4 (const char *Ns, const char *Ds, mpfr_rnd_t rnd_mode, int p, const char *Qs) { mpfr_t q, n, d; mpfr_inits2 (p, q, n, d, (mpfr_ptr) 0); mpfr_set_str1 (n, Ns); mpfr_set_str1 (d, Ds); test_div(q, n, d, rnd_mode); if (mpfr_cmp_str (q, Qs, ((p==53) ? 10 : 2), MPFR_RNDN) ) { printf ("mpfr_div failed for n=%s, d=%s, p=%d, rnd_mode=%s\n", Ns, Ds, p, mpfr_print_rnd_mode (rnd_mode)); printf ("got ");mpfr_print_binary(q); mpfr_set_str (q, Qs, ((p==53) ? 10 : 2), MPFR_RNDN); printf("\nexpected "); mpfr_print_binary(q); putchar('\n'); exit (1); } mpfr_clears (q, n, d, (mpfr_ptr) 0); }
static void check_64(void) { mpfr_t x,y,z; mpfr_inits2 (64, x, y, z, (mpfr_ptr) 0); mpfr_set_str_binary(x, "1.00100100110110101001010010101111000001011100100101010000000000E54"); mpfr_set_str_binary(y, "1.00000000000000000000000000000000000000000000000000000000000000E584"); test_div(z, x, y, MPFR_RNDU); if (mpfr_cmp_str (z, "0.1001001001101101010010100101011110000010111001001010100000000000E-529", 2, MPFR_RNDN)) { printf("Error for tdiv for MPFR_RNDU and p=64\nx="); mpfr_print_binary(x); printf("\ny="); mpfr_print_binary(y); printf("\ngot "); mpfr_print_binary(z); printf("\nexpected 0.1001001001101101010010100101011110000010111001001010100000000000E-529\n"); exit(1); } mpfr_clears (x, y, z, (mpfr_ptr) 0); }
int main(int argc, char *argv[]) { BN_CTX *ctx; BIO *out; char *outfile = NULL; results = 0; argc--; argv++; while (argc >= 1) { if (strcmp(*argv, "-results") == 0) results = 1; else if (strcmp(*argv, "-out") == 0) { if (--argc < 1) break; outfile= *(++argv); } argc--; argv++; } ctx = BN_CTX_new(); if (ctx == NULL) exit(1); out = BIO_new(BIO_s_file()); if (out == NULL) exit(1); if (outfile == NULL) { BIO_set_fp(out, stdout, BIO_NOCLOSE); } else { if (!BIO_write_filename(out, outfile)) { perror(outfile); exit(1); } } if (!results) BIO_puts(out, "obase=16\nibase=16\n"); message(out, "BN_add"); if (!test_add(out)) goto err; (void)BIO_flush(out); message(out, "BN_sub"); if (!test_sub(out)) goto err; (void)BIO_flush(out); message(out, "BN_lshift1"); if (!test_lshift1(out)) goto err; (void)BIO_flush(out); message(out, "BN_lshift (fixed)"); if (!test_lshift(out, ctx, BN_bin2bn(lst, sizeof(lst) - 1, NULL))) goto err; (void)BIO_flush(out); message(out, "BN_lshift"); if (!test_lshift(out, ctx, NULL)) goto err; (void)BIO_flush(out); message(out, "BN_rshift1"); if (!test_rshift1(out)) goto err; (void)BIO_flush(out); message(out, "BN_rshift"); if (!test_rshift(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_sqr"); if (!test_sqr(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mul"); if (!test_mul(out)) goto err; (void)BIO_flush(out); message(out, "BN_div"); if (!test_div(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_div_word"); if (!test_div_word(out)) goto err; (void)BIO_flush(out); message(out, "BN_div_recp"); if (!test_div_recp(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mod"); if (!test_mod(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mod_mul"); if (!test_mod_mul(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mont"); if (!test_mont(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mod_exp"); if (!test_mod_exp(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mod_exp_mont_consttime"); if (!test_mod_exp_mont_consttime(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_exp"); if (!test_exp(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_kronecker"); if (!test_kron(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mod_sqrt"); if (!test_sqrt(out, ctx)) goto err; (void)BIO_flush(out); message(out, "Modexp with different sizes"); if (!test_mod_exp_sizes(out, ctx)) goto err; (void)BIO_flush(out); #ifndef OPENSSL_NO_EC2M message(out, "BN_GF2m_add"); if (!test_gf2m_add(out)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod"); if (!test_gf2m_mod(out)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_mul"); if (!test_gf2m_mod_mul(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_sqr"); if (!test_gf2m_mod_sqr(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_inv"); if (!test_gf2m_mod_inv(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_div"); if (!test_gf2m_mod_div(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_exp"); if (!test_gf2m_mod_exp(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_sqrt"); if (!test_gf2m_mod_sqrt(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_solve_quad"); if (!test_gf2m_mod_solve_quad(out, ctx)) goto err; (void)BIO_flush(out); #endif BN_CTX_free(ctx); BIO_free(out); exit(0); err: BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices * the failure, see test_bn in test/Makefile.ssl*/ (void)BIO_flush(out); ERR_load_crypto_strings(); ERR_print_errors_fp(stderr); exit(1); }
static void check_special (void) { mpfr_t a, d, q; mpfr_exp_t emax, emin; int i; mpfr_init2 (a, 100L); mpfr_init2 (d, 100L); mpfr_init2 (q, 100L); /* 1/nan == nan */ mpfr_set_ui (a, 1L, MPFR_RNDN); MPFR_SET_NAN (d); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN); /* nan/1 == nan */ MPFR_SET_NAN (a); mpfr_set_ui (d, 1L, MPFR_RNDN); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN); /* +inf/1 == +inf */ MPFR_SET_INF (a); MPFR_SET_POS (a); mpfr_set_ui (d, 1L, MPFR_RNDN); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q)); MPFR_ASSERTN (mpfr_sgn (q) > 0); MPFR_ASSERTN (__gmpfr_flags == 0); /* +inf/-1 == -inf */ MPFR_SET_INF (a); MPFR_SET_POS (a); mpfr_set_si (d, -1, MPFR_RNDN); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q)); MPFR_ASSERTN (mpfr_sgn (q) < 0); MPFR_ASSERTN (__gmpfr_flags == 0); /* -inf/1 == -inf */ MPFR_SET_INF (a); MPFR_SET_NEG (a); mpfr_set_ui (d, 1L, MPFR_RNDN); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q)); MPFR_ASSERTN (mpfr_sgn (q) < 0); MPFR_ASSERTN (__gmpfr_flags == 0); /* -inf/-1 == +inf */ MPFR_SET_INF (a); MPFR_SET_NEG (a); mpfr_set_si (d, -1, MPFR_RNDN); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q)); MPFR_ASSERTN (mpfr_sgn (q) > 0); MPFR_ASSERTN (__gmpfr_flags == 0); /* 1/+inf == +0 */ mpfr_set_ui (a, 1L, MPFR_RNDN); MPFR_SET_INF (d); MPFR_SET_POS (d); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_number_p (q)); MPFR_ASSERTN (mpfr_sgn (q) == 0); MPFR_ASSERTN (MPFR_IS_POS (q)); MPFR_ASSERTN (__gmpfr_flags == 0); /* 1/-inf == -0 */ mpfr_set_ui (a, 1L, MPFR_RNDN); MPFR_SET_INF (d); MPFR_SET_NEG (d); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_number_p (q)); MPFR_ASSERTN (mpfr_sgn (q) == 0); MPFR_ASSERTN (MPFR_IS_NEG (q)); MPFR_ASSERTN (__gmpfr_flags == 0); /* -1/+inf == -0 */ mpfr_set_si (a, -1, MPFR_RNDN); MPFR_SET_INF (d); MPFR_SET_POS (d); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_number_p (q)); MPFR_ASSERTN (mpfr_sgn (q) == 0); MPFR_ASSERTN (MPFR_IS_NEG (q)); MPFR_ASSERTN (__gmpfr_flags == 0); /* -1/-inf == +0 */ mpfr_set_si (a, -1, MPFR_RNDN); MPFR_SET_INF (d); MPFR_SET_NEG (d); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_number_p (q)); MPFR_ASSERTN (mpfr_sgn (q) == 0); MPFR_ASSERTN (MPFR_IS_POS (q)); MPFR_ASSERTN (__gmpfr_flags == 0); /* 0/0 == nan */ mpfr_set_ui (a, 0L, MPFR_RNDN); mpfr_set_ui (d, 0L, MPFR_RNDN); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN); /* +inf/+inf == nan */ MPFR_SET_INF (a); MPFR_SET_POS (a); MPFR_SET_INF (d); MPFR_SET_POS (d); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN); /* 1/+0 = +inf */ mpfr_set_ui (a, 1, MPFR_RNDZ); mpfr_set_ui (d, 0, MPFR_RNDZ); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0); MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0); /* 1/-0 = -inf */ mpfr_set_ui (a, 1, MPFR_RNDZ); mpfr_set_ui (d, 0, MPFR_RNDZ); mpfr_neg (d, d, MPFR_RNDZ); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0); MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0); /* -1/+0 = -inf */ mpfr_set_si (a, -1, MPFR_RNDZ); mpfr_set_ui (d, 0, MPFR_RNDZ); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0); MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0); /* -1/-0 = +inf */ mpfr_set_si (a, -1, MPFR_RNDZ); mpfr_set_ui (d, 0, MPFR_RNDZ); mpfr_neg (d, d, MPFR_RNDZ); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0); MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0); /* +inf/+0 = +inf */ MPFR_SET_INF (a); MPFR_SET_POS (a); mpfr_set_ui (d, 0, MPFR_RNDZ); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0); MPFR_ASSERTN (__gmpfr_flags == 0); /* +inf/-0 = -inf */ MPFR_SET_INF (a); MPFR_SET_POS (a); mpfr_set_ui (d, 0, MPFR_RNDZ); mpfr_neg (d, d, MPFR_RNDZ); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0); MPFR_ASSERTN (__gmpfr_flags == 0); /* -inf/+0 = -inf */ MPFR_SET_INF (a); MPFR_SET_NEG (a); mpfr_set_ui (d, 0, MPFR_RNDZ); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0); MPFR_ASSERTN (__gmpfr_flags == 0); /* -inf/-0 = +inf */ MPFR_SET_INF (a); MPFR_SET_NEG (a); mpfr_set_ui (d, 0, MPFR_RNDZ); mpfr_neg (d, d, MPFR_RNDZ); mpfr_clear_flags (); MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0); MPFR_ASSERTN (__gmpfr_flags == 0); /* check overflow */ emax = mpfr_get_emax (); set_emax (1); mpfr_set_ui (a, 1, MPFR_RNDZ); mpfr_set_ui (d, 1, MPFR_RNDZ); mpfr_div_2exp (d, d, 1, MPFR_RNDZ); mpfr_clear_flags (); test_div (q, a, d, MPFR_RNDU); /* 1 / 0.5 = 2 -> overflow */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0); MPFR_ASSERTN (__gmpfr_flags == (MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT)); set_emax (emax); /* check underflow */ emin = mpfr_get_emin (); set_emin (-1); mpfr_set_ui (a, 1, MPFR_RNDZ); mpfr_div_2exp (a, a, 2, MPFR_RNDZ); mpfr_set_prec (d, mpfr_get_prec (q) + 8); for (i = -1; i <= 1; i++) { int sign; /* Test 2^(-2) / (+/- (2 + eps)), with eps < 0, eps = 0, eps > 0. -> underflow. With div.c r5513, this test fails for eps > 0 in MPFR_RNDN. */ mpfr_set_ui (d, 2, MPFR_RNDZ); if (i < 0) mpfr_nextbelow (d); if (i > 0) mpfr_nextabove (d); for (sign = 0; sign <= 1; sign++) { mpfr_clear_flags (); test_div (q, a, d, MPFR_RNDZ); /* result = 0 */ MPFR_ASSERTN (__gmpfr_flags == (MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT)); MPFR_ASSERTN (sign ? MPFR_IS_NEG (q) : MPFR_IS_POS (q)); MPFR_ASSERTN (MPFR_IS_ZERO (q)); mpfr_clear_flags (); test_div (q, a, d, MPFR_RNDN); /* result = 0 iff eps >= 0 */ MPFR_ASSERTN (__gmpfr_flags == (MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT)); MPFR_ASSERTN (sign ? MPFR_IS_NEG (q) : MPFR_IS_POS (q)); if (i < 0) mpfr_nexttozero (q); MPFR_ASSERTN (MPFR_IS_ZERO (q)); mpfr_neg (d, d, MPFR_RNDN); } } set_emin (emin); mpfr_clear (a); mpfr_clear (d); mpfr_clear (q); }
static void check_inexact (void) { mpfr_t x, y, z, u; mpfr_prec_t px, py, pu; int inexact, cmp; mpfr_rnd_t rnd; mpfr_init (x); mpfr_init (y); mpfr_init (z); mpfr_init (u); mpfr_set_prec (x, 28); mpfr_set_prec (y, 28); mpfr_set_prec (z, 1023); mpfr_set_str_binary (x, "0.1000001001101101111100010011E0"); mpfr_set_str (z, "48284762641021308813686974720835219181653367326353400027913400579340343320519877153813133510034402932651132854764198688352364361009429039801248971901380781746767119334993621199563870113045276395603170432175354501451429471578325545278975153148347684600400321033502982713296919861760382863826626093689036010394", 10, MPFR_RNDN); mpfr_div (x, x, z, MPFR_RNDN); mpfr_set_str_binary (y, "0.1111001011001101001001111100E-1023"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_div for prec=28, RNDN\n"); printf ("Expected "); mpfr_dump (y); printf ("Got "); mpfr_dump (x); exit (1); } mpfr_set_prec (x, 53); mpfr_set_str_binary (x, "0.11101100110010100011011000000100001111011111110010101E0"); mpfr_set_prec (u, 127); mpfr_set_str_binary (u, "0.1000001100110110110101110110101101111000110000001111111110000000011111001010110100110010111111111101000001011011101011101101000E-2"); mpfr_set_prec (y, 95); inexact = test_div (y, x, u, MPFR_RNDN); if (inexact != (cmp = get_inexact (y, x, u))) { printf ("Wrong inexact flag (0): expected %d, got %d\n", cmp, inexact); printf ("x="); mpfr_out_str (stdout, 10, 99, x, MPFR_RNDN); printf ("\n"); printf ("u="); mpfr_out_str (stdout, 10, 99, u, MPFR_RNDN); printf ("\n"); printf ("y="); mpfr_out_str (stdout, 10, 99, y, MPFR_RNDN); printf ("\n"); exit (1); } mpfr_set_prec (x, 33); mpfr_set_str_binary (x, "0.101111100011011101010011101100001E0"); mpfr_set_prec (u, 2); mpfr_set_str_binary (u, "0.1E0"); mpfr_set_prec (y, 28); if ((inexact = test_div (y, x, u, MPFR_RNDN) >= 0)) { printf ("Wrong inexact flag (1): expected -1, got %d\n", inexact); exit (1); } mpfr_set_prec (x, 129); mpfr_set_str_binary (x, "0.111110101111001100000101011100101100110011011101010001000110110101100101000010000001110110100001101010001010100010001111001101010E-2"); mpfr_set_prec (u, 15); mpfr_set_str_binary (u, "0.101101000001100E-1"); mpfr_set_prec (y, 92); if ((inexact = test_div (y, x, u, MPFR_RNDN)) <= 0) { printf ("Wrong inexact flag for rnd=MPFR_RNDN(1): expected 1, got %d\n", inexact); mpfr_dump (x); mpfr_dump (u); mpfr_dump (y); exit (1); } for (px=2; px<MAX_PREC; px++) { mpfr_set_prec (x, px); mpfr_urandomb (x, RANDS); for (pu=2; pu<=MAX_PREC; pu++) { mpfr_set_prec (u, pu); do { mpfr_urandomb (u, RANDS); } while (mpfr_cmp_ui (u, 0) == 0); { py = MPFR_PREC_MIN + (randlimb () % (MAX_PREC - MPFR_PREC_MIN)); mpfr_set_prec (y, py); mpfr_set_prec (z, py + pu); { rnd = RND_RAND (); inexact = test_div (y, x, u, rnd); if (mpfr_mul (z, y, u, rnd)) { printf ("z <- y * u should be exact\n"); exit (1); } cmp = mpfr_cmp (z, x); if (((inexact == 0) && (cmp != 0)) || ((inexact > 0) && (cmp <= 0)) || ((inexact < 0) && (cmp >= 0))) { printf ("Wrong inexact flag for rnd=%s\n", mpfr_print_rnd_mode(rnd)); printf ("expected %d, got %d\n", cmp, inexact); printf ("x="); mpfr_print_binary (x); puts (""); printf ("u="); mpfr_print_binary (u); puts (""); printf ("y="); mpfr_print_binary (y); puts (""); printf ("y*u="); mpfr_print_binary (z); puts (""); exit (1); } } } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (u); }
static void check_lowr (void) { mpfr_t x, y, z, z2, z3, tmp; int k, c, c2; mpfr_init2 (x, 1000); mpfr_init2 (y, 100); mpfr_init2 (tmp, 850); mpfr_init2 (z, 10); mpfr_init2 (z2, 10); mpfr_init2 (z3, 50); for (k = 1; k < KMAX; k++) { do { mpfr_urandomb (z, RANDS); } while (mpfr_cmp_ui (z, 0) == 0); do { mpfr_urandomb (tmp, RANDS); } while (mpfr_cmp_ui (tmp, 0) == 0); mpfr_mul (x, z, tmp, MPFR_RNDN); /* exact */ c = test_div (z2, x, tmp, MPFR_RNDN); if (c || mpfr_cmp (z2, z)) { printf ("Error in mpfr_div rnd=MPFR_RNDN\n"); printf ("got "); mpfr_print_binary(z2); puts (""); printf ("instead of "); mpfr_print_binary(z); puts (""); printf ("inex flag = %d, expected 0\n", c); exit (1); } } /* x has still precision 1000, z precision 10, and tmp prec 850 */ mpfr_set_prec (z2, 9); for (k = 1; k < KMAX; k++) { mpfr_urandomb (z, RANDS); do { mpfr_urandomb (tmp, RANDS); } while (mpfr_cmp_ui (tmp, 0) == 0); mpfr_mul (x, z, tmp, MPFR_RNDN); /* exact */ c = test_div (z2, x, tmp, MPFR_RNDN); /* since z2 has one less bit that z, either the division is exact if z is representable on 9 bits, or we have an even round case */ c2 = get_inexact (z2, x, tmp); if ((mpfr_cmp (z2, z) == 0 && c) || inex_cmp (c, c2)) { printf ("Error in mpfr_div rnd=MPFR_RNDN\n"); printf ("got "); mpfr_print_binary(z2); puts (""); printf ("instead of "); mpfr_print_binary(z); puts (""); printf ("inex flag = %d, expected %d\n", c, c2); exit (1); } else if (c == 2) { mpfr_nexttoinf (z); if (mpfr_cmp(z2, z)) { printf ("Error in mpfr_div [even rnd?] rnd=MPFR_RNDN\n"); printf ("Dividing "); printf ("got "); mpfr_print_binary(z2); puts (""); printf ("instead of "); mpfr_print_binary(z); puts (""); printf ("inex flag = %d\n", 1); exit (1); } } else if (c == -2) { mpfr_nexttozero (z); if (mpfr_cmp(z2, z)) { printf ("Error in mpfr_div [even rnd?] rnd=MPFR_RNDN\n"); printf ("Dividing "); printf ("got "); mpfr_print_binary(z2); puts (""); printf ("instead of "); mpfr_print_binary(z); puts (""); printf ("inex flag = %d\n", 1); exit (1); } } } mpfr_set_prec(x, 1000); mpfr_set_prec(y, 100); mpfr_set_prec(tmp, 850); mpfr_set_prec(z, 10); mpfr_set_prec(z2, 10); /* almost exact divisions */ for (k = 1; k < KMAX; k++) { do { mpfr_urandomb (z, RANDS); } while (mpfr_cmp_ui (z, 0) == 0); do { mpfr_urandomb (tmp, RANDS); } while (mpfr_cmp_ui (tmp, 0) == 0); mpfr_mul(x, z, tmp, MPFR_RNDN); mpfr_set(y, tmp, MPFR_RNDD); mpfr_nexttoinf (x); c = test_div(z2, x, y, MPFR_RNDD); test_div(z3, x, y, MPFR_RNDD); mpfr_set(z, z3, MPFR_RNDD); if (c != -1 || mpfr_cmp(z2, z)) { printf ("Error in mpfr_div rnd=MPFR_RNDD\n"); printf ("got "); mpfr_print_binary(z2); puts (""); printf ("instead of "); mpfr_print_binary(z); puts (""); printf ("inex flag = %d\n", c); exit (1); } mpfr_set (y, tmp, MPFR_RNDU); test_div (z3, x, y, MPFR_RNDU); mpfr_set (z, z3, MPFR_RNDU); c = test_div (z2, x, y, MPFR_RNDU); if (c != 1 || mpfr_cmp (z2, z)) { printf ("Error in mpfr_div rnd=MPFR_RNDU\n"); printf ("u="); mpfr_dump (x); printf ("v="); mpfr_dump (y); printf ("got "); mpfr_print_binary (z2); puts (""); printf ("instead of "); mpfr_print_binary (z); puts (""); printf ("inex flag = %d\n", c); exit (1); } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (z2); mpfr_clear (z3); mpfr_clear (tmp); }
static void check_hard (void) { mpfr_t u, v, q, q2; mpfr_prec_t precu, precv, precq; int rnd; int inex, inex2, i, j; mpfr_init (q); mpfr_init (q2); mpfr_init (u); mpfr_init (v); for (precq = MPFR_PREC_MIN; precq <= 64; precq ++) { mpfr_set_prec (q, precq); mpfr_set_prec (q2, precq + 1); for (j = 0; j < 2; j++) { if (j == 0) { do { mpfr_urandomb (q2, RANDS); } while (mpfr_cmp_ui (q2, 0) == 0); } else /* use q2=1 */ mpfr_set_ui (q2, 1, MPFR_RNDN); for (precv = precq; precv <= 10 * precq; precv += precq) { mpfr_set_prec (v, precv); do { mpfr_urandomb (v, RANDS); } while (mpfr_cmp_ui (v, 0) == 0); for (precu = precq; precu <= 10 * precq; precu += precq) { mpfr_set_prec (u, precu); mpfr_mul (u, v, q2, MPFR_RNDN); mpfr_nextbelow (u); for (i = 0; i <= 2; i++) { RND_LOOP(rnd) { inex = test_div (q, u, v, (mpfr_rnd_t) rnd); inex2 = get_inexact (q, u, v); if (inex_cmp (inex, inex2)) { printf ("Wrong inexact flag for rnd=%s: expected %d, got %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) rnd), inex2, inex); printf ("u= "); mpfr_dump (u); printf ("v= "); mpfr_dump (v); printf ("q= "); mpfr_dump (q); mpfr_set_prec (q2, precq + precv); mpfr_mul (q2, q, v, MPFR_RNDN); printf ("q*v="); mpfr_dump (q2); exit (1); } } mpfr_nextabove (u); } } } } } mpfr_clear (q); mpfr_clear (q2); mpfr_clear (u); mpfr_clear (v); }
static void check_nan (void) { mpfr_t a, d, q; mp_exp_t emax, emin; mpfr_init2 (a, 100L); mpfr_init2 (d, 100L); mpfr_init2 (q, 100L); /* 1/nan == nan */ mpfr_set_ui (a, 1L, GMP_RNDN); MPFR_SET_NAN (d); MPFR_ASSERTN (test_div (q, a, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_nan_p (q)); /* nan/1 == nan */ MPFR_SET_NAN (a); mpfr_set_ui (d, 1L, GMP_RNDN); MPFR_ASSERTN (test_div (q, a, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_nan_p (q)); /* +inf/1 == +inf */ MPFR_CLEAR_FLAGS (a); MPFR_SET_INF (a); MPFR_SET_POS (a); mpfr_set_ui (d, 1L, GMP_RNDN); MPFR_ASSERTN (test_div (q, a, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q)); MPFR_ASSERTN (mpfr_sgn (q) > 0); /* 1/+inf == 0 */ mpfr_set_ui (a, 1L, GMP_RNDN); MPFR_CLEAR_FLAGS (d); MPFR_SET_INF (d); MPFR_SET_POS (d); MPFR_ASSERTN (test_div (q, a, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_number_p (q)); MPFR_ASSERTN (mpfr_sgn (q) == 0); /* 0/0 == nan */ mpfr_set_ui (a, 0L, GMP_RNDN); mpfr_set_ui (d, 0L, GMP_RNDN); MPFR_ASSERTN (test_div (q, a, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_nan_p (q)); /* +inf/+inf == nan */ MPFR_CLEAR_FLAGS (a); MPFR_SET_INF (a); MPFR_SET_POS (a); MPFR_CLEAR_FLAGS (d); MPFR_SET_INF (d); MPFR_SET_POS (d); MPFR_ASSERTN (test_div (q, a, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_nan_p (q)); /* 1/+0 = +Inf */ mpfr_set_ui (a, 1, GMP_RNDZ); mpfr_set_ui (d, 0, GMP_RNDZ); MPFR_ASSERTN (test_div (q, a, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0); /* 1/-0 = -Inf */ mpfr_set_ui (a, 1, GMP_RNDZ); mpfr_set_ui (d, 0, GMP_RNDZ); mpfr_neg (d, d, GMP_RNDZ); MPFR_ASSERTN (test_div (q, a, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0); /* -1/+0 = -Inf */ mpfr_set_si (a, -1, GMP_RNDZ); mpfr_set_ui (d, 0, GMP_RNDZ); MPFR_ASSERTN (test_div (q, a, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0); /* -1/-0 = +Inf */ mpfr_set_si (a, -1, GMP_RNDZ); mpfr_set_ui (d, 0, GMP_RNDZ); mpfr_neg (d, d, GMP_RNDZ); MPFR_ASSERTN (test_div (q, a, d, GMP_RNDZ) == 0); /* exact */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0); /* check overflow */ emax = mpfr_get_emax (); set_emax (1); mpfr_set_ui (a, 1, GMP_RNDZ); mpfr_set_ui (d, 1, GMP_RNDZ); mpfr_div_2exp (d, d, 1, GMP_RNDZ); test_div (q, a, d, GMP_RNDU); /* 1 / 0.5 = 2 -> overflow */ MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0); set_emax (emax); /* check underflow */ emin = mpfr_get_emin (); set_emin (-1); mpfr_set_ui (a, 1, GMP_RNDZ); mpfr_div_2exp (a, a, 2, GMP_RNDZ); mpfr_set_ui (d, 2, GMP_RNDZ); test_div (q, a, d, GMP_RNDZ); /* 0.5*2^(-2) -> underflow */ MPFR_ASSERTN (mpfr_cmp_ui (q, 0) == 0 && MPFR_IS_POS (q)); test_div (q, a, d, GMP_RNDN); /* 0.5*2^(-2) -> underflow */ MPFR_ASSERTN (mpfr_cmp_ui (q, 0) == 0 && MPFR_IS_POS (q)); set_emin (emin); mpfr_clear (a); mpfr_clear (d); mpfr_clear (q); }
int main(int argc, char *argv[]) { BN_CTX *ctx; BIO *out; char *outfile=NULL; results = 0; RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */ argc--; argv++; while (argc >= 1) { if (strcmp(*argv,"-results") == 0) results=1; else if (strcmp(*argv,"-out") == 0) { if (--argc < 1) break; outfile= *(++argv); } argc--; argv++; } ctx=BN_CTX_new(); if (ctx == NULL) EXIT(1); out=BIO_new(BIO_s_file()); if (out == NULL) EXIT(1); if (outfile == NULL) { BIO_set_fp(out,stdout,BIO_NOCLOSE); } else { if (!BIO_write_filename(out,outfile)) { perror(outfile); EXIT(1); } } if (!results) BIO_puts(out,"obase=16\nibase=16\n"); message(out,"BN_add"); if (!test_add(out)) goto err; BIO_flush(out); message(out,"BN_sub"); if (!test_sub(out)) goto err; BIO_flush(out); message(out,"BN_lshift1"); if (!test_lshift1(out)) goto err; BIO_flush(out); message(out,"BN_lshift (fixed)"); if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL))) goto err; BIO_flush(out); message(out,"BN_lshift"); if (!test_lshift(out,ctx,NULL)) goto err; BIO_flush(out); message(out,"BN_rshift1"); if (!test_rshift1(out)) goto err; BIO_flush(out); message(out,"BN_rshift"); if (!test_rshift(out,ctx)) goto err; BIO_flush(out); message(out,"BN_sqr"); if (!test_sqr(out,ctx)) goto err; BIO_flush(out); message(out,"BN_mul"); if (!test_mul(out)) goto err; BIO_flush(out); message(out,"BN_div"); if (!test_div(out,ctx)) goto err; BIO_flush(out); message(out,"BN_div_recp"); if (!test_div_recp(out,ctx)) goto err; BIO_flush(out); message(out,"BN_mod"); if (!test_mod(out,ctx)) goto err; BIO_flush(out); message(out,"BN_mod_mul"); if (!test_mod_mul(out,ctx)) goto err; BIO_flush(out); message(out,"BN_mont"); if (!test_mont(out,ctx)) goto err; BIO_flush(out); message(out,"BN_mod_exp"); if (!test_mod_exp(out,ctx)) goto err; BIO_flush(out); message(out,"BN_exp"); if (!test_exp(out,ctx)) goto err; BIO_flush(out); message(out,"BN_kronecker"); if (!test_kron(out,ctx)) goto err; BIO_flush(out); message(out,"BN_mod_sqrt"); if (!test_sqrt(out,ctx)) goto err; BIO_flush(out); BN_CTX_free(ctx); BIO_free(out); /**/ EXIT(0); err: BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices * the failure, see test_bn in test/Makefile.ssl*/ BIO_flush(out); ERR_load_crypto_strings(); ERR_print_errors_fp(stderr); EXIT(1); return(1); }
int main(int argc , char ** argv) { int_vector_type * int_vector = int_vector_alloc( 0 , 99); test_abort(); test_assert_int_equal( -1 , int_vector_index(int_vector , 100)); test_assert_int_equal( -1 , int_vector_index_sorted(int_vector , 100)); test_assert_true( int_vector_is_instance( int_vector )); test_assert_false( double_vector_is_instance( int_vector )); int_vector_iset( int_vector , 2 , 0); int_vector_insert( int_vector , 2 , 77 ); int_vector_iset( int_vector , 5 , -10); assert_equal( int_vector_iget(int_vector , 0 ) == 99 ); assert_equal( int_vector_iget(int_vector , 1 ) == 99 ); assert_equal( int_vector_iget(int_vector , 2 ) == 77 ); assert_equal( int_vector_iget(int_vector , 3 ) == 00 ); assert_equal( int_vector_iget(int_vector , 4 ) == 99 ); assert_equal( int_vector_iget(int_vector , 5 ) == -10 ); { int N1 = 100000; int N2 = 10*N1; int_vector_type * v1 = int_vector_alloc( N1 , 0 ); int_vector_type * v2; int * data1 = int_vector_get_ptr( v1 ); int_vector_iset( v1 , N1 - 1, 99); int_vector_free_container( v1 ); v2 = int_vector_alloc( N2 , 0 ); int_vector_iset(v2 , N2 - 1, 77 ); test_assert_int_equal( data1[N1-1] , 99); int_vector_free( v2 ); free( data1 ); } test_assert_true( int_vector_init_range( int_vector , 100 , 1000 , 115 ) ); test_assert_int_equal( int_vector_iget( int_vector , 0 ) , 100); test_assert_int_equal( int_vector_iget( int_vector , 1 ) , 215); test_assert_int_equal( int_vector_iget( int_vector , 2 ) , 330); test_assert_int_equal( int_vector_iget( int_vector , 3 ) , 445); test_assert_int_equal( int_vector_get_last( int_vector ) , 1000); test_assert_false( int_vector_init_range( int_vector , 100 , -1000 , 115 ) ); test_assert_int_equal( int_vector_iget( int_vector , 0 ) , 100); test_assert_int_equal( int_vector_iget( int_vector , 1 ) , 215); test_assert_int_equal( int_vector_iget( int_vector , 2 ) , 330); test_assert_int_equal( int_vector_iget( int_vector , 3 ) , 445); test_assert_int_equal( int_vector_get_last( int_vector ) , 1000); { int_vector_type * v1 = int_vector_alloc(0,0); int_vector_type * v2 = int_vector_alloc(0,0); int_vector_append(v1 , 10); int_vector_append(v1 , 15); int_vector_append(v1 , 20); int_vector_append(v2 , 1); int_vector_append(v2 , 2); int_vector_append(v2 , 3); int_vector_append_vector( v1 , v2 ); test_assert_int_equal( int_vector_size( v1 ) , 6 ); test_assert_int_equal( int_vector_iget (v1 , 0 ), 10 ); test_assert_int_equal( int_vector_iget (v1 , 1 ), 15 ); test_assert_int_equal( int_vector_iget (v1 , 2 ), 20 ); test_assert_int_equal( int_vector_iget (v1 , 3 ), 1 ); test_assert_int_equal( int_vector_iget (v1 , 4 ), 2 ); test_assert_int_equal( int_vector_iget (v1 , 5 ), 3 ); int_vector_free( v1 ); int_vector_free( v2 ); } test_contains(); test_contains_sorted(); test_shift(); test_alloc(); test_div(); test_memcpy_from_data(); test_idel_insert(); test_del(); test_range_fill(); test_iset_block(); test_resize(); test_empty(); test_insert_double(); exit(0); }
int main(int argc, char *argv[]) { int ans; char *str, *strout; fmpz_poly_t zpoly; fmpz_poly_q_t qpoly1; mpz_t mpzzero, mpzone, mpztwo; mpq_t mpqzero, mpqone, mpqtwo, mpqtwoinv; FLINT_TEST_INIT(state); flint_printf("all... "); fflush(stdout); /* Accessing numerator and denominator ***********************************/ fmpz_poly_q_init(qpoly1); fmpz_poly_q_set_str(qpoly1, "2 -1 1/2 0 1"); str = "2 -1 1"; strout = fmpz_poly_get_str(fmpz_poly_q_numref(qpoly1)); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_numref: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); flint_printf(" qpoly1 = \""), fmpz_poly_q_print(qpoly1), flint_printf("\"\n"); abort(); } fmpz_poly_q_clear(qpoly1); flint_free(strout); fmpz_poly_q_init(qpoly1); fmpz_poly_q_set_str(qpoly1, "2 -1 1/2 0 1"); str = "2 0 1"; strout = fmpz_poly_get_str(fmpz_poly_q_denref(qpoly1)); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_denref: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); flint_free(strout); fmpz_poly_q_init(qpoly1); fmpz_poly_init(zpoly); fmpz_poly_q_set_str(qpoly1, "2 -1 1/2 0 1"); fmpz_poly_set(zpoly, fmpz_poly_q_numref(qpoly1)); str = "2 -1 1"; strout = fmpz_poly_get_str(zpoly); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_get_num: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_clear(zpoly); flint_free(strout); fmpz_poly_q_init(qpoly1); fmpz_poly_init(zpoly); fmpz_poly_q_set_str(qpoly1, "2 -1 1/2 0 1"); fmpz_poly_set(zpoly, fmpz_poly_q_denref(qpoly1)); str = "2 0 1"; strout = fmpz_poly_get_str(zpoly); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_get_den: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_clear(zpoly); flint_free(strout); fmpz_poly_q_init(qpoly1); fmpz_poly_init(zpoly); fmpz_poly_q_set_str(qpoly1, "1 1/1 1"); fmpz_poly_set_str(zpoly, "2 0 1"); fmpz_poly_set(fmpz_poly_q_numref(qpoly1), zpoly); str = "2 0 1"; strout = fmpz_poly_get_str(fmpz_poly_q_numref(qpoly1)); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_set_num: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_clear(zpoly); flint_free(strout); fmpz_poly_q_init(qpoly1); fmpz_poly_init(zpoly); fmpz_poly_q_set_str(qpoly1, "1 1/1 1"); fmpz_poly_set_str(zpoly, "2 0 1"); fmpz_poly_set(fmpz_poly_q_denref(qpoly1), zpoly); str = "2 0 1"; strout = fmpz_poly_get_str(fmpz_poly_q_denref(qpoly1)); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_set_den: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_clear(zpoly); flint_free(strout); /* Canonicalise **********************************************************/ fmpz_poly_q_init(qpoly1); str = "2 -1 1/2 0 1"; fmpz_poly_q_set_str(qpoly1, str); strout = fmpz_poly_q_get_str(qpoly1); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_canonicalize: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); flint_free(strout); fmpz_poly_q_init(qpoly1); str = "2 -1 -1/2 0 1"; fmpz_poly_q_set_str(qpoly1, "2 1 1/2 0 -1"); strout = fmpz_poly_q_get_str(qpoly1); ans = !strcmp("2 -1 -1/2 0 1", strout); if (!ans) { flint_printf("test_canonicalize: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } flint_free(strout); fmpz_poly_q_clear(qpoly1); /* Initialization, memory management and basic operations ****************/ test_set("0", "0"); test_set("0/1 1", "0"); test_set("3 -1 0 1/2 0 1", "3 -1 0 1/2 0 1"); test_set("3 -1 0 1/2 1 1", "2 -1 1"); test_set_si(-1, "1 -1"); test_set_si(13, "1 13"); test_set_si(0, "0"); test_swap("3 -1 0 1/2 0 1", "1 2/1 3", "1 2/1 3", "3 -1 0 1/2 0 1"); test_zero("0", "0"); test_zero("0/1 1", "0"); test_zero("3 -1 0 1/2 0 1", "0"); test_neg("0", "0"); test_neg("1 1/1 2", "1 -1/1 2"); test_neg("3 -1 0 1/2 0 1", "3 1 0 -1/2 0 1"); test_inv("1 1/1 2", "1 2"); test_inv("3 -1 0 1/2 0 1", "2 0 1/3 -1 0 1"); test_inv("3 -1 0 -1/2 0 1", "2 0 -1/3 1 0 1"); test_inv_inplace("1 1/1 2", "1 2"); test_inv_inplace("3 -1 0 1/2 0 1", "2 0 1/3 -1 0 1"); test_inv_inplace("3 -1 0 -1/2 0 1", "2 0 -1/3 1 0 1"); test_is_zero("0", 1); test_is_zero("0/1 1", 1); test_is_zero("3 -1 0 1/2 0 1", 0); test_is_zero("3 -1 0 1/2 1 1", 0); test_is_one("0", 0); test_is_one("0/1 1", 0); test_is_one("1 1/1 1", 1); test_is_one("2 1 1/2 1 1", 1); test_is_one("3 -1 0 1/2 0 1", 0); test_equal("1 1/1 2", "1 1/1 2", 1); test_equal("1 1/1 2", "1 1/1 2", 1); test_equal("3 -1 0 1/2 1 1", "2 -1 1", 1); test_equal("3 -1 0 1/2 -1 1", "2 -1 1", 0); /* Addition and subtraction **********************************************/ test_add("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 1 0 1/4 0 1 0 1"); test_add("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 3 -2 1/2 -1 1"); test_add("0/2 1 1", "1 2/1 1", "1 2"); test_add("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_add("2 1 1/1 1", "2 -1 1/1 1", "2 0 2"); test_add("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 -1 2 2/3 0 -1 1"); test_add("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 7 12 7 1/3 2 3 1"); test_add("2 1 1/2 -1 1", "2 1 1", "3 0 1 1/2 -1 1"); test_add("1 1/2 1 1", "2 0 1/2 1 1", "1 1"); test_add("2 1 1/3 4 -4 1", "1 1/2 -2 1", "2 -1 2/3 4 -4 1"); test_add("3 0 1 1/3 1 2 1", "2 0 -1/2 1 1", "0"); test_add("2 1 1/2 0 1", "2 -1 1/2 0 1", "1 2"); test_add("1 1/3 3 5 2", "1 1/3 6 7 2", "1 1/3 2 3 1"); test_add_in_place1("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 1 0 1/4 0 1 0 1"); test_add_in_place1("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 3 -2 1/2 -1 1"); test_add_in_place1("0/2 1 1", "1 2/1 1", "1 2"); test_add_in_place1("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_add_in_place1("2 1 1/1 1", "2 -1 1/1 1", "2 0 2"); test_add_in_place1("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 -1 2 2/3 0 -1 1"); test_add_in_place1("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 7 12 7 1/3 2 3 1"); test_add_in_place1("2 1 1/2 -1 1", "2 1 1", "3 0 1 1/2 -1 1"); test_add_in_place1("1 1/2 1 1", "2 0 1/2 1 1", "1 1"); test_add_in_place1("2 1 1/3 4 -4 1", "1 1/2 -2 1", "2 -1 2/3 4 -4 1"); test_add_in_place1("3 0 1 1/3 1 2 1", "2 0 -1/2 1 1", "0"); test_add_in_place1("2 1 1/2 0 1", "2 -1 1/2 0 1", "1 2"); test_add_in_place2("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 1 0 1/4 0 1 0 1"); test_add_in_place2("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 3 -2 1/2 -1 1"); test_add_in_place2("0/2 1 1", "1 2/1 1", "1 2"); test_add_in_place2("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_add_in_place2("2 1 1/1 1", "2 -1 1/1 1", "2 0 2"); test_add_in_place2("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 -1 2 2/3 0 -1 1"); test_add_in_place2("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 7 12 7 1/3 2 3 1"); test_add_in_place2("2 1 1/2 -1 1", "2 1 1", "3 0 1 1/2 -1 1"); test_add_in_place2("1 1/2 1 1", "2 0 1/2 1 1", "1 1"); test_add_in_place2("2 1 1/3 4 -4 1", "1 1/2 -2 1", "2 -1 2/3 4 -4 1"); test_add_in_place2("3 0 1 1/3 1 2 1", "2 0 -1/2 1 1", "0"); test_add_in_place2("2 1 1/2 0 1", "2 -1 1/2 0 1", "1 2"); test_add_in_place3("2 1 1", "2 2 2"); test_add_in_place3("2 1 1/1 2", "2 1 1"); test_sub("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 3 0 1/4 0 1 0 1"); test_sub("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 -1 -2 1/2 -1 1"); test_sub("0/2 1 1", "1 2/1 1", "1 -2"); test_sub("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_sub("2 1 1/1 1", "2 -1 1/1 1", "1 2"); test_sub("2 1 1/2 0 1", "2 2 1/2 -1 1", "2 -1 -2/3 0 -1 1"); test_sub("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 -9 -12 -5 -1/3 2 3 1"); test_sub("2 -1 1/2 0 1", "1 1", "1 -1/2 0 1"); test_sub("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 3 0 1/4 0 1 0 1"); test_sub("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 -1 -2 1/2 -1 1"); test_sub("0/2 1 1", "1 2/1 1", "1 -2"); test_sub("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_sub("2 1 1/1 1", "2 -1 1/1 1", "1 2"); test_sub("2 1 1/2 0 1", "2 2 1/2 -1 1", "2 -1 -2/3 0 -1 1"); test_sub("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 -9 -12 -5 -1/3 2 3 1"); test_sub("2 1 1/2 -1 1", "2 1 1", "3 2 1 -1/2 -1 1"); test_sub("1 1/2 1 1", "2 0 1/2 1 1", "2 1 -1/2 1 1"); test_sub("2 1 1/3 4 -4 1", "1 1/2 -2 1", "1 3/3 4 -4 1"); test_sub("3 0 1 1/3 1 2 1", "2 0 -1/2 1 1", "2 0 2/2 1 1"); test_sub("2 1 1/2 0 1", "2 -1 1/2 0 1", "1 2/2 0 1"); test_sub("1 1/3 3 5 2", "1 1/3 6 7 2", "1 1/4 6 13 9 2"); test_sub("2 1 1/2 0 2", "2 1 1/2 0 2", "0"); test_sub("2 -1 2/2 0 1", "2 -1 1/2 0 1", "1 1"); test_sub_in_place1("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 3 0 1/4 0 1 0 1"); test_sub_in_place1("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 -1 -2 1/2 -1 1"); test_sub_in_place1("0/2 1 1", "1 2/1 1", "1 -2"); test_sub_in_place1("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_sub_in_place1("2 1 1/1 1", "2 -1 1/1 1", "1 2"); test_sub_in_place1("2 1 1/2 0 1", "2 2 1/2 -1 1", "2 -1 -2/3 0 -1 1"); test_sub_in_place1("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 -9 -12 -5 -1/3 2 3 1"); test_sub_in_place2("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 3 0 1/4 0 1 0 1"); test_sub_in_place2("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 -1 -2 1/2 -1 1"); test_sub_in_place2("0/2 1 1", "1 2/1 1", "1 -2"); test_sub_in_place2("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_sub_in_place2("2 1 1/1 1", "2 -1 1/1 1", "1 2"); test_sub_in_place2("2 1 1/2 0 1", "2 2 1/2 -1 1", "2 -1 -2/3 0 -1 1"); test_sub_in_place2("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 -9 -12 -5 -1/3 2 3 1"); test_sub_in_place3("2 -1 1/2 2 1", "0"); test_addmul("1 1/2 0 2", "2 3 1/1 4", "3 1 0 1/4 -2 0 0 1", "5 -4 3 1 5 1/5 0 -8 0 0 4"); test_submul("1 1/2 0 2", "2 3 1/1 4", "3 1 0 1/4 -2 0 0 1", "5 -4 -3 -1 -1 -1/5 0 -8 0 0 4"); /* Scalar multiplication and devision ************************************/ flint_mpz_init_set_si(mpzzero, 0); flint_mpz_init_set_si(mpzone, 1); flint_mpz_init_set_si(mpztwo, 2); mpq_init(mpqzero); flint_mpq_set_si(mpqzero, 0, 1); mpq_init(mpqone); flint_mpq_set_si(mpqone, 1, 1); mpq_init(mpqtwo); flint_mpq_set_si(mpqtwo, 2, 1); mpq_init(mpqtwoinv); flint_mpq_set_si(mpqtwoinv, 1, 2); test_scalar_mul_si("0", 1, "0"); test_scalar_mul_si("0", 0, "0"); test_scalar_mul_si("1 2", 0, "0"); test_scalar_mul_si("1 1/1 2", -2, "1 -1"); test_scalar_mul_si("2 1 1/2 -2 3", 5, "2 5 5/2 -2 3"); test_scalar_mul_si("2 1 1/2 -2 2", 3, "2 3 3/2 -2 2"); test_scalar_mul_mpz("0", mpzone, "0"); test_scalar_mul_mpz("0", mpzzero, "0"); test_scalar_mul_mpz("1 2", mpzzero, "0"); test_scalar_mul_mpz("1 1/1 2", mpztwo, "1 1"); test_scalar_mul_mpq("0", mpqone, "0"); test_scalar_mul_mpq("0", mpqzero, "0"); test_scalar_mul_mpq("1 2", mpqzero, "0"); test_scalar_mul_mpq("1 1/1 2", mpqtwo, "1 1"); test_scalar_mul_mpq("1 -2/1 1", mpqtwoinv, "1 -1"); test_scalar_div_si("0", 1, "0"); test_scalar_div_si("1 2", 2, "1 1"); test_scalar_div_si("1 1/1 2", -2, "1 -1/1 4"); test_scalar_div_si("3 -5 0 3/2 1 1", 2, "3 -5 0 3/2 2 2"); test_scalar_div_si("3 2 8 4/2 0 1", 3, "3 2 8 4/2 0 3"); test_scalar_div_si("3 2 8 4/2 0 1", -3, "3 -2 -8 -4/2 0 3"); test_scalar_div_si("3 -27 0 9/2 0 1", -3, "3 9 0 -3/2 0 1"); test_scalar_div_mpz("0", mpzone, "0"); test_scalar_div_mpz("1 2", mpztwo, "1 1"); test_scalar_div_mpz("1 1/1 2", mpztwo, "1 1/1 4"); test_scalar_div_mpq("0", mpqone, "0"); test_scalar_div_mpq("1 2", mpqone, "1 2"); test_scalar_div_mpq("1 1/1 2", mpqtwo, "1 1/1 4"); test_scalar_div_mpq("1 -2/1 1", mpqtwoinv, "1 -4"); mpz_clear(mpzzero); mpz_clear(mpzone); mpz_clear(mpztwo); mpq_clear(mpqzero); mpq_clear(mpqone); mpq_clear(mpqtwo); mpq_clear(mpqtwoinv); /* Multiplication, division and powing *********************************/ test_mul("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "1 -1"); test_mul("3 -1 0 1/2 1 1", "1 2/2 -1 1", "1 2"); test_mul("0/2 1 1", "1 2/1 1", "0"); test_mul("1 -3/1 4", "0/3 1 0 1", "0"); test_mul("2 1 1/1 1", "2 -1 1/1 1", "3 -1 0 1"); test_mul("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 2 3 1/3 0 -1 1"); test_mul("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "3 -2 1 1/2 1 1"); test_mul_in_place1("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "1 -1"); test_mul_in_place1("3 -1 0 1/2 1 1", "1 2/2 -1 1", "1 2"); test_mul_in_place1("0/2 1 1", "1 2/1 1", "0"); test_mul_in_place1("1 -3/1 4", "0/3 1 0 1", "0"); test_mul_in_place1("2 1 1/1 1", "2 -1 1/1 1", "3 -1 0 1"); test_mul_in_place1("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 2 3 1/3 0 -1 1"); test_mul_in_place1("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "3 -2 1 1/2 1 1"); test_mul_in_place2("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "1 -1"); test_mul_in_place2("3 -1 0 1/2 1 1", "1 2/2 -1 1", "1 2"); test_mul_in_place2("0/2 1 1", "1 2/1 1", "0"); test_mul_in_place2("1 -3/1 4", "0/3 1 0 1", "0"); test_mul_in_place2("2 1 1/1 1", "2 -1 1/1 1", "3 -1 0 1"); test_mul_in_place2("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 2 3 1/3 0 -1 1"); test_mul_in_place2("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "3 -2 1 1/2 1 1"); test_mul_in_place3("2 0 1/2 1 1", "3 0 0 1/3 1 2 1"); test_div("3 -1 0 1/1 2", "2 1 1/1 1", "2 -1 1/1 2"); test_div("0/2 1 1", "2 1 1/1 1", "0"); test_div("3 -1 0 1/1 4", "2 -1 -1/1 2", "2 1 -1/1 2"); test_div("2 1 1", "2 1 -1/2 1 -1", "2 1 1"); test_div("2 1 1/3 4 4 1", "2 -1 1/3 6 5 1", "3 3 4 1/3 -2 1 1"); test_div_in_place1("3 -1 0 1/1 2", "2 1 1/1 1", "2 -1 1/1 2"); test_div_in_place1("0/2 1 1", "2 1 1/1 1", "0"); test_div_in_place1("3 -1 0 1/1 4", "2 -1 -1/1 2", "2 1 -1/1 2"); test_div_in_place1("2 1 1", "2 1 -1/2 1 -1", "2 1 1"); test_div_in_place1("2 1 1/3 4 4 1", "2 -1 1/3 6 5 1", "3 3 4 1/3 -2 1 1"); test_div_in_place1("0", "1 2/2 3 5", "0"); test_div_in_place2("3 -1 0 1/1 2", "2 1 1/1 1", "2 -1 1/1 2"); test_div_in_place2("0/2 1 1", "2 1 1/1 1", "0"); test_div_in_place2("3 -1 0 1/1 4", "2 -1 -1/1 2", "2 1 -1/1 2"); test_div_in_place2("2 1 1", "2 1 -1/2 1 -1", "2 1 1"); test_div_in_place2("2 1 1/3 4 4 1", "2 -1 1/3 6 5 1", "3 3 4 1/3 -2 1 1"); test_div_in_place3("3 -1 0 1/1 2", "1 1"); test_pow("2 0 -1/1 2", 3, "4 0 0 0 -1/1 8"); test_pow("0", 0, "1 1"); test_pow("2 1 -1", 0, "1 1"); test_pow("2 1 1/2 0 1", 0, "1 1"); /* Derivative ************************************************************/ test_derivative("0", "0"); test_derivative("1 2", "0"); test_derivative("1 -1/1 2", "0"); test_derivative("2 0 1", "1 1"); test_derivative("3 1 0 1", "2 0 2"); test_derivative("1 1/2 0 1", "1 -1/3 0 0 1"); test_derivative("2 2 1/2 -1 1", "1 -3/3 1 -2 1"); test_derivative("2 0 1/3 1 2 1", "2 1 -1/4 1 3 3 1"); /* Bug which allowed constant factors */ test_derivative("3 5 1 -2/2 10 2", "3 0 -10 -1/3 25 10 1"); /* Evaluation ************************************************************/ test_evaluate("1 1/1 2", -2, 3, "1/2"); test_evaluate("3 1 0 1/2 0 1", -1, 2, "-5/2"); test_evaluate("2 3 1/2 -1 1", 1, 1, "P"); test_evaluate("2 3 1/2 -1 1", 2, 3, "-11"); test_evaluate("2 3 1/2 -1 2", 1, 2, "P"); test_evaluate("2 1 1/2 -1 1", 2, 1, "3"); /* String methods ********************************************************/ fmpz_poly_q_init(qpoly1); ans = fmpz_poly_q_set_str(qpoly1, "1 3/xyz"); if ((ans == 0) || !fmpz_poly_q_is_zero(qpoly1)) { flint_printf("test_set_str: failed\n"); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_q_init(qpoly1); ans = fmpz_poly_q_set_str(qpoly1, "abc/1 3"); if ((ans == 0) || !fmpz_poly_q_is_zero(qpoly1)) { flint_printf("test_set_str: failed\n"); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_q_init(qpoly1); ans = fmpz_poly_q_set_str(qpoly1, "abc/xyz"); if ((ans == 0) || !fmpz_poly_q_is_zero(qpoly1)) { flint_printf("test_set_str: failed\n"); abort(); } fmpz_poly_q_clear(qpoly1); test_get_str_pretty("1 -3", "-3"); test_get_str_pretty("3 1 2 1", "t^2+2*t+1"); test_get_str_pretty("1 -2/2 1 1", "-2/(t+1)"); test_get_str_pretty("2 1 1/2 -1 1", "(t+1)/(t-1)"); test_get_str_pretty("2 1 1/1 2", "(t+1)/2"); test_get_str_pretty("1 1/1 2", "1/2"); FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return EXIT_SUCCESS; }