Format

Top  Previous  Next

Unit tests > Format

The formatting routines account for a considerable part of the SysUtils unit. Some of them are nested and consist in about 1000 lines of code. Nevertheless their translation with Delphi2Cpp is nearly perfect. Examples to the formatting routines from

 

http://www.delphibasics.co.uk/RTL.asp?Name=format

 

 

were modified slightly to be able to use them for test purposes. The code translated with Delphi2Cpp compiles and works without additional manual processing without faults.

 

bool FormatTest1()

{

  bool result = false;

  result = true;

  // Just 1 data item

  result = result && (Format(L"%s", OpenArray<TVarRec>(String(L"Hello"))) == L"Hello");

  

  // A mix of literal text and a data item

  result = result && (Format(L"String = %s", OpenArray<TVarRec>(String(L"Hello"))) == L"String = Hello");

   //ShowMessage('');

  

  // Examples of each of the data types

  result = result && (Format(L"Decimal          = %d", OpenArray<TVarRec>(-123)) == L"Decimal          = -123");

  result = result && (Format(L"Exponent         = %e", OpenArray<TVarRec>(12345.678L)) == L"Exponent         = 1,23456780000000E+004");

  result = result && (Format(L"Fixed            = %f", OpenArray<TVarRec>(12345.678L)) == L"Fixed            = 12345,68");

  result = result && (Format(L"General          = %g", OpenArray<TVarRec>(12345.678L)) == L"General          = 12345,678");

  result = result && (Format(L"Number           = %n", OpenArray<TVarRec>(12345.678L)) == L"Number           = 12.345,68");

  result = result && (Format(L"Money            = %m", OpenArray<TVarRec>(12345.678L)) == L"Money            = 12.345,68 €");

   // makes no sense under C#

   // result := result and (Format('Pointer          = %p', [addr(text)]) = 'Pointer          = 0069FC90');

  result = result && (Format(L"String           = %s", OpenArray<TVarRec>(String(L"Hello"))) == L"String           = Hello");

  result = result && (Format(L"Unsigned decimal = %u", OpenArray<TVarRec>(123)) == L"Unsigned decimal = 123");

  result = result && (Format(L"Hexadecimal      = %x", OpenArray<TVarRec>(140)) == L"Hexadecimal      = 8C");

  return result;

}

  

bool FormatTest2()

{

  bool result = false;

  result = true;

  // The width value dictates the output size

  // with blank padding to the left

  // Note the <> characters are added to show formatting

  result = result && (Format(L"Padded decimal    = <%7d>", OpenArray<TVarRec>(1234)) == L"Padded decimal    = <   1234>");

  

  // With the '-' operator, the data is left justified

  result = result && (Format(L"Justified decimal = <%-7d>", OpenArray<TVarRec>(1234)) == L"Justified decimal = <1234   >");

  

  // The precision value forces 0 padding to the desired size

  result = result && (Format(L"0 padded decimal  = <%.6d>", OpenArray<TVarRec>(1234)) == L"0 padded decimal  = <001234>");

  

  // A combination of width and precision

  // Note that width value precedes the precision value

  result = result && (Format(L"Width + precision = <%8.6d>", OpenArray<TVarRec>(1234)) == L"Width + precision = <  001234>");

  

  // The index value allows the next value in the data array

  // to be changed

  result = result && (Format(L"Reposition after 3 strings = %s %s %s %1:s %s", OpenArray<TVarRec>(String(L"Zero"), String(L"One"), String(L"Two"), String(L"Three"))) == L"Reposition after 3 strings = Zero One Two One Two");

  

  // One or more of the values may be provided by the

  // data array itself. Note that testing has shown that an *

  // for the width parameter can yield EConvertError.

  result = result && (Format(L"In line           = <%10.4d>", OpenArray<TVarRec>(1234)) == L"In line           = <      1234>");

  result = result && (Format(L"Part data driven  = <%*.4d>", OpenArray<TVarRec>(10, 1234)) == L"Part data driven  = <      1234>");

  result = result && (Format(L"Data driven       = <%*.*d>", OpenArray<TVarRec>(10, 4, 1234)) == L"Data driven       = <      1234>");

  return result;

}

  

bool FloatToStrTest1()

{

  bool result = false;

  long double amount1 = 0.0L;

  long double amount2 = 0.0L;

  long double amount3 = 0.0L;

  result = true;

  amount1 = 1234567890.123456789L;  // High precision number

  amount2 = 1234567890123456.123L;  // High mantissa digits

  amount3 = 1E100L;                 // High value number

  result = result && (FloatToStr(amount1) == L"1234567890,12346");

  result = result && (FloatToStr(amount2) == L"1,23456789012346E15");

  result = result && (FloatToStr(amount3) == L"1E100");

  return result;

}

  

bool FormatFloatTest1()

{

  bool result = false;

  long double flt = 0.0L;

  result = true;

  // Set up our floating point number

  flt = 1234.567L;

  

  // Display a sample value using all of the format options

  

  // Round out the decimal value

  result = result && (FormatFloat(L"#####", flt) == L"1235");

  result = result && (FormatFloat(L"00000", flt) == L"01235");

  result = result && (FormatFloat(L"0", flt) == L"1235");

  result = result && (FormatFloat(L"#,##0", flt) == L"1.235");

  result = result && (FormatFloat(L",0", flt) == L"1.235");

  

  // Include the decimal value

  result = result && (FormatFloat(L"0.####", flt) == L"1234,567");

  result = result && (FormatFloat(L"0.0000", flt) == L"1234,5670");

 

  

  // Scientific format

  result = result && (FormatFloat(L"0.0000000E+00", flt) == L"1,2345670E+03");

  result = result && (FormatFloat(L"0.0000000E-00", flt) == L"1,2345670E03");

  result = result && (FormatFloat(L"#.#######E-##", flt) == L"1,234567E3");

  

  // Include freeform text

  result = result && (FormatFloat(L"\"Value = \"0.0", flt) == L"Value = 1234,6");

 

  

  // Different formatting for negative numbers

  result = result && (FormatFloat(L"0.0", -1234.567) == L"-1234,6");

  result = result && (FormatFloat(L"0.0 \"CR\";0.0 \"DB\"", -1234.567) == L"1234,6 DB");

  result = result && (FormatFloat(L"0.0 \"CR\";0.0 \"DB\"", 1234.567L) == L"1234,6 CR");

  

  // Different format for zero value

  result = result && (FormatFloat(L"0.0", 0.0L) == L"0,0");

  result = result && (FormatFloat(L"0.0;-0.0;\"Nothing\"", 0.0L) == L"Nothing");

  return result;

}

  

bool FormatTest()

{

  bool result = false;

  result = true;

  result = result && FormatTest1();

  result = result && FormatTest2();

  result = result && FloatToStrTest1();

  result = result && FormatFloatTest1();

  return result;

}



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content