t-export.c
上传用户:qaz666999
上传日期:2022-08-06
资源大小:2570k
文件大小:7k
- /* Test mpz_export.
- Copyright 2002, 2003 Free Software Foundation, Inc.
- This file is part of the GNU MP Library.
- The GNU MP Library is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 3 of the License, or (at your
- option) any later version.
- The GNU MP Library is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
- License for more details.
- You should have received a copy of the GNU Lesser General Public License
- along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "gmp.h"
- #include "gmp-impl.h"
- #include "tests.h"
- void
- check_data (void)
- {
- static const struct {
- const char *src;
- size_t want_count;
- int order;
- size_t size;
- int endian;
- int nail;
- char want_data[64];
- } data[] = {
- { "0", 0,1, 1,1, 0 },
- { "0", 0,1, 2,1, 0 },
- { "0", 0,1, 3,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', ' 64', ' 22' } },
- { "0x12345678", 1,1, 4,-1, 0, { '170', '126', ' 64', ' 22' } },
- { "0x12345678", 1,-1, 4,-1, 0, { '170', '126', ' 64', ' 22' } },
- { "0x15", 5,1, 1,1, 7, { ' 01', ' 00', ' 01', ' 00', ' 01' } },
- { "0x1FFFFFFFFFFF", 3,1, 2,1, 1, {
- '177','377', '177','377', '177','377' } },
- { "0x1FFFFFFFFFFF", 3,1, 2,-1, 1, {
- '377','177', '377','177', '377','177' } },
- { "0x7", 3,1, 2,1, 15, {
- ' 00',' 01', ' 00',' 01', ' 00',' 01' } },
- { "0x7", 3,1, 2,-1, 15, {
- ' 01',' 00', ' 01',' 00', ' 01',' 00' } },
- { "0x24", 3,1, 2,1, 14, { ' 00',' 02', ' 00',' 01', ' 00',' 00' }},
- { "0x24", 3,1, 2,-1, 14, { ' 02',' 00', ' 01',' 00', ' 00',' 00' }},
- { "0x24", 3,-1, 2,-1, 14, { ' 00',' 00', ' 01',' 00', ' 02',' 00' }},
- { "0x24", 3,-1, 2,1, 14, { ' 00',' 00', ' 00',' 01', ' 00',' 02' }},
- { "0x123456789ABC", 3,1, 2,1, 0, {
- ' 22',' 64', '126','170', '232','274' } },
- { "0x123456789ABC", 3,-1, 2,1, 0, {
- '232','274', '126','170', ' 22',' 64' } },
- { "0x123456789ABC", 3,1, 2,-1, 0, {
- ' 64',' 22', '170','126', '274','232' } },
- { "0x123456789ABC", 3,-1, 2,-1, 0, {
- '274','232', '170','126', ' 64',' 22' } },
- { "0x112233445566778899AABBCC", 3,1, 4,1, 0,
- { ' 21',' 42',' 63','104',
- '125','146','167','210',
- '231','252','273','314' } },
- { "0x112233445566778899AABBCC", 3,-1, 4,1, 0,
- { '231','252','273','314',
- '125','146','167','210',
- ' 21',' 42',' 63','104' } },
- { "0x112233445566778899AABBCC", 3,1, 4,-1, 0,
- { '104',' 63',' 42',' 21',
- '210','167','146','125',
- '314','273','252','231' } },
- { "0x112233445566778899AABBCC", 3,-1, 4,-1, 0,
- { '314','273','252','231',
- '210','167','146','125',
- '104',' 63',' 42',' 21' } },
- { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,1, 0,
- { ' 20',' 01',' 40',' 02',' 60',' 03','100',' 04',
- '120',' 05','140',' 06','160',' 07','200',' 10',
- '220',' 11','240',' 12','260',' 13','300',' 14' } },
- { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,1, 0,
- { '220',' 11','240',' 12','260',' 13','300',' 14',
- '120',' 05','140',' 06','160',' 07','200',' 10',
- ' 20',' 01',' 40',' 02',' 60',' 03','100',' 04' } },
- { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,-1, 0,
- { ' 04','100',' 03',' 60',' 02',' 40',' 01',' 20',
- ' 10','200',' 07','160',' 06','140',' 05','120',
- ' 14','300',' 13','260',' 12','240',' 11','220' } },
- { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,-1, 0,
- { ' 14','300',' 13','260',' 12','240',' 11','220',
- ' 10','200',' 07','160',' 06','140',' 05','120',
- ' 04','100',' 03',' 60',' 02',' 40',' 01',' 20' } },
- { "0x155555555555555555555555", 3,1, 4,1, 1,
- { '125','125','125','125',
- ' 52','252','252','252',
- '125','125','125','125' } },
- { "0x155555555555555555555555", 3,-1, 4,1, 1,
- { '125','125','125','125',
- ' 52','252','252','252',
- '125','125','125','125' } },
- { "0x155555555555555555555555", 3,1, 4,-1, 1,
- { '125','125','125','125',
- '252','252','252',' 52',
- '125','125','125','125' } },
- { "0x155555555555555555555555", 3,-1, 4,-1, 1,
- { '125','125','125','125',
- '252','252','252',' 52',
- '125','125','125','125' } },
- };
- char buf[sizeof(data[0].src) + sizeof (mp_limb_t) + 128];
- char *got_data;
- void *ret;
- size_t align, got_count, j;
- int i, error = 0;
- mpz_t src;
- mpz_init (src);
- for (i = 0; i < numberof (data); i++)
- {
- for (align = 0; align < sizeof (mp_limb_t); align++)
- {
- mpz_set_str_or_abort (src, data[i].src, 0);
- MPZ_CHECK_FORMAT (src);
- got_data = buf + align;
- ASSERT_ALWAYS (data[i].want_count * data[i].size + align
- <= sizeof (buf));
- memset (got_data, ' ', data[i].want_count * data[i].size);
- ret = mpz_export (got_data, &got_count, data[i].order,
- data[i].size, data[i].endian, data[i].nail, src);
- if (ret != got_data)
- {
- printf ("return doesn't equal given pointern");
- error = 1;
- }
- if (got_count != data[i].want_count)
- {
- printf ("wrong countn");
- error = 1;
- }
- if (memcmp (got_data, data[i].want_data, got_count * data[i].size) != 0)
- {
- printf ("wrong result datan");
- error = 1;
- }
- if (error)
- {
- printf (" at data[%d] align=%dn", i, (int) align);
- printf (" src "%s"n", data[i].src);
- mpz_trace (" src", src);
- printf (" order=%d size=%lu endian=%d nail=%un",
- data[i].order,
- (unsigned long) data[i].size, data[i].endian, data[i].nail);
- printf (" want count %lun", (unsigned long) data[i].want_count);
- printf (" got count %lun", (unsigned long) got_count);
- printf (" want");
- for (j = 0; j < data[i].want_count*data[i].size; j++)
- printf (" 0x%02X,", (unsigned) (unsigned char) data[i].want_data[j]);
- printf ("n");
- printf (" got ");
- for (j = 0; j < got_count*data[i].size; j++)
- printf (" 0x%02X,", (unsigned) (unsigned char) got_data[j]);
- printf ("n");
- abort ();
- }
- }
- }
- mpz_clear (src);
- }
- int
- main (void)
- {
- tests_start ();
- mp_trace_base = -16;
- check_data ();
- tests_end ();
- exit (0);
- }