#================================================================--
# Design Unit  : cew testbench for stack
#
# File Name    : tb.cew
#
# Purpose      : unit testing
#
# Note         :
#
# Limitations  :
#
# Errors       : none known
#
# Modules      : stack_adt::stack
#
# Dependences  : cew
#
# Author       : Peter Walsh, Vancouver Island University
#
# System       : Perl (Linux)
#
#------------------------------------------------------------------
# Revision List
# Version      Author  Date    Changes
# 1.0          PW      Oct 12  New version
#================================================================--

$|=1;
use strict;
use warnings;











use lib '../';
use stack_adt::stack;
use exc::exception;
use Try::Tiny;

my $cew_Test_Count=0;
          my $cew_Error_Count=0;


# Local Function Load (s, n);
# pushes n values on the stack from
# the sequence 10, 100, 1000 ... 
# note: no exception checking

sub load {
   my $s=shift @_;
   my $n=shift @_;
   
   for (my $i=0; $i<$n; $i++) {
      $s->push(($i+1)*10);
   }
}

#############
# empty stack
#############

my $stack0=stack_adt::stack->new(10);
cew_Ecase(60,$stack0->top(), "empty")
cew_Ecase(61,$stack0->pop(), "empty")

#################
# half full stack
#################

my $stack1=stack_adt::stack->new(10);
$cew_Test_Count=$cew_Test_Count+1;
          load($stack1, 5) ;
          if (($stack1->top()) != (50)) {
             $cew_Error_Count=$cew_Error_Count+1;
             print("Test Case ERROR (Ncase) in script at line number ", 68, "\n");
             print("Actual Value is ", $stack1->top(), " \n");
             print("Expected Value is ", 50, "\n");
          }

$cew_Test_Count=$cew_Test_Count+1;
          $stack1->pop() ;
          if (($stack1->top()) != (40)) {
             $cew_Error_Count=$cew_Error_Count+1;
             print("Test Case ERROR (Ncase) in script at line number ", 69, "\n");
             print("Actual Value is ", $stack1->top(), " \n");
             print("Expected Value is ", 40, "\n");
          }

$cew_Test_Count=$cew_Test_Count+1;
          $stack1->pop() ;
          if (($stack1->top()) != (30)) {
             $cew_Error_Count=$cew_Error_Count+1;
             print("Test Case ERROR (Ncase) in script at line number ", 70, "\n");
             print("Actual Value is ", $stack1->top(), " \n");
             print("Expected Value is ", 30, "\n");
          }


#################
# full stack
#################

my $stack2=stack_adt::stack->new(10);
$cew_Test_Count=$cew_Test_Count+1;
          load($stack2, 10) ;
          if (($stack2->top()) != (100)) {
             $cew_Error_Count=$cew_Error_Count+1;
             print("Test Case ERROR (Ncase) in script at line number ", 77, "\n");
             print("Actual Value is ", $stack2->top(), " \n");
             print("Expected Value is ", 100, "\n");
          }

cew_Ecase(78,$stack2->push(110), "full")
$cew_Test_Count=$cew_Test_Count+1;
           ;
          if (($stack2->top()) != (100)) {
             $cew_Error_Count=$cew_Error_Count+1;
             print("Test Case ERROR (Ncase) in script at line number ", 79, "\n");
             print("Actual Value is ", $stack2->top(), " \n");
             print("Expected Value is ", 100, "\n");
          }

$cew_Test_Count=$cew_Test_Count+1;
          $stack2->pop() ;
          if (($stack2->top()) != (90)) {
             $cew_Error_Count=$cew_Error_Count+1;
             print("Test Case ERROR (Ncase) in script at line number ", 80, "\n");
             print("Actual Value is ", $stack2->top(), " \n");
             print("Expected Value is ", 90, "\n");
          }


################
# stress test
################

my $stack3=stack_adt::stack->new(100);

for (my $i=0; $i<100; $i++) {
   $cew_Test_Count=$cew_Test_Count+1;
          $stack3->push($i) ;
          if (($stack3->top()) != ($i)) {
             $cew_Error_Count=$cew_Error_Count+1;
             print("Test Case ERROR (Ncase) in script at line number ", 89, "\n");
             print("Actual Value is ", $stack3->top(), " \n");
             print("Expected Value is ", $i, "\n");
          }

}

for (my $i=99; $i>=0; $i--) {
   $cew_Test_Count=$cew_Test_Count+1;
           ;
          if (($stack3->top()) != ($i)) {
             $cew_Error_Count=$cew_Error_Count+1;
             print("Test Case ERROR (Ncase) in script at line number ", 93, "\n");
             print("Actual Value is ", $stack3->top(), " \n");
             print("Expected Value is ", $i, "\n");
          }

   $cew_Test_Count=$cew_Test_Count+1;
          $stack3->pop() ;
          if ((0) != (0)) {
             $cew_Error_Count=$cew_Error_Count+1;
             print("Test Case ERROR (Ncase) in script at line number ", 94, "\n");
             print("Actual Value is ", 0, " \n");
             print("Expected Value is ", 0, "\n");
          }

}
   
print("\n**********Summary**********\n");
          print("Total number of test cases = ", $cew_Test_Count, "\n");
          print("Total number of test cases in error = ", $cew_Error_Count, "\n");

