os/ossrv/genericopenlibs/cstdlib/TSTLIB/TMBTOWC.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1997-2007 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 *
    16 */
    17 
    18 
    19 #include <stdlib.h>
    20 #include <stdio.h>
    21 #include <string.h>
    22 #include "CTEST.H"
    23 
    24 
    25 #define TELLME "Tell Me The Answer"
    26 
    27 test_Data;
    28 
    29 /**
    30 @SYMTestCaseID          SYSLIB-STDLIB-CT-1081
    31 @SYMTestCaseDesc	    Tests for multibyte to wide character conversions
    32 @SYMTestPriority 	    High
    33 @SYMTestActions  	    Tests for conversions from multibyte to wide character and wide character to multibyte
    34 @SYMTestExpectedResults Test must not fail
    35 @SYMREQ                 REQ0000
    36 */		
    37 int main()
    38 {
    39 
    40 
    41     int      i,x;
    42     char    *pmbc    = (char *)malloc(MB_CUR_MAX);
    43     wchar_t  wc      = L'a';
    44     wchar_t *pwcnull = NULL;
    45     wchar_t *pwc     = (wchar_t *)malloc( sizeof( wchar_t ) );
    46 
    47 	char * narrowstring = (char*)malloc(50);
    48 	wchar_t * pwcstring = (wchar_t *)malloc(sizeof(wchar_t)*50);
    49 
    50 	wchar_t greek[] = {0x03a3, 0x03bf, 0x03bc, 0x03b4, 0x03b5, 0x0345, 0x03a9, 0x0392, 0x0395, 0x03a1, 0};
    51 	
    52 
    53 	
    54 	test_Title("Wide/Multibyte character conversions.");
    55 	
    56 	test_Next("Convert a NULL wide character to multibyte character");
    57     i = wctomb( pmbc, L'\0');
    58     test(1==i);
    59     test(*pmbc=='\0');
    60 
    61 
    62 	test_Next("Convert a wide character to multibyte character:");
    63     i = wctomb(pmbc, wc);
    64     test(i==1);
    65     test(*pmbc=='a');
    66 
    67     test_Next("Convert multibyte character back to a wide character");
    68     i = mbtowc(pwc, pmbc, 20);
    69     test(i==1);
    70     test(*pwc==L'a');
    71 
    72     //check this
    73 	test_Next("Attempt to convert when target is NULL");
    74     i = mbtowc(pwcnull, pmbc, MB_CUR_MAX);
    75     test(i==1);
    76 
    77     test_Next( "Attempt to convert a NULL pointer to a wide character");
    78     pmbc = NULL;
    79     i = mbtowc( pwc, pmbc, MB_CUR_MAX );
    80     test(i==0);
    81 
    82 	test_Next("Convert narrow string to unicode");
    83 	strcpy(narrowstring, TELLME);
    84 	i = mbstowcs(pwcstring, narrowstring, 50);
    85 	test(i==(int)strlen(TELLME));		//should be same
    86 	
    87 	test_Next("Convert unicode string back to UTF8");
    88 	i = wcstombs(narrowstring, pwcstring, 50);
    89 	test(i==(int)strlen(TELLME));		//should be same
    90 	test(strcmp(narrowstring, TELLME) == 0);	
    91 
    92 	test_Next("Get length of dest string");
    93 	i = wcstombs(NULL, pwcstring, 50);
    94 //	test(i==strlen(TELLME));		//should be same
    95 //	test(strcmp(narrowstring, TELLME) == 0);	
    96 
    97 	
    98 	test_Next("Convert first 10 wide characters");
    99 	strcpy(narrowstring,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
   100 	i = wcstombs(narrowstring, pwcstring, 10);
   101 	test(i==10);
   102 	test(memcmp(narrowstring,TELLME,10)==0);
   103 
   104 
   105 	test_Next("Convert greek characters to unicode and back");
   106 	i = wcstombs(narrowstring, &greek[0], 50);
   107 	x = i;
   108 	i = mbstowcs(pwcstring, narrowstring, 50);
   109 	test(2*i==x);
   110 	test(0==memcmp(pwcstring, greek, sizeof(greek)));
   111 
   112 	test_Next("onto mblen");
   113 	i = mblen("abc",3);
   114 	test(i==1);
   115 
   116 	test_Next("length of a greek character");
   117 	i = mblen(narrowstring,6);
   118 	test (i==2);
   119 
   120 	test_Next("test an empty string");
   121 	i = mblen("",2);
   122 	test(i==0);
   123 
   124 	test_Next("test a null pointer");
   125 	i = mblen(NULL,2);
   126 	test(i==0);
   127 
   128 	test_Next("test a failure");
   129 	i = mblen(narrowstring,1);
   130 	test(i==-1);
   131 	
   132 	
   133 	free(pmbc);
   134 	free(pwc);
   135 	free(pwcstring);
   136 	free(narrowstring);
   137 
   138 	test_Close();
   139 }