os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/sstream_test.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
#include <string>
sl@0
     2
#include "math_aux.h"
sl@0
     3
sl@0
     4
#if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
sl@0
     5
#  include <sstream>
sl@0
     6
#  include <memory>
sl@0
     7
sl@0
     8
#  include "full_streambuf.h"
sl@0
     9
sl@0
    10
#  include "cppunit/cppunit_proxy.h"
sl@0
    11
sl@0
    12
#  if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
sl@0
    13
using namespace std;
sl@0
    14
#  endif
sl@0
    15
sl@0
    16
//
sl@0
    17
// TestCase class
sl@0
    18
//
sl@0
    19
class SstreamTest : public CPPUNIT_NS::TestCase
sl@0
    20
{
sl@0
    21
  CPPUNIT_TEST_SUITE(SstreamTest);
sl@0
    22
  CPPUNIT_TEST(output);
sl@0
    23
  CPPUNIT_TEST(input);
sl@0
    24
  CPPUNIT_TEST(input_char);
sl@0
    25
  CPPUNIT_TEST(io);
sl@0
    26
  CPPUNIT_TEST(err);
sl@0
    27
  CPPUNIT_TEST(err_long);
sl@0
    28
  CPPUNIT_TEST(maxint);
sl@0
    29
  CPPUNIT_TEST(init_in);
sl@0
    30
  CPPUNIT_TEST(init_out);
sl@0
    31
  CPPUNIT_TEST(buf);
sl@0
    32
  CPPUNIT_TEST(rdbuf);
sl@0
    33
  CPPUNIT_TEST(streambuf_output);
sl@0
    34
  CPPUNIT_TEST(seek);
sl@0
    35
  CPPUNIT_TEST(negative);
sl@0
    36
  CPPUNIT_TEST_SUITE_END();
sl@0
    37
sl@0
    38
  protected:
sl@0
    39
    void output();
sl@0
    40
    void input();
sl@0
    41
    void input_char();
sl@0
    42
    void io();
sl@0
    43
    void err();
sl@0
    44
    void err_long();
sl@0
    45
    void maxint();
sl@0
    46
    void init_in();
sl@0
    47
    void init_out();
sl@0
    48
    void buf();
sl@0
    49
    void rdbuf();
sl@0
    50
    void streambuf_output();
sl@0
    51
    void seek();
sl@0
    52
    void negative();
sl@0
    53
};
sl@0
    54
sl@0
    55
CPPUNIT_TEST_SUITE_REGISTRATION(SstreamTest);
sl@0
    56
sl@0
    57
//
sl@0
    58
// tests implementation
sl@0
    59
//
sl@0
    60
void SstreamTest::output()
sl@0
    61
{
sl@0
    62
  {
sl@0
    63
    ostringstream s;
sl@0
    64
sl@0
    65
    s << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef";
sl@0
    66
    CPPUNIT_ASSERT( s.good() );
sl@0
    67
    CPPUNIT_ASSERT( s.str() == "1\n2\nabcd\nghk lm\nabcd ef" );
sl@0
    68
  }
sl@0
    69
sl@0
    70
  //Following tests are mostly used to reveal problem with the MSVC /Wp64 option
sl@0
    71
  //used to track 64 bits portability issue:
sl@0
    72
  {
sl@0
    73
    ostringstream s;
sl@0
    74
    size_t i = 0;
sl@0
    75
    s << i;
sl@0
    76
    CPPUNIT_ASSERT( s.good() );
sl@0
    77
    CPPUNIT_ASSERT( s.str() == "0" );
sl@0
    78
  }
sl@0
    79
  {
sl@0
    80
    ostringstream s;
sl@0
    81
    ptrdiff_t i = 0;
sl@0
    82
    s << i;
sl@0
    83
    CPPUNIT_ASSERT( s.good() );
sl@0
    84
    CPPUNIT_ASSERT( s.str() == "0" );
sl@0
    85
  }
sl@0
    86
}
sl@0
    87
sl@0
    88
