3 * $Id: mpint.c,v 1.4 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
34 /*----- Main code ---------------------------------------------------------*/
36 /* --- Conversion from C integers --- */
38 #define FROM(name, type) \
39 mp *mp_from##name(mp *d, type i) { \
40 MP_FROMINT(d, type, i); \
45 FROM(ushort
, unsigned short)
50 FROM(ulong
, unsigned long)
54 /* --- Conversion to C integers --- */
56 #define TO(name, type, max) \
57 type mp_to##name(const mp *m) \
60 MP_TOINT(m, type, max, i); \
64 TO(short, short, SHRT_MAX
)
65 TO(ushort
, unsigned short, USHRT_MAX
)
67 TO(uint
, unsigned, UINT_MAX
)
68 TO(uint32
, uint32
, 0xffffffff)
69 TO(long, long, LONG_MAX
)
70 TO(ulong
, unsigned long, ULONG_MAX
)
74 /*----- Test rig ----------------------------------------------------------*/
78 #include <mLib/testrig.h>
80 static int fromuint(dstr
*v
)
82 unsigned long i
= *(unsigned long *)v
[0].buf
;
83 mp
*m
= *(mp
**)v
[1].buf
;
84 mp
*d
= mp_fromuint(MP_NEW
, i
);
88 fputs("\n*** fromint failed.\n", stderr
);
89 fprintf(stderr
, "i = %lu", i
);
90 fputs("\nexpect = ", stderr
); mp_writefile(m
, stderr
, 10);
91 fputs("\nresult = ", stderr
); mp_writefile(d
, stderr
, 10);
98 assert(mparena_count(MPARENA_GLOBAL
) == 0);
102 static int fromint(dstr
*v
)
104 long i
= *(long *)v
[0].buf
;
105 mp
*m
= *(mp
**)v
[1].buf
;
106 mp
*d
= mp_fromint(MP_NEW
, i
);
110 fputs("\n*** fromint failed.\n", stderr
);
111 fprintf(stderr
, "i = %li", i
);
112 fputs("\nexpect = ", stderr
); mp_writefile(m
, stderr
, 10);
113 fputs("\nresult = ", stderr
); mp_writefile(d
, stderr
, 10);
120 assert(mparena_count(MPARENA_GLOBAL
) == 0);
124 static int touint(dstr
*v
)
126 mp
*m
= *(mp
**)v
[0].buf
;
127 unsigned long i
= *(unsigned long *)v
[1].buf
;
128 unsigned j
= mp_touint(m
);
132 fputs("\n*** touint failed.\n", stderr
);
133 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
134 fprintf(stderr
, "\nexpect = %lu; result = %u\n", i
, j
);
139 assert(mparena_count(MPARENA_GLOBAL
) == 0);
143 static int toint(dstr
*v
)
145 mp
*m
= *(mp
**)v
[0].buf
;
146 long i
= *(long *)v
[1].buf
;
151 fputs("\n*** toint failed.\n", stderr
);
152 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
153 fprintf(stderr
, "\nexpect = %li; result = %i\n", i
, j
);
158 assert(mparena_count(MPARENA_GLOBAL
) == 0);
162 static test_chunk tests
[] = {
163 { "fromuint", fromuint
, { &type_ulong
, &type_mp
, 0 } },
164 { "fromint", fromint
, { &type_long
, &type_mp
, 0 } },
165 { "touint", touint
, { &type_mp
, &type_ulong
, 0 } },
166 { "toint", toint
, { &type_mp
, &type_long
, 0 } },
170 int main(int argc
, char *argv
[])
173 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpint");
179 /*----- That's all, folks -------------------------------------------------*/