os/textandloc/textrendering/texthandling/tfields/T_ROMAN.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 "T_ROMAN.h"
    21 
    22 #define test(cond)											\
    23 	{														\
    24 	TBool __bb = (cond);									\
    25 	TEST(__bb);												\
    26 	if (!__bb)												\
    27 		{													\
    28 		ERR_PRINTF1(_L("ERROR: Test Failed"));				\
    29 		User::Leave(1);										\
    30 		}													\
    31 	}
    32 
    33 #define UNUSED_VAR(a) a = a
    34 
    35 const TInt KTestCleanupStack=0x40;
    36 
    37 LOCAL_D CTrapCleanup* TheTrapCleanup;
    38 
    39 //
    40 
    41 void CT_ROMAN::GetValue(HBufC* aBuf,TInt aDenery,TDeneryToCharBase& aNumeral)
    42 	{
    43 	TPtr ptr=aBuf->Des();
    44 	TInt size=aNumeral.DeneryToChar(ptr,aDenery);
    45 	if (size>0)
    46 		{
    47 		aBuf->ReAlloc(size);
    48 		ptr=aBuf->Des();
    49 		size = aNumeral.DeneryToChar(ptr,aDenery);
    50 			test(size==KErrNone);
    51 		}
    52 	}
    53 
    54 
    55 void CT_ROMAN::DisplayValue(HBufC* aBuf,TInt aDenery,TDeneryToCharBase& aNumeral)
    56 	{
    57 	GetValue(aBuf,aDenery,aNumeral);
    58 	INFO_PRINTF2(_L("%D: "),aDenery);
    59 	INFO_PRINTF1(*aBuf);
    60 	INFO_PRINTF1(_L("\n"));
    61 	}
    62 
    63 
    64 void CT_ROMAN::test1()
    65 	{
    66 	INFO_PRINTF1(_L("Testing with buffer of adequate size"));
    67 	INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-ETEXT-LEGACY-T_ROMAN-0001 "));
    68 	HBufC* hbuf = HBufC::NewL(10); 
    69 	CleanupStack::PushL(hbuf);
    70 	TRomanNumeral roman;
    71 	GetValue(hbuf,4,roman);
    72 	TPtr buf = hbuf->Des();
    73 		test(buf==_L("IV"));
    74 	CleanupStack::PopAndDestroy();
    75 	//
    76 	INFO_PRINTF1(_L("Testing with inadequate buffer"));
    77 	HBufC* minibuf = HBufC::NewL(1); 
    78 	CleanupStack::PushL(minibuf);
    79 	GetValue(minibuf,4,roman);
    80 	TPtr buf2 = minibuf->Des();
    81 		test(buf2==_L("IV"));
    82 	CleanupStack::PopAndDestroy();
    83 	}
    84 
    85 
    86 void CT_ROMAN::test2()
    87 	{
    88 	INFO_PRINTF1(_L("Testing Roman numerals for a range of denery values"));
    89 	HBufC* hbuf = HBufC::NewL(10); 
    90 	CleanupStack::PushL(hbuf);
    91 	TRomanNumeral roman;
    92 
    93 	DisplayValue(hbuf,1,roman);
    94 	test(0 == hbuf->CompareC(_L("i"), 1, 0));
    95 	DisplayValue(hbuf,4,roman);
    96 	test(0 == hbuf->CompareC(_L("iv"), 1, 0));
    97 	DisplayValue(hbuf,9,roman);
    98 	test(0 == hbuf->CompareC(_L("ix"), 1, 0));
    99 	DisplayValue(hbuf,17,roman);
   100 	test(0 == hbuf->CompareC(_L("xvii"), 1, 0));
   101 	DisplayValue(hbuf,34,roman);
   102 	test(0 == hbuf->CompareC(_L("xxxiv"), 1, 0));
   103 	DisplayValue(hbuf,99,roman);
   104 	test(0 == hbuf->CompareC(_L("xcix"), 1, 0));
   105 	DisplayValue(hbuf,143,roman);
   106 	test(0 == hbuf->CompareC(_L("cxliii"), 1, 0));
   107 	DisplayValue(hbuf,1982,roman);
   108 	test(0 == hbuf->CompareC(_L("mcmlxxxii"), 1, 0));
   109 
   110 	CleanupStack::PopAndDestroy();
   111 	}
   112 
   113 
   114 void CT_ROMAN::test3()
   115 	{
   116 	INFO_PRINTF1(_L("Testing alphabetic numerals for a range of denery values"));
   117 	HBufC* hbuf = HBufC::NewL(10); 
   118 	CleanupStack::PushL(hbuf);
   119 	TAlphabeticNumeral alpha;
   120 
   121 	DisplayValue(hbuf,1,alpha);
   122 	test(0 == hbuf->CompareC(_L("a"), 1, 0));
   123 	DisplayValue(hbuf,4,alpha);
   124 	test(0 == hbuf->CompareC(_L("d"), 1, 0));
   125 	DisplayValue(hbuf,9,alpha);
   126 	test(0 == hbuf->CompareC(_L("i"), 1, 0));
   127 	DisplayValue(hbuf,17,alpha);
   128 	test(0 == hbuf->CompareC(_L("q"), 1, 0));
   129 	DisplayValue(hbuf,34,alpha);
   130 	test(0 == hbuf->CompareC(_L("ah"), 1, 0));
   131 	DisplayValue(hbuf,99,alpha);
   132 	test(0 == hbuf->CompareC(_L("cu"), 1, 0));
   133 	DisplayValue(hbuf,143,alpha);
   134 	test(0 == hbuf->CompareC(_L("em"), 1, 0));
   135 	DisplayValue(hbuf,1982,alpha);
   136 	test(0 == hbuf->CompareC(_L("bxf"), 1, 0));
   137 
   138 	CleanupStack::PopAndDestroy();
   139 	}
   140 
   141 
   142 void CT_ROMAN::runTests()
   143 // Test the fields dll.
   144 //
   145     {
   146 	// test with HBufs both large enough and too small
   147 	test1();
   148 
   149 	// test for a range of denery values
   150 	test2();
   151 
   152 	// Test alphabetics 
   153 	test3();
   154     }
   155 
   156 
   157 void CT_ROMAN::setupCleanup()
   158 //
   159 // Initialise the cleanup stack.
   160 //
   161     {
   162 
   163 	TheTrapCleanup=CTrapCleanup::New();
   164 	TRAPD(r,\
   165 		{\
   166 		for (TInt i=KTestCleanupStack;i>0;i--)\
   167 			CleanupStack::PushL((TAny*)1);\
   168 		test(r==KErrNone);\
   169 		CleanupStack::Pop(KTestCleanupStack);\
   170 		});
   171 	}
   172 
   173 CT_ROMAN::CT_ROMAN()
   174     {
   175     SetTestStepName(KTestStep_T_ROMAN);
   176     }
   177 
   178 TVerdict CT_ROMAN::doTestStepL()
   179     {
   180 	INFO_PRINTF1(_L("Testing denery to Roman numeral converter"));
   181     SetTestStepResult(EFail);
   182 
   183     __UHEAP_MARK;
   184 
   185 	setupCleanup();
   186     
   187     TRAPD(error1, runTests());
   188 
   189 	delete TheTrapCleanup;
   190 
   191     __UHEAP_MARKEND;
   192 
   193     if(error1 == KErrNone)
   194         {
   195         SetTestStepResult(EPass);
   196         }
   197 
   198     return TestStepResult();
   199     }