void SstreamTest::input()
sl@0
    89
{
sl@0
    90
  {
sl@0
    91
    istringstream s( "1\n2\nabcd\nghk lm\nabcd ef" );
sl@0
    92
    int i = 0;
sl@0
    93
    s >> i;
sl@0
    94
    CPPUNIT_ASSERT( s.good() );
sl@0
    95
    CPPUNIT_ASSERT( i == 1 );
sl@0
    96
    double d = 0.0;
sl@0
    97
    s >> d;
sl@0
    98
    CPPUNIT_ASSERT( s.good() );
sl@0
    99
    CPPUNIT_ASSERT( d == 2.0 );
sl@0
   100
    string str;
sl@0
   101
    s >> str;
sl@0
   102
    CPPUNIT_ASSERT( s.good() );
sl@0
   103
    CPPUNIT_ASSERT( str == "abcd" );
sl@0
   104
    char c;
sl@0
   105
    s.get(c); // extract newline, that not extracted by operator >>
sl@0
   106
    CPPUNIT_ASSERT( s.good() );
sl@0
   107
    CPPUNIT_ASSERT( c == '\n' );
sl@0
   108
    getline( s, str );
sl@0
   109
    CPPUNIT_ASSERT( s.good() );
sl@0
   110
    CPPUNIT_ASSERT( str == "ghk lm" );
sl@0
   111
    getline( s, str );
sl@0
   112
    CPPUNIT_ASSERT( s.eof() );
sl@0
   113
    CPPUNIT_ASSERT( str == "abcd ef" );
sl@0
   114
  }
sl@0
   115
  {
sl@0
   116
    istringstream s("0");
sl@0
   117
    size_t i = 1;
sl@0
   118
    s >> i;
sl@0
   119
    CPPUNIT_ASSERT( !s.fail() );
sl@0
   120
    CPPUNIT_ASSERT( s.eof() );
sl@0
   121
    CPPUNIT_ASSERT( i == 0 );
sl@0
   122
  }
sl@0
   123
}
sl@0
   124
sl@0
   125
void SstreamTest::input_char()
sl@0
   126
{
sl@0
   127
  char buf[16] = { 0, '1', '2', '3' };
sl@0
   128
  istringstream s( "0" );
sl@0
   129
  s >> buf;
sl@0
   130
sl@0
   131
  CPPUNIT_ASSERT( buf[0] == '0' );
sl@0
   132
  CPPUNIT_ASSERT( buf[1] == 0 );
sl@0
   133
  CPPUNIT_ASSERT( buf[2] == '2' );
sl@0
   134
}
sl@0
   135
sl@0
   136
sl@0
   137
void SstreamTest::io()
sl@0
   138
{
sl@0
   139
  stringstream s;
sl@0
   140
  s << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef";
sl@0
   141
  CPPUNIT_ASSERT( s.good() );
sl@0
   142
sl@0
   143
  int i = 0;
sl@0
   144
  s >> i;
sl@0
   145
  CPPUNIT_ASSERT( i == 1 );
sl@0
   146
  CPPUNIT_ASSERT( s.good() );
sl@0
   147
  double d = 0.0;
sl@0
   148
  s >> d;
sl@0
   149
  CPPUNIT_ASSERT( d == 2.0 );
sl@0
   150
  CPPUNIT_ASSERT( s.good() );
sl@0
   151
  string str;
sl@0
   152
  s >> str;
sl@0
   153
  CPPUNIT_ASSERT( str == "abcd" );
sl@0
   154
  CPPUNIT_ASSERT( s.good() );
sl@0
   155
  char c;
sl@0
   156
  s.get(c); // extract newline, that not extracted by operator >>
sl@0
   157
  CPPUNIT_ASSERT( s.good() );
sl@0
   158
  CPPUNIT_ASSERT( c == '\n' );
sl@0
   159
  getline( s, str );
sl@0
   160
  CPPUNIT_ASSERT( s.good() );
sl@0
   161
  CPPUNIT_ASSERT( str == "ghk lm" );
sl@0
   162
  getline( s, str );
sl@0
   163
  CPPUNIT_ASSERT( str == "abcd ef" );
sl@0
   164
  CPPUNIT_ASSERT( s.eof() );
sl@0
   165
}
sl@0
   166
