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