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

program d092( output );

var
   k : integer;

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

   k := 1;
   writeln( '101 - 1 is ', 101 - side:1);
   if k <> 2 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) ); {!!! overflows}
   writeln( 'fails: overflow in minus operator error is not detected' );
end. { d092 }

