关于ada需要如何添加Global、Depends、Pre、Post 或 Contract_Cases

img

img


如果 I 和 J 在堆栈的当前大小范围内,该过程将只交换值; 否则,堆栈不变。
procedure Swap(I, J: in Pointer_Range)
with
请问如何,通过添加 Global、Depends、Pre、Post 或 Contract_Cases 方面来完成规范,以指定过程的预期行为。

您可以通过添加一个Global断言来指定过程的预期行为。Global断言是一种描述过程可能影响的所有对象的特殊断言。

例如,您可以添加以下Global断言来指定该过程的预期行为:

procedure Swap(I, J: in Pointer_Range)
with
  Global => (if I and J are within the current size range of the stack then the values at I and J are swapped, otherwise the stack remains unchanged)

还可以使用Depends断言来指定过程的预期行为。Depends断言描述了一个过程所依赖的条件。例如,您可以添加以下Depends断言来指定该过程的预期行为:

procedure Swap(I, J: in Pointer_Range)
with
  Depends => (the stack must have at least two elements)

您还可以使用Pre和Post断言来指定过程的预期行为。Pre断言描述了在过程开始时应该满足的条件,而Post断言描述了过程结束时应该满足的条件。例如,您可以添加以下Pre和Post断言来指定该过程的预期行为:

procedure Swap(I, J: in Pointer_Range)
with
  Pre  => (I and J are within the current size range of the stack),
  Post => (if I and J are within the current size range of the stack then the values at I and J are swapped, otherwise the stack remains unchanged)

此外,您还可以通过使用Contract_Cases断言来指定过程的预期行为。Contract_Cases断言允许您指定多种不同的预期行为

下面是一个完整的规范示例,其中包括了上述所有方面:

procedure Swap(I, J: in Pointer_Range)
with
   Global => (Stack: Stack_Type),
   Depends => (Is_Full, Is_Empty, Push, Pop),
   Pre => (not Is_Full(Stack) and not Is_Empty(Stack)),
   Post => (Stack'Old = Stack),
   Contract_Cases => (
      ("Test case 1: I and J within stack size range",
       (I in 1..Max_Size and J in 1..Max_Size),
       (Stack'Old = Stack)),
      ("Test case 2: I or J outside stack size range",
       (I not in 1..Max_Size or J not in 1..Max_Size),
       (Stack'Old = Stack))
   )
is
   Temp: Integer;
begin
   if (I in 1..Max_Size and J in 1..Max_Size) then
      Temp := Pop(Stack);
      Push(Stack, Pop(Stack));
      Push(Stack, Temp);
   end if;
end Swap;