Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Borland Delphi > Lesson 2: Integ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 3698 of 3851
Post > Topic >>

Lesson 2: Integer Range Checking (Perfection is the key ! :))

by "Skybuck Flying" <BloodyShame@[EMAIL PROTECTED] > Apr 17, 2008 at 01:56 AM

See comments or see website:

http://members.home.nl/hbthouppermans/D2007Analysis/Index.htm

(Me got pain in me back so I'll keep it short ;):))

// *** Begin of AnalysisLesson2.dpr ***

program AnalysisLesson2;

{$APPTYPE CONSOLE}

{

Version 0.01 created on 17 april 2008 by Skybuck Flying

Lesson 2, Integer Range Checking.

Conclusions:

Program Started

Range Checking must be on then these eight will be detected:

Int8 underflow detected: ERangeError: Range check error
Int8 overflow detected: ERangeError: Range check error

Uint8 underflow detected: ERangeError: Range check error
Uint8 overflow detected: ERangeError: Range check error

Int16 underflow detected: ERangeError: Range check error
Int16 overflow detected: ERangeError: Range check error

Uint16 underflow detected: ERangeError: Range check error
Uint16 overflow detected: ERangeError: Range check error

Overflow Checking must be on then these eight will be detected:

Int32 underflow detected: EIntOverflow: Integer overflow
Int32 overflow detected: EIntOverflow: Integer overflow

Uint32 underflow detected: EIntOverflow: Integer overflow
Uint32 overflow detected: EIntOverflow: Integer overflow

Int64 underflow detected: EIntOverflow: Integer overflow
Int64 overflow detected: EIntOverflow: Integer overflow

Uint64 underflow detected: EIntOverflow: Integer overflow
Uint64 overflow detected: EIntOverflow: Integer overflow

Program finished

(Little bit strange, range vs overflow checking, why not all the same (?)
Oh well at least it's working and all detected when both options on)

16 out of 16 tests passed.

Score: 10 out of 10.

Excellent !

This time I can say like Reptile:

"Perfection is the key !"

And so it is ! :)

And so say we all ! LOL.

}

uses
  SysUtils;

type
 Tint8 = shortint;
 Tuint8 = byte;

 Tint16 = smallint;
 Tuint16 = word;

 Tint32 = integer;
 Tuint32 = longword;

 Tint64 = int64;
// Tuint64 = int64; // uint64 is buggy, -1 point for delphi 2007.
 Tuint64 = uint64; // using buggy uint64 implementation for now just to
test 
ranges.

procedure Main;
var
 vMinInt8 : Tint8;
 vMaxInt8 : Tint8;

 vMinUint8 : Tuint8;
 vMaxUint8 : Tuint8;

 vMinInt16 : Tint16;
 vMaxInt16 : Tint16;

 vMinUint16 : Tuint16;
 vMaxUint16 : Tuint16;

 vMinInt32 : Tint32;
 vMaxInt32 : Tint32;

 vMinUint32 : Tuint32;
 vMaxUint32 : Tuint32;

 vMinInt64 : Tint64;
 vMaxInt64 : Tint64;

 vMinUint64 : Tuint64;
 vMaxUint64 : Tuint64;

begin
 writeln('Program Started');
 writeln;

 // test int8
 try
  vMinInt8 := -128;
  vMinInt8 := vMinInt8 - 1;
  writeln('vMinInt8: ', vMinInt8 );
  writeln('Int8 underflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Int8 underflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 try
  vMaxInt8 := 127;
  vMaxInt8 := vMaxInt8 + 1;
  writeln('vMaxInt8: ', vMaxInt8 );
  writeln('Int8 overflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Int8 overflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 writeln;

 // test uint8
 try
  vMinUint8 := 0;
  vMinUint8 := vMinUint8 - 1;
  writeln('vMinUint8: ', vMinUint8 );
  writeln('Uint8 underflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Uint8 underflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 try
  vMaxUint8 := 255;
  vMaxUint8 := vMaxUint8 + 1;
  writeln('vMaxUint8: ', vMaxUint8 );
  writeln('Uint8 overflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Uint8 overflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 writeln;

 // test int16
 try
  vMinInt16 := -32768;
  vMinInt16 := vMinInt16 - 1;
  writeln('vMinInt16: ', vMinInt16 );
  writeln('Int16 underflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Int16 underflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 try
  vMaxInt16 := 32767;
  vMaxInt16 := vMaxInt16 + 1;
  writeln('vMaxInt16: ', vMaxInt16 );
  writeln('Int16 overflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Int16 overflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 writeln;

 // test uint16
 try
  vMinUint16 := 0;
  vMinUint16 := vMinUint16 - 1;
  writeln('vMinUint16: ', vMinUint16 );
  writeln('Uint16 underflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Uint16 underflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 try
  vMaxUint16 := 65535;
  vMaxUint16 := vMaxUint16 + 1;
  writeln('vMaxUint16: ', vMaxUint16 );
  writeln('Uint16 overflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Uint16 overflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 writeln;

 // test int32
 try
  vMinInt32 := -2147483648;
  vMinInt32 := vMinInt32 - 1;
  writeln('vMinInt32: ', vMinInt32 );
  writeln('Int32 underflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Int32 underflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 try
  vMaxInt32 := 2147483647;
  vMaxInt32 := vMaxInt32 + 1;
  writeln('vMaxInt32: ', vMaxInt32 );
  writeln('Int32 overflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Int32 overflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 writeln;

 // test uint32
 try
  vMinUint32 := 0;
  vMinUint32 := vMinUint32 - 1;
  writeln('vMinUint32: ', vMinUint32 );
  writeln('Uint32 underflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Uint32 underflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 try
  vMaxUint32 := 4294967295;
  vMaxUint32 := vMaxUint32 + 1;
  writeln('vMaxUint32: ', vMaxUint32 );
  writeln('Uint32 overflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Uint32 overflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 writeln;

 // test int64
 try
  vMinInt64 := -9223372036854775808;
  vMinInt64 := vMinInt64 - 1;
  writeln('vMinInt64: ', vMinInt64 );
  writeln('Int64 underflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Int64 underflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 try
  vMaxInt64 := 9223372036854775807;
  vMaxInt64 := vMaxInt64 + 1;
  writeln('vMaxInt64: ', vMaxInt64 );
  writeln('Int64 overflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Int64 overflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 writeln;

 // test uint64
 try
  vMinUint64 := 0;
  vMinUint64 := vMinUint64 - 1;
  writeln('vMinUint64: ', vMinUint64 );
  writeln('Uint64 underflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Uint64 underflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 try
  vMaxUint64 := 18446744073709551615;
  vMaxUint64 := vMaxUint64 + 1;
  writeln('vMaxUint64: ', vMaxUint64 );
  writeln('Uint64 overflow NOT detected');
 except
  on E:Exception do
  begin
   writeln('Uint64 overflow detected: ' + E.Classname, ': ', E.Message);
  end;
 end;

 writeln;

 writeln('Program finished');
end;


begin
 try
  Main;
 except
  on E:Exception do
   Writeln(E.Classname, ': ', E.Message);
 end;
 readln;
end.

// *** End of AnalysisLesson2.dpr ***

Bye,
  Skybuck.
 




 2 Posts in Topic:
Lesson 2: Integer Range Checking (Perfection is the key ! :))
"Skybuck Flying"  2008-04-17 01:56:53 
Re: Lesson 2: Integer Range Checking (Perfection is the key ! :)
"Skybuck Flying"  2008-04-17 02:00:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 13:51:01 CDT 2008.