コード例 #1
0
ファイル: t-get_str.c プロジェクト: 119/aircam-openwrt
void
check_data (void)
{
  static const struct {
    int         base;
    const char  *num;
    const char  *den;
    const char  *want;
  } data[] = {
    { 10, "0", "1", "0" },
    { 10, "1", "1", "1" },

    { 16, "ffffffff", "1", "ffffffff" },
    { 16, "ffffffffffffffff", "1", "ffffffffffffffff" },

    { 16, "1", "ffffffff", "1/ffffffff" },
    { 16, "1", "ffffffffffffffff", "1/ffffffffffffffff" },
    { 16, "1", "10000000000000003", "1/10000000000000003" },

    { 10, "12345678901234567890", "9876543210987654323",
      "12345678901234567890/9876543210987654323" },
  };

  mpq_t  q;
  int    i;

  mpq_init (q);
  for (i = 0; i < numberof (data); i++)
    {
      mpz_set_str_or_abort (mpq_numref(q), data[i].num, data[i].base);
      mpz_set_str_or_abort (mpq_denref(q), data[i].den, data[i].base);
      check_all (q, data[i].base, data[i].want);
    }
  mpq_clear (q);
}
コード例 #2
0
ファイル: t-cong.c プロジェクト: AllardJ/Tomato
void
check_data (void)
{
  static const struct {
    const char *a;
    const char *c;
    const char *d;
    int        want;

  } data[] = {

    /* strict equality mod 0 */
    { "0", "0", "0", 1 },
    { "11", "11", "0", 1 },
    { "3", "11", "0", 0 },

    /* anything congruent mod 1 */
    { "0", "0", "1", 1 },
    { "1", "0", "1", 1 },
    { "0", "1", "1", 1 },
    { "123", "456", "1", 1 },
    { "0x123456789123456789", "0x987654321987654321", "1", 1 },

    /* csize==1, dsize==2 changing to 1 after stripping 2s */
    { "0x3333333333333333",  "0x33333333",
      "0x180000000", 1 },
    { "0x33333333333333333333333333333333", "0x3333333333333333",
      "0x18000000000000000", 1 },

    /* another dsize==2 becoming 1, with opposite signs this time */
    {  "0x444444441",
      "-0x22222221F",
       "0x333333330", 1 },
    {  "0x44444444444444441",
      "-0x2222222222222221F",
       "0x33333333333333330", 1 },
  };

  mpz_t   a, c, d;
  int     i;

  mpz_init (a);
  mpz_init (c);
  mpz_init (d);

  for (i = 0; i < numberof (data); i++)
    {
      mpz_set_str_or_abort (a, data[i].a, 0);
      mpz_set_str_or_abort (c, data[i].c, 0);
      mpz_set_str_or_abort (d, data[i].d, 0);
      check_one (a, c, d, data[i].want);
    }

  mpz_clear (a);
  mpz_clear (c);
  mpz_clear (d);
}
コード例 #3
0
ファイル: t-divis.c プロジェクト: AlexeiSheplyakov/gmp.pkg
void
check_data (void)
{
  static const struct {
    const char *a;
    const char *d;
    int        want;

  } data[] = {

    { "0",    "0", 1 },
    { "17",   "0", 0 },
    { "0",    "1", 1 },
    { "123",  "1", 1 },
    { "-123", "1", 1 },

    { "0",  "2", 1 },
    { "1",  "2", 0 },
    { "2",  "2", 1 },
    { "-2", "2", 1 },
    { "0x100000000000000000000000000000000", "2", 1 },
    { "0x100000000000000000000000000000001", "2", 0 },

    { "0x3333333333333333", "3", 1 },
    { "0x3333333333333332", "3", 0 },
    { "0x33333333333333333333333333333333", "3", 1 },
    { "0x33333333333333333333333333333332", "3", 0 },

    /* divisor changes from 2 to 1 limb after stripping 2s */
    {          "0x3333333300000000",         "0x180000000",         1 },
    {  "0x33333333333333330000000000000000", "0x18000000000000000", 1 },
    { "0x133333333333333330000000000000000", "0x18000000000000000", 0 },
  };

  mpz_t   a, d;
  int     i;

  mpz_init (a);
  mpz_init (d);

  for (i = 0; i < numberof (data); i++)
    {
      mpz_set_str_or_abort (a, data[i].a, 0);
      mpz_set_str_or_abort (d, data[i].d, 0);
      check_one (a, d, data[i].want);
    }

  mpz_clear (a);
  mpz_clear (d);
}
コード例 #4
0
ファイル: t-pprime_p.c プロジェクト: AllardJ/Tomato
static void
check_primes (void)
{
  static const char * const primes[] = {
    "2", "17", "65537",
    /* diffie-hellman-group1-sha1, also "Well known group 2" in RFC
       2412, 2^1024 - 2^960 - 1 + 2^64 * { [2^894 pi] + 129093 } */
    "0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
    "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
    "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
    "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
    "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381"
    "FFFFFFFFFFFFFFFF",
    NULL
  };

  mpz_t n;
  int i;

  mpz_init (n);

  for (i = 0; primes[i]; i++)
    {
      mpz_set_str_or_abort (n, primes[i], 0);
      check_one (n, 1);
    }
  mpz_clear (n);
}
コード例 #5
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);
}
コード例 #6
0
ファイル: t-cmp_d.c プロジェクト: ChristopherRussell/gap
void
check_data (void)
{
  static const struct {
    const char  *x;
    double      y;
    int         cmp, cmpabs;

  } data[] = {

    {  "0",  0.0,  0,  0 },

    {  "1",  0.0,  1,  1 },
    { "-1",  0.0, -1,  1 },

    {  "1",  0.5,  1,  1 },
    { "-1", -0.5, -1,  1 },

    {  "0",  1.0, -1, -1 },
    {  "0", -1.0,  1, -1 },

    {  "0x1000000000000000000000000000000000000000000000000", 1.0,  1, 1 },
    { "-0x1000000000000000000000000000000000000000000000000", 1.0, -1, 1 },

    {  "0",  1e100, -1, -1 },
    {  "0", -1e100,  1, -1 },

    {  "2",  1.5,   1,  1 },
    {  "2", -1.5,   1,  1 },
    { "-2",  1.5,  -1,  1 },
    { "-2", -1.5,  -1,  1 },
  };

  mpz_t    x;
  unsigned i;

  mpz_init (x);

  for (i = 0; i < numberof (data); i++)
    {
      mpz_set_str_or_abort (x, data[i].x, 0);
      check_one ("check_data", x, data[i].y, data[i].cmp, data[i].cmpabs);
    }

  mpz_clear (x);
}
コード例 #7
0
ファイル: t-gcd.c プロジェクト: AllardJ/Tomato
/* Test operands from a table of seed data.  This variant creates the operands
   using plain ol' mpz_rrandomb.  This is a hack for better coverage of the gcd
   code, which depends on that the random number generators give the exact
   numbers we expect.  */
