kerneltest/e32test/locale/t_currencyformat.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\locale\t_currencyformat.cpp
       
    15 // Overview:
       
    16 // Test the currency formatting capabilities of the locale class.
       
    17 // API Information:
       
    18 // TLocale.
       
    19 // Details:
       
    20 // - Set the currency symbol position before the currency amount.
       
    21 // - Set no space between the currency symbol and the currency amount.
       
    22 // - Set 2 decimal places to currency values. 
       
    23 // - Set negative currency value to be enclosed in brackets without a minus sign.
       
    24 // - Allow triads in currency values.
       
    25 // - Set comma as separator to separate groups of three digits to the left of the decimal
       
    26 // separator.
       
    27 // - Set dot as separator to separate a whole number from its fractional part.
       
    28 // - Set currency symbol to pound.
       
    29 // - Test some simple English Locale currency formats and check it is as expected.
       
    30 // - Render a currency value as text, based on the locale's currency and numeric format 
       
    31 // settings and check the Overflow handling capability is as expected.
       
    32 // - Change a variety of locale settings and verify currency formatting is as expected.
       
    33 // - Change a variety of locale settings display the results.
       
    34 // Platforms/Drives/Compatibility:
       
    35 // All 
       
    36 // Assumptions/Requirement/Pre-requisites:
       
    37 // Failures and causes:
       
    38 // Base Port information:
       
    39 // 
       
    40 //
       
    41 
       
    42 #include <e32test.h> 
       
    43 #include <e32std.h>
       
    44 #include <e32std_private.h>
       
    45 
       
    46 #ifdef __VC32__
       
    47     // Solve compilation problem caused by non-English locale
       
    48     #pragma setlocale("english")
       
    49 #endif
       
    50 
       
    51 _LIT(KNewLine, "\n");
       
    52 LOCAL_D RTest test(_L("T_CurrencyFormat"));
       
    53 LOCAL_D TLocale locale; 
       
    54 
       
    55 
       
    56 class TTestOverflowHandler : public TDesOverflow
       
    57 	{
       
    58 	void Overflow(TDes& aDes);
       
    59 	};
       
    60 
       
    61 void TTestOverflowHandler::Overflow(TDes& aDes)
       
    62 	{
       
    63 	test.Printf(_L(" %S is too large... Overflow Handled!!"), &aDes);
       
    64 	test.Printf(KNewLine);
       
    65 	}
       
    66 
       
    67 
       
    68 void SetEnglishCurrency()
       
    69 	{
       
    70 	locale.SetCurrencySymbolPosition(ELocaleBefore);
       
    71 	locale.SetCurrencySpaceBetween(EFalse);
       
    72 	locale.SetCurrencyDecimalPlaces(2);
       
    73 	locale.SetCurrencyNegativeInBrackets(ETrue);
       
    74 	locale.SetCurrencyTriadsAllowed(ETrue);
       
    75 	locale.SetThousandsSeparator(',');
       
    76 	locale.SetDecimalSeparator('.');
       
    77 	User::SetCurrencySymbol(_L("\xA3"));
       
    78 	}
       
    79 
       
    80 void TestSimpleEnglishCurrencyFormat()
       
    81 	{
       
    82 
       
    83 	TBuf<32> fcurrency; 
       
    84 	// Set a few of the locale variables and then test that formatting the amount
       
    85 	// returns what we expected..... 
       
    86 	test.Start(_L("Testing simple English Locale currency formats"));
       
    87 	locale.FormatCurrency(fcurrency, 4000);
       
    88 	test(fcurrency==_L("\xA3\x34\x30.00"));
       
    89 
       
    90 	locale.FormatCurrency(fcurrency, -567654);
       
    91 	test(fcurrency==_L("(\xA3\x35,676.54)"));
       
    92 	
       
    93 	locale.FormatCurrency(fcurrency,2);
       
    94 	test(fcurrency==_L("\xA3\x30.02"));
       
    95 
       
    96 	locale.FormatCurrency(fcurrency,12500000);
       
    97 	test(fcurrency==_L("\xA3\x31\x32\x35,000.00"));
       
    98 
       
    99 	locale.FormatCurrency(fcurrency, 00001);
       
   100 	test(fcurrency==_L("\xA3\x30.01"));
       
   101 	test.Printf(_L("SimplEnglishCurrencyPass"));
       
   102 	}
       
   103 
       
   104 void TestOverflowCapacity()
       
   105 	{
       
   106 	// try to test each stage that overflow at occur
       
   107 	TBuf<10> smallcurrency;
       
   108 	TTestOverflowHandler handler; 
       
   109 	test.Next(_L("Testing the Overflow handling capability"));
       
   110 	// test the overflow handling capability
       
   111 	
       
   112 	locale.SetCurrencyDecimalPlaces(11);
       
   113 	locale.FormatCurrency(smallcurrency,handler,1);
       
   114 	test(smallcurrency!=_L("0.00000000001"));
       
   115 	locale.SetCurrencyDecimalPlaces(2);
       
   116 
       
   117 	locale.FormatCurrency(smallcurrency,handler, -1234567890);
       
   118 	test(smallcurrency!=_L("(\xA3\x31\x32\x33\x34\x35\x36\x37\x38.90)"));
       
   119 
       
   120 	locale.FormatCurrency(smallcurrency, handler, 95000000);
       
   121 	test(smallcurrency!=_L("\xA3\x39\x35\x30,000.00"));
       
   122 
       
   123 	locale.SetCurrencySpaceBetween(ETrue);
       
   124 	locale.FormatCurrency(smallcurrency, handler, 2000000);
       
   125 	test(smallcurrency!=_L("\xA3 20,000.00"));
       
   126 
       
   127 	locale.FormatCurrency(smallcurrency, handler, -240000);
       
   128 	test(smallcurrency!=_L("(\xA3 2,400.00)"));
       
   129 
       
   130 	User::SetCurrencySymbol(_L("Fake\xA3"));
       
   131 	locale.FormatCurrency(smallcurrency, handler, 240);
       
   132 	test(smallcurrency==_L("Fake\xA3 2.40"));
       
   133 
       
   134 	locale.SetCurrencySpaceBetween(EFalse);
       
   135 	locale.FormatCurrency(smallcurrency, handler, -240);
       
   136 	test(smallcurrency!=_L("(Fake\xA3\x32.40)"));
       
   137 
       
   138 	test.Printf(_L("Overflow Pass"));
       
   139 		
       
   140 	}
       
   141 
       
   142 void TestChangeInformation()
       
   143 	{
       
   144 	TBuf<32> fcurrency; 
       
   145 	// Make changes to the locale currency informationa and check formatting
       
   146 	test.Next(_L("Changing locale information and testing currency Formatting "));
       
   147 	// Change the format, add space in between
       
   148 	locale.SetCurrencySpaceBetween(ETrue);
       
   149 	locale.FormatCurrency(fcurrency,450);
       
   150 	test(fcurrency==_L("\xA3 4.50"));
       
   151 	// change the position of the CurrencySymbol 
       
   152 	locale.SetCurrencySymbolPosition(ELocaleAfter);
       
   153 	locale.FormatCurrency(fcurrency, 300000);
       
   154 	test(fcurrency==_L("3,000.00 \xA3"));
       
   155 	// test the negative format implementation
       
   156 	locale.FormatCurrency(fcurrency , -3000);
       
   157 	test(fcurrency!=_L("-30.00 \xA3"));
       
   158 	test(fcurrency==_L("(30.00 \xA3)"));
       
   159 	// call the deprecated function to check for compatability
       
   160 	locale.SetCurrencyNegativeInBrackets(ETrue);
       
   161 	locale.SetNegativeLoseSpace(ETrue);
       
   162 	locale.SetNegativeCurrencySymbolOpposite(ETrue);
       
   163 	locale.FormatCurrency(fcurrency,-2500);
       
   164 	locale.SetNegativeCurrencySymbolOpposite(EFalse);
       
   165 	locale.SetNegativeLoseSpace(EFalse);
       
   166 	test(fcurrency==_L("(\xA3\x32\x35.00)"));
       
   167 
       
   168 	locale.SetCurrencySymbolPosition(ELocaleBefore);
       
   169 	locale.FormatCurrency(fcurrency, 300000);
       
   170 	test(fcurrency==_L("\xA3 3,000.00"));
       
   171 	// test the negative format implementation
       
   172 	locale.FormatCurrency(fcurrency , -3000);
       
   173 	test(fcurrency!=_L("-30.00 \xA3"));
       
   174 	test(fcurrency==_L("(\xA3 30.00)"));
       
   175 	// call the deprecated function to check for compatability
       
   176 	locale.SetCurrencyNegativeInBrackets(ETrue);
       
   177 	locale.SetNegativeLoseSpace(ETrue);
       
   178 	locale.SetNegativeCurrencySymbolOpposite(ETrue);
       
   179 	locale.FormatCurrency(fcurrency,-2500);
       
   180 	locale.SetNegativeCurrencySymbolOpposite(EFalse);
       
   181 	test(fcurrency==_L("(25.00\xA3)"));
       
   182 	// test the New NegativeCurrencyFormat
       
   183 	locale.SetNegativeCurrencyFormat(TLocale::EInterveningMinusSign);
       
   184 	locale.FormatCurrency(fcurrency,-32500);
       
   185 	test(fcurrency==_L("\xA3-325.00"));
       
   186 	// test the decimal places implementation
       
   187 	locale.SetCurrencyDecimalPlaces(6);
       
   188 	locale.FormatCurrency(fcurrency,-32500);
       
   189 	locale.SetNegativeLoseSpace(EFalse);
       
   190 	test(fcurrency==_L("\xA3-0.032500"));
       
   191 	// testing Thousandseparators and triads
       
   192 	locale.SetCurrencyDecimalPlaces(2);
       
   193 	locale.SetThousandsSeparator(' ');
       
   194 	locale.SetCurrencySymbolPosition(ELocaleAfter);
       
   195 	locale.FormatCurrency(fcurrency, 300000000);
       
   196 	test(fcurrency==_L("3 000 000.00 \xA3"));
       
   197 
       
   198 	locale.SetCurrencyTriadsAllowed(EFalse);
       
   199 	locale.FormatCurrency(fcurrency, 300000000);
       
   200 	test(fcurrency==_L("3000000.00 \xA3"));
       
   201 	// test the other NegativeCurrencyFormat Options
       
   202 	locale.SetNegativeCurrencyFormat(TLocale::ETrailingMinusSign);
       
   203 	locale.FormatCurrency(fcurrency, -24000000);
       
   204 	test(fcurrency==_L("240000.00 \xA3-"));
       
   205 
       
   206 	locale.SetNegativeCurrencyFormat(TLocale::EInterveningMinusSign);
       
   207 	locale.FormatCurrency(fcurrency, -40000000);
       
   208 	test(fcurrency== _L("400000.00- \xA3"));
       
   209 	// change the currency symbol 
       
   210 	User::SetCurrencySymbol(_L("NZ$"));
       
   211 	locale.FormatCurrency(fcurrency,250000);
       
   212 	test(fcurrency!=_L("2,500.00 \xA3"));
       
   213 	test(fcurrency==_L("2500.00 NZ$"));
       
   214 	// move the currency symbol forward
       
   215 	locale.SetCurrencySymbolPosition(ELocaleBefore);
       
   216 	locale.FormatCurrency(fcurrency,300);
       
   217 	test(fcurrency==_L("NZ$ 3.00"));
       
   218 
       
   219 	locale.SetCurrencySpaceBetween(EFalse);
       
   220 	locale.FormatCurrency(fcurrency, 43020);
       
   221 	test(fcurrency==_L("NZ$430.20"));
       
   222 
       
   223 	locale.SetCurrencyTriadsAllowed(ETrue);
       
   224 	locale.SetThousandsSeparator(',');
       
   225 	locale.SetCurrencySpaceBetween(ETrue);
       
   226 	locale.FormatCurrency(fcurrency,-2450000);
       
   227 	test(fcurrency==_L("NZ$ -24,500.00"));
       
   228 	locale.SetNegativeCurrencySymbolOpposite(EFalse);
       
   229 	
       
   230 	
       
   231 	}
       
   232 
       
   233 void TestDisplyCurrencyFormats() 
       
   234 	{
       
   235 
       
   236 	// Display Differenct locale currency as a test 
       
   237 	test.Next(_L("Samples of a few Currency formats that can be displayed"));
       
   238 	TBuf<32> fcurrency; 
       
   239 	locale.FormatCurrency (fcurrency, 2500000);
       
   240 	test.Printf(fcurrency);
       
   241 	test.Printf(KNewLine);
       
   242 	locale.SetCurrencySpaceBetween(EFalse);
       
   243 	User::SetCurrencySymbol(_L("\xA3"));
       
   244 	locale.SetCurrencySymbolPosition(ELocaleAfter);
       
   245 	locale.FormatCurrency (fcurrency, 2500000);
       
   246 	test.Printf(fcurrency);
       
   247 	test.Printf(KNewLine);
       
   248 	locale.SetCurrencySymbolPosition(ELocaleBefore);
       
   249 	User::SetCurrencySymbol(_L("$"));
       
   250 	locale.SetNegativeCurrencyFormat(TLocale::EInBrackets);
       
   251 	locale.FormatCurrency (fcurrency, -2500000);
       
   252 	test.Printf(fcurrency);
       
   253 	test.Printf(KNewLine);
       
   254 	locale.SetNegativeCurrencyFormat(TLocale::ELeadingMinusSign);
       
   255 	locale.FormatCurrency (fcurrency, -2500000);
       
   256 	test.Printf(fcurrency);
       
   257 	test.Printf(KNewLine);
       
   258 	locale.SetNegativeCurrencySymbolOpposite(ETrue);
       
   259 	locale.SetNegativeLoseSpace(ETrue);
       
   260 	locale.FormatCurrency (fcurrency, -2500000);
       
   261 	test.Printf(fcurrency);
       
   262 	test.Printf(KNewLine);
       
   263 	locale.SetNegativeCurrencySymbolOpposite(EFalse);
       
   264 	locale.SetNegativeCurrencyFormat(TLocale::EInterveningMinusSign);
       
   265 	locale.FormatCurrency (fcurrency, -2500000);
       
   266 	test.Printf(fcurrency);
       
   267 	test.Printf(KNewLine);
       
   268 	//******************
       
   269 
       
   270 	locale.SetCurrencySymbolPosition(ELocaleAfter);
       
   271 	User::SetCurrencySymbol(_L("$"));
       
   272 	locale.SetNegativeCurrencyFormat(TLocale::EInBrackets);
       
   273 	locale.SetNegativeLoseSpace(ETrue);
       
   274 	locale.FormatCurrency (fcurrency, -2500000);
       
   275 	test.Printf(fcurrency);
       
   276 	test.Printf(KNewLine);
       
   277 	locale.SetNegativeCurrencyFormat(TLocale::ELeadingMinusSign);
       
   278 	locale.FormatCurrency (fcurrency, -2500000);
       
   279 	test.Printf(fcurrency);
       
   280 	test.Printf(KNewLine);
       
   281 	locale.SetNegativeCurrencySymbolOpposite(ETrue);
       
   282 	locale.SetNegativeLoseSpace(ETrue);
       
   283 	locale.FormatCurrency (fcurrency, -2500000);
       
   284 	test.Printf(fcurrency);
       
   285 	test.Printf(KNewLine);
       
   286 	locale.SetNegativeCurrencySymbolOpposite(EFalse);
       
   287 	locale.SetNegativeCurrencyFormat(TLocale::EInterveningMinusSign);
       
   288 	locale.FormatCurrency (fcurrency, -2500000);
       
   289 	test.Printf(fcurrency);
       
   290 	test.Printf(KNewLine);
       
   291 
       
   292 	//******************
       
   293 	TInt64 x=25000000;
       
   294 	x*=10000;
       
   295 	locale.FormatCurrency (fcurrency,x );
       
   296 	test.Printf(fcurrency);
       
   297 	test.Printf(KNewLine);
       
   298 
       
   299 
       
   300 	}
       
   301 
       
   302 GLDEF_C TInt E32Main(void)
       
   303 	{
       
   304 
       
   305 	// Call all the tests from here 
       
   306 	test.Title();
       
   307 	SetEnglishCurrency(); 
       
   308 	TestSimpleEnglishCurrencyFormat();
       
   309 	TestOverflowCapacity();
       
   310 	SetEnglishCurrency(); 
       
   311 	TestChangeInformation();
       
   312 	TestDisplyCurrencyFormats();
       
   313 	test.End();
       
   314 	return KErrNone; 
       
   315 	}