More changes. Still embryonic.
[u/mdw/catacomb] / mpint.c
CommitLineData
70b904c5 1/* -*-c-*-
2 *
92c2a290 3 * $Id: mpint.c,v 1.2 1999/12/10 23:22:53 mdw Exp $
70b904c5 4 *
5 * Conversion between MPs and standard C integers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mpint.c,v $
92c2a290 33 * Revision 1.2 1999/12/10 23:22:53 mdw
34 * Support for uint32.
35 *
70b904c5 36 * Revision 1.1 1999/11/25 11:38:31 mdw
37 * Support for conversions between MPs and C integers.
38 *
39 */
40
41/*----- Header files ------------------------------------------------------*/
42
43#include "mpint.h"
44
45/*----- Main code ---------------------------------------------------------*/
46
47/* --- Conversion from C integers --- */
48
49#define FROM(name, type) \
50 mp *mp_from##name(mp *d, type i) { \
51 MP_FROMINT(d, type, i); \
52 return (d); \
53 }
54
55FROM(short, short)
56FROM(ushort, unsigned short)
57FROM(int, int)
58FROM(uint, unsigned)
92c2a290 59FROM(uint32, uint32)
70b904c5 60FROM(long, long)
61FROM(ulong, unsigned long)
62
63#undef FROM
64
65/* --- Conversion to C integers --- */
66
67#define TO(name, type, max) \
68 type mp_to##name(const mp *m) \
69 { \
70 type i; \
71 MP_TOINT(m, type, max, i); \
72 return (i); \
73 }
74
75TO(short, short, SHRT_MAX)
76TO(ushort, unsigned short, USHRT_MAX)
77TO(int, int, INT_MAX)
78TO(uint, unsigned, UINT_MAX)
92c2a290 79TO(uint32, uint32, 0xffffffff)
70b904c5 80TO(long, long, LONG_MAX)
81TO(ulong, unsigned long, ULONG_MAX)
82
83#undef TO
84
85/*----- Test rig ----------------------------------------------------------*/
86
87#ifdef TEST_RIG
88
89#include <mLib/testrig.h>
90
91static int fromuint(dstr *v)
92{
92c2a290 93 unsigned long i = *(unsigned long *)v[0].buf;
70b904c5 94 mp *m = *(mp **)v[1].buf;
95 mp *d = mp_fromuint(MP_NEW, i);
96 int ok = 1;
97
98 if (MP_CMP(d, !=, m)) {
99 fputs("\n*** fromint failed.\n", stderr);
92c2a290 100 fprintf(stderr, "i = %lu", i);
70b904c5 101 fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10);
102 fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10);
103 fputc('\n', stderr);
104 ok = 0;
105 }
106
107 mp_drop(m);
108 mp_drop(d);
92c2a290 109 assert(mparena_count(MPARENA_GLOBAL) == 0);
70b904c5 110 return (ok);
111}
112
113static int fromint(dstr *v)
114{
92c2a290 115 long i = *(long *)v[0].buf;
70b904c5 116 mp *m = *(mp **)v[1].buf;
117 mp *d = mp_fromint(MP_NEW, i);
118 int ok = 1;
119
120 if (MP_CMP(d, !=, m)) {
121 fputs("\n*** fromint failed.\n", stderr);
92c2a290 122 fprintf(stderr, "i = %li", i);
70b904c5 123 fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10);
124 fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10);
125 fputc('\n', stderr);
126 ok = 0;
127 }
128
129 mp_drop(m);
130 mp_drop(d);
92c2a290 131 assert(mparena_count(MPARENA_GLOBAL) == 0);
70b904c5 132 return (ok);
133}
134
135static int touint(dstr *v)
136{
137 mp *m = *(mp **)v[0].buf;
92c2a290 138 unsigned long i = *(unsigned long *)v[1].buf;
70b904c5 139 unsigned j = mp_touint(m);
140 int ok = 1;
141
142 if (i != j) {
143 fputs("\n*** touint failed.\n", stderr);
144 fputs("m = ", stderr); mp_writefile(m, stderr, 10);
92c2a290 145 fprintf(stderr, "\nexpect = %lu; result = %u\n", i, j);
70b904c5 146 ok = 0;
147 }
148
149 mp_drop(m);
92c2a290 150 assert(mparena_count(MPARENA_GLOBAL) == 0);
70b904c5 151 return (ok);
152}
153
154static int toint(dstr *v)
155{
156 mp *m = *(mp **)v[0].buf;
92c2a290 157 long i = *(long *)v[1].buf;
70b904c5 158 int j = mp_toint(m);
159 int ok = 1;
160
161 if (i != j) {
162 fputs("\n*** toint failed.\n", stderr);
163 fputs("m = ", stderr); mp_writefile(m, stderr, 10);
92c2a290 164 fprintf(stderr, "\nexpect = %li; result = %i\n", i, j);
70b904c5 165 ok = 0;
166 }
167
168 mp_drop(m);
92c2a290 169 assert(mparena_count(MPARENA_GLOBAL) == 0);
70b904c5 170 return (ok);
171}
172
173static test_chunk tests[] = {
92c2a290 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 } },
70b904c5 178 { 0, 0, { 0 } }
179};
180
181int main(int argc, char *argv[])
182{
183 sub_init();
184 test_run(argc, argv, tests, SRCDIR "/tests/mpint");
185 return (0);
186}
187
188#endif
189
190/*----- That's all, folks -------------------------------------------------*/