1 /* Test mpz_import.
2
3 Copyright 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library test suite.
6
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 Public License for more details.
16
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "gmp-impl.h"
24 #include "tests.h"
25
26
27 void
check_data(void)28 check_data (void)
29 {
30 static const struct {
31 const char *want;
32 size_t count;
33 int order;
34 size_t size;
35 int endian;
36 int nail;
37 char src[64];
38
39 } data[] = {
40
41 { "0", 0,1, 1,1, 0 },
42 { "0", 1,1, 0,1, 0 },
43
44 { "0x12345678", 4,1, 1,1, 0, { '\22', '\64', '\126', '\170' } },
45 { "0x12345678", 1,1, 4,1, 0, { '\22', '\64', '\126', '\170' } },
46 { "0x12345678", 1,-1, 4,1, 0, { '\22', '\64', '\126', '\170' } },
47
48 { "0x12345678", 4,-1, 1,-1, 0, { '\170', '\126', '\064', '\22' } },
49 { "0x12345678", 1,1, 4,-1, 0, { '\170', '\126', '\064', '\22' } },
50 { "0x12345678", 1,-1, 4,-1, 0, { '\170', '\126', '\064', '\22' } },
51
52 { "0", 5,1, 1,1, 7, { '\376', '\376', '\376', '\376', '\376' } },
53 { "0", 5,-1, 1,1, 7, { '\376', '\376', '\376', '\376', '\376' } },
54 { "0x15", 5,1, 1,1, 7, { '\377', '\376', '\377', '\376', '\377' } },
55
56 { "0", 3,1, 2,1, 1, { '\200','\000', '\200','\000', '\200','\000' }},
57 { "0", 3,1, 2,-1, 1, { '\000','\200', '\000','\200', '\000','\200' }},
58 { "0", 3,1, 2,1, 15, { '\377','\376', '\377','\376', '\377','\376' }},
59
60 { "0x2A", 3,1, 2,1, 14, { '\377','\376', '\377','\376', '\377','\376' } },
61 { "0x06", 3,1, 2,1, 14, { '\377','\374', '\377','\375', '\377','\376' } },
62 { "0x24", 3,-1, 2,1, 14, { '\377','\374', '\377','\375', '\377','\376' } },
63
64 { "0x123456789ABC", 3,1, 2,1, 0, {
65 '\022','\064', '\126','\170', '\232','\274' } },
66 { "0x123456789ABC", 3,-1, 2,1, 0, {
67 '\232','\274', '\126','\170', '\022','\064' } },
68 { "0x123456789ABC", 3,1, 2,-1, 0, {
69 '\064','\022', '\170','\126', '\274','\232' } },
70 { "0x123456789ABC", 3,-1, 2,-1, 0, {
71 '\274','\232', '\170','\126', '\064','\022' } },
72
73 { "0x112233445566778899AABBCC", 3,1, 4,1, 0,
74 { '\021','\042','\063','\104',
75 '\125','\146','\167','\210',
76 '\231','\252','\273','\314' } },
77 { "0x112233445566778899AABBCC", 3,-1, 4,1, 0,
78 { '\231','\252','\273','\314',
79 '\125','\146','\167','\210',
80 '\021','\042','\063','\104' } },
81 { "0x112233445566778899AABBCC", 3,1, 4,-1, 0,
82 { '\104','\063','\042','\021',
83 '\210','\167','\146','\125',
84 '\314','\273','\252','\231' } },
85 { "0x112233445566778899AABBCC", 3,-1, 4,-1, 0,
86 { '\314','\273','\252','\231',
87 '\210','\167','\146','\125',
88 '\104','\063','\042','\021' } },
89
90 { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,1, 0,
91 { '\020','\001','\040','\002','\060','\003','\100','\004',
92 '\120','\005','\140','\006','\160','\007','\200','\010',
93 '\220','\011','\240','\012','\260','\013','\300','\014' } },
94 { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,1, 0,
95 { '\220','\011','\240','\012','\260','\013','\300','\014',
96 '\120','\005','\140','\006','\160','\007','\200','\010',
97 '\020','\001','\040','\002','\060','\003','\100','\004' } },
98 { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,-1, 0,
99 { '\004','\100','\003','\060','\002','\040','\001','\020',
100 '\010','\200','\007','\160','\006','\140','\005','\120',
101 '\014','\300','\013','\260','\012','\240','\011','\220' } },
102 { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,-1, 0,
103 { '\014','\300','\013','\260','\012','\240','\011','\220',
104 '\010','\200','\007','\160','\006','\140','\005','\120',
105 '\004','\100','\003','\060','\002','\040','\001','\020' } },
106
107 { "0x155555555555555555555555", 3,1, 4,1, 1,
108 { '\325','\125','\125','\125',
109 '\252','\252','\252','\252',
110 '\325','\125','\125','\125' } },
111 { "0x155555555555555555555555", 3,-1, 4,1, 1,
112 { '\325','\125','\125','\125',
113 '\252','\252','\252','\252',
114 '\325','\125','\125','\125' } },
115 { "0x155555555555555555555555", 3,1, 4,-1, 1,
116 { '\125','\125','\125','\325',
117 '\252','\252','\252','\252',
118 '\125','\125','\125','\325' } },
119 { "0x155555555555555555555555", 3,-1, 4,-1, 1,
120 { '\125','\125','\125','\325',
121 '\252','\252','\252','\252',
122 '\125','\125','\125','\325' } },
123 };
124
125 char buf[sizeof(data[0].src) + sizeof (mp_limb_t)];
126 char *src;
127 size_t align;
128 int i;
129 mpz_t got, want;
130
131 mpz_init (got);
132 mpz_init (want);
133
134 for (i = 0; i < numberof (data); i++)
135 {
136 for (align = 0; align < sizeof (mp_limb_t); align++)
137 {
138 mpz_set_str_or_abort (want, data[i].want, 0);
139 src = buf + align;
140 memcpy (src, data[i].src, data[i].count * data[i].size);
141
142 mpz_set_ui (got, 0L);
143 mpz_import (got, data[i].count, data[i].order,
144 data[i].size, data[i].endian, data[i].nail, src);
145
146 MPZ_CHECK_FORMAT (got);
147 if (mpz_cmp (got, want) != 0)
148 {
149 printf ("wrong at data[%d]\n", i);
150 printf (" count=%lu order=%d size=%lu endian=%d nail=%u align=%lu\n",
151 (unsigned long) data[i].count, data[i].order,
152 (unsigned long) data[i].size, data[i].endian, data[i].nail,
153 (unsigned long) align);
154 mpz_trace (" got ", got);
155 mpz_trace (" want", want);
156 abort ();
157 }
158 }
159 }
160 mpz_clear (got);
161 mpz_clear (want);
162 }
163
164
165 int
main(void)166 main (void)
167 {
168 tests_start ();
169
170 mp_trace_base = -16;
171 check_data ();
172
173 tests_end ();
174 exit (0);
175 }
176