diff options
author | Kevin Ryde <user42@zip.com.au> | 2001-11-30 00:20:00 +0100 |
---|---|---|
committer | Kevin Ryde <user42@zip.com.au> | 2001-11-30 00:20:00 +0100 |
commit | a7bc5985a43fcefa6f8bb2ed6d67b1c631f590ac (patch) | |
tree | 74367f45f461d4a52242f7c6de0144a05f93d290 | |
parent | c9f170e93a16f2acb43242353f477669000f0238 (diff) | |
download | gmp-a7bc5985a43fcefa6f8bb2ed6d67b1c631f590ac.tar.gz |
2001-11-30 Gerardo Ballabio <ballabio@sissa.it>
* tests/cxx/t-constr.cc, tests/cxx/t-expr.cc: New files.
-rw-r--r-- | tests/cxx/t-constr.cc | 345 | ||||
-rw-r--r-- | tests/cxx/t-expr.cc | 382 |
2 files changed, 727 insertions, 0 deletions
diff --git a/tests/cxx/t-constr.cc b/tests/cxx/t-constr.cc new file mode 100644 index 000000000..abdc0fc1b --- /dev/null +++ b/tests/cxx/t-constr.cc @@ -0,0 +1,345 @@ +/* Test mp*_class constructors. + +Copyright 2001 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your +option) any later version. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the GNU MP Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +MA 02111-1307, USA. */ + +#include <iostream> +#include <strstream> +#include <string> +#include <cstdlib> +#include "gmp.h" +#include "gmp-impl.h" +#include "gmpxx.h" +#include "tests.h" + +using namespace std; + + +#define CHECK_MPZ(args, want) \ + do { \ + mpz_set_str (ref, want, 0); \ + if (mpz_cmp (z.get_mpz_t(), ref) != 0) \ + { \ + cout << "mpz_class constructor wrong, args: " << args << "\n"; \ + cout << " want: " << ref << "\n"; \ + cout << " got: " << z.get_mpz_t() << "\n"; \ + abort (); \ + } \ + } while (0) + +#define CHECK_MPQ(args, want) \ + do { \ + mpq_set_str (ref, want, 0); \ + if (mpq_cmp (q.get_mpq_t(), ref) != 0) \ + { \ + cout << "mpq_class constructor wrong, args: " << args << "\n"; \ + cout << " want: " << ref << "\n"; \ + cout << " got: " << q.get_mpq_t() << "\n"; \ + abort (); \ + } \ + } while (0) + +#define CHECK_MPF(args, want) \ + do { \ + mpf_set_str (ref, want, 10); \ + if (mpf_cmp (f.get_mpf_t(), ref) != 0) \ + { \ + cout << "mpf_class constructor wrong, args: " << args << "\n"; \ + cout << " want: " << ref << "\n"; \ + cout << " got: " << f.get_mpf_t() << "\n"; \ + abort (); \ + } \ + } while (0) + +void +check_mpz (void) +{ + mpz_t ref; + mpz_init (ref); + + { // no arguments + mpz_class z; + CHECK_MPZ ("none", "0"); + } + + { // argument: mpz_class + mpz_class w; + mpz_class z (w); + CHECK_MPZ ("w [mpz_class]", "0"); + } + + { // argument: int + mpz_class z (0); + CHECK_MPZ ("0", "0"); + } + + { // argument: bool + mpz_class z (true); + CHECK_MPZ ("true", "1"); + } + + { // argument: unsigned short + mpz_class z ((unsigned short) 1); + CHECK_MPZ ("(unsigned short) 1", "1"); + } + + { // argument: long + mpz_class z (-1234567890L); + CHECK_MPZ ("-1234567890L", "-1234567890"); + } + + { // argument: double + mpz_class z (3.141592653589793238); + CHECK_MPZ ("3.141592653589793238", "3"); + } + + { // argument: char * + mpz_class z ("12345678901234567890"); + CHECK_MPZ ("\"12345678901234567890\"", "12345678901234567890"); + } + + { // arguments: char *, int + mpz_class z ("FFFF", 16); + CHECK_MPZ ("\"FFFF\", 16", "65535"); + } + + { // argument: string + mpz_class z (string ("1234567890")); + CHECK_MPZ ("string (\"1234567890\")", "1234567890"); + } + + { // arguments: string, int + mpz_class z (string ("7777"), 8); + CHECK_MPZ ("string (\"7777\", 8)", "4095"); + } + + { // argument: mpz_t + mpz_class z (ref); + CHECK_MPZ ("ref [mpz_t]", "4095"); + } + + mpz_clear (ref); +} + +void +check_mpq (void) +{ + mpq_t ref; + mpq_init (ref); + + { // no arguments + mpq_class q; + CHECK_MPQ ("none", "0"); + } + + { // argument: mpq_class + mpq_class r; + mpq_class q (r); + CHECK_MPQ ("r [mpq_class]", "0"); + } + + { // argument: int + mpq_class q (0); + CHECK_MPQ ("0", "0"); + } + + { // argument: bool + mpq_class q (true); + CHECK_MPQ ("true", "1"); + } + + { // argument: unsigned short + mpq_class q ((unsigned short) 1); + CHECK_MPQ ("(unsigned short) 1", "1"); + } + + { // argument: long + mpq_class q (-1234567890L); + CHECK_MPQ ("-1234567890L", "-1234567890"); + } + + { // argument: double + mpq_class q (1.25); + CHECK_MPQ ("1.25", "5/4"); + } + + { // argument: char * + mpq_class q ("12345678901234567890"); + CHECK_MPQ ("\"12345678901234567890\"", "12345678901234567890"); + } + + { // arguments: char *, int + mpq_class q ("FFFF", 16); + CHECK_MPQ ("\"FFFF\", 16", "65535"); + } + + { // argument: string + mpq_class q (string ("1234567890")); + CHECK_MPQ ("string (\"1234567890\")", "1234567890"); + } + + { // arguments: string, int + mpq_class q (string ("7777"), 8); + CHECK_MPQ ("string (\"7777\", 8)", "4095"); + } + + { // argument: mpz_t + mpq_class q (ref); + CHECK_MPQ ("ref [mpq_t]", "4095"); + } + + { // arguments: int, int + mpq_class q (1, 2); + CHECK_MPQ ("1, 2", "1/2"); + } + + { // arguments: mpz_class, mpz_class + mpz_class z (3), w (4); + mpq_class q (z, w); + CHECK_MPQ ("z, w [mpz_class, mpz_class]", "3/4"); + } + + { // arguments: int, mpz_class + mpz_class z (5); + mpq_class q (6, z); + CHECK_MPQ ("11, z [mpz_class]", "6/5"); + } + + mpq_clear (ref); +} + +void +check_mpf (void) +{ + mpf_t ref; + mpf_init (ref); + + { // no arguments + mpf_class f; + CHECK_MPF ("none", "0.0"); + } + + { // argument: mpf_class + mpf_class g; + mpf_class f (g); + CHECK_MPF ("g [mpf_class]", "0.0"); + } + + { // argument: int + mpf_class f (0); + CHECK_MPF ("0", "0.0"); + } + + { // argument: bool + mpf_class f (true); + CHECK_MPF ("true", "1.0"); + } + + { // argument: unsigned short + mpf_class f ((unsigned short) 1); + CHECK_MPF ("(unsigned short) 1", "1.0"); + } + + { // argument: long + mpf_class f (-1234567890L); + CHECK_MPF ("-1234567890L", "-1234567890.0"); + } + + { // argument: double + mpf_class f (3.125e+4); + CHECK_MPF ("3.125e+4", "31250.0"); + } + + { // argument: char * + mpf_class f ("12345678901234567890"); + CHECK_MPF ("\"12345678901234567890\"", "12345678901234567890.0"); + } + + { // argument: string + mpf_class f (string ("1234567890")); + CHECK_MPF ("string (\"1234567890\")", "1234567890.0"); + } + + { // argument: mpf_t + mpf_class f (ref); + CHECK_MPF ("ref [mpf_t]", "1234567890.0"); + } + + { // arguments: mpf_class, int + mpf_class g; + mpf_class f (g, 64); + CHECK_MPF ("g [mpf_class], 64", "0.0"); + } + + { // arguments: int, int + mpf_class f (0, 128); + CHECK_MPF ("0, 128", "0.0"); + } + + { // arguments: bool, int + mpf_class f (true, 192); + CHECK_MPF ("true, 192", "1.0"); + } + + { // arguments: unsigned short, int + mpf_class f ((unsigned short) 1, 256); + CHECK_MPF ("(unsigned short) 1, 256", "1.0"); + } + + { // arguments: long, unsigned + mpf_class f (-1234567890L, 64u); + CHECK_MPF ("-1234567890L, 64u", "-1234567890.0"); + } + + { // argument: double, unsigned + mpf_class f (3.125e+4, 128u); + CHECK_MPF ("3.125e+4, 128u", "31250.0"); + } + + { // argument: char *, unsigned + mpf_class f ("12345678901234567890", 192u); + CHECK_MPF ("\"12345678901234567890\", 192u", "12345678901234567890.0"); + } + + { // argument: string, unsigned + mpf_class f (string ("1234567890", 256u)); + CHECK_MPF ("string (\"1234567890\", 256u)", "1234567890.0"); + } + + { // argument: mpf_t, long + mpf_class f (ref, 64L); + CHECK_MPF ("ref [mpf_t], 64L", "1234567890.0"); + } + + mpf_clear (ref); +} + + +int +main (int argc, char *argv[]) +{ + tests_start (); + + check_mpz (); + check_mpq (); + check_mpf (); + + tests_end (); + exit (0); +} diff --git a/tests/cxx/t-expr.cc b/tests/cxx/t-expr.cc new file mode 100644 index 000000000..072620661 --- /dev/null +++ b/tests/cxx/t-expr.cc @@ -0,0 +1,382 @@ +/* Test mp*_class arithmetic expressions. + +Copyright 2001 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your +option) any later version. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the GNU MP Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +MA 02111-1307, USA. */ + +#include <iostream> +#include <strstream> +#include <string> +#include <cstdlib> +#include "gmp.h" +#include "gmp-impl.h" +#include "gmpxx.h" +#include "tests.h" + +using namespace std; + + +#define CHECK_MPZ(expr, want) \ + do { \ + mpz_set_str (ref, want, 0); \ + if (mpz_cmp (z.get_mpz_t(), ref) != 0) \ + { \ + cout << "mpz_class expression wrong: " << expr << "\n"; \ + cout << " want: " << ref << "\n"; \ + cout << " got: " << z.get_mpz_t() << "\n"; \ + abort (); \ + } \ + } while (0) + +#define CHECK_MPQ(expr, want) \ + do { \ + mpq_set_str (ref, want, 0); \ + if (mpq_cmp (q.get_mpq_t(), ref) != 0) \ + { \ + cout << "mpq_class expression wrong: " << expr << "\n"; \ + cout << " want: " << ref << "\n"; \ + cout << " got: " << q.get_mpq_t() << "\n"; \ + abort (); \ + } \ + } while (0) + +#define CHECK_MPF(expr, want) \ + do { \ + mpf_set_str (ref, want, 10); \ + if (mpf_cmp (f.get_mpf_t(), ref) != 0) \ + { \ + cout << "mpf_class constructor wrong: " << expr << "\n"; \ + cout << " want: " << ref << "\n"; \ + cout << " got: " << f.get_mpf_t() << "\n"; \ + abort (); \ + } \ + } while (0) + +void +check_mpz (void) +{ + mpz_class z, w (1), v (2), u(3); + mpz_t ref; + mpz_init(ref); + + // simple assignments + + // mpz_class + z = w; + CHECK_MPZ ("z = w", "1"); + + // int + z = -1; + CHECK_MPZ ("z = -1", "-1"); + + // unsigned long + z = 3456789012ul; + CHECK_MPZ ("z = 3456789012ul", "3456789012"); + + // char * + z = "12345678901234567890"; + CHECK_MPZ ("z = \"12345678901234567890\"", "12345678901234567890"); + + // string + z = string ("1234567890"); + CHECK_MPZ ("z = string (\"1234567890\")", "1234567890"); + + // compound expressions + + // template<class Op> + // __gmp_expr<__gmpz_value, __gmp_unary_expr<mpz_class, Op> > + // [Op = __gmp_unary_minus] + z = -w; + CHECK_MPZ ("z = -w", "-1"); + + // template<class Op> + // __gmp_expr<__gmpz_value, __gmp_binary_expr<mpz_class, mpz_class, Op> > + // [Op = __gmp_binary_plus] + z = w + v; + CHECK_MPZ ("z = w + v", "3"); + + // template<class T, class Op> + // __gmp_expr<__gmpz_value, __gmp_binary_expr<mpz_class, T, Op> > + // [T = int, Op = __gmp_binary_minus] + z = w - 2; + CHECK_MPZ ("z = w - 2", "-1"); + + // template<class T, class Op> + // __gmp_expr<__gmpz_value, __gmp_binary_expr<T, mpz_class, Op> > + // [T = int, Op = __gmp_binary_divides] + z = 3 / w; + CHECK_MPZ ("z = 3 / w", "3"); + + // template<class T, class U, class Op> + // __gmp_expr + // <__gmpz_value, __gmp_binary_expr<mpz_class, __gmp_expr<T, U>, Op> + // [T = __gmpz_value, U = __gmp_unary_expr<mpz_class, __gmp_unary_minus>, + // Op = __gmp_binary_multiplies] + z = w * (-v); + CHECK_MPZ ("z = w * (-v)", "-2"); + + // template<class T, class U, class Op> + // __gmp_expr<__gmpz_value, + // __gmp_binary_expr<__gmp_expr<T, U>, mpz_class, Op> + // [T = __gmpz_value, + // U = __gmp_binary_expr<mpz_class, mpz_class, __gmp_binary_modulus>, + // Op = __gmp_binary_plus] + z = (w % v) + u; + CHECK_MPZ ("z = (w % v) + u", "4"); + + // template<class T, class U, class V, class Op> + // __gmp_expr<__gmpz_value, __gmp_binary_expr<__gmp_expr<T, U>, V, Op> + // [T = __gmpz_value, U = __gmp_unary_expr<mpz_class, __gmp_unary_minus>, + // V = int, Op = __gmp_binary_lshift] + z = (-w) << 2; + CHECK_MPZ ("z = (-w) << 2", "-4"); + + // template<class T, class U, class V, class Op> + // __gmp_expr<__gmpz_value, __gmp_binary_expr<T, __gmp_expr<U, V>, Op> + // [T = double, U = __gmpz_value, + // V = __gmp_binary_expr<mpz_class, mpz_class, __gmp_binary_plus>, + // Op = __gmp_binary_divides] + z = 6.0 / (w + v); + CHECK_MPZ ("z = 6.0 / (w + v)", "2"); + + // template<class T, class U, class V, class W, class Op> + // __gmp_expr + // <__gmpz_value, __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, Op> + // [T = __gmpz_value, + // U = __gmp_binary_expr<mpz_class, mpz_class, __gmp_binary_minus>, + // V = __gmpz_value, W = __gmp_unary_expr<mpz_class, __gmp_unary_minus>, + // Op = __gmp_binary_multiplies] + z = (w - v) * (-u); + CHECK_MPZ ("z = (w - v) * (-u)", "3"); + + mpz_clear(ref); +} + +void +check_mpq (void) +{ + mpq_class q, r ("1/2"), s ("3/4"), t ("5/6"); + mpq_t ref; + mpq_init(ref); + + // simple assignments + + // mpq_class + q = r; + CHECK_MPQ ("q = r", "1/2"); + + // int + q = -1; + CHECK_MPQ ("q = -1", "-1"); + + // unsigned long + q = 3456789012ul; + CHECK_MPQ ("q = 3456789012ul", "3456789012"); + + // char * + q = "12345678901234567890"; + CHECK_MPQ ("q = \"12345678901234567890\"", "12345678901234567890"); + + // string + q = string ("1234567890"); + CHECK_MPQ ("q = string (\"1234567890\")", "1234567890"); + + // compound expressions + + // template<class Op> + // __gmp_expr<__gmpq_value, __gmp_unary_expr<mpq_class, Op> > + // [Op = __gmp_unary_minus] + q = -r; + CHECK_MPQ ("q = -r", "-1/2"); + + // template<class Op> + // __gmp_expr<__gmpq_value, __gmp_binary_expr<mpq_class, mpq_class, Op> > + // [Op = __gmp_binary_plus] + q = r + s; + CHECK_MPQ ("q = r + s", "5/4"); + + // template<class T, class Op> + // __gmp_expr<__gmpq_value, __gmp_binary_expr<mpq_class, T, Op> > + // [T = int, Op = __gmp_binary_minus] + q = r - 2; + CHECK_MPQ ("q = r - 2", "-3/2"); + + // template<class T, class Op> + // __gmp_expr<__gmpq_value, __gmp_binary_expr<T, mpq_class, Op> > + // [T = int, Op = __gmp_binary_divides] + q = 3 / r; + CHECK_MPQ ("q = 3 / r", "6"); + + // template<class T, class U, class Op> + // __gmp_expr + // <__gmpq_value, __gmp_binary_expr<mpq_class, __gmp_expr<T, U>, Op> + // [T = __gmpq_value, U = __gmp_unary_expr<mpq_class, __gmp_unary_minus>, + // Op = __gmp_binary_multiplies] + q = r * (-s); + CHECK_MPQ ("q = r * (-s)", "-3/8"); + + // template<class T, class U, class Op> + // __gmp_expr<__gmpq_value, + // __gmp_binary_expr<__gmp_expr<T, U>, mpq_class, Op> + // [T = __gmpq_value, + // U = __gmp_binary_expr<mpq_class, mpq_class, __gmp_binary_divides>, + // Op = __gmp_binary_plus] + q = (r / s) + t; + CHECK_MPQ ("q = (r / s) + t", "3/2"); + + // template<class T, class U, class V, class Op> + // __gmp_expr<__gmpq_value, __gmp_binary_expr<__gmp_expr<T, U>, V, Op> + // [T = __gmpq_value, U = __gmp_unary_expr<mpq_class, __gmp_unary_minus>, + // V = int, Op = __gmp_binary_lshift] + q = (-r) << 2; + CHECK_MPQ ("q = (-r) << 2", "-2"); + + // template<class T, class U, class V, class Op> + // __gmp_expr<__gmpq_value, __gmp_binary_expr<T, __gmp_expr<U, V>, Op> + // [T = double, U = __gmpq_value, + // V = __gmp_binary_expr<mpq_class, mpq_class, __gmp_binary_plus>, + // Op = __gmp_binary_divides] + q = 6.0 / (r + s); + CHECK_MPQ ("q = 6.0 / (r + s)", "24/5"); + + // template<class T, class U, class V, class W, class Op> + // __gmp_expr + // <__gmpq_value, __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, Op> + // [T = __gmpq_value, + // U = __gmp_binary_expr<mpq_class, mpq_class, __gmp_binary_minus>, + // V = __gmpq_value, W = __gmp_unary_expr<mpq_class, __gmp_unary_minus>, + // Op = __gmp_binary_multiplies] + q = (r - s) * (-t); + CHECK_MPQ ("q = (r - s) * (-t)", "5/24"); + + mpq_clear(ref); +} + +void +check_mpf (void) +{ + mpf_class f, g ("1.0"), h ("0.25"), i ("3e+2"); + mpf_t ref; + mpf_init(ref); + + // simple assignments + + // mpf_class + f = g; + CHECK_MPF ("f = g", "1.0"); + + // int + f = -1; + CHECK_MPF ("f = -1", "-1.0"); + + // unsigned long + f = 3456789012ul; + CHECK_MPF ("f = 3456789012ul", "3456789012.0"); + + // char * + f = "1234567890"; + CHECK_MPF ("f = \"1234567890\"", "1234567890.0"); + + // string + f = string ("123456"); + CHECK_MPF ("f = string (\"123456\")", "123456"); + + // compound expressions + + // template<class Op> + // __gmp_expr<__gmpf_value, __gmp_unary_expr<mpf_class, Op> > + // [Op = __gmp_unary_minus] + f = -g; + CHECK_MPF ("f = -g", "-1.0"); + + // template<class Op> + // __gmp_expr<__gmpf_value, __gmp_binary_expr<mpf_class, mpf_class, Op> > + // [Op = __gmp_binary_plus] + f = g + h; + CHECK_MPF ("f = g + h", "1.25"); + + // template<class T, class Op> + // __gmp_expr<__gmpf_value, __gmp_binary_expr<mpf_class, T, Op> > + // [T = int, Op = __gmp_binary_minus] + f = g - 2; + CHECK_MPF ("f = g - 2", "-1.0"); + + // template<class T, class Op> + // __gmp_expr<__gmpf_value, __gmp_binary_expr<T, mpf_class, Op> > + // [T = int, Op = __gmp_binary_divides] + f = 3 / g; + CHECK_MPF ("f = 3 / g", "3.0"); + + // template<class T, class U, class Op> + // __gmp_expr + // <__gmpf_value, __gmp_binary_expr<mpf_class, __gmp_expr<T, U>, Op> + // [T = __gmpf_value, U = __gmp_unary_expr<mpf_class, __gmp_unary_minus>, + // Op = __gmp_binary_multiplies] + f = g * (-h); + CHECK_MPF ("f = g * (-h)", "-0.25"); + + // template<class T, class U, class Op> + // __gmp_expr<__gmpf_value, + // __gmp_binary_expr<__gmp_expr<T, U>, mpf_class, Op> + // [T = __gmpf_value, + // U = __gmp_binary_expr<mpf_class, mpf_class, __gmp_binary_divides>, + // Op = __gmp_binary_plus] + f = (g / h) + i; + CHECK_MPF ("f = (g / h) + i", "304.0"); + + // template<class T, class U, class V, class Op> + // __gmp_expr<__gmpf_value, __gmp_binary_expr<__gmp_expr<T, U>, V, Op> + // [T = __gmpf_value, U = __gmp_unary_expr<mpf_class, __gmp_unary_minus>, + // V = int, Op = __gmp_binary_lshift] + f = (-g) << 2; + CHECK_MPF ("f = (-g) << 2", "-4.0"); + + // template<class T, class U, class V, class Op> + // __gmp_expr<__gmpqfvalue, __gmp_binary_expr<T, __gmp_expr<U, V>, Op> + // [T = double, U = __gmpf_value, + // V = __gmp_binary_expr<mpf_class, mpf_class, __gmp_binary_plus>, + // Op = __gmp_binary_divides] + f = 5.0 / (g + h); + CHECK_MPF ("f = 5.0 / (g + h)", "4.0"); + + // template<class T, class U, class V, class W, class Op> + // __gmp_expr + // <__gmpf_value, __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, Op> + // [T = __gmpf_value, + // U = __gmp_binary_expr<mpf_class, mpf_class, __gmp_binary_minus>, + // V = __gmpf_value, W = __gmp_unary_expr<mpf_class, __gmp_unary_minus>, + // Op = __gmp_binary_multiplies] + f = (g - h) * (-i); + CHECK_MPF ("f = (g - h) * (-i)", "-225.0"); + + mpf_clear(ref); +} + + +int +main (int argc, char *argv[]) +{ + tests_start (); + + check_mpz (); + check_mpq (); + check_mpf (); + + tests_end (); + exit (0); +} |