{: test that overflow on addition causes a fatal error }

program d091( output );

var
   k : integer;

function side : integer;
begin
   side := k;
   k := k+1;
end;
begin {d091}

   k := 1;
   writeln( '99 + 1 is ', 99 + side:1);
   if k <> 2 then
      writeln( 'add operator for debug has side effects' );

   k := -maxint + maxint;
   if k <> 0 then
      writeln( 'addition error line ', __LINE__:1 );

   k := maxint + (-maxint);
   if k <> 0 then
      writeln( 'addition error line ', __LINE__:1 );

   k := maxint - 1;
   k := k + (-maxint);
   if k <> -1 then
      writeln( 'addition error line ', __LINE__:1 );

   k := maxint-1;
   if k+1 <> maxint then
      writeln( 'addition error line ', __LINE__:1 );

   k := -maxint + 1;
   k := k + maxint;
   if k <> 1 then
      writeln( 'addition error line ', __LINE__:1 );

   k := 1 + (-maxint);
   if k+maxint <> 1 then
      writeln( 'addition error line ', __LINE__:1 );

   {check for addition result < -maxint ....}
   k := -maxint div 2 + (-maxint div 2);
   if odd(maxint) and (k <> -maxint+1) then
      writeln( 'addition error line ', __LINE__:1 );

   if not odd(maxint) and (k <> -maxint)  then
      writeln( 'addition error line ', __LINE__:1 );

   k := -maxint + 1;
   if k-1 <> -maxint then
      writeln( 'addition error line ', __LINE__:1 );

   k := 1 + (-maxint);
   if k-1 <> -maxint then
      writeln( 'addition error line ', __LINE__:1 );

   k := -maxint div 2 + (-maxint) div 2; {xxx}
   if odd(maxint) and (k <> -maxint+1) then
      writeln( 'addition error line ', __LINE__:1 );

   if not odd(maxint) and (k <> -maxint)  then
      writeln( 'addition error line ', __LINE__:1 );

   k := -maxint + 1;
   if -k+1 <> maxint then
      writeln( 'addition error line ', __LINE__:1 );

   k := -maxint;
   writeln( '-maxint + (-1) is ', k+(-1) ); { this might pass ... }
   k := k-1;
   writeln( k:1, ' + (-1) is ', k+(-1) ); { !!! ... if so, this should fail }

   writeln( 'fails: overflow in add operator error is not detected' );
end. { d091 }

