source: trunk/third/gmp/tests/mpz/t-export.c @ 22254

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