Update contrib.
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
23 #include <StifParser.h>
24 #include <Stiftestinterface.h>
37 #include <stl/char_traits.h>
43 #include "tiostreams.h"
45 #define STDCPP_OOM FALSE// TRUE for OOM testing
48 // ============================ MEMBER FUNCTIONS ===============================
50 // -----------------------------------------------------------------------------
51 // Ctiostreams::Delete
52 // Delete here all resources allocated and opened from test methods.
53 // Called from destructor.
54 // -----------------------------------------------------------------------------
56 void Ctiostreams::Delete()
61 // -----------------------------------------------------------------------------
62 // Ctiostreams::RunMethodL
63 // Run specified method. Contains also table of test mothods and their names.
64 // -----------------------------------------------------------------------------
66 TInt Ctiostreams::RunMethodL(
67 CStifItemParser& aItem )
70 static TStifFunctionInfo const KFunctions[] =
72 // Copy this line for every implemented function.
73 // First string is the function name used in TestScripter script file.
74 // Second is the actual implementation member function.
75 // Second is the actual implementation member function.
76 ENTRY( "iofstreamL", Ctiostreams::iofstreamL ),
77 ENTRY( "stringbufL", Ctiostreams::stringbufL ),
78 ENTRY( "stringstreamL", Ctiostreams::stringstreamL ),
79 ENTRY( "streambufL", Ctiostreams:: streambufL ),
80 ENTRY( "ostreamL", Ctiostreams:: ostreamL ),
81 ENTRY( "istreamL", Ctiostreams:: istreamL ),
82 ENTRY( "istringstreamL", Ctiostreams:: istringstreamL ),
83 ENTRY( "ostringstreamL", Ctiostreams:: ostringstreamL ),
84 ENTRY( "ostreamiterators", Ctiostreams::ostreamiterators ),
85 ENTRY( "fstreamL", Ctiostreams::fstreamL),
86 ENTRY( "istrstreamL", Ctiostreams::istrstreamL),
87 ENTRY( "strstreamL", Ctiostreams::strstreamL),
88 ENTRY( "ostrstreamL", Ctiostreams::ostrstreamL),
89 ENTRY( "istreamiterators", Ctiostreams::istreamiterators ),
90 ENTRY( "istreambufiterators", Ctiostreams::istreambufiterators ),
91 ENTRY( "strstreambufL", Ctiostreams::strstreambufL ),
92 ENTRY( "freezeL", Ctiostreams::freezeL ),
93 ENTRY( "fposL", Ctiostreams::fposL ),
94 ENTRY( "filebufL", Ctiostreams::filebufL ),
95 ENTRY( "seekpL", Ctiostreams::seekpL ),
100 const TInt count = sizeof( KFunctions ) /
101 sizeof( TStifFunctionInfo );
103 return RunInternalL( KFunctions, count, aItem );
108 // -----------------------------------------------------------------------------
109 // Ctiostreams:: ofstream,ifstream
110 // Example test method function.
111 // (other items were commented in a header).
112 // -----------------------------------------------------------------------------
116 TInt Ctiostreams::iofstreamL( CStifItemParser& aItem )
126 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
133 myfile.open ("c:\\TestFramework\\docs\\example.txt");
134 if(!myfile.is_open())
137 myfile << "Writing this to a file.";
150 myfile1.open("c:\\TestFramework\\docs\\example.txt" );
152 fbuf=myfile1.rdbuf();
155 // get file size using buffer's members
156 size=fbuf->pubseekoff (0,ios::end,ios::in);
157 fbuf->pubseekpos (0,ios::in);
159 // allocate memory to contain file data
160 buffer=new char[size];
163 fbuf->sgetn (buffer,size);
166 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
170 if(buffer != "Writing this to a file.");
178 if(!myfile1.is_open())
181 if(myfile1.is_open())
185 ios_base::Init(); // 0 - 218 FUNCTION Init() iostream.cpp
186 filebuf* _Stl_create_filebuf(FILE* f, ios_base::openmode mode ); // 0 - 225 FUNCTION _Stl_create_filebuf() iostream.cpp
187 ios_base::sync_with_stdio();//0 - 445 FUNCTION ios_base::sync_with_stdio()
222 // -----------------------------------------------------------------------------
223 // Ctiostreams:: stringbuf
224 // Example test method function.
225 // (other items were commented in a header).
226 // -----------------------------------------------------------------------------
229 TInt Ctiostreams::stringbufL(CStifItemParser& aItem )
237 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
242 sb.sputn("Sample string",13);
247 if(sb.in_avail()!=13)
251 char ch = sb.sgetc();
260 if(mystr.compare("Sample string") != 0)
264 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
295 // -----------------------------------------------------------------------------
296 // Ctiostreams:: stringstream
297 // Example test method function.
298 // (other items were commented in a header).
299 // -----------------------------------------------------------------------------
303 TInt Ctiostreams::stringstreamL(CStifItemParser& aItem )
313 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
315 stringstream ss (stringstream::in | stringstream::out);
316 stringstream teststring;
319 teststring<<"stringstream testcase";
321 teststr = teststring.str();
323 if(!teststr.compare("stringstream testcase"))
328 ss << "120 42 377 6 5 2000";
330 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
332 for (int n=0; n<6; n++)
400 // -----------------------------------------------------------------------------
401 // Ctiostreams:: streambuf
402 // Example test method function.
403 // (other items were commented in a header).
404 // -----------------------------------------------------------------------------
407 TInt Ctiostreams::streambufL(CStifItemParser& aItem )
409 // locale loc ("en_GB.UTF-8");
423 char sentence[]= "Sample sentence";
426 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
429 ofstream ostr ("c:\\TestFramework\\docs\\streambuf.txt");
433 if(!cout.rdbuf( )->getloc( ).name( ).c_str( )) //failing
446 pbuf->sputn (sentence,sizeof(sentence)-1);
449 ostr.open("c:\\TestFramework\\docs\\streambuf.txt");
455 long size1 = pbuf->pubseekoff(0,ios_base::end);
463 long size2 = pbuf->pubseekoff(0,ios_base::end);
467 ifstream istr("c:\\TestFramework\\docs\\streambuf.txt");
470 streamsize i = istr.rdbuf()->sgetn(&a[0], 3);
472 // Display the size and contents of the buffer passed to sgetn.
477 int k = pbuf->snextc();
481 while (pbuf->sgetc()!=EOF)
486 ch[i] = pbuf->sbumpc();
501 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
534 // -----------------------------------------------------------------------------
535 // Ctiostreams:: ostream
536 // Example test method function.
537 // (other items were commented in a header).
538 // -----------------------------------------------------------------------------
541 TInt Ctiostreams::ostreamL(CStifItemParser& aItem )
552 char input[17] = "ostream testcase";
554 fb.open ("c:\\TestFramework\\docs\\ostream.txt",ios::out);
557 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
561 os.write(input,size);
562 streamoff i = os.tellp();
567 streamoff j = os.tellp();
573 streamoff k = os.tellp();
581 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
613 // -----------------------------------------------------------------------------
614 // Ctiostreams:: istream
615 // Example test method function.
616 // (other items were commented in a header).
617 // -----------------------------------------------------------------------------
619 TInt Ctiostreams::istreamL(CStifItemParser& aItem )
631 fb.open ("c:\\TestFramework\\docs\\istream.txt",ios::in);
634 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
641 // get length of file:
642 is.seekg (0, ios::end);
644 is.seekg (0, ios::beg);
669 char pk1 = is.peek();
677 char pk2 = is.peek();
684 is.getline(getl,8,'\0');
686 if(getl == "Khaheen")
703 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
727 // -----------------------------------------------------------------------------
728 // Ctiostreams:: istringstream
729 // Example test method function.
730 // (other items were commented in a header).
731 // -----------------------------------------------------------------------------
734 TInt Ctiostreams::istringstreamL(CStifItemParser& aItem )
740 string strvalues = "125 320 512 750 333";
743 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
746 istringstream iss (strvalues,istringstream::in);
748 if(iss.str() != "125 320 512 750 333")
752 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
820 // -----------------------------------------------------------------------------
821 // Ctiostreams:: ostringstream,
822 // Example test method function.
823 // (other items were commented in a header).
824 // -----------------------------------------------------------------------------
827 TInt Ctiostreams::ostringstreamL(CStifItemParser& aItem )
834 basic_string<char> i( "test" );
837 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
841 ss.rdbuf( )->str( i );
842 if(ss.str( ).compare("test") != 0)
846 if(ss.str( ) .compare("zest")!=0)
849 ss.rdbuf( )->str( "be" );
850 if(ss.str( ).compare("be")!=0)
854 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
887 TInt Ctiostreams::ostreamiterators(CStifItemParser& aItem )
892 //____________________
894 int arr[4] = { 3,4,7,8 };
896 deque<int> d(arr+0, arr+4);
898 // stream the whole vector and a sum to cout
901 copy(d.begin(),(d.end()-1),ostream_iterator<int,char>(cout,""));
904 if( *(d.end()-1) == 8)
913 // -----------------------------------------------------------------------------
914 // Ctiostreams:: fstream
915 // Example test method function.
916 // (other items were commented in a header).
917 // -----------------------------------------------------------------------------
921 TInt Ctiostreams::fstreamL(CStifItemParser& aItem )
929 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
931 fstream filestr ("c:\\TestFramework\\docs\\fstream.txt", fstream::in | fstream::out);
933 if(!filestr.is_open())
937 if(filestr.is_open())
947 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
970 // -----------------------------------------------------------------------------
971 // Ctiostreams:: istrstream
972 // Example test method function.
973 // (other items were commented in a header).
974 // -----------------------------------------------------------------------------
976 TInt Ctiostreams::istrstreamL(CStifItemParser& aItem )
984 char* p = "This is first string";
985 char* q = "This is second string";
987 const char* s ="const char";
991 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
994 istrstream second(q);
999 istrstream five(p,n);
1000 istrstream six(s,n);
1001 /* if(first.str() == "This is first string")
1002 if(second.str()!= " This is second string")
1003 if(third.str() == "")
1013 if(first.str() != "This is first string")
1014 if(second.str()== " This is second string")
1015 if(third.str() != "")
1019 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
1044 TInt Ctiostreams::strstreamL(CStifItemParser& aItem )
1053 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
1057 // ios_base::openmode mode;
1058 // strstream ss3(s, n, mode);
1062 if(ss1.rdbuf( )->pcount( ) != 0)
1065 string str1= ss1.str();
1066 if(str1.compare("")!=0)
1069 ss2 << "strstream testcase";
1071 if( ss2.rdbuf( )->pcount( ) != sizeof("strstream testcase\0")-1)
1074 string str = ss2.str();
1076 if(str.compare("strstream testcase")!= 0)
1089 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
1114 TInt Ctiostreams::ostrstreamL(CStifItemParser& aItem )
1123 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
1127 // ios_base::openmode mode;
1128 // ostrstream oss3( s, n, mode);
1129 ostrstream oss1 ,oss2;
1133 if(oss1.rdbuf( )->pcount( ) != 0)
1136 oss2 << "ostrstream testcase";
1140 if(str.compare("ostrstream testcase") !=0)
1146 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
1180 TInt Ctiostreams::istreamiterators(CStifItemParser& aItem )
1184 // Typedefs for convenience.
1190 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
1193 typedef std::vector<int, std::allocator<int> > Vector;
1195 typedef std::istream_iterator<Vector::value_type,char, std::char_traits<char>, ptrdiff_t> is_iter;
1198 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
1229 TInt Ctiostreams::istreambufiterators(CStifItemParser& aItem )
1234 // create a temporary filename
1241 const char *fname = tmpnam (0);
1246 // open the file is_iter.out for reading and writing
1247 std::ofstream out (fname, std::ios::out | std::ios::in |
1250 // output the example sentence into the file
1252 // seek to the beginning of the file
1256 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
1258 // construct an istreambuf_iterator pointing to
1259 // the ofstream object underlying streambuffer
1260 std::istreambuf_iterator<char, std::char_traits<char> >iter (out.rdbuf ());
1262 // construct an end of stream iterator
1263 const std::istreambuf_iterator<char,std::char_traits<char> > end;
1265 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
1267 std::cout << std::endl;
1269 // output the content of the file
1270 while (!iter.equal (end)) {
1272 // use both operator++ and operator*
1273 std::cout << *iter++;
1276 std::cout << std::endl;
1278 // remove temporary file
1306 TInt Ctiostreams::strstreambufL(CStifItemParser& aItem )
1319 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
1322 signed char* get = NULL;
1324 signed char* put = NULL;
1325 unsigned char* uget = NULL;
1326 unsigned char* uput = NULL;
1327 const char* cget = NULL;
1328 const signed char* csget = NULL;
1330 const unsigned char* cucget = NULL;
1333 typedef void* (*__alloc_fn)(size_t);
1334 typedef void (*__free_fn)(void*);
1336 // __alloc_fn alloc_f;
1338 // __free_fn free_f;
1341 strstreambuf buf1(get, n,put);
1345 strstreambuf buf2(uget,n,uput);
1348 strstreambuf buf3(cget,n);
1350 strstreambuf buf4(csget,n);
1352 strstreambuf buf5(cucget,n);
1355 // strstreambuf buf6( alloc_f, free_f);
1356 strstreambuf buf7(n);
1360 int i = buf.sputn("strstreambuf testcase", sizeof("strstreambuf testcase")-1);
1362 if((buf.pcount())!= i)
1366 //if(buf.str() != "strstreambuf testcase") //fails
1370 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
1404 TInt Ctiostreams::freezeL(CStifItemParser& aItem )
1420 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
1423 x.rdbuf()->freeze();
1425 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
1430 // Stream is bad now, wrote on frozen stream
1436 // Unfreeze stream, but it is still bad
1437 x.rdbuf()->freeze(false);
1453 // Clean up. Failure to unfreeze stream will cause a
1455 x.rdbuf()->freeze(false);
1488 TInt Ctiostreams::fposL(CStifItemParser& aItem )
1496 ifstream file( "c:\\TestFramework\\docs\\fpos_state.txt" );
1499 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
1501 fpos<mbstate_t> f = file.tellg( );
1503 while ( !file.eof( ) )
1509 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
1545 // -----------------------------------------------------------------------------
1546 // Cstdcpp_filebuf::filebufL
1547 // Example test method function.
1548 // (other items were commented in a header).
1549 // -----------------------------------------------------------------------------
1551 TInt Ctiostreams::filebufL( CStifItemParser& aItem )
1562 User::__DbgSetAllocFail(FALSE,RHeap::EDeterministic,1);
1567 fb->open ("c:\\TestFramework\\docs\\filebuf.txt",ios::in);
1579 User::__DbgSetAllocFail(FALSE,RHeap::ENone,1);
1610 TInt Ctiostreams::seekpL( CStifItemParser& aItem )
1615 oss2<<"Do me a favor";
1616 long pos2 = oss2.tellp(); // 13
1619 pos2 = oss2.tellp(); // 15
1620 oss2.seekp(pos2 - 2);
1621 pos2 = oss2.tellp(); // 13
1623 oss2.write(temp.c_str(), temp.size());
1624 pos2 = oss2.tellp(); // 15
1625 oss2.seekp(pos2 - 2);
1626 pos2 = oss2.tellp(); // 13
1628 pos2 = oss2.tellp(); // 15
1629 oss2.seekp(pos2 - 2);
1630 pos2 = oss2.tellp(); // 13
1633 pos2 = oss2.tellp();
1635 oss2.seekp(pos2 - 2);
1636 pos2 = oss2.tellp();
1638 pos2 = oss2.tellp(); //15
1640 oss2.seekp(pos2 - 5);
1641 pos2 = oss2.tellp();
1643 pos2 = oss2.tellp(); //14
1644 oss2.seekp(pos2 - 3);
1645 pos2 = oss2.tellp();
1647 pos2 = oss2.tellp(); //14
1649 pos2 = oss2.tellp(); //20