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