sl@0
   167
void SstreamTest::err()
sl@0
   168
{
sl@0
   169
  stringstream s( "9" );
sl@0
   170
sl@0
   171
  int i = 0;
sl@0
   172
  s >> i;
sl@0
   173
  CPPUNIT_ASSERT( !s.fail() );
sl@0
   174
  CPPUNIT_ASSERT( i == 9 );
sl@0
   175
  s >> i;
sl@0
   176
  CPPUNIT_ASSERT( s.fail() );
sl@0
   177
  CPPUNIT_ASSERT( s.eof() );
sl@0
   178
  CPPUNIT_ASSERT( i == 9 );
sl@0
   179
}
sl@0
   180
sl@0
   181
void SstreamTest::err_long()
sl@0
   182
{
sl@0
   183
  stringstream s( "9" );
sl@0
   184
sl@0
   185
  long i = 0;
sl@0
   186
  s >> i;
sl@0
   187
  CPPUNIT_ASSERT( !s.fail() );
sl@0
   188
  CPPUNIT_ASSERT( i == 9 );
sl@0
   189
  s >> i;
sl@0
   190
  CPPUNIT_ASSERT( s.fail() );
sl@0
   191
  CPPUNIT_ASSERT( s.eof() );
sl@0
   192
  CPPUNIT_ASSERT( i == 9 );
sl@0
   193
}
sl@0
   194
sl@0
   195
void SstreamTest::maxint()
sl@0
   196
{
sl@0
   197
  stringstream s;
sl@0
   198
sl@0
   199
  s << INT_MAX << " " << UINT_MAX << " " << LONG_MAX << " " << ULONG_MAX << " "
sl@0
   200
    << INT_MIN << " " << LONG_MIN;
sl@0
   201
  CPPUNIT_ASSERT( s.good() );
sl@0
   202
sl@0
   203
  int i = 0;
sl@0
   204
  unsigned int u = 0;
sl@0
   205
  long l = 0;
sl@0
   206
  unsigned long ul = 0;
sl@0
   207
sl@0
   208
  s >> i >> u >> l >> ul;
sl@0
   209
  CPPUNIT_ASSERT( s.good() );
sl@0
   210
  CPPUNIT_ASSERT( i == INT_MAX );
sl@0
   211
  CPPUNIT_ASSERT( u == UINT_MAX );
sl@0
   212
  CPPUNIT_ASSERT( l == LONG_MAX );
sl@0
   213
  CPPUNIT_ASSERT( ul == ULONG_MAX );
sl@0
   214
sl@0
   215
  s >> i >> l;
sl@0
   216
  CPPUNIT_ASSERT( !s.fail() );
sl@0
   217
  CPPUNIT_ASSERT( i == INT_MIN );
sl@0
   218
  CPPUNIT_ASSERT( l == LONG_MIN );
sl@0
   219
}
sl@0
   220
sl@0
   221
void SstreamTest::init_in()
sl@0
   222
{
sl@0
   223
  istringstream is( "12345" );
sl@0
   224
  int n;
sl@0
   225
sl@0
   226
  is >> n;
sl@0
   227
  CPPUNIT_ASSERT( !is.fail() );
sl@0
   228
  CPPUNIT_ASSERT( n == 12345 );
sl@0
   229
sl@0
   230
  istringstream dis( "1.2345" );
sl@0
   231
  double d;
sl@0
   232
sl@0
   233
  dis >> d;
sl@0
   234
  CPPUNIT_ASSERT( !dis.fail() );
sl@0
   235
  CPPUNIT_ASSERT( are_equals(d, 1.2345) );
sl@0
   236
sl@0
   237
  istringstream fis( "1.2345" );
sl@0
   238
  float f;
sl@0
   239
sl@0
   240
  fis >> f;
sl@0
   241
  CPPUNIT_ASSERT( !fis.fail() );
sl@0
   242
  CPPUNIT_ASSERT( are_equals(f, 1.2345f) );
sl@0
   243
}
sl@0
   244