void
check_kolmo1 (void)
{
  static const struct {
    unsigned int seed;
    int nb;
    const char *want;
  } data[] = {
    { 59618, 38208, "5"},
    { 76521, 49024, "3"},
    { 85869, 54976, "1"},
    { 99449, 63680, "1"},
    {112453, 72000, "1"}
  };

  gmp_randstate_t rs;
  mpz_t  bs, a, b, want;
  int    i, unb, vnb, nb;

  gmp_randinit_default (rs);

  mpz_inits (bs, a, b, want, NULL);

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

      gmp_randseed_ui (rs, data[i].seed);

      mpz_urandomb (bs, rs, 32);
      unb = mpz_get_ui (bs) % nb;
      mpz_urandomb (bs, rs, 32);
      vnb = mpz_get_ui (bs) % nb;

      mpz_rrandomb (a, rs, unb);
      mpz_rrandomb (b, rs, vnb);

      mpz_set_str_or_abort (want, data[i].want, 0);

      one_test (a, b, want, -1);
    }

  mpz_clears (bs, a, b, want, NULL);
  gmp_randclear (rs);
}
コード例 #8
0
ファイル: t-popcount.c プロジェクト: AlexeiSheplyakov/gmp.pkg
void
check_data (void)
{
  static const struct {
    const char     *n;
    unsigned long  want;
  } data[] = {
    { "-1", ~ (unsigned long) 0 },
    { "-12345678", ~ (unsigned long) 0 },
    { "0", 0 },
    { "1", 1 },
    { "3", 2 },
    { "5", 2 },
    { "0xFFFF", 16 },
    { "0xFFFFFFFF", 32 },
    { "0xFFFFFFFFFFFFFFFF", 64 },
    { "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128 },
  };

  unsigned long   got;
  int    i;
  mpz_t  n;

  mpz_init (n);
  for (i = 0; i < numberof (data); i++)
    {
      mpz_set_str_or_abort (n, data[i].n, 0);
      got = mpz_popcount (n);
      if (got != data[i].want)
	{
	  printf ("mpz_popcount wrong at data[%d]\n", i);
	  printf ("   n     \"%s\"\n", data[i].n);
	  printf ("         ");   mpz_out_str (stdout, 10, n); printf ("\n");
	  printf ("         0x"); mpz_out_str (stdout, 16, n); printf ("\n");
	  printf ("   got   %lu\n", got);
	  printf ("   want  %lu\n", data[i].want);
	  abort ();
	}
    }
  mpz_clear (n);
}
コード例 #9
0
void
check_data (void)
{
  static const struct {
    const char  *n;
    long        want;
  } data[] = {
    { "0",      0L },
    { "1",      1L },
    { "-1",     -1L },
    { "2",      2L },
    { "-2",     -2L },
    { "12345",  12345L },
    { "-12345", -12345L },
  };

  int    i;
  mpz_t  n;
  long   got;

  mpz_init (n);
  for (i = 0; i < numberof (data); i++)
    {
      mpz_set_str_or_abort (n, data[i].n, 0);

      got = mpz_get_si (n);
      if (got != data[i].want)
        {
          printf ("mpz_get_si wrong at data[%d]\n", i); 
          printf ("   n     \"%s\" (", data[i].n);
          mpz_out_str (stdout, 10, n); printf (", hex ");
          mpz_out_str (stdout, 16, n); printf (")\n");
          printf ("   got   %ld (0x%lX)\n", got, got);
          printf ("   want  %ld (0x%lX)\n", data[i].want, data[i].want);
          abort();                                    
        }
    }
  mpz_clear (n);
}
コード例 #10
0
ファイル: t-cmp_si.c プロジェクト: AlexeiSheplyakov/gmp.pkg
void
check_data (void)
{
  static const struct {
    int         a_base;
    const char  *a;
    const char  *b;
    int         want;
  } data[] = {
    { 10, "0",  "1", -1 },
    { 10, "0",  "0",  0 },
    { 10, "0", "-1",  1 },

    { 10, "1",  "1", 0 },
    { 10, "1",  "0", 1 },
    { 10, "1", "-1", 1 },

    { 10, "-1",  "1", -1 },
    { 10, "-1",  "0", -1 },
    { 10, "-1", "-1", 0 },

    { 16,         "0", "-0x80000000",  1 },
    { 16,  "80000000", "-0x80000000",  1 },
    { 16,  "80000001", "-0x80000000",  1 },
    { 16, "-80000000", "-0x80000000",  0 },
    { 16, "-80000001", "-0x80000000", -1 },
    { 16, "-FF0080000001", "-0x80000000", -1 },

    { 16,                 "0", "-0x8000000000000000",  1 },
    { 16,  "8000000000000000", "-0x8000000000000000",  1 },
    { 16,  "8000000000000001", "-0x8000000000000000",  1 },
    { 16, "-8000000000000000", "-0x8000000000000000",  0 },
    { 16, "-8000000000000001", "-0x8000000000000000", -1 },
    { 16, "-FF008000000000000001", "-0x8000000000000000", -1 },
  };

  mpf_t  a;
  mpz_t  bz;
  long   b;
  int    got;
  int    i;

  mpf_init (a);
  mpz_init (bz);
  for (i = 0; i < numberof (data); i++)
    {
      mpf_set_str_or_abort (a, data[i].a, data[i].a_base);
      mpz_set_str_or_abort (bz, data[i].b, 0);

      if (mpz_fits_slong_p (bz))
        {
          b = mpz_get_si (bz);
          got = mpf_cmp_si (a, b);
          if (SGN (got) != data[i].want)
            {
              printf ("mpf_cmp_si wrong on data[%d]\n", i);
              printf ("  a="); mpf_out_str (stdout, 10, 0, a);
              printf (" (%s)\n", data[i].a);
              printf ("  b=%ld (%s)\n", b, data[i].b);
              printf ("  got=%d\n", got);
              printf ("  want=%d\n", data[i].want);
              abort();
            }
        }
    }

  mpf_clear (a);
  mpz_clear (bz);
}
コード例 #11
0
int
main (int argc, char **argv)
{
    mpz_t op1, op2;
    mp_size_t size;
    int i;
    int reps = 10000;
    char *str, *buf;
    int base;
    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 (op1);
    mpz_init (op2);

    for (i = 0; i < reps; i++)
    {
        /* 1. Generate random mpz_t and convert to a string and back to mpz_t
        again.  */
        mpz_urandomb (bs, rands, 32);
        size_range = mpz_get_ui (bs) % 12 + 2;	/* 2..13 */
        mpz_urandomb (bs, rands, size_range);	/* 3..8191 bits */
        size = mpz_get_ui (bs);
        mpz_rrandomb (op1, rands, size);

        mpz_urandomb (bs, rands, 1);
        bsi = mpz_get_ui (bs);
        if ((bsi & 1) != 0)
            mpz_neg (op1, op1);

        mpz_urandomb (bs, rands, 32);
        bsi = mpz_get_ui (bs);
        base = bsi % 62 + 1;
        if (base == 1)
            base = 0;

        str = mpz_get_str ((char *) 0, base, op1);
        mpz_set_str_or_abort (op2, str, base);

        if (mpz_cmp (op1, op2))
        {
            fprintf (stderr, "ERROR, op1 and op2 different in test %d\n", i);
            fprintf (stderr, "str  = %s\n", str);
            fprintf (stderr, "base = %d\n", base);
            fprintf (stderr, "op1  = ");
            debug_mp (op1, -16);
            fprintf (stderr, "op2  = ");
            debug_mp (op2, -16);
            abort ();
        }

        (*__gmp_free_func) (str, strlen (str) + 1);

#if 0
        /* 2. Generate random string and convert to mpz_t and back to a string
        again.  */
        mpz_urandomb (bs, rands, 32);
        size_range = mpz_get_ui (bs) % 10 + 2;	/* 2..11 */
        mpz_urandomb (bs, rands, size_range);	/* 3..2047 bits */
        len = mpz_get_ui (bs);
        buf = (*__gmp_allocate_func) (len + 1);
        string_urandomb (buf, len, base);
        mpz_set_str_or_abort (op1, buf, base);
        str = mpz_get_str ((char *) 0, base, op1);

        if (strcmp (str, buf) != 0)
        {
            fprintf (stderr, "ERROR, str and buf different\n");
            fprintf (stderr, "str  = %s\n", str);
            fprintf (stderr, "buf  = %s\n", buf);
            fprintf (stderr, "base = %d\n", base);
            fprintf (stderr, "op1  = ");
            debug_mp (op1, -16);
            abort ();
        }

        (*__gmp_free_func) (buf, len + 1);
        (*__gmp_free_func) (str, strlen (str) + 1);
#endif
    }

    mpz_clear (bs);
    mpz_clear (op1);
    mpz_clear (op2);

    tests_end ();
    exit (0);
}
コード例 #12
0
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);
}
コード例 #13
0
void
check_z (void)
{
  static const struct {
    const char  *fmt;
    const char  *z;
    const char  *want;
  } data[] = {
    { "%Zd", "0",    "0" },
    { "%Zd", "1",    "1" },
    { "%Zd", "123",  "123" },
    { "%Zd", "-1",   "-1" },
    { "%Zd", "-123", "-123" },

    { "%+Zd", "0",      "+0" },
    { "%+Zd", "123",  "+123" },
    { "%+Zd", "-123", "-123" },

    { "%Zx",  "123",   "7b" },
    { "%ZX",  "123",   "7B" },
    { "%Zx", "-123",  "-7b" },
    { "%ZX", "-123",  "-7B" },
    { "%Zo",  "123",  "173" },
    { "%Zo", "-123", "-173" },

    { "%#Zx",    "0",     "0" },
    { "%#ZX",    "0",     "0" },
    { "%#Zx",  "123",  "0x7b" },
    { "%#ZX",  "123",  "0X7B" },
    { "%#Zx", "-123", "-0x7b" },
    { "%#ZX", "-123", "-0X7B" },

    { "%#Zo",    "0",     "0" },
    { "%#Zo",  "123",  "0173" },
    { "%#Zo", "-123", "-0173" },

    { "%10Zd",      "0", "         0" },
    { "%10Zd",    "123", "       123" },
    { "%10Zd",   "-123", "      -123" },

    { "%-10Zd",     "0", "0         " },
    { "%-10Zd",   "123", "123       " },
    { "%-10Zd",  "-123", "-123      " },

    { "%+10Zd",   "123", "      +123" },
    { "%+-10Zd",  "123", "+123      " },
    { "%+10Zd",  "-123", "      -123" },
    { "%+-10Zd", "-123", "-123      " },

    { "%08Zd",    "0", "00000000" },
    { "%08Zd",  "123", "00000123" },
    { "%08Zd", "-123", "-0000123" },

    { "%+08Zd",    "0", "+0000000" },
    { "%+08Zd",  "123", "+0000123" },
    { "%+08Zd", "-123", "-0000123" },

    { "%#08Zx",    "0", "00000000" },
    { "%#08Zx",  "123", "0x00007b" },
    { "%#08Zx", "-123", "-0x0007b" },

    { "%+#08Zx",    "0", "+0000000" },
    { "%+#08Zx",  "123", "+0x0007b" },
    { "%+#08Zx", "-123", "-0x0007b" },

    { "%.0Zd", "0", "" },
    { "%.1Zd", "0", "0" },
    { "%.2Zd", "0", "00" },
    { "%.3Zd", "0", "000" },
  };

  int        i, j;
  mpz_t      z;
  char       *nfmt;
  mp_size_t  nsize, zeros;

  mpz_init (z);

  for (i = 0; i < numberof (data); i++)
    {
      mpz_set_str_or_abort (z, data[i].z, 0);

      /* don't try negatives or forced sign in hex or octal */
      if (mpz_fits_slong_p (z)
	  && ! (hex_or_octal_p (data[i].fmt)
		&& (strchr (data[i].fmt, '+') != NULL || mpz_sgn(z) < 0)))
	{
	  check_plain (data[i].want, data[i].fmt, mpz_get_si (z));
	}

      check_one (data[i].want, data[i].fmt, z);

      /* Same again, with %N and possibly some high zero limbs */
      nfmt = __gmp_allocate_strdup (data[i].fmt);
      for (j = 0; nfmt[j] != '\0'; j++)
	if (nfmt[j] == 'Z')
	  nfmt[j] = 'N';
      for (zeros = 0; zeros <= 3; zeros++)
	{
	  nsize = ABSIZ(z)+zeros;
	  MPZ_REALLOC (z, nsize);
	  nsize = (SIZ(z) >= 0 ? nsize : -nsize);
	  refmpn_zero (PTR(z)+ABSIZ(z), zeros);
	  check_one (data[i].want, nfmt, PTR(z), nsize);
	}
      __gmp_free_func (nfmt, strlen(nfmt)+1);
    }

  mpz_clear (z);
}
コード例 #14
0
ファイル: t-import.c プロジェクト: 119/aircam-openwrt
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);
}
コード例 #15
0
ファイル: t-set_f.c プロジェクト: mahdiz/mpclib
int
main (int argc, char **argv)
{
#if GMP_NAIL_BITS == 0
  static const struct {
    int         f_base;
    const char  *f;
    int         z_base;
    const char  *want_num;
    const char  *want_den;

  } data[] = {

    { -2, "0",    16, "0", "1" },
    { -2, "1",    16, "1", "1" },
    { -2, "1@1",  16, "2", "1" },
    { -2, "1@2",  16, "4", "1" },
    { -2, "1@3",  16, "8", "1" },

    { -2, "1@30", 16,  "40000000", "1" },
    { -2, "1@31", 16,  "80000000", "1" },
    { -2, "1@32", 16, "100000000", "1" },
    { -2, "1@33", 16, "200000000", "1" },
    { -2, "1@34", 16, "400000000", "1" },

    { -2, "1@62", 16,  "4000000000000000", "1" },
    { -2, "1@63", 16,  "8000000000000000", "1" },
    { -2, "1@64", 16, "10000000000000000", "1" },
    { -2, "1@65", 16, "20000000000000000", "1" },
    { -2, "1@66", 16, "40000000000000000", "1" },

    { -2, "1@126", 16,  "40000000000000000000000000000000", "1" },
    { -2, "1@127", 16,  "80000000000000000000000000000000", "1" },
    { -2, "1@128", 16, "100000000000000000000000000000000", "1" },
    { -2, "1@129", 16, "200000000000000000000000000000000", "1" },
    { -2, "1@130", 16, "400000000000000000000000000000000", "1" },

    { -2, "1@-1",  16, "1", "2" },
    { -2, "1@-2",  16, "1", "4" },
    { -2, "1@-3",  16, "1", "8" },

    { -2, "1@-30", 16, "1",  "40000000" },
    { -2, "1@-31", 16, "1",  "80000000" },
    { -2, "1@-32", 16, "1", "100000000" },
    { -2, "1@-33", 16, "1", "200000000" },
    { -2, "1@-34", 16, "1", "400000000" },

    { -2, "1@-62", 16, "1",  "4000000000000000" },
    { -2, "1@-63", 16, "1",  "8000000000000000" },
    { -2, "1@-64", 16, "1", "10000000000000000" },
    { -2, "1@-65", 16, "1", "20000000000000000" },
    { -2, "1@-66", 16, "1", "40000000000000000" },

    { -2, "1@-126", 16, "1",  "40000000000000000000000000000000" },
    { -2, "1@-127", 16, "1",  "80000000000000000000000000000000" },
    { -2, "1@-128", 16, "1", "100000000000000000000000000000000" },
    { -2, "1@-129", 16, "1", "200000000000000000000000000000000" },
    { -2, "1@-130", 16, "1", "400000000000000000000000000000000" },

    { -2, "1@-30", 16, "1",  "40000000" },
    { -2, "1@-31", 16, "1",  "80000000" },
    { -2, "1@-32", 16, "1", "100000000" },
    { -2, "1@-33", 16, "1", "200000000" },
    { -2, "1@-34", 16, "1", "400000000" },

    { -2, "11@-62", 16, "3",  "4000000000000000" },
    { -2, "11@-63", 16, "3",  "8000000000000000" },
    { -2, "11@-64", 16, "3", "10000000000000000" },
    { -2, "11@-65", 16, "3", "20000000000000000" },
    { -2, "11@-66", 16, "3", "40000000000000000" },

    { 16, "80000000.00000001", 16, "8000000000000001", "100000000" },
    { 16, "80000000.00000008", 16, "1000000000000001",  "20000000" },
    { 16, "80000000.8",        16, "100000001", "2" },

  };

  mpf_t  f;
  mpq_t  got;
  mpz_t  want_num, want_den;
  int    i, neg;

  tests_start ();

  mpf_init2 (f, 1024L);
  mpq_init (got);
  mpz_init (want_num);
  mpz_init (want_den);

  for (i = 0; i < numberof (data); i++)
    {
      for (neg = 0; neg <= 1; neg++)
        {
          mpf_set_str_or_abort (f, data[i].f, data[i].f_base);
          mpz_set_str_or_abort (want_num, data[i].want_num, data[i].z_base);
          mpz_set_str_or_abort (want_den, data[i].want_den, data[i].z_base);

          if (neg)
            {
              mpf_neg (f, f);
              mpz_neg (want_num, want_num);
            }

          mpq_set_f (got, f);
          MPQ_CHECK_FORMAT (got);

          if (mpz_cmp (mpq_numref(got), want_num) != 0
              || mpz_cmp (mpq_denref(got), want_den) != 0)
            {
              printf ("wrong at data[%d]\n", i);
              printf ("   f_base %d, z_base %d\n",
                      data[i].f_base, data[i].z_base);

              printf ("   f \"%s\" hex ", data[i].f);
              mpf_out_str (stdout, 16, 0, f);
              printf ("\n");

              printf ("   want num 0x");
              mpz_out_str (stdout, 16, want_num);
              printf ("\n");
              printf ("   want den 0x");
              mpz_out_str (stdout, 16, want_den);
              printf ("\n");

              printf ("   got num 0x");
              mpz_out_str (stdout, 16, mpq_numref(got));
              printf ("\n");
              printf ("   got den 0x");
              mpz_out_str (stdout, 16, mpq_denref(got));
              printf ("\n");

              abort ();
            }
        }
    }

  mpf_clear (f);
  mpq_clear (got);
  mpz_clear (want_num);
  mpz_clear (want_den);

  tests_end ();
#endif
  exit (0);
}
コード例 #16
0
ファイル: t-pow.c プロジェクト: 119/aircam-openwrt
void
check_various (void)
{
  static const struct {
    const char *base;
  } data[] = {
    { "0" },
    { "1" },
    { "2" },
    { "3" },
    { "4" },
    { "5" },
    { "6" },
    { "10" },
    { "15" },
    { "16" },

    { "0x1F" },
    { "0xFF" },
    { "0x1001" },
    { "0xFFFF" },
    { "0x10000001" },
    { "0x1000000000000001" },

    /* actual size closest to estimate */
    { "0xFFFFFFFF" },
    { "0xFFFFFFFFFFFFFFFF" },

    /* same after rshift */
    { "0xFFFFFFFF0" },
    { "0xFFFFFFFF00" },
    { "0xFFFFFFFFFFFFFFFF0" },
    { "0xFFFFFFFFFFFFFFFF00" },

    /* change from 2 limbs to 1 after rshift */
    { "0x180000000" },
    { "0x18000000000000000" },

    /* change from 3 limbs to 2 after rshift */
    { "0x18000000100000000" },
    { "0x180000000000000010000000000000000" },

    /* handling of absolute value */
    { "-0x80000000" },
    { "-0x8000000000000000" },

    /* low zero limb, and size>2, checking argument overlap detection */
    { "0x3000000000000000300000000000000030000000000000000" },
  };

  mpz_t  base;
  int    i;

  mpz_init (base);

  for (i = 0; i < numberof (data); i++)
    {
      mpz_set_str_or_abort (base, data[i].base, 0);
      check_base (base);
    }

  mpz_clear (base);
}
コード例 #17
0
ファイル: t-divis_2exp.c プロジェクト: BrianGladman/mpir
void
check_data (void)
{
  static const struct {
    const char    *a;
    unsigned long d;
    int           want;

  } data[] = {

    { "0", 0, 1 },
    { "0", 1, 1 },
    { "0", 2, 1 },
    { "0", 3, 1 },

    { "1", 0, 1 },
    { "1", 1, 0 },
    { "1", 2, 0 },
    { "1", 3, 0 },
    { "1", 10000, 0 },

    { "4", 0, 1 },
    { "4", 1, 1 },
    { "4", 2, 1 },
    { "4", 3, 0 },
    { "4", 4, 0 },
    { "4", 10000, 0 },

    { "0x80000000", 31, 1 },
    { "0x80000000", 32, 0 },
    { "0x80000000", 64, 0 },

    { "0x100000000", 32, 1 },
    { "0x100000000", 33, 0 },
    { "0x100000000", 64, 0 },

    { "0x8000000000000000", 63, 1 },
    { "0x8000000000000000", 64, 0 },
    { "0x8000000000000000", 128, 0 },

    { "0x10000000000000000", 64, 1 },
    { "0x10000000000000000", 65, 0 },
    { "0x10000000000000000", 128, 0 },
    { "0x10000000000000000", 256, 0 },

    { "0x10000000000000000100000000", 32, 1 },
    { "0x10000000000000000100000000", 33, 0 },
    { "0x10000000000000000100000000", 64, 0 },

    { "0x1000000000000000010000000000000000", 64, 1 },
    { "0x1000000000000000010000000000000000", 65, 0 },
    { "0x1000000000000000010000000000000000", 128, 0 },
    { "0x1000000000000000010000000000000000", 256, 0 },
    { "0x1000000000000000010000000000000000", 1024, 0 },

  };

  mpz_t   a, d;
  int     i;

  mpz_init (a);
  mpz_init (d);

  for (i = 0; i < numberof (data); i++)
    {
      mpz_set_str_or_abort (a, data[i].a, 0);
      check_one (a, data[i].d, data[i].want);

      mpz_neg (a, a);
      check_one (a, data[i].d, data[i].want);
    }

  mpz_clear (a);
  mpz_clear (d);
}
コード例 #18
0
ファイル: t-md_2exp.c プロジェクト: AlexeiSheplyakov/gmp.pkg
int
main (int argc, char **argv)
{
  static const struct {
    struct pair_t  left;
    unsigned long  n;
    struct pair_t  right;

  } data[] = {
    { {"0","1"}, 0, {"0","1"} },
    { {"0","1"}, 1, {"0","1"} },
    { {"0","1"}, 2, {"0","1"} },

    { {"1","1"}, 0, {"1","1"} },
    { {"1","1"}, 1, {"2","1"} },
    { {"1","1"}, 2, {"4","1"} },
    { {"1","1"}, 3, {"8","1"} },

    { {"1","1"}, 31, {"0x80000000","1"} },
    { {"1","1"}, 32, {"0x100000000","1"} },
    { {"1","1"}, 33, {"0x200000000","1"} },
    { {"1","1"}, 63, {"0x8000000000000000","1"} },
    { {"1","1"}, 64, {"0x10000000000000000","1"} },
    { {"1","1"}, 65, {"0x20000000000000000","1"} },
    { {"1","1"}, 95, {"0x800000000000000000000000","1"} },
    { {"1","1"}, 96, {"0x1000000000000000000000000","1"} },
    { {"1","1"}, 97, {"0x2000000000000000000000000","1"} },
    { {"1","1"}, 127, {"0x80000000000000000000000000000000","1"} },
    { {"1","1"}, 128, {"0x100000000000000000000000000000000","1"} },
    { {"1","1"}, 129, {"0x200000000000000000000000000000000","1"} },

    { {"1","2"}, 31, {"0x40000000","1"} },
    { {"1","2"}, 32, {"0x80000000","1"} },
    { {"1","2"}, 33, {"0x100000000","1"} },
    { {"1","2"}, 63, {"0x4000000000000000","1"} },
    { {"1","2"}, 64, {"0x8000000000000000","1"} },
    { {"1","2"}, 65, {"0x10000000000000000","1"} },
    { {"1","2"}, 95, {"0x400000000000000000000000","1"} },
    { {"1","2"}, 96, {"0x800000000000000000000000","1"} },
    { {"1","2"}, 97, {"0x1000000000000000000000000","1"} },
    { {"1","2"}, 127, {"0x40000000000000000000000000000000","1"} },
    { {"1","2"}, 128, {"0x80000000000000000000000000000000","1"} },
    { {"1","2"}, 129, {"0x100000000000000000000000000000000","1"} },

    { {"1","0x80000000"}, 30, {"1","2"} },
    { {"1","0x80000000"}, 31, {"1","1"} },
    { {"1","0x80000000"}, 32, {"2","1"} },
    { {"1","0x80000000"}, 33, {"4","1"} },
    { {"1","0x80000000"}, 62, {"0x80000000","1"} },
    { {"1","0x80000000"}, 63, {"0x100000000","1"} },
    { {"1","0x80000000"}, 64, {"0x200000000","1"} },
    { {"1","0x80000000"}, 94, {"0x8000000000000000","1"} },
    { {"1","0x80000000"}, 95, {"0x10000000000000000","1"} },
    { {"1","0x80000000"}, 96, {"0x20000000000000000","1"} },
    { {"1","0x80000000"}, 126, {"0x800000000000000000000000","1"} },
    { {"1","0x80000000"}, 127, {"0x1000000000000000000000000","1"} },
    { {"1","0x80000000"}, 128, {"0x2000000000000000000000000","1"} },

    { {"1","0x100000000"}, 1, {"1","0x80000000"} },
    { {"1","0x100000000"}, 2, {"1","0x40000000"} },
    { {"1","0x100000000"}, 3, {"1","0x20000000"} },

    { {"1","0x10000000000000000"}, 1, {"1","0x8000000000000000"} },
    { {"1","0x10000000000000000"}, 2, {"1","0x4000000000000000"} },
    { {"1","0x10000000000000000"}, 3, {"1","0x2000000000000000"} },
  };

  void (*fun) (mpq_ptr, mpq_srcptr, unsigned long);
  const struct pair_t  *p_start, *p_want;
  const char  *name;
  mpq_t    sep, got, want;
  mpq_ptr  q;
  int      i, muldiv, sign, overlap;

  tests_start ();

  mpq_init (sep);
  mpq_init (got);
  mpq_init (want);

  for (i = 0; i < numberof (data); i++)
    {
      for (muldiv = 0; muldiv < 2; muldiv++)
        {
          if (muldiv == 0)
            {
              fun = mpq_mul_2exp;
              name = "mpq_mul_2exp";
              p_start = &data[i].left;
              p_want = &data[i].right;
            }
          else
            {
              fun = mpq_div_2exp;
              name = "mpq_div_2exp";
              p_start = &data[i].right;
              p_want = &data[i].left;
            }

          for (sign = 0; sign <= 1; sign++)
            {
              mpz_set_str_or_abort (mpq_numref(want), p_want->num, 0);
              mpz_set_str_or_abort (mpq_denref(want), p_want->den, 0);
              if (sign)
                mpq_neg (want, want);

              for (overlap = 0; overlap <= 1; overlap++)
                {
                  q = overlap ? got : sep;

                  /* initial garbage in "got" */
                  mpq_set_ui (got, 123L, 456L);

                  mpz_set_str_or_abort (mpq_numref(q), p_start->num, 0);
                  mpz_set_str_or_abort (mpq_denref(q), p_start->den, 0);
                  if (sign)
                    mpq_neg (q, q);

                  (*fun) (got, q, data[i].n);
                  MPQ_CHECK_FORMAT (got);

                  if (! mpq_equal (got, want))
                    {
                      printf ("%s wrong at data[%d], sign %d, overlap %d\n",
                              name, i, sign, overlap);
                      printf ("   num \"%s\"\n", p_start->num);
                      printf ("   den \"%s\"\n", p_start->den);
                      printf ("   n   %lu\n", data[i].n);

                      printf ("   got  ");
                      mpq_out_str (stdout, 16, got);
                      printf (" (hex)\n");

                      printf ("   want ");
                      mpq_out_str (stdout, 16, want);
                      printf (" (hex)\n");

                      abort ();
                    }
                }
            }
        }
    }

  check_random ();

  mpq_clear (sep);
  mpq_clear (got);
  mpq_clear (want);

  tests_end ();
  exit (0);
}
コード例 #19
0
ファイル: t-inp_str.c プロジェクト: AlexeiSheplyakov/gmp.pkg
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);
}