70b904c5 |
1 | /* -*-c-*- |
70b904c5 |
2 | * |
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 | |
2302cd86 |
36 | #define FROM(name, type, max) \ |
70b904c5 |
37 | mp *mp_from##name(mp *d, type i) { \ |
38 | MP_FROMINT(d, type, i); \ |
39 | return (d); \ |
40 | } |
2302cd86 |
41 | MPINT_CONVERSIONS(FROM) |
70b904c5 |
42 | |
43 | /* --- Conversion to C integers --- */ |
44 | |
45 | #define TO(name, type, max) \ |
46 | type mp_to##name(const mp *m) \ |
47 | { \ |
48 | type i; \ |
49 | MP_TOINT(m, type, max, i); \ |
50 | return (i); \ |
51 | } |
2302cd86 |
52 | MPINT_CONVERSIONS(TO) |
70b904c5 |
53 | |
54 | #undef TO |
55 | |
56 | /*----- Test rig ----------------------------------------------------------*/ |
57 | |
58 | #ifdef TEST_RIG |
59 | |
60 | #include <mLib/testrig.h> |
61 | |
62 | static int fromuint(dstr *v) |
63 | { |
92c2a290 |
64 | unsigned long i = *(unsigned long *)v[0].buf; |
70b904c5 |
65 | mp *m = *(mp **)v[1].buf; |
66 | mp *d = mp_fromuint(MP_NEW, i); |
67 | int ok = 1; |
68 | |
22bab86c |
69 | if (!MP_EQ(d, m)) { |
70b904c5 |
70 | fputs("\n*** fromint failed.\n", stderr); |
92c2a290 |
71 | fprintf(stderr, "i = %lu", i); |
70b904c5 |
72 | fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10); |
73 | fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10); |
74 | fputc('\n', stderr); |
75 | ok = 0; |
76 | } |
77 | |
78 | mp_drop(m); |
79 | mp_drop(d); |
92c2a290 |
80 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
70b904c5 |
81 | return (ok); |
82 | } |
83 | |
84 | static int fromint(dstr *v) |
85 | { |
92c2a290 |
86 | long i = *(long *)v[0].buf; |
70b904c5 |
87 | mp *m = *(mp **)v[1].buf; |
88 | mp *d = mp_fromint(MP_NEW, i); |
89 | int ok = 1; |
90 | |
22bab86c |
91 | if (!MP_EQ(d, m)) { |
70b904c5 |
92 | fputs("\n*** fromint failed.\n", stderr); |
92c2a290 |
93 | fprintf(stderr, "i = %li", i); |
70b904c5 |
94 | fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10); |
95 | fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10); |
96 | fputc('\n', stderr); |
97 | ok = 0; |
98 | } |
99 | |
100 | mp_drop(m); |
101 | mp_drop(d); |
92c2a290 |
102 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
70b904c5 |
103 | return (ok); |
104 | } |
105 | |
106 | static int touint(dstr *v) |
107 | { |
108 | mp *m = *(mp **)v[0].buf; |
92c2a290 |
109 | unsigned long i = *(unsigned long *)v[1].buf; |
70b904c5 |
110 | unsigned j = mp_touint(m); |
111 | int ok = 1; |
112 | |
908ebb29 |
113 | if ((unsigned)i != j) { |
70b904c5 |
114 | fputs("\n*** touint failed.\n", stderr); |
115 | fputs("m = ", stderr); mp_writefile(m, stderr, 10); |
92c2a290 |
116 | fprintf(stderr, "\nexpect = %lu; result = %u\n", i, j); |
70b904c5 |
117 | ok = 0; |
118 | } |
119 | |
120 | mp_drop(m); |
92c2a290 |
121 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
70b904c5 |
122 | return (ok); |
123 | } |
124 | |
125 | static int toint(dstr *v) |
126 | { |
127 | mp *m = *(mp **)v[0].buf; |
92c2a290 |
128 | long i = *(long *)v[1].buf; |
70b904c5 |
129 | int j = mp_toint(m); |
130 | int ok = 1; |
131 | |
132 | if (i != j) { |
133 | fputs("\n*** toint failed.\n", stderr); |
134 | fputs("m = ", stderr); mp_writefile(m, stderr, 10); |
92c2a290 |
135 | fprintf(stderr, "\nexpect = %li; result = %i\n", i, j); |
70b904c5 |
136 | ok = 0; |
137 | } |
138 | |
139 | mp_drop(m); |
92c2a290 |
140 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
70b904c5 |
141 | return (ok); |
142 | } |
143 | |
144 | static test_chunk tests[] = { |
92c2a290 |
145 | { "fromuint", fromuint, { &type_ulong, &type_mp, 0 } }, |
146 | { "fromint", fromint, { &type_long, &type_mp, 0 } }, |
147 | { "touint", touint, { &type_mp, &type_ulong, 0 } }, |
148 | { "toint", toint, { &type_mp, &type_long, 0 } }, |
70b904c5 |
149 | { 0, 0, { 0 } } |
150 | }; |
151 | |
152 | int main(int argc, char *argv[]) |
153 | { |
154 | sub_init(); |
0f00dc4c |
155 | test_run(argc, argv, tests, SRCDIR "/t/mpint"); |
70b904c5 |
156 | return (0); |
157 | } |
158 | |
159 | #endif |
160 | |
161 | /*----- That's all, folks -------------------------------------------------*/ |