3 * $Id: mpint.c,v 1.3 2000/10/08 12:11:22 mdw Exp $
5 * Conversion between MPs and standard C integers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 2000/10/08 12:11:22 mdw
34 * Use @MP_EQ@ instead of @MP_CMP@.
36 * Revision 1.2 1999/12/10 23:22:53 mdw
39 * Revision 1.1 1999/11/25 11:38:31 mdw
40 * Support for conversions between MPs and C integers.
44 /*----- Header files ------------------------------------------------------*/
48 /*----- Main code ---------------------------------------------------------*/
50 /* --- Conversion from C integers --- */
52 #define FROM(name, type) \
53 mp *mp_from##name(mp *d, type i) { \
54 MP_FROMINT(d, type, i); \
59 FROM(ushort
, unsigned short)
64 FROM(ulong
, unsigned long)
68 /* --- Conversion to C integers --- */
70 #define TO(name, type, max) \
71 type mp_to##name(const mp *m) \
74 MP_TOINT(m, type, max, i); \
78 TO(short, short, SHRT_MAX
)
79 TO(ushort
, unsigned short, USHRT_MAX
)
81 TO(uint
, unsigned, UINT_MAX
)
82 TO(uint32
, uint32
, 0xffffffff)
83 TO(long, long, LONG_MAX
)
84 TO(ulong
, unsigned long, ULONG_MAX
)
88 /*----- Test rig ----------------------------------------------------------*/
92 #include <mLib/testrig.h>
94 static int fromuint(dstr
*v
)
96 unsigned long i
= *(unsigned long *)v
[0].buf
;
97 mp
*m
= *(mp
**)v
[1].buf
;
98 mp
*d
= mp_fromuint(MP_NEW
, i
);
102 fputs("\n*** fromint failed.\n", stderr
);
103 fprintf(stderr
, "i = %lu", i
);
104 fputs("\nexpect = ", stderr
); mp_writefile(m
, stderr
, 10);
105 fputs("\nresult = ", stderr
); mp_writefile(d
, stderr
, 10);
112 assert(mparena_count(MPARENA_GLOBAL
) == 0);
116 static int fromint(dstr
*v
)
118 long i
= *(long *)v
[0].buf
;
119 mp
*m
= *(mp
**)v
[1].buf
;
120 mp
*d
= mp_fromint(MP_NEW
, i
);
124 fputs("\n*** fromint failed.\n", stderr
);
125 fprintf(stderr
, "i = %li", i
);
126 fputs("\nexpect = ", stderr
); mp_writefile(m
, stderr
, 10);
127 fputs("\nresult = ", stderr
); mp_writefile(d
, stderr
, 10);
134 assert(mparena_count(MPARENA_GLOBAL
) == 0);
138 static int touint(dstr
*v
)
140 mp
*m
= *(mp
**)v
[0].buf
;
141 unsigned long i
= *(unsigned long *)v
[1].buf
;
142 unsigned j
= mp_touint(m
);
146 fputs("\n*** touint failed.\n", stderr
);
147 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
148 fprintf(stderr
, "\nexpect = %lu; result = %u\n", i
, j
);
153 assert(mparena_count(MPARENA_GLOBAL
) == 0);
157 static int toint(dstr
*v
)
159 mp
*m
= *(mp
**)v
[0].buf
;
160 long i
= *(long *)v
[1].buf
;
165 fputs("\n*** toint failed.\n", stderr
);
166 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
167 fprintf(stderr
, "\nexpect = %li; result = %i\n", i
, j
);
172 assert(mparena_count(MPARENA_GLOBAL
) == 0);
176 static test_chunk tests
[] = {
177 { "fromuint", fromuint
, { &type_ulong
, &type_mp
, 0 } },
178 { "fromint", fromint
, { &type_long
, &type_mp
, 0 } },
179 { "touint", touint
, { &type_mp
, &type_ulong
, 0 } },
180 { "toint", toint
, { &type_mp
, &type_long
, 0 } },
184 int main(int argc
, char *argv
[])
187 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpint");
193 /*----- That's all, folks -------------------------------------------------*/