xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpz/t-export.c (revision ead2c0eee3abe6bcf08c63bfc78eb8a93a579b2b)
1 /* Test mpz_export.
2 
3 Copyright 2002, 2003 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 3 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.  If not, see http://www.gnu.org/licenses/.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "gmp.h"
24 #include "gmp-impl.h"
25 #include "tests.h"
26 
27 
28 void
29 check_data (void)
30 {
31   static const struct {
32     const char  *src;
33     size_t      want_count;
34     int         order;
35     size_t      size;
36     int         endian;
37     int         nail;
38     char        want_data[64];
39 
40   } data[] = {
41 
42     { "0", 0,1, 1,1, 0 },
43     { "0", 0,1, 2,1, 0 },
44     { "0", 0,1, 3,1, 0 },
45 
46     { "0x12345678", 4,1,  1,1, 0, { '\022', '\064', '\126', '\170' } },
47     { "0x12345678", 1,1,  4,1, 0, { '\022', '\064', '\126', '\170' } },
48     { "0x12345678", 1,-1, 4,1, 0, { '\022', '\064', '\126', '\170' } },
49 
50     { "0x12345678", 4,-1, 1,-1, 0, { '\170', '\126', '\064', '\022' } },
51     { "0x12345678", 1,1,  4,-1, 0, { '\170', '\126', '\064', '\022' } },
52     { "0x12345678", 1,-1, 4,-1, 0, { '\170', '\126', '\064', '\022' } },
53 
54     { "0x15", 5,1,  1,1, 7, { '\001', '\000', '\001', '\000', '\001' } },
55 
56     { "0x1FFFFFFFFFFF", 3,1,  2,1,   1, {
57 	'\177','\377', '\177','\377', '\177','\377' } },
58     { "0x1FFFFFFFFFFF", 3,1,  2,-1,  1, {
59 	'\377','\177', '\377','\177', '\377','\177' } },
60     { "0x7",            3,1,  2,1,  15, {
61 	'\000','\001', '\000','\001', '\000','\001' } },
62     { "0x7",            3,1,  2,-1, 15, {
63 	'\001','\000', '\001','\000', '\001','\000' } },
64 
65     { "0x24", 3,1,  2,1,  14, { '\000','\002', '\000','\001', '\000','\000' }},
66     { "0x24", 3,1,  2,-1, 14, { '\002','\000', '\001','\000', '\000','\000' }},
67     { "0x24", 3,-1, 2,-1, 14, { '\000','\000', '\001','\000', '\002','\000' }},
68     { "0x24", 3,-1, 2,1,  14, { '\000','\000', '\000','\001', '\000','\002' }},
69 
70     { "0x123456789ABC", 3,1,  2,1,  0, {
71 	'\022','\064', '\126','\170', '\232','\274' } },
72     { "0x123456789ABC", 3,-1, 2,1,  0, {
73 	'\232','\274', '\126','\170', '\022','\064' } },
74     { "0x123456789ABC", 3,1,  2,-1, 0, {
75 	'\064','\022', '\170','\126', '\274','\232' } },
76     { "0x123456789ABC", 3,-1, 2,-1, 0, {
77 	'\274','\232', '\170','\126', '\064','\022' } },
78 
79     { "0x112233445566778899AABBCC", 3,1,  4,1,  0,
80       { '\021','\042','\063','\104',
81 	'\125','\146','\167','\210',
82 	'\231','\252','\273','\314' } },
83     { "0x112233445566778899AABBCC", 3,-1, 4,1,  0,
84       { '\231','\252','\273','\314',
85 	'\125','\146','\167','\210',
86 	'\021','\042','\063','\104' } },
87     { "0x112233445566778899AABBCC", 3,1,  4,-1, 0,
88       { '\104','\063','\042','\021',
89 	'\210','\167','\146','\125',
90 	'\314','\273','\252','\231' } },
91     { "0x112233445566778899AABBCC", 3,-1, 4,-1, 0,
92       { '\314','\273','\252','\231',
93 	'\210','\167','\146','\125',
94 	'\104','\063','\042','\021' } },
95 
96     { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1,  8,1,  0,
97       { '\020','\001','\040','\002','\060','\003','\100','\004',
98 	'\120','\005','\140','\006','\160','\007','\200','\010',
99 	'\220','\011','\240','\012','\260','\013','\300','\014' } },
100     { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,1,  0,
101       { '\220','\011','\240','\012','\260','\013','\300','\014',
102 	'\120','\005','\140','\006','\160','\007','\200','\010',
103 	'\020','\001','\040','\002','\060','\003','\100','\004' } },
104     { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1,  8,-1, 0,
105       { '\004','\100','\003','\060','\002','\040','\001','\020',
106 	'\010','\200','\007','\160','\006','\140','\005','\120',
107 	'\014','\300','\013','\260','\012','\240','\011','\220' } },
108     { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,-1, 0,
109       { '\014','\300','\013','\260','\012','\240','\011','\220',
110 	'\010','\200','\007','\160','\006','\140','\005','\120',
111 	'\004','\100','\003','\060','\002','\040','\001','\020' } },
112 
113     { "0x155555555555555555555555", 3,1,  4,1,  1,
114       { '\125','\125','\125','\125',
115 	'\052','\252','\252','\252',
116 	'\125','\125','\125','\125' } },
117     { "0x155555555555555555555555", 3,-1,  4,1,  1,
118       { '\125','\125','\125','\125',
119 	'\052','\252','\252','\252',
120 	'\125','\125','\125','\125' } },
121     { "0x155555555555555555555555", 3,1,  4,-1,  1,
122       { '\125','\125','\125','\125',
123 	'\252','\252','\252','\052',
124 	'\125','\125','\125','\125' } },
125     { "0x155555555555555555555555", 3,-1,  4,-1,  1,
126       { '\125','\125','\125','\125',
127 	'\252','\252','\252','\052',
128 	'\125','\125','\125','\125' } },
129   };
130 
131   char    buf[sizeof(data[0].src) + sizeof (mp_limb_t) + 128];
132   char    *got_data;
133   void    *ret;
134   size_t  align, got_count, j;
135   int     i, error = 0;
136   mpz_t   src;
137 
138   mpz_init (src);
139 
140   for (i = 0; i < numberof (data); i++)
141     {
142       for (align = 0; align < sizeof (mp_limb_t); align++)
143 	{
144 	  mpz_set_str_or_abort (src, data[i].src, 0);
145 	  MPZ_CHECK_FORMAT (src);
146 	  got_data = buf + align;
147 
148 	  ASSERT_ALWAYS (data[i].want_count * data[i].size + align
149 			 <= sizeof (buf));
150 
151 	  memset (got_data, '\0', data[i].want_count * data[i].size);
152 	  ret = mpz_export (got_data, &got_count, data[i].order,
153 			    data[i].size, data[i].endian, data[i].nail, src);
154 
155 	  if (ret != got_data)
156 	    {
157 	      printf ("return doesn't equal given pointer\n");
158 	      error = 1;
159 	    }
160 	  if (got_count != data[i].want_count)
161 	    {
162 	      printf ("wrong count\n");
163 	      error = 1;
164 	    }
165 	  if (memcmp (got_data, data[i].want_data, got_count * data[i].size) != 0)
166 	    {
167 	      printf ("wrong result data\n");
168 	      error = 1;
169 	    }
170 	  if (error)
171 	    {
172 	      printf ("    at data[%d]  align=%d\n", i, (int) align);
173 	      printf ("    src \"%s\"\n", data[i].src);
174 	      mpz_trace ("    src", src);
175 	      printf ("    order=%d  size=%lu endian=%d nail=%u\n",
176 		      data[i].order,
177 		      (unsigned long) data[i].size, data[i].endian, data[i].nail);
178 	      printf ("    want count %lu\n", (unsigned long) data[i].want_count);
179 	      printf ("    got count  %lu\n", (unsigned long) got_count);
180 	      printf ("    want");
181 	      for (j = 0; j < data[i].want_count*data[i].size; j++)
182 		printf (" 0x%02X,", (unsigned) (unsigned char) data[i].want_data[j]);
183 	      printf ("\n");
184 	      printf ("    got ");
185 	      for (j = 0; j < got_count*data[i].size; j++)
186 		printf (" 0x%02X,", (unsigned) (unsigned char) got_data[j]);
187 	      printf ("\n");
188 	      abort ();
189 	    }
190 	}
191     }
192   mpz_clear (src);
193 }
194 
195 
196 int
197 main (void)
198 {
199   tests_start ();
200 
201   mp_trace_base = -16;
202   check_data ();
203 
204   tests_end ();
205   exit (0);
206 }
207