The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,842 other followers

In `System.SysUtils`, `class function TCardinalHelper.Parse` throws errors for valid Cardinal values

Posted by jpluimers on 2016/10/05

Oh nice System.SysUtils.TCardinalHelper.Parse:

class function TCardinalHelper.Parse(const S: string): Cardinal;
begin
  Result := StrToInt(S);
end;

Which means you get this nice EConvertError with message ''4294967295' is not a valid integer value'. with this simple test (which doesn’t even reach the Assert):

uses
  System.SysUtils;

procedure Cardinal_Parse_High_Cardinal_Succeeds();
var
  Expected: Cardinal;
  Value: string;
  Actual: Cardinal;
begin
  Expected := High(Cardinal);
  Value := Expected.ToString();
  Actual := Cardinal.Parse(Value);
  Assert(Expected = Actual);
end;

So I write some unit tests (see below) of which helpers for these types fail in one way or the other:

  • Cardinal
  • NativeUInt
  • Single
  • Double
  • Extended

These work for the boundary cases:

  • SmallInt
  • ShortInt
  • Integer
  • Int64
  • NativeInt
  • Byte
  • Word
  • UInt64
  • Boolean
  • ByteBool
  • WordBool
  • LongBool

 

–jeroen

via: Oh nice, in System.SysUtils: “` class function TCardinalHelper.Parse(const…

5 Responses to “In `System.SysUtils`, `class function TCardinalHelper.Parse` throws errors for valid Cardinal values”

  1. If you look at the code for TCardinalHelper.Parse, it is not really surprise that anything above MaxInt gets rejected:

    class function TCardinalHelper.Parse(const S: string): Cardinal;
    begin
      Result := StrToInt(S);
    end;
    
  2. Wodzu said

    Amazing, Embarcadero haven’t wrote tests for their own stuff… good job Jeroen!

  3. Reported to QualityPortal as RSP-16026 “Parse() methods of Integral helper classes raise errors for valid values”, https://quality.embarcadero.com/browse/RSP-16026

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: