/*
  testll.c

  Walter Goodwater

  CSc 300: Copyright Violation Lab
*/


#include <unistd.h>
#include "linklist.h"


int print_check_list_delete( Nodeptr head, char * deleted )
{
  Nodeptr current;
  int result;

  result = 1;
  current = head;

  printf( "  " );

  if( head == NULL )
  {
    printf( "EMPTY!\n" );
    return result;
  }
  
  while( current != NULL )
  {
    printf( current->name );
    if( strcmp( current->name, deleted ) == 0 )
    {
      result = 0;
    }
    
    if( current->next != NULL )
    {
      printf( " -> " );
    }
    current = current->next; 
  }
  printf( "\n" );
  return result;
}

  
  

  

int print_check_list_insert( Nodeptr head )
{
  Nodeptr current;
  int result;
  int i;

  result = 1;
  current = head;

  printf( "  " );
  
  for( i = 0; current != NULL; i++ )
  {
    if ( i == 0 && result == 1 )
    {
      if( strcmp( current->name, "Sue" ) != 0 )
	result = 0;
    }
    else if ( i == 1 && result == 1 )
    {
      if( strcmp( current->name, "Bob" ) != 0 )
	result = 0;
    }

    else if ( i == 2 && result == 1 )
    {
      if( strcmp( current->name, "Dr. Turner" ) != 0 )
        result = 0;
    }
    else if ( i == 3 && result == 1 )
    {
      if( strcmp( current->name, "Tom" ) != 0 )
        result = 0;
    }
    else if ( i > 3 )
    {
      result = 0;
    }
   
    printf( current->name );
    if( current->next != NULL )
    {
      printf( " -> " );
    }
    current = current->next;
  }
  printf( "\n" );
  return result;
  
}


int main( int argc, char * argv[] )
{
  Nodeptr head;
  int test_num;
  
  head = NULL;

  printf( "Testing linked list.......\n" );

  sleep( 1 );
  
  /* Inserts */
  printf( "\n\nTesting Insert(): \n" );
  printf( " inserting: Bob, age 51 \n" );
  head = insert( "Bob", 51, head );
  printf( " inserting: Tom, age 10 \n" );
  head = insert( "Tom", 10, head );
  printf( " inserting: Sue, age 99 \n" );
  head = insert( "Sue", 99, head );
  printf( " inserting: Dr. Turner, age 25 \n" );
  head = insert( "Dr. Turner", 25, head );

  /* Test inserts */
  printf( " list should be:\n" );
  printf( "  Sue -> Bob -> Dr. Turner -> Tom\n" );
  printf( " your list is:\n" );
  test_num = print_check_list_insert( head );

  if( !test_num )
  {
    printf( " INSERTION FAILED!\n" );
    exit( -1 );
  }
  else
  {
    printf( " INSERTION PASSED!\n" );
  }

  sleep( 1 );
  
  /* Removes */
  printf( "\n\nTesting delete(): \n" );

  printf( " deleting: Bob \n" );
  head = delete( "Bob", head );
  printf( " list should be:\n" );
  printf( "  Sue -> Dr. Turner -> Tom\n" );
  printf( " your list is:\n" );
  test_num = print_check_list_delete( head, "Bob" );
  if( !test_num )
  {
    printf( " DELETION FAILED!\n" );
    exit( -1 );    
  }
  else
  {
    printf( " DELETION PASSED!\n" );  
  }
  sleep(1);
  
  printf( " deleting: Dr. Turner \n" );
  head = delete( "Dr. Turner", head );
  printf( " list should be:\n" );
  printf( "  Sue -> Tom\n" );
  printf( " your list is:\n" );
  test_num = print_check_list_delete( head, "Dr. Turner" );
  if( !test_num )
  {     
    printf( " DELETION FAILED!\n" );
    exit( -1 );   
  }
  else
  {
     printf( " DELETION PASSED!\n" );     
  }

  sleep(1);
  
  printf( " deleting: Sue and Tom \n" );

  head = delete( "Sue", head );
  head = delete( "Tom", head );

  printf( " list should be:\n" );
  printf( "  EMPTY!\n" );
  printf( " your list is:\n" );
  test_num = print_check_list_delete( head, "Bob" );
  if( !test_num )
  {
    printf( " DELETION FAILED!\n" );
    exit( -1 );    
  }
  else
  {
    printf( " DELETION PASSED!\n" );    
  }

  
  
  return 1;
}