home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / ADA / LOVELACE / tegensta.adb < prev    next >
Text File  |  1996-10-01  |  3KB  |  111 lines

  1. with Stack_Int, Text_IO; use Stack_Int, Text_IO;
  2.  
  3. procedure Test_Generic_Stack is
  4.  -- Test the generic "Generic_Stack" using "Stack_Int" as an instantiation.
  5.  -- If all is okay, print "No failures."
  6.  
  7.  Stack1, Stack2 : Stack;
  8.  Dummy : Integer;
  9.  
  10.  Count_Of_Failures : Integer := 0;
  11.  
  12.  procedure Failure(Error_Message : in String) is
  13.    -- call if there was a failure.
  14.  begin
  15.    Put("Failure: ");
  16.    Put_Line(Error_Message);
  17.    Count_Of_Failures := Count_Of_Failures + 1;
  18.  end Failure;
  19.  
  20.  procedure Assert(Claim : in Boolean; Error_Message : in String) is
  21.    -- If Claim is not true, print the error message.
  22.  begin
  23.    if not Claim then
  24.      Failure(Error_Message);
  25.    end if;
  26.  end Assert;
  27.  
  28. begin
  29.  -- Put the Stack type through a number of tests.
  30.  
  31.  Assert(Is_Empty(Stack1), "Stack does not start empty");
  32.  Assert(Stack1 = Stack2,  "Stacks start different");
  33.  
  34.  Push(Stack1, 1);
  35.  Push(Stack1, 2);
  36.  Push(Stack1, 3);
  37.  
  38.  Assert(Stack1 /= Stack2,   "Stacks same after pushing");
  39.  Assert(Length(Stack1) = 3, "Stack1 not length 3"); 
  40.  Assert(Length(Stack2) = 0, "Stack2 not length 0"); 
  41.  
  42.  Swap(Stack1, Stack2);
  43.  Assert(Length(Stack1) = 0, "Post-swap: Stack1 not length 0"); 
  44.  Assert(Length(Stack2) = 3, "Post-swap: Stack2 not length 3"); 
  45.  
  46.  Swap(Stack1, Stack2);
  47.  Assert(Length(Stack1) = 3, "Double swap: Stack1 not length 3"); 
  48.  Assert(Length(Stack2) = 0, "Double swap: Stack2 not length 0"); 
  49.  
  50.  Stack2 := Stack1;
  51.  Assert(Length(Stack2) = 3, "Stack2 not length 3 after assignment!"); 
  52.  Assert(Stack1 = Stack2,    "Stacks differ after assignment");
  53.  
  54.  Pop(Stack1, Dummy);
  55.  Assert(Dummy = 3, "Did't get 3 back from Stack1");
  56.  Assert(Stack1 /= Stack2, "Stacks same after popping a value from Stack1");
  57.  
  58.  Pop(Stack1, Dummy);
  59.  Assert(Dummy = 2, "Did't get 2 back from Stack1");
  60.  
  61.  Pop(Stack1, Dummy);
  62.  Assert(Dummy = 1, "Did't get 1 back from Stack1");
  63.  Assert(Is_Empty(Stack1), "Stack1 does not end up empty");
  64.  Assert(not Is_Empty(Stack2), "Stack2 unexpectedly empty");
  65.  Assert(Stack1 /= Stack2, "Stacks same after popping just Stack1");
  66.  
  67.  Pop(Stack2, Dummy);
  68.  Assert(Dummy = 3, "Did't get 3 back from Stack2");
  69.  Assert(Stack1 /= Stack2, "Stacks same after popping a value from Stack2");
  70.  
  71.  Pop(Stack2, Dummy);
  72.  Assert(Dummy = 2, "Did't get 2 back from Stack2");
  73.  
  74.  Pop(Stack2, Dummy);
  75.  Assert(Dummy = 1, "Did't get 1 back from Stack2");
  76.  Assert(Is_Empty(Stack2), "Stack2 does not end up empty");
  77.  Assert(Stack1 = Stack2, "Empty stacks differ");
  78.  
  79.  -- Empty an empty stack.
  80.  Empty(Stack2);
  81.  Assert(Is_Empty(Stack2), "Empty on Empty failed");
  82.  
  83.  -- Empty with one element.
  84.  Push(Stack2, 1);
  85.  Empty(Stack2);
  86.  Assert(Is_Empty(Stack2), "Empty on one element failed.");
  87.  
  88.  -- Empty with two elements.
  89.  Push(Stack2, 1);
  90.  Push(Stack2, 2);
  91.  Empty(Stack2);
  92.  Assert(Is_Empty(Stack2), "Empty on two elements failed.");
  93.  
  94.  Push(Stack2, 12);
  95.  Top(Stack2, Dummy);
  96.  Assert(Dummy = 12, "Top failed.");
  97.  Assert(Length(Stack2) = 1, "Top didn't leave one item on the stack.");
  98.  
  99.  Empty(Stack1);
  100.  Assert(Is_Empty(Stack1), "Last Empty of Stack1 failed");
  101.  Empty(Stack2);
  102.  Assert(Is_Empty(Stack2), "Last Empty of Stack2 failed");
  103.  
  104.  -- All done.
  105.  if (Count_Of_Failures = 0) then
  106.    Put_Line("No failures.");
  107.  else
  108.    Put_Line("Failures Occurred.");
  109.  end if;
  110. end Test_Generic_Stack;
  111.