3 * Conversion between MPs and standard C integers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 /*----- Main code ---------------------------------------------------------*/
34 /* --- Conversion from C integers --- */
36 #define FROM(name, type) \
37 mp *mp_from##name(mp *d, type i) { \
38 MP_FROMINT(d, type, i); \
43 FROM(ushort, unsigned short)
48 FROM(ulong, unsigned long)
52 /* --- Conversion to C integers --- */
54 #define TO(name, type, max) \
55 type mp_to##name(const mp *m) \
58 MP_TOINT(m, type, max, i); \
62 TO(short, short, SHRT_MAX)
63 TO(ushort, unsigned short, USHRT_MAX)
65 TO(uint, unsigned, UINT_MAX)
66 TO(uint32, uint32, 0xffffffff)
67 TO(long, long, LONG_MAX)
68 TO(ulong, unsigned long, ULONG_MAX)
72 /*----- Test rig ----------------------------------------------------------*/
76 #include <mLib/testrig.h>
78 static int fromuint(dstr *v)
80 unsigned long i = *(unsigned long *)v[0].buf;
81 mp *m = *(mp **)v[1].buf;
82 mp *d = mp_fromuint(MP_NEW, i);
86 fputs("\n*** fromint failed.\n", stderr);
87 fprintf(stderr, "i = %lu", i);
88 fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10);
89 fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10);
96 assert(mparena_count(MPARENA_GLOBAL) == 0);
100 static int fromint(dstr *v)
102 long i = *(long *)v[0].buf;
103 mp *m = *(mp **)v[1].buf;
104 mp *d = mp_fromint(MP_NEW, i);
108 fputs("\n*** fromint failed.\n", stderr);
109 fprintf(stderr, "i = %li", i);
110 fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10);
111 fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10);
118 assert(mparena_count(MPARENA_GLOBAL) == 0);
122 static int touint(dstr *v)
124 mp *m = *(mp **)v[0].buf;
125 unsigned long i = *(unsigned long *)v[1].buf;
126 unsigned j = mp_touint(m);
129 if ((unsigned)i != j) {
130 fputs("\n*** touint failed.\n", stderr);
131 fputs("m = ", stderr); mp_writefile(m, stderr, 10);
132 fprintf(stderr, "\nexpect = %lu; result = %u\n", i, j);
137 assert(mparena_count(MPARENA_GLOBAL) == 0);
141 static int toint(dstr *v)
143 mp *m = *(mp **)v[0].buf;
144 long i = *(long *)v[1].buf;
149 fputs("\n*** toint failed.\n", stderr);
150 fputs("m = ", stderr); mp_writefile(m, stderr, 10);
151 fprintf(stderr, "\nexpect = %li; result = %i\n", i, j);
156 assert(mparena_count(MPARENA_GLOBAL) == 0);
160 static test_chunk tests[] = {
161 { "fromuint", fromuint, { &type_ulong, &type_mp, 0 } },
162 { "fromint", fromint, { &type_long, &type_mp, 0 } },
163 { "touint", touint, { &type_mp, &type_ulong, 0 } },
164 { "toint", toint, { &type_mp, &type_long, 0 } },
168 int main(int argc, char *argv[])
171 test_run(argc, argv, tests, SRCDIR "/t/mpint");
177 /*----- That's all, folks -------------------------------------------------*/