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 the translated code works nearly perfect.

 

#include "pch.h"

 

 

#include "d7_formatting.h"

#include "System.SysUtils.h"

#include "d2c_sysvariant.h"

#include "d2c_openarray.h"

#include "OnLeavingScope.h"

 

using namespace std;

using namespace d2c_system;

using namespace System;

using namespace System::Sysutils;

 

namespace D7_formatting

{

 

 

 

bool CheckBuildMessage()

{

  bool result = false;

  String MessageText;

  MessageText = Format(L"Build %s produced %d artifacts", ArrayOfConst(String(L"linux-x64"), 18));

  result = MessageText == L"Build linux-x64 produced 18 artifacts";

  return result;

}

 

bool CheckIdentifierLayout()

{

  bool result = false;

  result = true;

  if(Format(L"ID-%6.4d", ArrayOfConst(73)) != L"ID-  0073")

    result = false;

  if(Format(L"HEX-%4.4x", ArrayOfConst(0x2A)) != L"HEX-002A")

    result = false;

  if(Format(L"%1:s/%0:s/%1:s", ArrayOfConst(String(L"source"), String(L"target"))) != L"target/source/target")

    result = false;

  return result;

}

 

bool CheckFloatingPointText()

{

  bool result = false;

  TFormatSettings SavedSettings = {};

  double Measurement = 0.0;

  SavedSettings = FormatSettings;

  {

    auto olsLambda = onLeavingScope([&] 

    {

      FormatSettings = SavedSettings;

    });

    FormatSettings.DecimalSeparator = L',';

    FormatSettings.ThousandSeparator = L'.';

    Measurement = 4312.24;

    result = true;

    if(FloatToStr(12.5L) != L"12,5")

      result = false;

    if(FloatToStr(1E40L) != L"1E40")

      result = false;

    if(FormatFloat(L"#,##0.00", Measurement) != L"4.312,24")

      result = false;

    if(FormatFloat(L"000000", Measurement) != L"004312")

      result = false;

    if(FormatFloat(L"0.000", Measurement) != L"4312,240")

      result = false;

    if(FormatFloat(L"0.000E+00", Measurement) != L"4,312E+03")

      result = false;

    if(FormatFloat(L"0.0 \"up\";0.0 \"down\"", -Measurement) != L"4312,2 down")

      result = false;

    if(FormatFloat(L"0.0;-0.0;\"idle\"", 0.0L) != L"idle")

      result = false;

  }

  return result;

}

 

bool RunFormattingChecks()

{

  bool result = false;

  result = true;

  if(!CheckBuildMessage())

    result = false;

  if(!CheckIdentifierLayout())

    result = false;

  if(!CheckFloatingPointText())

    result = false;

  return result;

}

 

 

}  // namespace D7_formatting

 

 

This code was generated from:

 

 

unit d7_formatting;

 

interface

 

function RunFormattingChecks: Boolean;

 

implementation

 

uses

  System.SysUtils;

 

function CheckBuildMessage: Boolean;

var

  MessageText: string;

begin

  MessageText := Format('Build %s produced %d artifacts',

    ['linux-x64', 18]);

 

  Result := MessageText =

    'Build linux-x64 produced 18 artifacts';

end;

 

function CheckIdentifierLayout: Boolean;

begin

  Result := True;

 

  if Format('ID-%6.4d', [73]) <> 'ID-  0073' then

    Result := False;

 

  if Format('HEX-%4.4x', [$2A]) <> 'HEX-002A' then

    Result := False;

 

  if Format('%1:s/%0:s/%1:s', ['source', 'target']) <>

    'target/source/target' then

    Result := False;

end;

 

function CheckFloatingPointText: Boolean;

var

  SavedSettings: TFormatSettings;

  Measurement: Double;

begin

  SavedSettings := FormatSettings;

  try

    FormatSettings.DecimalSeparator := ',';

    FormatSettings.ThousandSeparator := '.';

    Measurement := 4312.24;

 

    Result := True;

 

    if FloatToStr(12.5) <> '12,5' then

      Result := False;

 

    if FloatToStr(1E40) <> '1E40' then

      Result := False;

 

    if FormatFloat('#,##0.00', Measurement) <> '4.312,24' then

      Result := False;

 

    if FormatFloat('000000', Measurement) <> '004312' then

      Result := False;

 

    if FormatFloat('0.000', Measurement) <> '4312,240' then

      Result := False;

 

    if FormatFloat('0.000E+00', Measurement) <> '4,312E+03' then

      Result := False;

 

    if FormatFloat('0.0 "up";0.0 "down"', -Measurement) <>

      '4312,2 down' then

      Result := False;

 

    if FormatFloat('0.0;-0.0;"idle"', 0) <> 'idle' then

      Result := False;

  finally

    FormatSettings := SavedSettings;

  end;

end;

 

function RunFormattingChecks: Boolean;

begin

  Result := True;

 

  if not CheckBuildMessage then

    Result := False;

 

  if not CheckIdentifierLayout then

    Result := False;

 

  if not CheckFloatingPointText then

    Result := False;

end;

 

end.

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content