os/ossrv/stdcpp/tsrc/BC/apps/widecharclassapiBCTest/src/widecharclassapiBCTestCases.cpp
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.
19 // [INCLUDE FILES] - do not remove
21 #include "widecharclassapiBCTest.h"
23 #include <stl\_cwchar.h>
29 // EXTERNAL DATA STRUCTURES
30 //extern ?external_data;
32 // EXTERNAL FUNCTION PROTOTYPES
33 //extern ?external_function( ?arg_type,?arg_type );
36 //const ?type ?constant_var = ?constant;
39 //#define ?macro ?macro_def
41 // LOCAL CONSTANTS AND MACROS
42 //const ?type ?constant_var = ?constant;
43 //#define ?macro_name ?macro_def
45 // MODULE DATA STRUCTURES
47 //typedef ?declaration
49 // LOCAL FUNCTION PROTOTYPES
50 //?type ?function_name( ?arg_type, ?arg_type );
52 // FORWARD DECLARATIONS
53 //class ?FORWARD_CLASSNAME;
55 // ============================= LOCAL FUNCTIONS ===============================
57 // -----------------------------------------------------------------------------
58 // ?function_name ?description.
60 // Returns: ?value_1: ?description
61 // ?value_n: ?description_line1
63 // -----------------------------------------------------------------------------
67 ?arg_type arg, // ?description
68 ?arg_type arg) // ?description
78 // ============================ MEMBER FUNCTIONS ===============================
80 // -----------------------------------------------------------------------------
81 // CwidecharclassapiBCTest::Case
82 // Returns a test case by number.
84 // This function contains an array of all available test cases
85 // i.e pair of case name and test function. If case specified by parameter
86 // aCaseNumber is found from array, then that item is returned.
88 // The reason for this rather complicated function is to specify all the
89 // test cases only in one place. It is not necessary to understand how
90 // function pointers to class member functions works when adding new test
91 // cases. See function body for instructions how to add new test case.
92 // -----------------------------------------------------------------------------
94 const TCaseInfo CwidecharclassapiBCTest::Case (
95 const TInt aCaseNumber ) const
99 * To add new test cases, implement new test case function and add new
100 * line to KCases array specify the name of the case and the function
101 * doing the test case
102 * In practice, do following
103 * 1) Make copy of existing test case function and change its name
104 * and functionality. Note that the function must be added to
105 * widecharclassapiBCTest.cpp file and to widecharclassapiBCTest.h
108 * 2) Add entry to following KCases array either by using:
110 * 2.1: FUNCENTRY or ENTRY macro
111 * ENTRY macro takes two parameters: test case name and test case
114 * FUNCENTRY macro takes only test case function name as a parameter and
115 * uses that as a test case name and test case function name.
119 * 2.2: OOM_FUNCENTRY or OOM_ENTRY macro. Note that these macros are used
120 * only with OOM (Out-Of-Memory) testing!
122 * OOM_ENTRY macro takes five parameters: test case name, test case
123 * function name, TBool which specifies is method supposed to be run using
124 * OOM conditions, TInt value for first heap memory allocation failure and
125 * TInt value for last heap memory allocation failure.
127 * OOM_FUNCENTRY macro takes test case function name as a parameter and uses
128 * that as a test case name, TBool which specifies is method supposed to be
129 * run using OOM conditions, TInt value for first heap memory allocation
130 * failure and TInt value for last heap memory allocation failure.
133 static TCaseInfoInternal const KCases[] =
135 // [test cases entries] - do not remove
137 // NOTE: When compiled to GCCE, there must be Classname::
138 // declaration in front of the method name, e.g.
139 // CwidecharclassapiBCTest::PrintTest. Otherwise the compiler
142 ENTRY( "Arithmetic Functions test", CwidecharclassapiBCTest::ArithmeticTest ),
143 ENTRY( "File Manipulation test", CwidecharclassapiBCTest::FileManipulationTest ),
144 ENTRY( "Console Operations test", CwidecharclassapiBCTest::ConsoleOperationsTest ),
145 ENTRY( "String Operations test", CwidecharclassapiBCTest::StringOperationsTest ),
146 ENTRY( "Conversion Operations test", CwidecharclassapiBCTest::ConversionOperationsTest ),
147 // Example how to use OOM functionality
148 //OOM_ENTRY( "Loop test with OOM", CwidecharclassapiBCTest::LoopTest, ETrue, 2, 3),
149 //OOM_FUNCENTRY( CwidecharclassapiBCTest::PrintTest, ETrue, 1, 3 ),
152 // Verify that case number is valid
153 if( (TUint) aCaseNumber >= sizeof( KCases ) /
154 sizeof( TCaseInfoInternal ) )
156 // Invalid case, construct empty object
157 TCaseInfo null( (const TText*) L"" );
159 null.iIsOOMTest = EFalse;
160 null.iFirstMemoryAllocation = 0;
161 null.iLastMemoryAllocation = 0;
165 // Construct TCaseInfo object and return it
166 TCaseInfo tmp ( KCases[ aCaseNumber ].iCaseName );
167 tmp.iMethod = KCases[ aCaseNumber ].iMethod;
168 tmp.iIsOOMTest = KCases[ aCaseNumber ].iIsOOMTest;
169 tmp.iFirstMemoryAllocation = KCases[ aCaseNumber ].iFirstMemoryAllocation;
170 tmp.iLastMemoryAllocation = KCases[ aCaseNumber ].iLastMemoryAllocation;
176 int WriteVarPrintfConsole (char *format, ...)
180 va_start (args, format);
181 result = std::vwprintf (L"%d", args);
186 int WriteVarPrintfFile (FILE * stream, char * format, ...)
190 va_start (args, format);
191 result = std::vfwprintf (stream, L"%d", args);
196 int WriteVarPrintfString (wchar_t * outputBuf, int sizeBuf, char * format, ...)
200 va_start (args, format);
201 result = std::vswprintf (outputBuf, sizeBuf, L"%d", args);
209 TInt CwidecharclassapiBCTest::FileManipulationTest( TTestResult& aResult )
215 const wchar_t *wsList = L"This is Wide Character Class API test.";
216 const wchar_t *wPrintfTestFormat = L"%d";
218 wchar_t wsListRead[64];
219 wchar_t wPrintfTestReadList[64];
224 char fileName[]="c:\\data\\others\\widechartest.txt";
229 /* opening the output file */
230 fp = fopen(fileName,"w");
237 int fileOrientation ;
239 fileOrientation = std::fwide(fp, 0);
243 int fWritePrResult = std::fwprintf(fp, wPrintfTestFormat, c);
244 if(fWritePrResult < 0)
247 //Test std::vfwprintf
248 int fWriteVPrResult = WriteVarPrintfFile(fp, "%d", c);
249 if(fWriteVPrResult < 0)
254 std::wint_t fWriteWCResult = std::fputwc(wc,fp);
255 if(fWriteWCResult == WEOF)
259 int fWriteWSResult = std::fputws(wsList, fp);
260 if(fWriteWSResult < 0)
268 int wsListLength = std::wcslen(wsList);
269 int wPrintTestListLength = std::wcslen(wPrintfTestFormat);
272 /* opening the input file */
273 fp = fopen(fileName,"r");
281 int fReadPrResult = std::fwscanf(fp, wPrintfTestFormat, &inpC);
282 if(fReadPrResult < 0)
284 fReadPrResult = std::fwscanf(fp, wPrintfTestFormat, &inpC);
285 if(fReadPrResult < 0)
290 std::wint_t fReadWCResult = std::fgetwc(fp);
291 if(fReadWCResult == WEOF)
295 wchar_t * ptrReadWSResult = std::fgetws(wsListRead, wsListLength, fp);
296 if(ptrReadWSResult == 0)
306 // Sets test case result and description(Maximum size is KStifMaxResultDes)
309 _LIT( KDescriptionErr, "FileManipulationTest failed" );
310 aResult.SetResult( KErrGeneral, KDescriptionErr );
324 TInt CwidecharclassapiBCTest::ConsoleOperationsTest( TTestResult& aResult )
329 freopen("c:\\data\\others\\myconsole.txt", "w", stdout);
331 wchar_t * consoleMessage = L"Enter Character 'v' ";
332 fputws(consoleMessage, stdout);
333 wint_t putWCResult = std::putwc(L':', stdout);
337 int fWriteVPrResult = WriteVarPrintfConsole("%d", c);
338 if(fWriteVPrResult < 0)
341 ungetwc(L'v', stdin);
345 getWCResult = std::getwc(stdin);
348 ungetwc(L'v', stdin);
349 getWCResult = std::getwchar();
358 // Sets test case result and description(Maximum size is KStifMaxResultDes)
374 TInt CwidecharclassapiBCTest::StringOperationsTest( TTestResult& aResult )
380 wchar_t wcSRC[64] = L"Wide character";
381 wchar_t wcDST[64] = L"Class API";
382 wchar_t *wcWCSCATResult= std::wcscat(wcDST, (const wchar_t*)wcSRC);
385 wchar_t *wcWCSNCATResult= std::wcsncat(wcDST, (const wchar_t*)wcSRC, 4);
388 wchar_t *wcSTR = L"Wide Character Class API";
389 wchar_t wcSearch = L'A';
391 wchar_t* wcWCSCHRResult = std::wcschr(wcSTR, wcSearch);
394 wchar_t* wcWCSRCHRResult = std::wcsrchr(wcSTR, wcSearch);
397 wchar_t* wcWMEMCHRResult = std::wmemchr(wcSTR, wcSearch, 4);
401 wchar_t *wcS1 = L"ABCDE";
402 wchar_t *wcS2 = L"ACCD";
403 int intWCSCMPResult = std::wcscmp(wcS1, wcS2);
406 int intWCSNCMPResult = std::wcsncmp(wcS1, wcS2, 4);
409 int intWCSCOLLResult = std::wcscoll(wcS1, wcS2);
412 int intWMEMCMPResult = std::wmemcmp(wcS1, wcS2, 4);
416 wchar_t wcSRC1[64] = L"Wide character";
417 wchar_t wcDST1[64] = L"Class API";
418 wchar_t *wcWCSCPYResult = std::wcscpy(wcDST1, wcSRC1);
421 wchar_t *wcWCSNCPYResult = std::wcsncpy(wcDST1, wcSRC1, 4);
424 wchar_t *wcWMEMCPYResult = std::wmemcpy(wcDST1, wcSRC1, 4);
427 wchar_t *wcWMEMMOVEResult = std::wmemmove(wcDST1, wcSRC1, 4);
430 wchar_t *wcWMEMSETResult = std::wmemset(wcDST1, L'V', 4);
433 size_t lenWCSXFRMResult = std::wcsxfrm(wcDST1, wcSRC1, 15);
436 const wchar_t wcSTR1[30] = L"1234567890ABCDE!@#$$";
437 const wchar_t wcSpanset[20] = L"ABCDE!@#$$";
438 size_t intWCSCSPNResult = std::wcscspn(wcSTR1, wcSpanset);
441 size_t intWCSSPNResult = std::wcsspn(wcSTR1, wcSpanset);
444 wchar_t * wcptrWCSSPNResult = std::wcspbrk(wcSTR1, wcSpanset);
449 struct tm * timeinfo;
450 wchar_t wcTimeOutput [80];
452 timeinfo = localtime ( &rawtime );
453 size_t lenString = std::wcsftime (wcTimeOutput,80,L"Now is %I:%M%p.",timeinfo);
456 size_t lenSTR = std::wcslen(wcSTR);
459 wchar_t wcSTR2[64] = L"Wide character Class API";
460 wchar_t wcSUBSTR[64] = L"Class API";
461 wchar_t* wcptrWCSSTR = std::wcsstr(wcSTR2, wcSUBSTR);
464 wchar_t wcSTRDest[64];
466 wchar_t *formatSTR = L"%d";
467 int intSampleOutput = 78;
468 int returnPrintf = std::swprintf(wcSTRDest, 63, formatSTR, intSampleOutput);
472 int returnScanf = std::swscanf(wcSTRDest, formatSTR, &intSampleInput);
475 //Test std::vswprintf
476 wchar_t outputSTR[32];
478 int fWriteVPrResult = WriteVarPrintfString(outputSTR, 32, "%d", c);
479 if(fWriteVPrResult < 0)
485 // Sets test case result and description(Maximum size is KStifMaxResultDes)
500 TInt CwidecharclassapiBCTest::ConversionOperationsTest( TTestResult& aResult )
506 char *szMBTest = "this is test";
513 /* converting single byte to wide-character */
518 wchar_t wcs1[21] = L" 1.23abcd";
519 wchar_t wcs2[5]=L"abcd";
522 /* convert wide-char string to double */
523 d = std::wcstod(wcs1, &eptr);
524 /* compare the result */
525 if(!((d == 1.23F) && !(std::wcscmp (eptr, wcs2))))
530 wchar_t wcs3[8]=L".23abcd";
532 /* convert wide-char string to long int */
533 longintOutput = std::wcstol(wcs1, &eptr, 10);
534 /* compare the result */
535 if(!((longintOutput == 1) && !(std::wcscmp (eptr, wcs3))))
539 unsigned long unslongintOutput;
540 /* convert wide-char string to long int */
541 unslongintOutput = std::wcstoul(wcs1, &eptr, 10);
542 /* compare the result */
543 if(!((unslongintOutput == 1) && !(std::wcscmp (eptr, wcs3))))
549 const wchar_t *seps = L":";
550 wchar_t *last, *tok, text[] = L"one:twothree";
552 tok = std::wcstok(text, seps, &last);
557 //Test std::mbsrtowcs
558 wchar_t wideCharStringOutput[64];
559 len = std::mbsrtowcs(wideCharStringOutput, (const char**)&szMBTest, 10, &ps);
560 /* checking for error */
566 //Test std::wcsrtombs
567 char mbOutputString[64];
568 wchar_t *wcptrString = wideCharStringOutput;
569 size_t lenWCS = std::wcsrtombs(mbOutputString, (const wchar_t**) &wcptrString, len, &ps);
573 wchar_t wcWCRTOMB = L'v';
575 len = std::wcrtomb(mbWCRTOMB, wcWCRTOMB, &ps);
581 // Sets test case result and description(Maximum size is KStifMaxResultDes)
598 TInt CwidecharclassapiBCTest::ArithmeticTest( TTestResult& aResult )
606 char *szMBTest = "this is test";
609 memset(&mbs, 0, sizeof(mbs));
610 // size_t charlen = std::mbrlen(szMBTest, 6, &mbs);
611 size_t charlen = std::mbrlen((const char*)&aa, 6, &mbs);
615 wchar_t wcMBCOutput[100];
619 /* converting multibyte sequence to a wide-char sequence */
620 len = std::mbrtowc(wcMBCOutput,(const char *) s, n, &ps);
621 /* checking for error value */
629 int mbINITOutput = std::mbsinit((const mbstate_t*) &ps);
634 // Sets test case result and description(Maximum size is KStifMaxResultDes)
637 _LIT( KDescriptionErr, "ArithmeticTest failed" );
638 aResult.SetResult( KErrGeneral, KDescriptionErr );
645 _LIT( KDescription, "LoopTest passed" );
646 aResult.SetResult( KErrNone, KDescription );
657 // -----------------------------------------------------------------------------
658 // ?classname::?member_function
659 // ?implementation_description
660 // (other items were commented in a header).
661 // -----------------------------------------------------------------------------
664 ?type ?classname::?member_function(
674 // ========================== OTHER EXPORTED FUNCTIONS =========================
676 // -----------------------------------------------------------------------------
677 // ?function_name implements...
678 // ?implementation_description.
679 // Returns: ?value_1: ?description
680 // ?value_n: ?description
682 // -----------------------------------------------------------------------------
685 ?type ?function_name(
686 ?arg_type arg, // ?description
687 ?arg_type arg ) // ?description
694 // [End of File] - do not remove