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