[BACK]Return to t-export.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / tests / mpz

Annotation of OpenXM_contrib/gmp/tests/mpz/t-export.c, Revision 1.1

1.1     ! ohara       1: /* Test mpz_export.
        !             2:
        !             3: Copyright 2002 Free Software Foundation, Inc.
        !             4:
        !             5: This file is part of the GNU MP Library.
        !             6:
        !             7: The GNU MP Library is free software; you can redistribute it and/or modify
        !             8: it under the terms of the GNU Lesser General Public License as published by
        !             9: the Free Software Foundation; either version 2.1 of the License, or (at your
        !            10: option) any later version.
        !            11:
        !            12: The GNU MP Library is distributed in the hope that it will be useful, but
        !            13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
        !            15: License for more details.
        !            16:
        !            17: You should have received a copy of the GNU Lesser General Public License
        !            18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
        !            19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
        !            20: MA 02111-1307, USA. */
        !            21:
        !            22: #include <stdio.h>
        !            23: #include <stdlib.h>
        !            24: #include "gmp.h"
        !            25: #include "gmp-impl.h"
        !            26: #include "tests.h"
        !            27:
        !            28:
        !            29: void
        !            30: check_data (void)
        !            31: {
        !            32:   static const struct {
        !            33:     const char  *src;
        !            34:     size_t      want_count;
        !            35:     int         order;
        !            36:     size_t      size;
        !            37:     int         endian;
        !            38:     int         nail;
        !            39:     char        want_data[64];
        !            40:
        !            41:   } data[] = {
        !            42:
        !            43:     { "0", 0,1, 1,1, 0 },
        !            44:     { "0", 0,1, 2,1, 0 },
        !            45:     { "0", 0,1, 3,1, 0 },
        !            46:
        !            47:     { "0x12345678", 4,1,  1,1, 0, { 0x12, 0x34, 0x56, 0x78 } },
        !            48:     { "0x12345678", 1,1,  4,1, 0, { 0x12, 0x34, 0x56, 0x78 } },
        !            49:     { "0x12345678", 1,-1, 4,1, 0, { 0x12, 0x34, 0x56, 0x78 } },
        !            50:
        !            51:     { "0x12345678", 4,-1, 1,-1, 0, { 0x78, 0x56, 0x34, 0x12 } },
        !            52:     { "0x12345678", 1,1,  4,-1, 0, { 0x78, 0x56, 0x34, 0x12 } },
        !            53:     { "0x12345678", 1,-1, 4,-1, 0, { 0x78, 0x56, 0x34, 0x12 } },
        !            54:
        !            55:     { "0x15", 5,1,  1,1, 7, { 0x01, 0x00, 0x01, 0x00, 0x01 } },
        !            56:
        !            57:     { "0x1FFFFFFFFFFF", 3,1,  2,1,   1, { 0x7F,0xFF, 0x7F,0xFF, 0x7F,0xFF } },
        !            58:     { "0x1FFFFFFFFFFF", 3,1,  2,-1,  1, { 0xFF,0x7F, 0xFF,0x7F, 0xFF,0x7F } },
        !            59:     { "0x7",            3,1,  2,1,  15, { 0x00,0x01, 0x00,0x01, 0x00,0x01 } },
        !            60:     { "0x7",            3,1,  2,-1, 15, { 0x01,0x00, 0x01,0x00, 0x01,0x00 } },
        !            61:
        !            62:     { "0x24", 3,1,  2,1,  14, { 0x00,0x02, 0x00,0x01, 0x00,0x00 } },
        !            63:     { "0x24", 3,1,  2,-1, 14, { 0x02,0x00, 0x01,0x00, 0x00,0x00 } },
        !            64:     { "0x24", 3,-1, 2,-1, 14, { 0x00,0x00, 0x01,0x00, 0x02,0x00 } },
        !            65:     { "0x24", 3,-1, 2,1,  14, { 0x00,0x00, 0x00,0x01, 0x00,0x02 } },
        !            66:
        !            67:     { "0x123456789ABC", 3,1,  2,1,  0, { 0x12,0x34, 0x56,0x78, 0x9A,0xBC } },
        !            68:     { "0x123456789ABC", 3,-1, 2,1,  0, { 0x9A,0xBC, 0x56,0x78, 0x12,0x34 } },
        !            69:     { "0x123456789ABC", 3,1,  2,-1, 0, { 0x34,0x12, 0x78,0x56, 0xBC,0x9A } },
        !            70:     { "0x123456789ABC", 3,-1, 2,-1, 0, { 0xBC,0x9A, 0x78,0x56, 0x34,0x12 } },
        !            71:
        !            72:     { "0x112233445566778899AABBCC", 3,1,  4,1,  0,
        !            73:       { 0x11,0x22,0x33,0x44, 0x55,0x66,0x77,0x88, 0x99,0xAA,0xBB,0xCC } },
        !            74:     { "0x112233445566778899AABBCC", 3,-1, 4,1,  0,
        !            75:       { 0x99,0xAA,0xBB,0xCC, 0x55,0x66,0x77,0x88, 0x11,0x22,0x33,0x44 } },
        !            76:     { "0x112233445566778899AABBCC", 3,1,  4,-1, 0,
        !            77:       { 0x44,0x33,0x22,0x11, 0x88,0x77,0x66,0x55, 0xCC,0xBB,0xAA,0x99 } },
        !            78:     { "0x112233445566778899AABBCC", 3,-1, 4,-1, 0,
        !            79:       { 0xCC,0xBB,0xAA,0x99, 0x88,0x77,0x66,0x55, 0x44,0x33,0x22,0x11 } },
        !            80:
        !            81:     { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1,  8,1,  0,
        !            82:       { 0x10,0x01,0x20,0x02,0x30,0x03,0x40,0x04,
        !            83:         0x50,0x05,0x60,0x06,0x70,0x07,0x80,0x08,
        !            84:         0x90,0x09,0xA0,0x0A,0xB0,0x0B,0xC0,0x0C } },
        !            85:     { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,1,  0,
        !            86:       { 0x90,0x09,0xA0,0x0A,0xB0,0x0B,0xC0,0x0C,
        !            87:         0x50,0x05,0x60,0x06,0x70,0x07,0x80,0x08,
        !            88:         0x10,0x01,0x20,0x02,0x30,0x03,0x40,0x04 } },
        !            89:     { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1,  8,-1, 0,
        !            90:       { 0x04,0x40,0x03,0x30,0x02,0x20,0x01,0x10,
        !            91:         0x08,0x80,0x07,0x70,0x06,0x60,0x05,0x50,
        !            92:         0x0C,0xC0,0x0B,0xB0,0x0A,0xA0,0x09,0x90 } },
        !            93:     { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,-1, 0,
        !            94:       { 0x0C,0xC0,0x0B,0xB0,0x0A,0xA0,0x09,0x90,
        !            95:         0x08,0x80,0x07,0x70,0x06,0x60,0x05,0x50,
        !            96:         0x04,0x40,0x03,0x30,0x02,0x20,0x01,0x10 } },
        !            97:
        !            98:     { "0x155555555555555555555555", 3,1,  4,1,  1,
        !            99:       { 0x55,0x55,0x55,0x55, 0x2A,0xAA,0xAA,0xAA, 0x55,0x55,0x55,0x55 } },
        !           100:     { "0x155555555555555555555555", 3,-1,  4,1,  1,
        !           101:       { 0x55,0x55,0x55,0x55, 0x2A,0xAA,0xAA,0xAA, 0x55,0x55,0x55,0x55 } },
        !           102:     { "0x155555555555555555555555", 3,1,  4,-1,  1,
        !           103:       { 0x55,0x55,0x55,0x55, 0xAA,0xAA,0xAA,0x2A, 0x55,0x55,0x55,0x55 } },
        !           104:     { "0x155555555555555555555555", 3,-1,  4,-1,  1,
        !           105:       { 0x55,0x55,0x55,0x55, 0xAA,0xAA,0xAA,0x2A, 0x55,0x55,0x55,0x55 } },
        !           106:   };
        !           107:
        !           108:   char    buf[sizeof(data[0].src) + sizeof (mp_limb_t) + 128];
        !           109:   char    *got_data;
        !           110:   void    *ret;
        !           111:   size_t  align, got_count, j;
        !           112:   int     i, error = 0;
        !           113:   mpz_t   src;
        !           114:
        !           115:   mpz_init (src);
        !           116:
        !           117:   for (i = 0; i < numberof (data); i++)
        !           118:     {
        !           119:       for (align = 0; align < sizeof (mp_limb_t); align++)
        !           120:         {
        !           121:           mpz_set_str_or_abort (src, data[i].src, 0);
        !           122:           MPZ_CHECK_FORMAT (src);
        !           123:           got_data = buf + align;
        !           124:
        !           125:           ASSERT_ALWAYS (data[i].want_count * data[i].size + align
        !           126:                          <= sizeof (buf));
        !           127:
        !           128:           memset (got_data, '\0', data[i].want_count * data[i].size);
        !           129:           ret = mpz_export (got_data, &got_count, data[i].order,
        !           130:                             data[i].size, data[i].endian, data[i].nail, src);
        !           131:
        !           132:           if (ret != got_data)
        !           133:             {
        !           134:               printf ("return doesn't equal given pointer\n");
        !           135:               error = 1;
        !           136:             }
        !           137:           if (got_count != data[i].want_count)
        !           138:             {
        !           139:               printf ("wrong count\n");
        !           140:               error = 1;
        !           141:             }
        !           142:           if (memcmp (got_data, data[i].want_data, got_count * data[i].size) != 0)
        !           143:             {
        !           144:               printf ("wrong result data\n");
        !           145:               error = 1;
        !           146:             }
        !           147:           if (error)
        !           148:             {
        !           149:               printf ("    at data[%d]\n", i);
        !           150:               printf ("    src \"%s\"\n", data[i].src);
        !           151:               mpz_trace ("    src", src);
        !           152:               printf ("    order=%d  size=%u endian=%d nail=%u\n",
        !           153:                       data[i].order,
        !           154:                       data[i].size, data[i].endian, data[i].nail);
        !           155:               printf ("    want count %u\n", data[i].want_count);
        !           156:               printf ("    got count  %u\n", got_count);
        !           157:               printf ("    want");
        !           158:               for (j = 0; j < data[i].want_count*data[i].size; j++)
        !           159:                 printf (" 0x%02X,", (unsigned) (unsigned char) data[i].want_data[j]);
        !           160:               printf ("\n");
        !           161:               printf ("    got ");
        !           162:               for (j = 0; j < got_count*data[i].size; j++)
        !           163:                 printf (" 0x%02X,", (unsigned) (unsigned char) got_data[j]);
        !           164:               printf ("\n");
        !           165:               abort ();
        !           166:             }
        !           167:         }
        !           168:     }
        !           169:   mpz_clear (src);
        !           170: }
        !           171:
        !           172:
        !           173: int
        !           174: main (void)
        !           175: {
        !           176:   tests_start ();
        !           177:
        !           178:   mp_trace_base = -16;
        !           179:   check_data ();
        !           180:
        !           181:   tests_end ();
        !           182:   exit (0);
        !           183: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>