ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

istmit_test.cpp
Go to the documentation of this file.
00001 #include <algorithm>
00002 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
00003 #  include <sstream>
00004 #  include <functional>
00005 #  include <iterator>
00006 #  include <vector>
00007 #  include <string>
00008 #endif
00009 
00010 #include "cppunit/cppunit_proxy.h"
00011 
00012 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
00013 using namespace std;
00014 #endif
00015 
00016 //
00017 // TestCase class
00018 //
00019 class IStreamIteratorTest : public CPPUNIT_NS::TestCase
00020 {
00021   CPPUNIT_TEST_SUITE(IStreamIteratorTest);
00022 #if defined (STLPORT) && defined (_STLP_USE_NO_IOSTREAMS)
00023   CPPUNIT_IGNORE;
00024 #endif
00025   CPPUNIT_TEST(istmit1);
00026 #if !defined (STLPORT) || defined (_STLP_NO_EXTENSIONS)
00027   CPPUNIT_IGNORE;
00028 #endif
00029   CPPUNIT_TEST(copy_n_test);
00030   CPPUNIT_TEST_SUITE_END();
00031 
00032 protected:
00033   void istmit1();
00034   void copy_n_test();
00035 };
00036 
00037 CPPUNIT_TEST_SUITE_REGISTRATION(IStreamIteratorTest);
00038 
00039 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
00040 #  if !defined (STLPORT) || !defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
00041 typedef istream_iterator<char> istream_char_ite;
00042 typedef istream_iterator<int> istream_int_ite;
00043 typedef istream_iterator<string> istream_string_ite;
00044 #  else
00045 typedef istream_iterator<char, ptrdiff_t> istream_char_ite;
00046 typedef istream_iterator<int, ptrdiff_t> istream_int_ite;
00047 typedef istream_iterator<string, ptrdiff_t> istream_string_ite;
00048 #  endif
00049 #endif
00050 
00051 //
00052 // tests implementation
00053 //
00054 void IStreamIteratorTest::istmit1()
00055 {
00056 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
00057   const char* buff = "MyString";
00058   istringstream istr(buff);
00059 
00060   char buffer[100];
00061   size_t i = 0;
00062   istr.unsetf(ios::skipws); // Disable white-space skipping.
00063   istream_char_ite s(istr), meos;
00064   while (!(s == meos)  &&
00065   //*TY 01/10/1999 - added end of stream check
00066   // NOTE operator!= should not be used here ifndef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
00067          (*s != '\n') &&
00068          (i < sizeof(buffer) / sizeof(buffer[0]))) {  //*TY 07/28/98 - added index check
00069     buffer[i++] = *s++;
00070   }
00071   buffer[i] = '\0'; // Null terminate buffer.
00072 
00073   CPPUNIT_ASSERT(!strcmp(buffer, buff));
00074 
00075   {
00076     istringstream empty_istr;
00077     CPPUNIT_ASSERT( istream_char_ite(empty_istr) == istream_char_ite() );
00078   }
00079 #endif
00080 }
00081 
00082 void IStreamIteratorTest::copy_n_test()
00083 {
00084 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && !defined (_STLP_USE_NO_IOSTREAMS)
00085   //This test check that no character is lost while reading the istream
00086   //through a istream_iterator.
00087   {
00088     istringstream istr("aabbcd");
00089     string chars;
00090     istream_char_ite ite = copy_n(copy_n(istream_char_ite(istr),
00091                                          2, back_inserter(chars)).first,
00092                                   2, back_inserter(chars)).first;
00093     CPPUNIT_ASSERT( chars == "aabb" );
00094     copy_n(ite, 2, back_inserter(chars));
00095     CPPUNIT_ASSERT( chars == "aabbcd" );
00096   }
00097 
00098   {
00099     istringstream istr("11 22 AA BB 33 44 CC DD");
00100     vector<int> ints;
00101     vector<string> strings;
00102 
00103     copy_n(istream_int_ite(istr), 2, back_inserter(ints));
00104     CPPUNIT_ASSERT( ints.size() == 2 );
00105     CPPUNIT_ASSERT( ints[0] == 11 );
00106     CPPUNIT_ASSERT( ints[1] == 22 );
00107     ints.clear();
00108     istr.clear();
00109     copy_n(istream_string_ite(istr), 2, back_inserter(strings));
00110     CPPUNIT_ASSERT( strings.size() == 2 );
00111     CPPUNIT_ASSERT( strings[0] == "AA" );
00112     CPPUNIT_ASSERT( strings[1] == "BB" );
00113     strings.clear();
00114     istr.clear();
00115     /* The following code cannot work, '33' is extracted as a string
00116      * in the previous copy_n call, this value is returned in the pair
00117      * returned by copy_n but is lost as this istream_iterator is not used.
00118      * copy_n and istream_iterator can only be combined safely if:
00119      * - you always extract the same type of istream_iterator and you always reuse
00120      * the istream_iterator returned by copy_n (see previous test with "aabbcd")
00121      * - you extract different type of object and no object is convertible to an other
00122      * as in this current test when you extract int and string (when you extract ints
00123      * again it fails as int can be converted to strings.
00124      *
00125     copy_n(istream_int_ite(istr), 2, back_inserter(ints));
00126     CPPUNIT_ASSERT( ints.size() == 2 );
00127     CPPUNIT_ASSERT( ints[0] == 33 );
00128     CPPUNIT_ASSERT( ints[1] == 44 );
00129     istr.clear();
00130     copy_n(istream_string_ite(istr), 2, back_inserter(strings));
00131     CPPUNIT_ASSERT( strings.size() == 2 );
00132     CPPUNIT_ASSERT( strings[0] == "CC" );
00133     CPPUNIT_ASSERT( strings[1] == "DD" );
00134     */
00135   }
00136 
00137   {
00138     istringstream is("1 2 3 4 5 6 7 8 9 10");
00139     vector<int> ints;
00140     istream_iterator<int> itr(is);
00141     itr = copy_n(itr, 0, back_inserter(ints)).first;
00142     CPPUNIT_ASSERT( ints.empty() );
00143     itr = copy_n(itr, -1, back_inserter(ints)).first;
00144     CPPUNIT_ASSERT( ints.empty() );
00145     itr = copy_n(itr, 2, back_inserter(ints)).first;
00146     CPPUNIT_ASSERT( ints.size() == 2 );
00147     CPPUNIT_ASSERT( ints[0] == 1 );
00148     CPPUNIT_ASSERT( ints[1] == 2 );
00149     itr = copy_n(itr, 2, back_inserter(ints)).first;
00150     CPPUNIT_ASSERT( ints.size() == 4 );
00151     CPPUNIT_ASSERT( ints[2] == 3 );
00152     CPPUNIT_ASSERT( ints[3] == 4 );
00153     itr = copy_n(itr, 2, back_inserter(ints)).first;
00154     CPPUNIT_ASSERT( ints.size() == 6 );
00155     CPPUNIT_ASSERT( ints[4] == 5 );
00156     CPPUNIT_ASSERT( ints[5] == 6 );
00157   }
00158 #endif
00159 }

Generated on Sun May 27 2012 04:35:28 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.