sl@0
   245
void SstreamTest::init_out()
sl@0
   246
{
sl@0
   247
  ostringstream os( "12345" );
sl@0
   248
  CPPUNIT_ASSERT( os.str() == "12345" );
sl@0
   249
sl@0
   250
  os << 67;
sl@0
   251
  CPPUNIT_ASSERT( os.good() );
sl@0
   252
sl@0
   253
  // This satisfy to the Standard:
sl@0
   254
  CPPUNIT_ASSERT( os.str() == "67345" );
sl@0
   255
  // But we don't know the reason, why standard state that.
sl@0
   256
sl@0
   257
  os.str( "89ab" );
sl@0
   258
  CPPUNIT_ASSERT( os.str() == "89ab" );
sl@0
   259
sl@0
   260
  os << 10;
sl@0
   261
  CPPUNIT_ASSERT( os.good() );
sl@0
   262
  CPPUNIT_ASSERT( os.str() == "10ab" );
sl@0
   263
}
sl@0
   264
sl@0
   265
void SstreamTest::buf()
sl@0
   266
{
sl@0
   267
  stringstream ss;
sl@0
   268
sl@0
   269
  ss << "1234567\n89\n";
sl@0
   270
  char buf[10];
sl@0
   271
  buf[7] = 'x';
sl@0
   272
  ss.get( buf, 10 );
sl@0
   273
  CPPUNIT_ASSERT( !ss.fail() );
sl@0
   274
  CPPUNIT_ASSERT( buf[0] == '1' );
sl@0
   275
  CPPUNIT_ASSERT( buf[1] == '2' );
sl@0
   276
  CPPUNIT_ASSERT( buf[2] == '3' );
sl@0
   277
  CPPUNIT_ASSERT( buf[3] == '4' );
sl@0
   278
  CPPUNIT_ASSERT( buf[4] == '5' );
sl@0
   279
  CPPUNIT_ASSERT( buf[5] == '6' );
sl@0
   280
  CPPUNIT_ASSERT( buf[6] == '7' ); // 27.6.1.3 paragraph 10, paragraph 7
sl@0
   281
  CPPUNIT_ASSERT( buf[7] == 0 ); // 27.6.1.3 paragraph 8
sl@0
   282
  char c;
sl@0
   283
  ss.get(c);
sl@0
   284
  CPPUNIT_ASSERT( !ss.fail() );
sl@0
   285
  CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 10, paragraph 7
sl@0
   286
  ss.get(c);
sl@0
   287
  CPPUNIT_ASSERT( !ss.fail() );
sl@0
   288
  CPPUNIT_ASSERT( c == '8' );
sl@0
   289
}
sl@0
   290
sl@0
   291
void SstreamTest::rdbuf()
sl@0
   292
{
sl@0
   293
  stringstream ss;
sl@0
   294
sl@0
   295
  ss << "1234567\n89\n";
sl@0
   296
sl@0
   297
  ostringstream os;
sl@0
   298
  ss.get( *os.rdbuf(), '\n' );
sl@0
   299
  CPPUNIT_ASSERT( !ss.fail() );
sl@0
   300
  char c;
sl@0
   301
  ss.get(c);
sl@0
   302
  CPPUNIT_ASSERT( !ss.fail() );
sl@0
   303
  CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 12
sl@0
   304
  CPPUNIT_ASSERT( os.str() == "1234567" );
sl@0
   305
}
sl@0
   306
sl@0
   307
