chiark / gitweb /
Support for uint32.
[catacomb] / mpint.c
1 /* -*-c-*-
2  *
3  * $Id: mpint.c,v 1.2 1999/12/10 23:22:53 mdw Exp $
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 $
33  * Revision 1.2  1999/12/10 23:22:53  mdw
34  * Support for uint32.
35  *
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
55 FROM(short, short)
56 FROM(ushort, unsigned short)
57 FROM(int, int)
58 FROM(uint, unsigned)
59 FROM(uint32, uint32)
60 FROM(long, long)
61 FROM(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
75 TO(short, short, SHRT_MAX)
76 TO(ushort, unsigned short, USHRT_MAX)
77 TO(int, int, INT_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)
82
83 #undef TO
84
85 /*----- Test rig ----------------------------------------------------------*/
86
87 #ifdef TEST_RIG
88
89 #include <mLib/testrig.h>
90
91 static int fromuint(dstr *v)
92 {
93   unsigned long i = *(unsigned long *)v[0].buf;
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);
100     fprintf(stderr, "i = %lu", i);
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);
109   assert(mparena_count(MPARENA_GLOBAL) == 0);
110   return (ok);
111 }
112
113 static int fromint(dstr *v)
114 {
115   long i = *(long *)v[0].buf;
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);
122     fprintf(stderr, "i = %li", i);
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);
131   assert(mparena_count(MPARENA_GLOBAL) == 0);
132   return (ok);
133 }
134
135 static int touint(dstr *v)
136 {
137   mp *m = *(mp **)v[0].buf;
138   unsigned long i = *(unsigned long *)v[1].buf;
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);
145     fprintf(stderr, "\nexpect = %lu; result = %u\n", i, j);
146     ok = 0;
147   }
148
149   mp_drop(m);
150   assert(mparena_count(MPARENA_GLOBAL) == 0);
151   return (ok);
152 }
153
154 static int toint(dstr *v)
155 {
156   mp *m = *(mp **)v[0].buf;
157   long i = *(long *)v[1].buf;
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);
164     fprintf(stderr, "\nexpect = %li; result = %i\n", i, j);
165     ok = 0;
166   }
167
168   mp_drop(m);
169   assert(mparena_count(MPARENA_GLOBAL) == 0);
170   return (ok);
171 }
172
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 } },
178   { 0, 0, { 0 } }
179 };
180
181 int 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 -------------------------------------------------*/