textrendering/texthandling/tfields/T_ROMAN.CPP
changeset 32 8b9155204a54
parent 0 1fb32624e06b
child 51 a7c938434754
equal deleted inserted replaced
31:b9ad20498fb4 32:8b9155204a54
       
     1 /*
       
     2 * Copyright (c) 1997-2009 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 "../sfields/FLDNUMS.H"
       
    20 #include <e32test.h>
       
    21 #include <e32std.h>
       
    22 
       
    23 #define UNUSED_VAR(a) a = a
       
    24 
       
    25 const TInt KTestCleanupStack=0x40;
       
    26 
       
    27 LOCAL_D RTest test(_L("Testing denery to Roman numeral converter"));
       
    28 LOCAL_D CTrapCleanup* TheTrapCleanup;
       
    29 
       
    30 //
       
    31 
       
    32 LOCAL_C void GetValue(HBufC* aBuf,TInt aDenery,TDeneryToCharBase& aNumeral)
       
    33 	{
       
    34 	TPtr ptr=aBuf->Des();
       
    35 	TInt size=aNumeral.DeneryToChar(ptr,aDenery);
       
    36 	if (size>0)
       
    37 		{
       
    38 		aBuf->ReAlloc(size);
       
    39 		ptr=aBuf->Des();
       
    40 		size = aNumeral.DeneryToChar(ptr,aDenery);
       
    41 			test(size==KErrNone);
       
    42 		}
       
    43 	}
       
    44 
       
    45 
       
    46 LOCAL_D void DisplayValue(HBufC* aBuf,TInt aDenery,TDeneryToCharBase& aNumeral)
       
    47 	{
       
    48 	GetValue(aBuf,aDenery,aNumeral);
       
    49 	test.Printf(_L("%D: "),aDenery);
       
    50 		test.Printf(*aBuf);
       
    51 		test.Printf(_L("\n"));
       
    52 	}
       
    53 
       
    54 
       
    55 LOCAL_C void test1()
       
    56 	{
       
    57 	test.Start(_L("Testing with buffer of adequate size"));
       
    58 	test.Next(_L(" @SYMTestCaseID:SYSLIB-ETEXT-LEGACY-T_ROMAN-0001 "));
       
    59 	HBufC* hbuf = HBufC::NewL(10); 
       
    60 	CleanupStack::PushL(hbuf);
       
    61 	TRomanNumeral roman;
       
    62 	GetValue(hbuf,4,roman);
       
    63 	TPtr buf = hbuf->Des();
       
    64 		test(buf==_L("IV"));
       
    65 	CleanupStack::PopAndDestroy();
       
    66 	//
       
    67 	test.Next(_L("Testing with inadequate buffer"));
       
    68 	HBufC* minibuf = HBufC::NewL(1); 
       
    69 	CleanupStack::PushL(minibuf);
       
    70 	GetValue(minibuf,4,roman);
       
    71 	TPtr buf2 = minibuf->Des();
       
    72 		test(buf2==_L("IV"));
       
    73 	CleanupStack::PopAndDestroy();
       
    74 	}
       
    75 
       
    76 
       
    77 LOCAL_C void test2()
       
    78 	{
       
    79 	test.Next(_L("Testing Roman numerals for a range of denery values"));
       
    80 	HBufC* hbuf = HBufC::NewL(10); 
       
    81 	CleanupStack::PushL(hbuf);
       
    82 	TRomanNumeral roman;
       
    83 
       
    84 	DisplayValue(hbuf,1,roman);
       
    85 	test(0 == hbuf->CompareC(_L("i"), 1, 0));
       
    86 	DisplayValue(hbuf,4,roman);
       
    87 	test(0 == hbuf->CompareC(_L("iv"), 1, 0));
       
    88 	DisplayValue(hbuf,9,roman);
       
    89 	test(0 == hbuf->CompareC(_L("ix"), 1, 0));
       
    90 	DisplayValue(hbuf,17,roman);
       
    91 	test(0 == hbuf->CompareC(_L("xvii"), 1, 0));
       
    92 	DisplayValue(hbuf,34,roman);
       
    93 	test(0 == hbuf->CompareC(_L("xxxiv"), 1, 0));
       
    94 	DisplayValue(hbuf,99,roman);
       
    95 	test(0 == hbuf->CompareC(_L("xcix"), 1, 0));
       
    96 	DisplayValue(hbuf,143,roman);
       
    97 	test(0 == hbuf->CompareC(_L("cxliii"), 1, 0));
       
    98 	DisplayValue(hbuf,1982,roman);
       
    99 	test(0 == hbuf->CompareC(_L("mcmlxxxii"), 1, 0));
       
   100 
       
   101 	CleanupStack::PopAndDestroy();
       
   102 	}
       
   103 
       
   104 
       
   105 LOCAL_C void test3()
       
   106 	{
       
   107 	test.Next(_L("Testing alphabetic numerals for a range of denery values"));
       
   108 	HBufC* hbuf = HBufC::NewL(10); 
       
   109 	CleanupStack::PushL(hbuf);
       
   110 	TAlphabeticNumeral alpha;
       
   111 
       
   112 	DisplayValue(hbuf,1,alpha);
       
   113 	test(0 == hbuf->CompareC(_L("a"), 1, 0));
       
   114 	DisplayValue(hbuf,4,alpha);
       
   115 	test(0 == hbuf->CompareC(_L("d"), 1, 0));
       
   116 	DisplayValue(hbuf,9,alpha);
       
   117 	test(0 == hbuf->CompareC(_L("i"), 1, 0));
       
   118 	DisplayValue(hbuf,17,alpha);
       
   119 	test(0 == hbuf->CompareC(_L("q"), 1, 0));
       
   120 	DisplayValue(hbuf,34,alpha);
       
   121 	test(0 == hbuf->CompareC(_L("ah"), 1, 0));
       
   122 	DisplayValue(hbuf,99,alpha);
       
   123 	test(0 == hbuf->CompareC(_L("cu"), 1, 0));
       
   124 	DisplayValue(hbuf,143,alpha);
       
   125 	test(0 == hbuf->CompareC(_L("em"), 1, 0));
       
   126 	DisplayValue(hbuf,1982,alpha);
       
   127 	test(0 == hbuf->CompareC(_L("bxf"), 1, 0));
       
   128 
       
   129 	CleanupStack::PopAndDestroy();
       
   130 	}
       
   131 
       
   132 
       
   133 LOCAL_C void runTests()
       
   134 // Test the fields dll.
       
   135 //
       
   136     {
       
   137 	// test with HBufs both large enough and too small
       
   138 	test1();
       
   139 
       
   140 	// test for a range of denery values
       
   141 	test2();
       
   142 
       
   143 	// Test alphabetics 
       
   144 	test3();
       
   145     }
       
   146 
       
   147 
       
   148 LOCAL_C void setupCleanup()
       
   149 //
       
   150 // Initialise the cleanup stack.
       
   151 //
       
   152     {
       
   153 
       
   154 	TheTrapCleanup=CTrapCleanup::New();
       
   155 	TRAPD(r,\
       
   156 		{\
       
   157 		for (TInt i=KTestCleanupStack;i>0;i--)\
       
   158 			CleanupStack::PushL((TAny*)1);\
       
   159 		test(r==KErrNone);\
       
   160 		CleanupStack::Pop(KTestCleanupStack);\
       
   161 		});
       
   162 	}
       
   163 
       
   164 
       
   165 GLDEF_C TInt E32Main()
       
   166 //
       
   167 // Test the streaming framework.
       
   168 //
       
   169     {
       
   170 
       
   171 	test.Title();
       
   172 	
       
   173 	__UHEAP_MARK;
       
   174 	setupCleanup();
       
   175        
       
   176 	TRAPD(r,runTests());
       
   177     test(r == KErrNone);
       
   178 	
       
   179 	delete TheTrapCleanup;
       
   180 	__UHEAP_MARKEND;
       
   181 	test.End();
       
   182 	test.Close();
       
   183 	return 0;
       
   184     }