void SstreamTest::streambuf_output()
sl@0
   308
{
sl@0
   309
  {
sl@0
   310
    istringstream in("01234567890123456789");
sl@0
   311
    CPPUNIT_ASSERT( in );
sl@0
   312
sl@0
   313
    auto_ptr<full_streambuf> pfull_buf(new full_streambuf(10));
sl@0
   314
    ostream out(pfull_buf.get());
sl@0
   315
    CPPUNIT_ASSERT( out );
sl@0
   316
sl@0
   317
    out << in.rdbuf();
sl@0
   318
    CPPUNIT_ASSERT( out );
sl@0
   319
    CPPUNIT_ASSERT( in );
sl@0
   320
    //out is good we can check what has been extracted:
sl@0
   321
    CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" );
sl@0
   322
sl@0
   323
    out << in.rdbuf();
sl@0
   324
    CPPUNIT_ASSERT( out.fail() );
sl@0
   325
    CPPUNIT_ASSERT( in );
sl@0
   326
sl@0
   327
    ostringstream ostr;
sl@0
   328
    ostr << in.rdbuf();
sl@0
   329
    CPPUNIT_ASSERT( ostr );
sl@0
   330
    CPPUNIT_ASSERT( in );
sl@0
   331
    CPPUNIT_ASSERT( ostr.str() == "0123456789" );
sl@0
   332
  }
sl@0
   333
sl@0
   334
#  if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
sl@0
   335
  {
sl@0
   336
    //If the output stream buffer throws:
sl@0
   337
    istringstream in("01234567890123456789");
sl@0
   338
    CPPUNIT_ASSERT( in );
sl@0
   339
sl@0
   340
    auto_ptr<full_streambuf> pfull_buf(new full_streambuf(10, true));
sl@0
   341
    ostream out(pfull_buf.get());
sl@0
   342
    CPPUNIT_ASSERT( out );
sl@0
   343
sl@0
   344
    out << in.rdbuf();
sl@0
   345
    CPPUNIT_ASSERT( out.bad() );
sl@0
   346
    CPPUNIT_ASSERT( in );
sl@0
   347
    //out is bad we have no guaranty on what has been extracted:
sl@0
   348
    //CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" );
sl@0
   349
sl@0
   350
    out.clear();
sl@0
   351
    out << in.rdbuf();
sl@0
   352
    CPPUNIT_ASSERT( out.fail() && out.bad() );
sl@0
   353
    CPPUNIT_ASSERT( in );
sl@0
   354
sl@0
   355
    ostringstream ostr;
sl@0
   356
    ostr << in.rdbuf();
sl@0
   357
    CPPUNIT_ASSERT( ostr );
sl@0
   358
    CPPUNIT_ASSERT( in );
sl@0
   359
    CPPUNIT_ASSERT( ostr.str() == "01234567890123456789" );
sl@0
   360
  }
sl@0
   361
#  endif
sl@0
   362
}
sl@0
   363
sl@0
   364
void SstreamTest::seek()
sl@0
   365
{
sl@0
   366
  stringstream s( "0123456789" );
sl@0
   367
sl@0
   368
  CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(0) );
sl@0
   369
  s.seekg( 6, ios::beg );
sl@0
   370
  CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(6) );
sl@0
   371
  s.seekg( -3, ios::cur );
sl@0
   372
  CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(3) );
sl@0
   373
sl@0
   374
  istringstream is( "0123456789" );
sl@0
   375
  CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(0) );
sl@0
   376
  is.seekg( 6, ios::beg );
sl@0
   377
  CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(6) );
sl@0
   378
  is.seekg( -3, ios::cur );
sl@0
   379
  CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(3) );
sl@0
   380
}
sl@0
   381
sl@0
   382
sl@0
   383
template < class T >
sl@0
   384
string to_string( const T& v )
sl@0
   385
{
sl@0
   386
  ostringstream oss;
sl@0
   387
  oss << v;
sl@0
   388
  return oss.str();
sl@0
   389
}
sl@0
   390
sl@0
   391
void SstreamTest::negative()
sl@0
   392
{
sl@0
   393
  CPPUNIT_CHECK( to_string<int>(-1) == "-1" );
sl@0
   394
  CPPUNIT_CHECK( to_string<long>(-1) == "-1" );
sl@0
   395
}
sl@0
   396
sl@0
   397
#endif