Пример #1
0
void
check_one (mpq_srcptr want, int base, const char *str)
{
  mpq_t   got;

  MPQ_CHECK_FORMAT (want);
  mp_trace_base = base;

  mpq_init (got);

  if (mpq_set_str (got, str, base) != 0)
    {
      printf ("mpq_set_str unexpectedly failed\n");
      printf ("  base %d\n", base);
      printf ("  str  \"%s\"\n", str);
      abort ();
    }
  MPQ_CHECK_FORMAT (got);

  if (! mpq_equal (got, want))
    {
      printf ("mpq_set_str wrong\n");
      printf ("  base %d\n", base);
      printf ("  str  \"%s\"\n", str);
      mpq_trace ("got ", got);
      mpq_trace ("want", want);
      abort ();
    }

  mpq_clear (got);
}
void
check_all (mpq_ptr x, mpq_ptr y, mpq_ptr want_add, mpq_ptr want_sub)
{
  mpq_t  got;
  int    neg_x, neg_y, swap;

  mpq_init (got);

  MPQ_CHECK_FORMAT (want_add);
  MPQ_CHECK_FORMAT (want_sub);
  MPQ_CHECK_FORMAT (x);
  MPQ_CHECK_FORMAT (y);

  for (swap = 0; swap <= 1; swap++)
    {
      for (neg_x = 0; neg_x <= 1; neg_x++)
        {
          for (neg_y = 0; neg_y <= 1; neg_y++)
            {
              mpq_add (got, x, y);
              MPQ_CHECK_FORMAT (got);
              if (! mpq_equal (got, want_add))
                {
                  printf ("mpq_add wrong\n");
                  mpq_trace ("  x   ", x);
                  mpq_trace ("  y   ", y);
                  mpq_trace ("  got ", got);
                  mpq_trace ("  want", want_add);
                  abort ();
                }

              mpq_sub (got, x, y);
              MPQ_CHECK_FORMAT (got);
              if (! mpq_equal (got, want_sub))
                {
                  printf ("mpq_sub wrong\n");
                  mpq_trace ("  x   ", x);
                  mpq_trace ("  y   ", y);
                  mpq_trace ("  got ", got);
                  mpq_trace ("  want", want_sub);
                  abort ();
                }


              mpq_neg (y, y);
              mpq_swap (want_add, want_sub);
            }

          mpq_neg (x, x);
          mpq_swap (want_add, want_sub);
          mpq_neg (want_add, want_add);
          mpq_neg (want_sub, want_sub);
        }

      mpq_swap (x, y);
      mpq_neg (want_sub, want_sub);
    }

  mpq_clear (got);
}
void
check_one (mpq_srcptr x, mpq_srcptr y, int want)
{
  int  got;

  MPQ_CHECK_FORMAT (x);
  MPQ_CHECK_FORMAT (y);

  got = mpq_equal (x, y);
  if ((got != 0) != (want != 0))
    {
      printf ("mpq_equal got %d want %d\n", got, want);
      mpq_trace ("x", x);
      mpq_trace ("y", y);
      abort ();
    }
}
Пример #4
0
/* Check various values 2^n and 1/2^n. */
void
check_onebit (void)
{
  static const long data[] = {
    -3*GMP_NUMB_BITS-1, -3*GMP_NUMB_BITS, -3*GMP_NUMB_BITS+1,
    -2*GMP_NUMB_BITS-1, -2*GMP_NUMB_BITS, -2*GMP_NUMB_BITS+1,
    -GMP_NUMB_BITS-1, -GMP_NUMB_BITS, -GMP_NUMB_BITS+1,
    -5, -2, -1, 0, 1, 2, 5,
    GMP_NUMB_BITS-1, GMP_NUMB_BITS, GMP_NUMB_BITS+1,
    2*GMP_NUMB_BITS-1, 2*GMP_NUMB_BITS, 2*GMP_NUMB_BITS+1,
    3*GMP_NUMB_BITS-1, 3*GMP_NUMB_BITS, 3*GMP_NUMB_BITS+1,
  };

  int     i, neg;
  long    exp, l;
  mpq_t   q;
  double  got, want;

  mpq_init (q);

  for (i = 0; i < numberof (data); i++)
    {
      exp = data[i];

      mpq_set_ui (q, 1L, 1L);
      if (exp >= 0)
	mpq_mul_2exp (q, q, exp);
      else
	mpq_div_2exp (q, q, -exp);

      want = 1.0;
      for (l = 0; l < exp; l++)
	want *= 2.0;
      for (l = 0; l > exp; l--)
	want /= 2.0;

      for (neg = 0; neg <= 1; neg++)
	{
	  if (neg)
	    {
	      mpq_neg (q, q);
	      want = -want;
	    }

	  got = mpq_get_d (q);

	  if (got != want)
	    {
	      printf    ("mpq_get_d wrong on %s2**%ld\n", neg ? "-" : "", exp);
	      mpq_trace ("   q    ", q);
	      d_trace   ("   want ", want);
	      d_trace   ("   got  ", got);
	      abort();
	    }
	}
    }
  mpq_clear (q);
}
Пример #5
0
void
check_one (mpq_srcptr q, int base, const char *want)
{
  char    *str, *ret;
  size_t  str_alloc;

  MPQ_CHECK_FORMAT (q);
  mp_trace_base = base;

  str_alloc =
    mpz_sizeinbase (mpq_numref(q), ABS(base)) +
    mpz_sizeinbase (mpq_denref(q), ABS(base)) + 3;

  str = mpq_get_str (NULL, base, q);
  if (strlen(str)+1 > str_alloc)
    {
      printf ("mpq_get_str size bigger than should be (passing NULL)\n");
      printf ("  base %d\n", base);
      printf ("  got  size %lu \"%s\"\n", (unsigned long)  strlen(str)+1, str);
      printf ("  want size %lu\n", (unsigned long) str_alloc);
      abort ();
    }
  if (strcmp (str, want) != 0)
    {
      printf ("mpq_get_str wrong (passing NULL)\n");
      printf ("  base %d\n", base);
      printf ("  got  \"%s\"\n", str);
      printf ("  want \"%s\"\n", want);
      mpq_trace ("  q", q);
      abort ();
    }
  (*__gmp_free_func) (str, strlen (str) + 1);

  str = (char *) (*__gmp_allocate_func) (str_alloc);

  ret = mpq_get_str (str, base, q);
  if (str != ret)
    {
      printf ("mpq_get_str wrong return value (passing non-NULL)\n");
      printf ("  base %d\n", base);
      printf ("  got  %p\n", ret);
      printf ("  want %p\n", want);
      abort ();
    }
  if (strcmp (str, want) != 0)
    {
      printf ("mpq_get_str wrong (passing non-NULL)\n");
      printf ("  base %d\n", base);
      printf ("  got  \"%s\"\n", str);
      printf ("  want \"%s\"\n", want);
      abort ();
    }
  (*__gmp_free_func) (str, str_alloc);
}
/* Print "name=value\n" to stdout for an mpz_t value.  */
void
mpz_trace (const char *name, mpz_srcptr z)
{
  mpq_t      q;
  mp_limb_t  one;

  if (z == NULL)
    {
      mpq_trace (name, NULL);
      return;
    }

  q->_mp_num._mp_alloc = ALLOC(z);
  q->_mp_num._mp_size = SIZ(z);
  q->_mp_num._mp_d = PTR(z);

  one = 1;
  q->_mp_den._mp_alloc = 1;
  q->_mp_den._mp_size = 1;
  q->_mp_den._mp_d = &one;

  mpq_trace(name, q);
}
Пример #7
0
void
check_one (mpf_ptr got, mpq_srcptr q)
{
  mpf_t  n, d;

  mpf_set_q (got, q);

  PTR(n) = PTR(&q->_mp_num);
  SIZ(n) = SIZ(&q->_mp_num);
  EXP(n) = ABSIZ(&q->_mp_num);

  PTR(d) = PTR(&q->_mp_den);
  SIZ(d) = SIZ(&q->_mp_den);
  EXP(d) = ABSIZ(&q->_mp_den);

  if (! refmpf_validate_division ("mpf_set_q", got, n, d))
    {
      mp_trace_base = -16;
      mpq_trace ("   q", q);
      abort ();
    }
}
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);
}
Пример #9
0
void
check_data (void)
{
  static const struct {
    const char  *inp;
    int         base;
    const char  *want;
    int         want_nread;

  } data[] = {

    { "0",   10, "0", 1 },
    { "0/1", 10, "0", 3 },

    { "0/",   10, "0", 0 },
    { "/123", 10, "0", 0 },
    { "blah", 10, "0", 0 },
    { "123/blah", 10, "0", 0 },
    { "5 /8", 10, "5", 1 },
    { "5/ 8", 10, "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 },

    {  "0x0",    0,   "0", 3 },
    {  "0x10",   0,  "16", 4 },
    { "-0x0",    0,   "0", 4 },
    { "-0x10",   0, "-16", 5 },
    { "-0x10/5", 0, "-16/5", 7 },

    {  "00",   0,  "0", 2 },
    {  "010",  0,  "8", 3 },
    { "-00",   0,  "0", 3 },
    { "-010",  0, "-8", 4 },
  };

  mpq_t  got, want;
  long   ftell_nread;
  int    i, post, j, got_nread;
  FILE   *fp;

  mpq_init (got);
  mpq_init (want);

  for (i = 0; i < numberof (data); i++)
    {
      for (post = 0; post <= 2; post++)
	{
	  mpq_set_str_or_abort (want, data[i].want, 0);
	  MPQ_CHECK_FORMAT (want);

	  fp = fopen (FILENAME, "w+");
	  ASSERT_ALWAYS (fp != NULL);
	  fputs (data[i].inp, fp);
	  for (j = 0; j < post; j++)
	    putc (' ', fp);
	  fflush (fp);
	  ASSERT_ALWAYS (! ferror(fp));

	  rewind (fp);
	  got_nread = mpq_inp_str (got, fp, data[i].base);

	  if (got_nread != 0)
	    {
	      ftell_nread = ftell (fp);
	      if (got_nread != ftell_nread)
		{
		  printf ("mpq_inp_str nread wrong\n");
		  printf ("  inp          \"%s\"\n", data[i].inp);
		  printf ("  base         %d\n", data[i].base);
		  printf ("  got_nread    %d\n", got_nread);
		  printf ("  ftell_nread  %ld\n", ftell_nread);
		  abort ();
		}
	    }

	  if (post == 0 && data[i].want_nread == strlen(data[i].inp))
	    {
	      int  c = getc(fp);
	      if (c != EOF)
		{
		  printf ("mpq_inp_str didn't read to EOF\n");
		  printf ("  inp         \"%s\"\n", data[i].inp);
		  printf ("  base        %d\n", data[i].base);
		  printf ("  c '%c' %#x\n", c, c);
		  abort ();
		}
	    }

	  if (got_nread != data[i].want_nread)
	    {
	      printf ("mpq_inp_str nread wrong\n");
	      printf ("  inp         \"%s\"\n", data[i].inp);
	      printf ("  base        %d\n", data[i].base);
	      printf ("  got_nread   %d\n", got_nread);
	      printf ("  want_nread  %d\n", data[i].want_nread);
	      abort ();
	    }

	  MPQ_CHECK_FORMAT (got);

	  if (! mpq_equal (got, want))
	    {
	      printf ("mpq_inp_str wrong result\n");
	      printf ("  inp   \"%s\"\n", data[i].inp);
	      printf ("  base  %d\n", data[i].base);
	      mpq_trace ("  got ",  got);
	      mpq_trace ("  want", want);
	      abort ();
	    }

	  ASSERT_ALWAYS (fclose (fp) == 0);
	}
    }

  mpq_clear (got);
  mpq_clear (want);
}
Пример #10
0
void
check_data (void)
{
  static const struct {
    const char     *q;
    mpir_si         n;
    mpir_ui         d;
    int            want;
  } data[] = {
    { "0", 0, 1, 0 },
    { "0", 0, 123, 0 },
    { "0", 0, GMP_UI_MAX, 0 },
    { "1", 0, 1, 1 },
    { "1", 0, 123, 1 },
    { "1", 0, GMP_UI_MAX, 1 },
    { "-1", 0, 1, -1 },
    { "-1", 0, 123, -1 },
    { "-1", 0, GMP_UI_MAX, -1 },

    { "123", 123, 1, 0 },
    { "124", 123, 1, 1 },
    { "122", 123, 1, -1 },

    { "-123", 123, 1, -1 },
    { "-124", 123, 1, -1 },
    { "-122", 123, 1, -1 },

    { "123", -123, 1, 1 },
    { "124", -123, 1, 1 },
    { "122", -123, 1, 1 },

    { "-123", -123, 1, 0 },
    { "-124", -123, 1, -1 },
    { "-122", -123, 1, 1 },

    { "5/7", 3,4, -1 },
    { "5/7", -3,4, 1 },
    { "-5/7", 3,4, -1 },
    { "-5/7", -3,4, 1 },
  };

  mpq_t  q;
  int    i, got;

  mpq_init (q);

  for (i = 0; i < numberof (data); i++)
    {
      mpq_set_str_or_abort (q, data[i].q, 0);
      MPQ_CHECK_FORMAT (q);

      got = mpq_cmp_si (q, data[i].n, data[i].d);
      if (SGN(got) != data[i].want)
        {
          printf ("mpq_cmp_si wrong\n");
        error:
          mpq_trace ("  q", q);
          printf ("  n=%Md\n", data[i].n);
          printf ("  d=%Mu\n", data[i].d);
          printf ("  got=%d\n", got);
          printf ("  want=%Md\n", data[i].want);
          abort ();
        }

      if (data[i].n == 0)
        {
          got = mpq_cmp_si (q, 0L, data[i].d);
          if (SGN(got) != data[i].want)
            {
              printf ("mpq_cmp_si wrong\n");
              goto error;
            }
        }
    }

  mpq_clear (q);
}