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

program d093( output );

var
   k : integer;

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

   k := 101;
   writeln( '101 - 1 is ', side-1:1);
   if k <> 102 then
      writeln( 'minus operator for debug has side effects' );

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

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

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

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

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

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

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

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

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

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

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

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

   k := -maxint;
   writeln( '-maxint - 1 is ', k-1 );  { might or might not fail ... }
   k := k-1;
   writeln( k:1, ' - 1 is ', k-1 );    { !!! otherwise, this should fail }
   writeln( 'fails: overflow in minus operator error is not detected' );
end. { d093 }

