[BACK]Return to t-expr.cc CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / tests / cxx

Annotation of OpenXM_contrib/gmp/tests/cxx/t-expr.cc, Revision 1.1

1.1     ! ohara       1: /* Test mp*_class arithmetic expressions.
        !             2:
        !             3: Copyright 2001 Free Software Foundation, Inc.
        !             4:
        !             5: This file is part of the GNU MP Library.
        !             6:
        !             7: The GNU MP Library is free software; you can redistribute it and/or modify
        !             8: it under the terms of the GNU Lesser General Public License as published by
        !             9: the Free Software Foundation; either version 2.1 of the License, or (at your
        !            10: option) any later version.
        !            11:
        !            12: The GNU MP Library is distributed in the hope that it will be useful, but
        !            13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
        !            15: License for more details.
        !            16:
        !            17: You should have received a copy of the GNU Lesser General Public License
        !            18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
        !            19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
        !            20: MA 02111-1307, USA. */
        !            21:
        !            22: #include <iostream>
        !            23: #include <strstream>
        !            24: #include <string>
        !            25: #include <cstdlib>
        !            26: #include "gmp.h"
        !            27: #include "gmp-impl.h"
        !            28: #include "gmpxx.h"
        !            29: #include "tests.h"
        !            30:
        !            31: using namespace std;
        !            32:
        !            33:
        !            34: #define CHECK_MPZ(expr, want)                                   \
        !            35:   do {                                                          \
        !            36:     mpz_set_str (ref, want, 0);                                 \
        !            37:     if (mpz_cmp (z.get_mpz_t(), ref) != 0)                      \
        !            38:       {                                                         \
        !            39:         cout << "mpz_class expression wrong: " << expr << "\n"; \
        !            40:         cout << "  want:  " << ref << "\n";                     \
        !            41:         cout << "  got:   " << z.get_mpz_t() << "\n";           \
        !            42:         abort ();                                               \
        !            43:       }                                                         \
        !            44:   } while (0)
        !            45:
        !            46: #define CHECK_MPQ(expr, want)                                   \
        !            47:   do {                                                          \
        !            48:     mpq_set_str (ref, want, 0);                                 \
        !            49:     if (mpq_cmp (q.get_mpq_t(), ref) != 0)                      \
        !            50:       {                                                         \
        !            51:         cout << "mpq_class expression wrong: " << expr << "\n"; \
        !            52:         cout << "  want:  " << ref << "\n";                     \
        !            53:         cout << "  got:   " << q.get_mpq_t() << "\n";           \
        !            54:         abort ();                                               \
        !            55:       }                                                         \
        !            56:   } while (0)
        !            57:
        !            58: #define CHECK_MPF(expr, want)                                    \
        !            59:   do {                                                           \
        !            60:     mpf_set_str (ref, want, 10);                                 \
        !            61:     if (mpf_cmp (f.get_mpf_t(), ref) != 0)                       \
        !            62:       {                                                          \
        !            63:         cout << "mpf_class constructor wrong: " << expr << "\n"; \
        !            64:         cout << "  want:  " << ref << "\n";                      \
        !            65:         cout << "  got:   " << f.get_mpf_t() << "\n";            \
        !            66:         abort ();                                                \
        !            67:       }                                                          \
        !            68:   } while (0)
        !            69:
        !            70: void
        !            71: check_mpz (void)
        !            72: {
        !            73:   mpz_class z, w (1), v (2), u(3);
        !            74:   mpz_t ref;
        !            75:   mpz_init(ref);
        !            76:
        !            77:   // simple assignments
        !            78:
        !            79:   // mpz_class
        !            80:   z = w;
        !            81:   CHECK_MPZ ("z = w", "1");
        !            82:
        !            83:   // int
        !            84:   z = -1;
        !            85:   CHECK_MPZ ("z = -1", "-1");
        !            86:
        !            87:   // unsigned long
        !            88:   z = 3456789012ul;
        !            89:   CHECK_MPZ ("z = 3456789012ul", "3456789012");
        !            90:
        !            91:   // char *
        !            92:   z = "12345678901234567890";
        !            93:   CHECK_MPZ ("z = \"12345678901234567890\"", "12345678901234567890");
        !            94:
        !            95:   // string
        !            96:   z = string ("1234567890");
        !            97:   CHECK_MPZ ("z = string (\"1234567890\")", "1234567890");
        !            98:
        !            99:   // compound expressions
        !           100:
        !           101:   // template<class Op>
        !           102:   // __gmp_expr<__gmpz_value, __gmp_unary_expr<mpz_class, Op> >
        !           103:   // [Op = __gmp_unary_minus]
        !           104:   z = -w;
        !           105:   CHECK_MPZ ("z = -w", "-1");
        !           106:
        !           107:   // template<class Op>
        !           108:   // __gmp_expr<__gmpz_value, __gmp_binary_expr<mpz_class, mpz_class, Op> >
        !           109:   // [Op = __gmp_binary_plus]
        !           110:   z = w + v;
        !           111:   CHECK_MPZ ("z = w + v", "3");
        !           112:
        !           113:   // template<class T, class Op>
        !           114:   // __gmp_expr<__gmpz_value, __gmp_binary_expr<mpz_class, T, Op> >
        !           115:   // [T = int, Op = __gmp_binary_minus]
        !           116:   z = w - 2;
        !           117:   CHECK_MPZ ("z = w - 2", "-1");
        !           118:
        !           119:   // template<class T, class Op>
        !           120:   // __gmp_expr<__gmpz_value, __gmp_binary_expr<T, mpz_class, Op> >
        !           121:   // [T = int, Op = __gmp_binary_divides]
        !           122:   z = 3 / w;
        !           123:   CHECK_MPZ ("z = 3 / w", "3");
        !           124:
        !           125:   // template<class T, class U, class Op>
        !           126:   // __gmp_expr
        !           127:   // <__gmpz_value, __gmp_binary_expr<mpz_class, __gmp_expr<T, U>, Op>
        !           128:   // [T = __gmpz_value, U = __gmp_unary_expr<mpz_class, __gmp_unary_minus>,
        !           129:   // Op = __gmp_binary_multiplies]
        !           130:   z = w * (-v);
        !           131:   CHECK_MPZ ("z = w * (-v)", "-2");
        !           132:
        !           133:   // template<class T, class U, class Op>
        !           134:   // __gmp_expr<__gmpz_value,
        !           135:   // __gmp_binary_expr<__gmp_expr<T, U>, mpz_class, Op>
        !           136:   // [T = __gmpz_value,
        !           137:   // U = __gmp_binary_expr<mpz_class, mpz_class, __gmp_binary_modulus>,
        !           138:   // Op = __gmp_binary_plus]
        !           139:   z = (w % v) + u;
        !           140:   CHECK_MPZ ("z = (w % v) + u", "4");
        !           141:
        !           142:   // template<class T, class U, class V, class Op>
        !           143:   // __gmp_expr<__gmpz_value, __gmp_binary_expr<__gmp_expr<T, U>, V, Op>
        !           144:   // [T = __gmpz_value, U = __gmp_unary_expr<mpz_class, __gmp_unary_minus>,
        !           145:   // V = int, Op = __gmp_binary_lshift]
        !           146:   z = (-w) << 2;
        !           147:   CHECK_MPZ ("z = (-w) << 2", "-4");
        !           148:
        !           149:   // template<class T, class U, class V, class Op>
        !           150:   // __gmp_expr<__gmpz_value, __gmp_binary_expr<T, __gmp_expr<U, V>, Op>
        !           151:   // [T = double, U = __gmpz_value,
        !           152:   // V = __gmp_binary_expr<mpz_class, mpz_class, __gmp_binary_plus>,
        !           153:   // Op = __gmp_binary_divides]
        !           154:   z = 6.0 / (w + v);
        !           155:   CHECK_MPZ ("z = 6.0 / (w + v)", "2");
        !           156:
        !           157:   // template<class T, class U, class V, class W, class Op>
        !           158:   // __gmp_expr
        !           159:   // <__gmpz_value, __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, Op>
        !           160:   // [T = __gmpz_value,
        !           161:   // U = __gmp_binary_expr<mpz_class, mpz_class, __gmp_binary_minus>,
        !           162:   // V = __gmpz_value, W = __gmp_unary_expr<mpz_class, __gmp_unary_minus>,
        !           163:   // Op = __gmp_binary_multiplies]
        !           164:   z = (w - v) * (-u);
        !           165:   CHECK_MPZ ("z = (w - v) * (-u)", "3");
        !           166:
        !           167:   mpz_clear(ref);
        !           168: }
        !           169:
        !           170: void
        !           171: check_mpq (void)
        !           172: {
        !           173:   mpq_class q, r ("1/2"), s ("3/4"), t ("5/6");
        !           174:   mpq_t ref;
        !           175:   mpq_init(ref);
        !           176:
        !           177:   // simple assignments
        !           178:
        !           179:   // mpq_class
        !           180:   q = r;
        !           181:   CHECK_MPQ ("q = r", "1/2");
        !           182:
        !           183:   // int
        !           184:   q = -1;
        !           185:   CHECK_MPQ ("q = -1", "-1");
        !           186:
        !           187:   // unsigned long
        !           188:   q = 3456789012ul;
        !           189:   CHECK_MPQ ("q = 3456789012ul", "3456789012");
        !           190:
        !           191:   // char *
        !           192:   q = "12345678901234567890";
        !           193:   CHECK_MPQ ("q = \"12345678901234567890\"", "12345678901234567890");
        !           194:
        !           195:   // string
        !           196:   q = string ("1234567890");
        !           197:   CHECK_MPQ ("q = string (\"1234567890\")", "1234567890");
        !           198:
        !           199:   // compound expressions
        !           200:
        !           201:   // template<class Op>
        !           202:   // __gmp_expr<__gmpq_value, __gmp_unary_expr<mpq_class, Op> >
        !           203:   // [Op = __gmp_unary_minus]
        !           204:   q = -r;
        !           205:   CHECK_MPQ ("q = -r", "-1/2");
        !           206:
        !           207:   // template<class Op>
        !           208:   // __gmp_expr<__gmpq_value, __gmp_binary_expr<mpq_class, mpq_class, Op> >
        !           209:   // [Op = __gmp_binary_plus]
        !           210:   q = r + s;
        !           211:   CHECK_MPQ ("q = r + s", "5/4");
        !           212:
        !           213:   // template<class T, class Op>
        !           214:   // __gmp_expr<__gmpq_value, __gmp_binary_expr<mpq_class, T, Op> >
        !           215:   // [T = int, Op = __gmp_binary_minus]
        !           216:   q = r - 2;
        !           217:   CHECK_MPQ ("q = r - 2", "-3/2");
        !           218:
        !           219:   // template<class T, class Op>
        !           220:   // __gmp_expr<__gmpq_value, __gmp_binary_expr<T, mpq_class, Op> >
        !           221:   // [T = int, Op = __gmp_binary_divides]
        !           222:   q = 3 / r;
        !           223:   CHECK_MPQ ("q = 3 / r", "6");
        !           224:
        !           225:   // template<class T, class U, class Op>
        !           226:   // __gmp_expr
        !           227:   // <__gmpq_value, __gmp_binary_expr<mpq_class, __gmp_expr<T, U>, Op>
        !           228:   // [T = __gmpq_value, U = __gmp_unary_expr<mpq_class, __gmp_unary_minus>,
        !           229:   // Op = __gmp_binary_multiplies]
        !           230:   q = r * (-s);
        !           231:   CHECK_MPQ ("q = r * (-s)", "-3/8");
        !           232:
        !           233:   // template<class T, class U, class Op>
        !           234:   // __gmp_expr<__gmpq_value,
        !           235:   // __gmp_binary_expr<__gmp_expr<T, U>, mpq_class, Op>
        !           236:   // [T = __gmpq_value,
        !           237:   // U = __gmp_binary_expr<mpq_class, mpq_class, __gmp_binary_divides>,
        !           238:   // Op = __gmp_binary_plus]
        !           239:   q = (r / s) + t;
        !           240:   CHECK_MPQ ("q = (r / s) + t", "3/2");
        !           241:
        !           242:   // template<class T, class U, class V, class Op>
        !           243:   // __gmp_expr<__gmpq_value, __gmp_binary_expr<__gmp_expr<T, U>, V, Op>
        !           244:   // [T = __gmpq_value, U = __gmp_unary_expr<mpq_class, __gmp_unary_minus>,
        !           245:   // V = int, Op = __gmp_binary_lshift]
        !           246:   q = (-r) << 2;
        !           247:   CHECK_MPQ ("q = (-r) << 2", "-2");
        !           248:
        !           249:   // template<class T, class U, class V, class Op>
        !           250:   // __gmp_expr<__gmpq_value, __gmp_binary_expr<T, __gmp_expr<U, V>, Op>
        !           251:   // [T = double, U = __gmpq_value,
        !           252:   // V = __gmp_binary_expr<mpq_class, mpq_class, __gmp_binary_plus>,
        !           253:   // Op = __gmp_binary_divides]
        !           254:   q = 6.0 / (r + s);
        !           255:   CHECK_MPQ ("q = 6.0 / (r + s)", "24/5");
        !           256:
        !           257:   // template<class T, class U, class V, class W, class Op>
        !           258:   // __gmp_expr
        !           259:   // <__gmpq_value, __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, Op>
        !           260:   // [T = __gmpq_value,
        !           261:   // U = __gmp_binary_expr<mpq_class, mpq_class, __gmp_binary_minus>,
        !           262:   // V = __gmpq_value, W = __gmp_unary_expr<mpq_class, __gmp_unary_minus>,
        !           263:   // Op = __gmp_binary_multiplies]
        !           264:   q = (r - s) * (-t);
        !           265:   CHECK_MPQ ("q = (r - s) * (-t)", "5/24");
        !           266:
        !           267:   mpq_clear(ref);
        !           268: }
        !           269:
        !           270: void
        !           271: check_mpf (void)
        !           272: {
        !           273:   mpf_class f, g ("1.0"), h ("0.25"), i ("3e+2");
        !           274:   mpf_t ref;
        !           275:   mpf_init(ref);
        !           276:
        !           277:   // simple assignments
        !           278:
        !           279:   // mpf_class
        !           280:   f = g;
        !           281:   CHECK_MPF ("f = g", "1.0");
        !           282:
        !           283:   // int
        !           284:   f = -1;
        !           285:   CHECK_MPF ("f = -1", "-1.0");
        !           286:
        !           287:   // unsigned long
        !           288:   f = 3456789012ul;
        !           289:   CHECK_MPF ("f = 3456789012ul", "3456789012.0");
        !           290:
        !           291:   // char *
        !           292:   f = "1234567890";
        !           293:   CHECK_MPF ("f = \"1234567890\"", "1234567890.0");
        !           294:
        !           295:   // string
        !           296:   f = string ("123456");
        !           297:   CHECK_MPF ("f = string (\"123456\")", "123456");
        !           298:
        !           299:   // compound expressions
        !           300:
        !           301:   // template<class Op>
        !           302:   // __gmp_expr<__gmpf_value, __gmp_unary_expr<mpf_class, Op> >
        !           303:   // [Op = __gmp_unary_minus]
        !           304:   f = -g;
        !           305:   CHECK_MPF ("f = -g", "-1.0");
        !           306:
        !           307:   // template<class Op>
        !           308:   // __gmp_expr<__gmpf_value, __gmp_binary_expr<mpf_class, mpf_class, Op> >
        !           309:   // [Op = __gmp_binary_plus]
        !           310:   f = g + h;
        !           311:   CHECK_MPF ("f = g + h", "1.25");
        !           312:
        !           313:   // template<class T, class Op>
        !           314:   // __gmp_expr<__gmpf_value, __gmp_binary_expr<mpf_class, T, Op> >
        !           315:   // [T = int, Op = __gmp_binary_minus]
        !           316:   f = g - 2;
        !           317:   CHECK_MPF ("f = g - 2", "-1.0");
        !           318:
        !           319:   // template<class T, class Op>
        !           320:   // __gmp_expr<__gmpf_value, __gmp_binary_expr<T, mpf_class, Op> >
        !           321:   // [T = int, Op = __gmp_binary_divides]
        !           322:   f = 3 / g;
        !           323:   CHECK_MPF ("f = 3 / g", "3.0");
        !           324:
        !           325:   // template<class T, class U, class Op>
        !           326:   // __gmp_expr
        !           327:   // <__gmpf_value, __gmp_binary_expr<mpf_class, __gmp_expr<T, U>, Op>
        !           328:   // [T = __gmpf_value, U = __gmp_unary_expr<mpf_class, __gmp_unary_minus>,
        !           329:   // Op = __gmp_binary_multiplies]
        !           330:   f = g * (-h);
        !           331:   CHECK_MPF ("f = g * (-h)", "-0.25");
        !           332:
        !           333:   // template<class T, class U, class Op>
        !           334:   // __gmp_expr<__gmpf_value,
        !           335:   // __gmp_binary_expr<__gmp_expr<T, U>, mpf_class, Op>
        !           336:   // [T = __gmpf_value,
        !           337:   // U = __gmp_binary_expr<mpf_class, mpf_class, __gmp_binary_divides>,
        !           338:   // Op = __gmp_binary_plus]
        !           339:   f = (g / h) + i;
        !           340:   CHECK_MPF ("f = (g / h) + i", "304.0");
        !           341:
        !           342:   // template<class T, class U, class V, class Op>
        !           343:   // __gmp_expr<__gmpf_value, __gmp_binary_expr<__gmp_expr<T, U>, V, Op>
        !           344:   // [T = __gmpf_value, U = __gmp_unary_expr<mpf_class, __gmp_unary_minus>,
        !           345:   // V = int, Op = __gmp_binary_lshift]
        !           346:   f = (-g) << 2;
        !           347:   CHECK_MPF ("f = (-g) << 2", "-4.0");
        !           348:
        !           349:   // template<class T, class U, class V, class Op>
        !           350:   // __gmp_expr<__gmpqfvalue, __gmp_binary_expr<T, __gmp_expr<U, V>, Op>
        !           351:   // [T = double, U = __gmpf_value,
        !           352:   // V = __gmp_binary_expr<mpf_class, mpf_class, __gmp_binary_plus>,
        !           353:   // Op = __gmp_binary_divides]
        !           354:   f = 5.0 / (g + h);
        !           355:   CHECK_MPF ("f = 5.0 / (g + h)", "4.0");
        !           356:
        !           357:   // template<class T, class U, class V, class W, class Op>
        !           358:   // __gmp_expr
        !           359:   // <__gmpf_value, __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, Op>
        !           360:   // [T = __gmpf_value,
        !           361:   // U = __gmp_binary_expr<mpf_class, mpf_class, __gmp_binary_minus>,
        !           362:   // V = __gmpf_value, W = __gmp_unary_expr<mpf_class, __gmp_unary_minus>,
        !           363:   // Op = __gmp_binary_multiplies]
        !           364:   f = (g - h) * (-i);
        !           365:   CHECK_MPF ("f = (g - h) * (-i)", "-225.0");
        !           366:
        !           367:   mpf_clear(ref);
        !           368: }
        !           369:
        !           370:
        !           371: int
        !           372: main (int argc, char *argv[])
        !           373: {
        !           374:   tests_start ();
        !           375:
        !           376:   check_mpz ();
        !           377:   check_mpq ();
        !           378:   check_mpf ();
        !           379:
        !           380:   tests_end ();
        !           381:   exit (0);
        !           382: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>