gcc/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc

321 lines
9.3 KiB
C++
Raw Normal View History

// 1999-11-15 Kevin Ediger <kediger@licor.com>
// test the floating point inserters (facet num_put)
// Copyright (C) 1999 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This 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 General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
[multiple changes] 2000-10-30 Benjamin Kosnik <bkoz@redhat.com> * src/complex_io.cc : Remove ancient defines. * config/os/gnu-linux/bits/os_defines.h: Add defines. * libsupc++/tinfo2.cc: Change to cstddef. * include/bits/codecvt.h: Add include of c++config.h, so that __USE_GNU gets defined. (Important for alpha.) * include/c/bits/std_cwctype.h: Same. * include/c/bits/std_ctime.h: And here. * include/c/bits/std_cstdarg.h: Same. * include/c/bits/std_csignal.h: Same. * include/c/bits/std_csetjmp.h: Same. * include/c/bits/std_clocale.h: Same. * include/c/bits/std_climits.h: Touch. * include/c/bits/std_cfloat.h: Same. * include/c/bits/std_cerrno.h: Same. * include/c/bits/std_cwchar.h: Same. * include/c/bits/std_cassert.h: Same. * include/c/bits/std_cctype.h: Same. * include/c/bits/std_cstddef.h: And here. * include/c/bits/std_cstdlib.h: And here. * include/c/bits/std_cstdio.h: Same. * include/c/bits/std_cstring.h: Add names to namespace std::. * include/bits/c++config (_GNU_SOURCE): Move linux-specific macros from here... (_ISOC99_SOURCE): And this one.... * config/os/gnu-linux/bits/os_defines.h: ...to here. * include/bits/codecvt.h (codecvt<_InternT, _ExternT, __enc_traits>::do_in): Don't cast to const, this is a bug in glibc prior to 2.2. (codecvt<_InternT, _ExternT, __enc_traits>::do_out): Same. * include/c/bits/std_cwchar.h: Add using declarations for mbstate_t. 2000-10-30 Steven King <sxking@uswest.net> * include/bits/codecvt.h: Add cast. * include/c_std/stdio.h: Re-add printf using declaration. * include/c_std/bits/std_cstdio.h: Same * testsuite/22_locale/codecvt_wchar_t_char.cc: Fixup testsuite. * testsuite/22_locale/ctor_copy_dtor.cc: Same. * testsuite/22_locale/facet.cc: Same. * testsuite/22_locale/global_templates.cc: Same. * testsuite/22_locale/operators.cc: Same. * testsuite/22_locale/static_members.cc: Same. * testsuite/26_numerics/c_math.cc: Same. * testsuite/26_numerics/complex_inserters_extractors.cc: Same. * testsuite/27_io/fpos.cc: Same. * testsuite/27_io/istream_extractor_arith.cc: Same. * testsuite/27_io/istream_unformatted.cc: Same. * testsuite/27_io/ostream_inserter_arith.cc: Same. * testsuite/27_io/streambuf.cc: Same. From-SVN: r37149
2000-10-31 02:26:06 +01:00
#include <cstdio> // for sprintf
#include <iostream>
#include <iomanip>
#include <locale>
#include <sstream>
#include <limits>
acinclude.m4 (GLIBCPP_CONFIGURE_TESTSUITE): New macro, calls... 2001-08-06 Phil Edwards <pme@sources.redhat.com> * acinclude.m4 (GLIBCPP_CONFIGURE_TESTSUITE): New macro, calls... (GLIBCPP_CHECK_SETRLIMIT): ...this new macro, which also uses... (GLIBCPP_CHECK_SETRLIMIT_ancilliary): ...this new macro. * configure.in (AM_CONFIG_HEADER): Also generate testsuite_hooks.h. Call GLIBCPP_CONFIGURE_TESTSUITE. * aclocal.m4: Regenerate. * config.h.in: Regenerate. * configure: Regenerate. * testsuite/lib/libstdc++-v3-dg.exp: Download new header instead of old header. * testsuite_flags.in: Add build dir's testsuite to INCLUDES. * testsuite/debug_assert.h: Removed; contents merged into... * testsuite/testsuite_hooks.h.in: ...here. New file. * testsuite/17_intro/header_ciso646.cc: No longer include debug_assert.h, include testsuite_hooks.h instead. * testsuite/17_intro/header_fstream.cc: Likewise. * testsuite/17_intro/header_iomanip.cc: Likewise. * testsuite/17_intro/header_ios.cc: Likewise. * testsuite/17_intro/header_iosfwd.cc: Likewise. * testsuite/17_intro/header_iostream.cc: Likewise. * testsuite/17_intro/header_istream.cc: Likewise. * testsuite/17_intro/header_ostream.cc: Likewise. * testsuite/17_intro/header_sstream.cc: Likewise. * testsuite/17_intro/header_streambuf.cc: Likewise. * testsuite/18_support/numeric_limits.cc: Likewise. * testsuite/19_diagnostics/stdexceptions.cc: Likewise. * testsuite/20_util/allocator_members.cc: Likewise. * testsuite/20_util/auto_ptr.cc: Likewise. * testsuite/20_util/pairs.cc: Likewise. * testsuite/21_strings/append.cc: Likewise. * testsuite/21_strings/capacity.cc: Likewise. * testsuite/21_strings/char_traits_requirements.cc: Likewise. * testsuite/21_strings/compare.cc: Likewise. * testsuite/21_strings/ctor_copy_dtor.cc: Likewise. Also set up call to (disabled) __set_testsuite_memlimit() wrapper. * testsuite/21_strings/element_access.cc: Likewise. * testsuite/21_strings/find.cc: Likewise. * testsuite/21_strings/insert.cc: Likewise. Also set up call to (disabled) __set_testsuite_memlimit() wrapper. * testsuite/21_strings/inserters_extractors.cc: Likewise. * testsuite/21_strings/invariants.cc: Likewise. * testsuite/21_strings/nonmember.cc: Likewise. * testsuite/21_strings/operations.cc: Likewise. * testsuite/21_strings/replace.cc: Likewise. * testsuite/21_strings/rfind.cc: Likewise. * testsuite/21_strings/substr.cc: Likewise. * testsuite/22_locale/codecvt_char_char.cc: Likewise. * testsuite/22_locale/codecvt_unicode_char.cc: Likewise. * testsuite/22_locale/codecvt_unicode_wchar_t.cc: Likewise. * testsuite/22_locale/codecvt_wchar_t_char.cc: Likewise. * testsuite/22_locale/ctor_copy_dtor.cc: Likewise. * testsuite/22_locale/ctype_char_members.cc: Likewise. * testsuite/22_locale/ctype_wchar_t_members.cc: Likewise. * testsuite/22_locale/facet.cc: Likewise. * testsuite/22_locale/global_templates.cc: Likewise. * testsuite/22_locale/members.cc: Likewise. * testsuite/22_locale/numpunct_byname.cc: Likewise. * testsuite/22_locale/numpunct_char_members.cc: Likewise. * testsuite/22_locale/operators.cc: Likewise. * testsuite/22_locale/static_members.cc: Likewise. * testsuite/23_containers/bitset_ctor.cc: Likewise. * testsuite/23_containers/bitset_members.cc: Likewise. * testsuite/23_containers/bitset_shift.cc: Likewise. * testsuite/23_containers/vector_capacity.cc: Likewise. * testsuite/23_containers/vector_ctor.cc: Likewise. * testsuite/23_containers/vector_element_access.cc: Likewise. * testsuite/23_containers/vector_modifiers.cc: Likewise. * testsuite/24_iterators/istreambuf_iterator.cc: Likewise. * testsuite/24_iterators/iterator.cc: Likewise. * testsuite/24_iterators/ostreambuf_iterator.cc: Likewise. * testsuite/25_algorithms/binary_search.cc: Likewise. * testsuite/25_algorithms/copy.cc: Likewise. * testsuite/25_algorithms/heap.cc: Likewise. * testsuite/25_algorithms/lower_bound.cc: Likewise. * testsuite/25_algorithms/min_max.cc: Likewise. * testsuite/25_algorithms/partition.cc: Likewise. * testsuite/25_algorithms/rotate.cc: Likewise. * testsuite/25_algorithms/sort.cc: Likewise. * testsuite/26_numerics/c_math.cc: Likewise. * testsuite/26_numerics/complex_inserters_extractors.cc: Likewise. * testsuite/26_numerics/complex_value.cc: Likewise. * testsuite/27_io/filebuf.cc: Likewise. * testsuite/27_io/filebuf_members.cc: Likewise. * testsuite/27_io/filebuf_virtuals.cc: Likewise. * testsuite/27_io/fpos.cc: Likewise. * testsuite/27_io/fstream_members.cc: Likewise. * testsuite/27_io/ifstream_members.cc: Likewise. * testsuite/27_io/ios_base_callbacks.cc: Likewise. * testsuite/27_io/ios_base_members_static.cc: Likewise. * testsuite/27_io/ios_base_storage.cc: Likewise. * testsuite/27_io/ios_ctor.cc: Likewise. * testsuite/27_io/ios_init.cc: Likewise. * testsuite/27_io/ios_manip_basefield.cc: Likewise. * testsuite/27_io/ios_manip_fmtflags.cc: Likewise. * testsuite/27_io/ios_members.cc: Likewise. * testsuite/27_io/istream_extractor_arith.cc: Likewise. * testsuite/27_io/istream_extractor_char.cc: Likewise. * testsuite/27_io/istream_extractor_other.cc: Likewise. * testsuite/27_io/istream_manip.cc: Likewise. * testsuite/27_io/istream_seeks.cc: Likewise. * testsuite/27_io/istream_sentry.cc: Likewise. * testsuite/27_io/istream_unformatted.cc: Likewise. * testsuite/27_io/istringstream_members.cc: Likewise. * testsuite/27_io/narrow_stream_objects.cc: Likewise. * testsuite/27_io/ofstream_members.cc: Likewise. * testsuite/27_io/ostream_inserter_arith.cc: Likewise. * testsuite/27_io/ostream_inserter_char.cc: Likewise. * testsuite/27_io/ostream_inserter_other.cc: Likewise. * testsuite/27_io/ostream_manip.cc: Likewise. * testsuite/27_io/ostream_seeks.cc: Likewise. * testsuite/27_io/ostream_unformatted.cc: Likewise. * testsuite/27_io/ostringstream_members.cc: Likewise. * testsuite/27_io/streambuf.cc: Likewise. * testsuite/27_io/stringbuf.cc: Likewise. * testsuite/27_io/stringbuf_virtuals.cc: Likewise. * testsuite/27_io/stringstream.cc: Likewise. * testsuite/27_io/stringstream_members.cc: Likewise. * testsuite/27_io/wide_stream_objects.cc: Likewise. From-SVN: r44679
2001-08-07 05:38:33 +02:00
#include <testsuite_hooks.h>
using namespace std;
#ifndef DEBUG_ASSERT
# define TEST_NUMPUT_VERBOSE 1
#endif
struct _TestCase
{
double val;
int precision;
int width;
char decimal;
char fill;
bool fixed;
bool scientific;
bool showpos;
bool showpoint;
bool uppercase;
bool internal;
bool left;
bool right;
const char* result;
#if _GLIBCPP_USE_WCHAR_T
const wchar_t* wresult;
#endif
};
static bool T=true;
static bool F=false;
static _TestCase testcases[] =
{
#if _GLIBCPP_USE_WCHAR_T
// standard output (no formatting applied)
{ 1.2, 6,0,'.',' ', F,F,F,F,F,F,F,F, "1.2",L"1.2" },
{ 54, 6,0,'.',' ', F,F,F,F,F,F,F,F, "54",L"54" },
{ -.012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-0.012",L"-0.012" },
{ -.00000012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-1.2e-07",L"-1.2e-07" },
// fixed formatting
{ 10.2345, 0,0,'.',' ', T,F,F,F,F,F,F,F, "10",L"10" },
{ 10.2345, 0,0,'.',' ', T,F,F,T,F,F,F,F, "10.",L"10." },
{ 10.2345, 1,0,'.',' ', T,F,F,F,F,F,F,F, "10.2",L"10.2" },
{ 10.2345, 4,0,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
{ 10.2345, 6,0,'.',' ', T,F,T,F,F,F,F,F, "+10.234500",L"+10.234500" },
{ -10.2345, 6,0,'.',' ', T,F,F,F,F,F,F,F, "-10.234500",L"-10.234500" },
{ -10.2345, 6,0,',',' ', T,F,F,F,F,F,F,F, "-10,234500",L"-10,234500" },
// fixed formatting with width
{ 10.2345, 4,5,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
{ 10.2345, 4,6,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
{ 10.2345, 4,7,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
{ 10.2345, 4,8,'.',' ', T,F,F,F,F,F,F,F, " 10.2345",L" 10.2345" },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,F, " 10.2345",L" 10.2345" },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,F,T,F, "10.2345 ",L"10.2345 " },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,T, " 10.2345",L" 10.2345" },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, " 10.2345",L" 10.2345" },
{ -10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, "- 10.2345",L"- 10.2345" },
{ -10.2345, 4,10,'.','A', T,F,F,F,F,T,F,F, "-AA10.2345",L"-AA10.2345" },
{ 10.2345, 4,10,'.','#', T,F,T,F,F,T,F,F, "+##10.2345",L"+##10.2345" },
// scientific formatting
{ 1.23e+12, 1,0,'.',' ', F,T,F,F,F,F,F,F, "1.2e+12",L"1.2e+12" },
{ 1.23e+12, 1,0,'.',' ', F,T,F,F,T,F,F,F, "1.2E+12",L"1.2E+12" },
{ 1.23e+12, 2,0,'.',' ', F,T,F,F,F,F,F,F, "1.23e+12",L"1.23e+12" },
{ 1.23e+12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "1.230e+12",L"1.230e+12" },
{ 1.23e+12, 3,0,'.',' ', F,T,T,F,F,F,F,F, "+1.230e+12",L"+1.230e+12" },
{ -1.23e-12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "-1.230e-12",L"-1.230e-12" },
{ 1.23e+12, 3,0,',',' ', F,T,F,F,F,F,F,F, "1,230e+12",L"1,230e+12" },
#else
// standard output (no formatting applied)
{ 1.2, 6,0,'.',' ', F,F,F,F,F,F,F,F, "1.2" },
{ 54, 6,0,'.',' ', F,F,F,F,F,F,F,F, "54" },
{ -.012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-0.012" },
{ -.00000012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-1.2e-07" },
// fixed formatting
{ 10.2345, 0,0,'.',' ', T,F,F,F,F,F,F,F, "10" },
{ 10.2345, 0,0,'.',' ', T,F,F,T,F,F,F,F, "10." },
{ 10.2345, 1,0,'.',' ', T,F,F,F,F,F,F,F, "10.2" },
{ 10.2345, 4,0,'.',' ', T,F,F,F,F,F,F,F, "10.2345" },
{ 10.2345, 6,0,'.',' ', T,F,T,F,F,F,F,F, "+10.234500" },
{ -10.2345, 6,0,'.',' ', T,F,F,F,F,F,F,F, "-10.234500" },
{ -10.2345, 6,0,',',' ', T,F,F,F,F,F,F,F, "-10,234500" },
// fixed formatting with width
{ 10.2345, 4,5,'.',' ', T,F,F,F,F,F,F,F, "10.2345" },
{ 10.2345, 4,6,'.',' ', T,F,F,F,F,F,F,F, "10.2345" },
{ 10.2345, 4,7,'.',' ', T,F,F,F,F,F,F,F, "10.2345" },
{ 10.2345, 4,8,'.',' ', T,F,F,F,F,F,F,F, " 10.2345" },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,F, " 10.2345" },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,F,T,F, "10.2345 " },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,T, " 10.2345" },
{ 10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, " 10.2345" },
{ -10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, "- 10.2345" },
{ -10.2345, 4,10,'.','A', T,F,F,F,F,T,F,F, "-AA10.2345" },
{ 10.2345, 4,10,'.','#', T,F,T,F,F,T,F,F, "+##10.2345" },
// scientific formatting
{ 1.23e+12, 1,0,'.',' ', F,T,F,F,F,F,F,F, "1.2e+12" },
{ 1.23e+12, 1,0,'.',' ', F,T,F,F,T,F,F,F, "1.2E+12" },
{ 1.23e+12, 2,0,'.',' ', F,T,F,F,F,F,F,F, "1.23e+12" },
{ 1.23e+12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "1.230e+12" },
{ 1.23e+12, 3,0,'.',' ', F,T,T,F,F,F,F,F, "+1.230e+12" },
{ -1.23e-12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "-1.230e-12" },
{ 1.23e+12, 3,0,',',' ', F,T,F,F,F,F,F,F, "1,230e+12" },
#endif
};
template<typename _CharT>
class testpunct : public numpunct<_CharT>
{
public:
typedef _CharT char_type;
const char_type dchar;
explicit
testpunct(char_type decimal_char) : numpunct<_CharT>(), dchar(decimal_char)
{ }
protected:
char_type
do_decimal_point() const
{ return dchar; }
char_type
do_thousands_sep() const
{ return ','; }
string
do_grouping() const
{ return string(); }
};
template<typename _CharT>
void apply_formatting(const _TestCase & tc, basic_ostream<_CharT> & os)
{
os.precision(tc.precision);
os.width(tc.width);
os.fill(static_cast<_CharT>(tc.fill));
if (tc.fixed)
os.setf(ios::fixed);
if (tc.scientific)
os.setf(ios::scientific);
if (tc.showpos)
os.setf(ios::showpos);
if (tc.showpoint)
os.setf(ios::showpoint);
if (tc.uppercase)
os.setf(ios::uppercase);
if (tc.internal)
os.setf(ios::internal);
if (tc.left)
os.setf(ios::left);
if (tc.right)
os.setf(ios::right);
}
int
test01()
{
bool test = true;
for (int j=0; j<sizeof(testcases)/sizeof(testcases[0]); j++)
{
_TestCase & tc = testcases[j];
#ifdef TEST_NUMPUT_VERBOSE
cout << "expect: " << tc.result << endl;
#endif
// test double with char type
{
testpunct<char>* __tp = new testpunct<char>(tc.decimal);
ostringstream os;
locale __loc(os.getloc(), __tp);
os.imbue(__loc);
apply_formatting(tc, os);
os << tc.val;
#ifdef TEST_NUMPUT_VERBOSE
cout << "result: " << os.str() << endl;
#endif
VERIFY( os && os.str() == tc.result );
}
// test long double with char type
{
testpunct<char>* __tp = new testpunct<char>(tc.decimal);
ostringstream os;
locale __loc(os.getloc(), __tp);
os.imbue(__loc);
apply_formatting(tc, os);
os << (long double)tc.val;
#ifdef TEST_NUMPUT_VERBOSE
cout << "result: " << os.str() << endl;
#endif
VERIFY( os && os.str() == tc.result );
}
#if _GLIBCPP_USE_WCHAR_T
// test double with wchar_t type
{
testpunct<wchar_t>* __tp = new testpunct<wchar_t>(tc.decimal);
wostringstream os;
locale __loc(os.getloc(), __tp);
os.imbue(__loc);
apply_formatting(tc, os);
os << tc.val;
VERIFY( os && os.str() == tc.wresult );
}
// test long double with wchar_t type
{
testpunct<wchar_t>* __tp = new testpunct<wchar_t>(tc.decimal);
wostringstream os;
locale __loc(os.getloc(), __tp);
os.imbue(__loc);
apply_formatting(tc, os);
os << (long double)tc.val;
VERIFY( os && os.str() == tc.wresult );
}
#endif
}
return 0;
}
int
test02()
{
bool test = true;
// make sure we can output a very long float
long double val = 1.2345678901234567890123456789e+1000L;
int prec = numeric_limits<long double>::digits10;
ostringstream os;
os.precision(prec);
os.setf(ios::scientific);
os << val;
char largebuf[512];
sprintf(largebuf, "%.*Le", prec, val);
#ifdef TEST_NUMPUT_VERBOSE
cout << "expect: " << largebuf << endl;
cout << "result: " << os.str() << endl;
#endif
VERIFY(os && os.str() == largebuf);
return 0;
}
int
test03()
{
short s = -1;
int i = -1;
long l = -1;
bool test = true;
const string str_blank;
string str_tmp;
stringbuf strbuf;
ostream o(&strbuf);
o << oct << s << ' ' << hex << s;
VERIFY( strbuf.str() == "177777 ffff" ); // Assuming 2byte-shorts
strbuf.str(str_blank);
o << oct << i << ' ' << hex << i;
VERIFY( strbuf.str() == "37777777777 ffffffff" );
strbuf.str(str_blank);
o << oct << l << ' ' << hex << l;
VERIFY( strbuf.str() == "37777777777 ffffffff" );
strbuf.str(str_blank);
o << showpos << hex << showbase << 11;
VERIFY( strbuf.str() == "0xb" );
VERIFY(test);
return 0;
}
int
main()
{
test01();
test02();
#ifdef TEST_NUMPUT_VERBOSE
cout << "Test passed!" << endl;
#endif
return 0;
}