os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/istmit_test.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/istmit_test.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,159 @@
     1.4 +#include <algorithm>
     1.5 +#if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
     1.6 +#  include <sstream>
     1.7 +#  include <functional>
     1.8 +#  include <iterator>
     1.9 +#  include <vector>
    1.10 +#  include <string>
    1.11 +#endif
    1.12 +
    1.13 +#include "cppunit/cppunit_proxy.h"
    1.14 +
    1.15 +#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
    1.16 +using namespace std;
    1.17 +#endif
    1.18 +
    1.19 +//
    1.20 +// TestCase class
    1.21 +//
    1.22 +class IStreamIteratorTest : public CPPUNIT_NS::TestCase
    1.23 +{
    1.24 +  CPPUNIT_TEST_SUITE(IStreamIteratorTest);
    1.25 +#if defined (STLPORT) && defined (_STLP_USE_NO_IOSTREAMS)
    1.26 +  CPPUNIT_IGNORE;
    1.27 +#endif
    1.28 +  CPPUNIT_TEST(istmit1);
    1.29 +#if !defined (STLPORT) || defined (_STLP_NO_EXTENSIONS)
    1.30 +  CPPUNIT_IGNORE;
    1.31 +#endif
    1.32 +  CPPUNIT_TEST(copy_n_test);
    1.33 +  CPPUNIT_TEST_SUITE_END();
    1.34 +
    1.35 +protected:
    1.36 +  void istmit1();
    1.37 +  void copy_n_test();
    1.38 +};
    1.39 +
    1.40 +CPPUNIT_TEST_SUITE_REGISTRATION(IStreamIteratorTest);
    1.41 +
    1.42 +#if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
    1.43 +#  if !defined (STLPORT) || !defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
    1.44 +typedef istream_iterator<char> istream_char_ite;
    1.45 +typedef istream_iterator<int> istream_int_ite;
    1.46 +typedef istream_iterator<string> istream_string_ite;
    1.47 +#  else
    1.48 +typedef istream_iterator<char, ptrdiff_t> istream_char_ite;
    1.49 +typedef istream_iterator<int, ptrdiff_t> istream_int_ite;
    1.50 +typedef istream_iterator<string, ptrdiff_t> istream_string_ite;
    1.51 +#  endif
    1.52 +#endif
    1.53 +
    1.54 +//
    1.55 +// tests implementation
    1.56 +//
    1.57 +void IStreamIteratorTest::istmit1()
    1.58 +{
    1.59 +#if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
    1.60 +  const char* buff = "MyString";
    1.61 +  istringstream istr(buff);
    1.62 +
    1.63 +  char buffer[100];
    1.64 +  size_t i = 0;
    1.65 +  istr.unsetf(ios::skipws); // Disable white-space skipping.
    1.66 +  istream_char_ite s(istr), meos;
    1.67 +  while (!(s == meos)  &&
    1.68 +  //*TY 01/10/1999 - added end of stream check
    1.69 +  // NOTE operator!= should not be used here ifndef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
    1.70 +         (*s != '\n') &&
    1.71 +         (i < sizeof(buffer) / sizeof(buffer[0]))) {  //*TY 07/28/98 - added index check
    1.72 +    buffer[i++] = *s++;
    1.73 +  }
    1.74 +  buffer[i] = '\0'; // Null terminate buffer.
    1.75 +
    1.76 +  CPPUNIT_ASSERT(!strcmp(buffer, buff));
    1.77 +
    1.78 +  {
    1.79 +    istringstream empty_istr;
    1.80 +    CPPUNIT_ASSERT( istream_char_ite(empty_istr) == istream_char_ite() );
    1.81 +  }
    1.82 +#endif
    1.83 +}
    1.84 +
    1.85 +void IStreamIteratorTest::copy_n_test()
    1.86 +{
    1.87 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && !defined (_STLP_USE_NO_IOSTREAMS)
    1.88 +  //This test check that no character is lost while reading the istream
    1.89 +  //through a istream_iterator.
    1.90 +  {
    1.91 +    istringstream istr("aabbcd");
    1.92 +    string chars;
    1.93 +    istream_char_ite ite = copy_n(copy_n(istream_char_ite(istr),
    1.94 +                                         2, back_inserter(chars)).first,
    1.95 +                                  2, back_inserter(chars)).first;
    1.96 +    CPPUNIT_ASSERT( chars == "aabb" );
    1.97 +    copy_n(ite, 2, back_inserter(chars));
    1.98 +    CPPUNIT_ASSERT( chars == "aabbcd" );
    1.99 +  }
   1.100 +
   1.101 +  {
   1.102 +    istringstream istr("11 22 AA BB 33 44 CC DD");
   1.103 +    vector<int> ints;
   1.104 +    vector<string> strings;
   1.105 +
   1.106 +    copy_n(istream_int_ite(istr), 2, back_inserter(ints));
   1.107 +    CPPUNIT_ASSERT( ints.size() == 2 );
   1.108 +    CPPUNIT_ASSERT( ints[0] == 11 );
   1.109 +    CPPUNIT_ASSERT( ints[1] == 22 );
   1.110 +    ints.clear();
   1.111 +    istr.clear();
   1.112 +    copy_n(istream_string_ite(istr), 2, back_inserter(strings));
   1.113 +    CPPUNIT_ASSERT( strings.size() == 2 );
   1.114 +    CPPUNIT_ASSERT( strings[0] == "AA" );
   1.115 +    CPPUNIT_ASSERT( strings[1] == "BB" );
   1.116 +    strings.clear();
   1.117 +    istr.clear();
   1.118 +    /* The following code cannot work, '33' is extracted as a string
   1.119 +     * in the previous copy_n call, this value is returned in the pair
   1.120 +     * returned by copy_n but is lost as this istream_iterator is not used.
   1.121 +     * copy_n and istream_iterator can only be combined safely if:
   1.122 +     * - you always extract the same type of istream_iterator and you always reuse
   1.123 +     * the istream_iterator returned by copy_n (see previous test with "aabbcd")
   1.124 +     * - you extract different type of object and no object is convertible to an other
   1.125 +     * as in this current test when you extract int and string (when you extract ints
   1.126 +     * again it fails as int can be converted to strings.
   1.127 +     *
   1.128 +    copy_n(istream_int_ite(istr), 2, back_inserter(ints));
   1.129 +    CPPUNIT_ASSERT( ints.size() == 2 );
   1.130 +    CPPUNIT_ASSERT( ints[0] == 33 );
   1.131 +    CPPUNIT_ASSERT( ints[1] == 44 );
   1.132 +    istr.clear();
   1.133 +    copy_n(istream_string_ite(istr), 2, back_inserter(strings));
   1.134 +    CPPUNIT_ASSERT( strings.size() == 2 );
   1.135 +    CPPUNIT_ASSERT( strings[0] == "CC" );
   1.136 +    CPPUNIT_ASSERT( strings[1] == "DD" );
   1.137 +    */
   1.138 +  }
   1.139 +
   1.140 +  {
   1.141 +    istringstream is("1 2 3 4 5 6 7 8 9 10");
   1.142 +    vector<int> ints;
   1.143 +    istream_iterator<int> itr(is);
   1.144 +    itr = copy_n(itr, 0, back_inserter(ints)).first;
   1.145 +    CPPUNIT_ASSERT( ints.empty() );
   1.146 +    itr = copy_n(itr, -1, back_inserter(ints)).first;
   1.147 +    CPPUNIT_ASSERT( ints.empty() );
   1.148 +    itr = copy_n(itr, 2, back_inserter(ints)).first;
   1.149 +    CPPUNIT_ASSERT( ints.size() == 2 );
   1.150 +    CPPUNIT_ASSERT( ints[0] == 1 );
   1.151 +    CPPUNIT_ASSERT( ints[1] == 2 );
   1.152 +    itr = copy_n(itr, 2, back_inserter(ints)).first;
   1.153 +    CPPUNIT_ASSERT( ints.size() == 4 );
   1.154 +    CPPUNIT_ASSERT( ints[2] == 3 );
   1.155 +    CPPUNIT_ASSERT( ints[3] == 4 );
   1.156 +    itr = copy_n(itr, 2, back_inserter(ints)).first;
   1.157 +    CPPUNIT_ASSERT( ints.size() == 6 );
   1.158 +    CPPUNIT_ASSERT( ints[4] == 5 );
   1.159 +    CPPUNIT_ASSERT( ints[5] == 6 );
   1.160 +  }
   1.161 +#endif
   1.162 +}