3 * $Id: mpint.c,v 1.2 1999/12/10 23:22:53 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.2 1999/12/10 23:22:53 mdw
36 * Revision 1.1 1999/11/25 11:38:31 mdw
37 * Support for conversions between MPs and C integers.
41 /*----- Header files ------------------------------------------------------*/
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- Conversion from C integers --- */
49 #define FROM(name, type) \
50 mp *mp_from##name(mp *d, type i) { \
51 MP_FROMINT(d, type, i); \
56 FROM(ushort
, unsigned short)
61 FROM(ulong
, unsigned long)
65 /* --- Conversion to C integers --- */
67 #define TO(name, type, max) \
68 type mp_to##name(const mp *m) \
71 MP_TOINT(m, type, max, i); \
75 TO(short, short, SHRT_MAX
)
76 TO(ushort
, unsigned short, USHRT_MAX
)
78 TO(uint
, unsigned, UINT_MAX
)
79 TO(uint32
, uint32
, 0xffffffff)
80 TO(long, long, LONG_MAX
)
81 TO(ulong
, unsigned long, ULONG_MAX
)
85 /*----- Test rig ----------------------------------------------------------*/
89 #include <mLib/testrig.h>
91 static int fromuint(dstr
*v
)
93 unsigned long i
= *(unsigned long *)v
[0].buf
;
94 mp
*m
= *(mp
**)v
[1].buf
;
95 mp
*d
= mp_fromuint(MP_NEW
, i
);
98 if (MP_CMP(d
, !=, m
)) {
99 fputs("\n*** fromint failed.\n", stderr
);
100 fprintf(stderr
, "i = %lu", i
);
101 fputs("\nexpect = ", stderr
); mp_writefile(m
, stderr
, 10);
102 fputs("\nresult = ", stderr
); mp_writefile(d
, stderr
, 10);
109 assert(mparena_count(MPARENA_GLOBAL
) == 0);
113 static int fromint(dstr
*v
)
115 long i
= *(long *)v
[0].buf
;
116 mp
*m
= *(mp
**)v
[1].buf
;
117 mp
*d
= mp_fromint(MP_NEW
, i
);
120 if (MP_CMP(d
, !=, m
)) {
121 fputs("\n*** fromint failed.\n", stderr
);
122 fprintf(stderr
, "i = %li", i
);
123 fputs("\nexpect = ", stderr
); mp_writefile(m
, stderr
, 10);
124 fputs("\nresult = ", stderr
); mp_writefile(d
, stderr
, 10);
131 assert(mparena_count(MPARENA_GLOBAL
) == 0);
135 static int touint(dstr
*v
)
137 mp
*m
= *(mp
**)v
[0].buf
;
138 unsigned long i
= *(unsigned long *)v
[1].buf
;
139 unsigned j
= mp_touint(m
);
143 fputs("\n*** touint failed.\n", stderr
);
144 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
145 fprintf(stderr
, "\nexpect = %lu; result = %u\n", i
, j
);
150 assert(mparena_count(MPARENA_GLOBAL
) == 0);
154 static int toint(dstr
*v
)
156 mp
*m
= *(mp
**)v
[0].buf
;
157 long i
= *(long *)v
[1].buf
;
162 fputs("\n*** toint failed.\n", stderr
);
163 fputs("m = ", stderr
); mp_writefile(m
, stderr
, 10);
164 fprintf(stderr
, "\nexpect = %li; result = %i\n", i
, j
);
169 assert(mparena_count(MPARENA_GLOBAL
) == 0);
173 static test_chunk tests
[] = {
174 { "fromuint", fromuint
, { &type_ulong
, &type_mp
, 0 } },
175 { "fromint", fromint
, { &type_long
, &type_mp
, 0 } },
176 { "touint", touint
, { &type_mp
, &type_ulong
, 0 } },
177 { "toint", toint
, { &type_mp
, &type_long
, 0 } },
181 int main(int argc
, char *argv
[])
184 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mpint");
190 /*----- That's all, folks -------------------------------------------------*/