#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "queue.h"

typedef
  struct linke {
    int nr;
    struct linke *next;
  } linke, *Linke;


static Linke l = NULL;


void test(int nr, int p){
  if (!p) {
    Linke g = (Linke) malloc(sizeof(linke));
    if (l == NULL){
      l = g;
      l->next = NULL;
      l->nr = nr;
    } else {
      Linke h = l;
      g->next = h;
      g->nr = nr;
      l = g;
    }
  }
}
/*  
  printf("%d -", nr);
  if (p) printf("Success\n");
  else printf("Fail\n");
  return;
}
*/
void tester(){
  Queue q = newQueue();
  test(1001, qLength(q) == 0);
  test(1002, first(q) == NULL);
  int x = 3;
  QueueData a = &x;
  enque(a, q);
  test(1003, qLength(q) == 1);
  test(1004, *((int*) first(q)) == 3);
  char *b = "b";
  enque(b, q);
  test(1005, qLength(q) == 2);
  test(1006, *((int*) first(q)) == 3);
  char z[] = "See";
  QueueData c = &z;
  enque(c, q);
  test(1007, qLength(q) == 3);
  test(1008, *((int*) first(q)) == 3);
  itReset(q);
  test(1009, *((int*) itNext(q)) == 3);
  test(1010, itMore(q) == 1);
  test(1011, itNext(q) == "b");
  test(1012, itMore(q) == 1);
  Queue r = newQueue();
  int y = 19;
  QueueData d = &y;
  enque(b, r);
  enque(d, r);
  itReset(r);
  test(1013, qLength(r) == 2);
  test(1014, itNext(r) == "b");
  test(1015, *((int*) itNext(r)) == 19);
  test(1016, itMore(r) == 0);
  test(1017, deque(r) == "b");
  test(1018, *((int*) deque(r)) == 19);
  itReset(r);
  test(1019, first(r) == NULL);
  itReset(q);
  test(1020, *((int*) itNext(q)) == 3);
  test(1021, itMore(q) == 1);
  test(1022, itNext(q) == "b");
  test(1023, itMore(q) == 1);
  test(1024, !strcmp(itNext(q), "See"));
  test(1025, itMore(q) == 0);
  enque(c, q);
  test(1026, qLength(q) == 4);
  test(1027, itMore(q) == 0);
  itReset(q);
  test(1028, qLength(q) == 4);
  test(1029, *((int*) first(q)) == 3);
  test(1030, *((int*) deque(q)) == 3);
  test(1031, qLength(q) == 3);
  test(1032, first(q) == "b");
  test(1033, deque(q) == "b");
  test(1034, qLength(q) == 2);
  test(1035, !strcmp(first(q), "See"));
  test(1036, !strcmp(deque(q), "See"));
  test(1037, qLength(q) == 1);
  test(1038, !strcmp(first(q), "See"));
  test(1039, !strcmp(deque(q), "See"));
  test(1040, qLength(q) == 0);
  test(1041, first(q) == NULL);

  if (l != NULL) {
    printf("Failed: ");
    while(l != NULL){
      printf("%d", l->nr);
      if (l->next != NULL)
	printf(" , ");
      l = l->next;
      }
  } else printf("Success!");

  printf("\n");
  return;
}

int main(){
  tester();
  return 0;
}
