📄 t-ostream.cc
字号:
/* Test ostream formatted output.Copyright 2001, 2002 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 modifyit under the terms of the GNU Lesser General Public License as published bythe Free Software Foundation; either version 2.1 of the License, or (at youroption) any later version.The GNU MP Library is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITYor FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General PublicLicense for more details.You should have received a copy of the GNU Lesser General Public Licensealong with the GNU MP Library; see the file COPYING.LIB. If not, write tothe Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,MA 02111-1307, USA. */#include <iostream>#include <strstream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "gmp.h"#include "gmp-impl.h"#include "tests.h"using namespace std;int option_check_standard = 0;#define CALL(expr) \ do { \ got.flags (data[i].flags); \ got.width (data[i].width); \ got.precision (data[i].precision); \ if (data[i].fill == '\0') \ got.fill (' '); \ else \ got.fill (data[i].fill); \ \ expr; \ \ if (!got) \ { \ printf ("\"got\" output error\n"); \ abort (); \ } \ if (got.width() != 0) \ { \ printf ("\"got\" width not reset to 0\n"); \ abort (); \ } \ \ } while (0)#define DUMP() \ do { \ printf (" want: |%s|\n", data[i].want); \ printf (" got: |%s|\n", got.str()); \ printf (" width: %d\n", data[i].width); \ printf (" prec: %d\n", got.precision()); \ printf (" flags: 0x%lX\n", (unsigned long) got.flags()); \ } while (0)#define ABORT() \ do { \ DUMP (); \ abort (); \ } while (0)voidcheck_mpz (void){ static const struct { const char *z; const char *want; ios::fmtflags flags; int width; int precision; char fill; } data[] = { { "0", "0", ios::dec }, { "0", "0", ios::oct }, { "0", "0", ios::oct | ios::showbase }, { "0", "0", ios::hex }, { "0", "0x0", ios::hex | ios::showbase }, { "0", "0X0", ios::hex | ios::showbase | ios::uppercase }, { "1", "****1", ios::dec, 5, 0, '*' }, { "-1", " -1", ios::dec | ios::right, 5 }, { "-1", "- 1", ios::dec | ios::internal, 5 }, { "-1", "-1 ", ios::dec | ios::left, 5 }, { "1", " 0x1", ios::hex | ios::showbase | ios::right, 6 }, { "1", "0x 1", ios::hex | ios::showbase | ios::internal, 6 }, { "1", "0x1 ", ios::hex | ios::showbase | ios::left, 6 }, { "1", " +0x1", ios::hex | ios::showbase | ios::showpos | ios::right, 7 }, { "1", "+0x 1", ios::hex | ios::showbase | ios::showpos | ios::internal, 7 }, { "1", "+0x1 ", ios::hex | ios::showbase | ios::showpos | ios::left, 7 }, { "123", "7b", ios::hex }, { "123", "7B", ios::hex | ios::uppercase }, { "123", "0x7b", ios::hex | ios::showbase }, { "123", "0X7B", ios::hex | ios::showbase | ios::uppercase }, { "-123", "-0x7b", ios::hex | ios::showbase }, { "-123", "-0X7B", ios::hex | ios::showbase | ios::uppercase }, { "123", "173", ios::oct }, { "123", "173", ios::oct | ios::uppercase }, { "123", "0173", ios::oct | ios::showbase }, { "123", "0173", ios::oct | ios::showbase | ios::uppercase }, { "-123", "-0173", ios::oct | ios::showbase }, { "-123", "-0173", ios::oct | ios::showbase | ios::uppercase }, }; size_t i; mpz_t z; mpz_init (z); for (i = 0; i < numberof (data); i++) { mpz_set_str_or_abort (z, data[i].z, 0); if (option_check_standard && mpz_fits_slong_p (z) /* no negatives or showpos in hex or oct */ && (((data[i].flags & ios::basefield) == ios::hex || (data[i].flags & ios::basefield) == ios::oct) ? (mpz_sgn (z) >= 0 && ! (data[i].flags & ios::showpos)) : 1) ) { ostrstream got; long n = mpz_get_si (z); CALL (got << n << '\0'); if (strcmp (got.str(), data[i].want) != 0) { printf ("check_mpz data[%d] doesn't match standard ostream output\n", i); printf (" z: %s\n", data[i].z); printf (" n: %ld\n", n); DUMP (); } } { ostrstream got; CALL (operator<< (got, z) << '\0'); if (strcmp (got.str(), data[i].want) != 0) { printf ("mpz operator<< wrong, data[%d]\n", i); printf (" z: %s\n", data[i].z); mpz_trace (" z", z); ABORT (); } } } mpz_clear (z);}voidcheck_mpq (void){ static const struct { const char *q; const char *want; ios::fmtflags flags; int width; int precision; char fill; } data[] = { { "0", "0", ios::dec }, { "0", "0", ios::hex }, { "0", "0x0", ios::hex | ios::showbase }, { "0", "0X0", ios::hex | ios::showbase | ios::uppercase }, { "5/8", "5/8", ios::dec }, { "5/8", "0X5/0X8", ios::hex | ios::showbase | ios::uppercase }, /* zero denominator with showbase */ { "0/0", " 0/0", ios::oct | ios::showbase, 10 }, { "0/0", " 0/0", ios::dec | ios::showbase, 10 }, { "0/0", " 0x0/0x0", ios::hex | ios::showbase, 10 }, { "123/0", " 0173/0", ios::oct | ios::showbase, 10 }, { "123/0", " 123/0", ios::dec | ios::showbase, 10 }, { "123/0", " 0x7b/0x0", ios::hex | ios::showbase, 10 }, { "123/0", " 0X7B/0X0", ios::hex | ios::showbase | ios::uppercase, 10 }, { "0/123", " 0/0173", ios::oct | ios::showbase, 10 }, { "0/123", " 0/123", ios::dec | ios::showbase, 10 }, { "0/123", " 0x0/0x7b", ios::hex | ios::showbase, 10 }, { "0/123", " 0X0/0X7B", ios::hex | ios::showbase | ios::uppercase, 10 }, }; size_t i; mpq_t q; mpq_init (q);#define mpq_integer_p(q) (mpz_cmp_ui (mpq_denref(q), 1L) == 0) for (i = 0; i < numberof (data); i++) { mpq_set_str_or_abort (q, data[i].q, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -