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