#include "test_str8_list.h"
#include "wapp.h"

TestFuncResult test_str8_list_get(void) {
  b8 result;

  Str8 s1 = wapp_str8_lit("1");
  Str8 s2 = wapp_str8_lit("2");
  Str8 s3 = wapp_str8_lit("3");
  Str8 s4 = wapp_str8_lit("4");
  Str8 s5 = wapp_str8_lit("5");

  Str8List list = {};
  Str8Node n1   = { &s1, nullptr, nullptr };
  Str8Node n2   = { &s2, nullptr, nullptr };
  Str8Node n3   = { &s3, nullptr, nullptr };
  Str8Node n4   = { &s4, nullptr, nullptr };
  Str8Node n5   = { &s5, nullptr, nullptr };

  wapp_str8_list_push_back(&list, &n1);
  wapp_str8_list_push_back(&list, &n2);
  wapp_str8_list_push_back(&list, &n3);
  wapp_str8_list_push_back(&list, &n4);
  wapp_str8_list_push_back(&list, &n5);

  Str8Node *node = wapp_str8_list_get(&list, 0);
  result = node->item == &s1 && wapp_str8_equal(node->item, &s1);

  node = wapp_str8_list_get(&list, 1);
  result = result && node->item == &s2 && wapp_str8_equal(node->item, &s2);

  node = wapp_str8_list_get(&list, 2);
  result = result && node->item == &s3 && wapp_str8_equal(node->item, &s3);

  node = wapp_str8_list_get(&list, 3);
  result = result && node->item == &s4 && wapp_str8_equal(node->item, &s4);

  node = wapp_str8_list_get(&list, 4);
  result = result && node->item == &s5 && wapp_str8_equal(node->item, &s5);

  return wapp_tester_result(result);
}

TestFuncResult test_str8_list_push_front(void) {
  b8 result;

  Str8 s1 = wapp_str8_lit("1");
  Str8 s2 = wapp_str8_lit("2");
  Str8 s3 = wapp_str8_lit("3");

  Str8List list = {};
  Str8Node n1   = { &s1, nullptr, nullptr };
  Str8Node n2   = { &s2, nullptr, nullptr };
  Str8Node n3   = { &s3, nullptr, nullptr };

  wapp_str8_list_push_front(&list, &n1);
  result = list.first == list.last && list.first == &n1 && list.first->item == &s1 && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1;

  wapp_str8_list_push_front(&list, &n2);
  result = result && list.first == &n2 && list.first->item == &s2 && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2;

  wapp_str8_list_push_front(&list, &n3);
  result = result && list.first == &n3 && list.first->item == &s3 && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3;

  return wapp_tester_result(result);
}

TestFuncResult test_str8_list_push_back(void) {
  b8 result;

  Str8 s1 = wapp_str8_lit("1");
  Str8 s2 = wapp_str8_lit("2");
  Str8 s3 = wapp_str8_lit("3");

  Str8List list = {};
  Str8Node n1   = { &s1, nullptr, nullptr };
  Str8Node n2   = { &s2, nullptr, nullptr };
  Str8Node n3   = { &s3, nullptr, nullptr };

  wapp_str8_list_push_back(&list, &n1);
  result = list.first == list.last && list.last == &n1 && list.last->item == &s1 && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1;

  wapp_str8_list_push_back(&list, &n2);
  result = result && list.last == &n2 && list.last->item == &s2 && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2;

  wapp_str8_list_push_back(&list, &n3);
  result = result && list.last == &n3 && list.last->item == &s3 && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3;

  return wapp_tester_result(result);
}

TestFuncResult test_str8_list_insert(void) {
  b8 result;

  Str8 s1 = wapp_str8_lit("1");
  Str8 s2 = wapp_str8_lit("2");
  Str8 s3 = wapp_str8_lit("3");
  Str8 s4 = wapp_str8_lit("4");
  Str8 s5 = wapp_str8_lit("5");
  Str8 s6 = wapp_str8_lit("6");
  Str8 s7 = wapp_str8_lit("7");

  Str8List list = {};
  Str8Node n1   = { &s1, nullptr, nullptr };
  Str8Node n2   = { &s2, nullptr, nullptr };
  Str8Node n3   = { &s3, nullptr, nullptr };
  Str8Node n4   = { &s4, nullptr, nullptr };
  Str8Node n5   = { &s5, nullptr, nullptr };
  Str8Node n6   = { &s6, nullptr, nullptr };
  Str8Node n7   = { &s7, nullptr, nullptr };

  wapp_str8_list_push_back(&list, &n1);
  wapp_str8_list_push_back(&list, &n2);
  wapp_str8_list_push_back(&list, &n3);
  wapp_str8_list_push_back(&list, &n4);
  wapp_str8_list_push_back(&list, &n5);

  Str8Node *node;
  wapp_str8_list_insert(&list, &n6, 2);
  node   = wapp_str8_list_get(&list, 2);
  result = node != NULL && node->item == &s6 && wapp_str8_list_total_size(&list) == 6 && list.node_count == 6;
  wapp_str8_list_insert(&list, &n7, 5);
  node   = wapp_str8_list_get(&list, 5);
  result = result && node != NULL && node->item == &s7 && wapp_str8_list_total_size(&list) == 7 && list.node_count == 7;

  return wapp_tester_result(result);
}

TestFuncResult test_str8_list_pop_front(void) {
  b8 result;

  Str8 s1 = wapp_str8_lit("1");
  Str8 s2 = wapp_str8_lit("2");
  Str8 s3 = wapp_str8_lit("3");
  Str8 s4 = wapp_str8_lit("4");
  Str8 s5 = wapp_str8_lit("5");

  Str8List list = {};
  Str8Node n1   = { &s1, nullptr, nullptr };
  Str8Node n2   = { &s2, nullptr, nullptr };
  Str8Node n3   = { &s3, nullptr, nullptr };
  Str8Node n4   = { &s4, nullptr, nullptr };
  Str8Node n5   = { &s5, nullptr, nullptr };

  wapp_str8_list_push_back(&list, &n1);
  wapp_str8_list_push_back(&list, &n2);
  wapp_str8_list_push_back(&list, &n3);
  wapp_str8_list_push_back(&list, &n4);
  wapp_str8_list_push_back(&list, &n5);

  Str8Node *node = wapp_str8_list_pop_front(&list);
  result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4;

  node = wapp_str8_list_pop_front(&list);
  result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3;

  node = wapp_str8_list_pop_front(&list);
  result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2;

  node = wapp_str8_list_pop_front(&list);
  result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1;

  node = wapp_str8_list_pop_front(&list);
  result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0;

  return wapp_tester_result(result);
}

TestFuncResult test_str8_list_pop_back(void) {
  b8 result;

  Str8 s1 = wapp_str8_lit("1");
  Str8 s2 = wapp_str8_lit("2");
  Str8 s3 = wapp_str8_lit("3");
  Str8 s4 = wapp_str8_lit("4");
  Str8 s5 = wapp_str8_lit("5");

  Str8List list = {};
  Str8Node n1   = { &s1, nullptr, nullptr };
  Str8Node n2   = { &s2, nullptr, nullptr };
  Str8Node n3   = { &s3, nullptr, nullptr };
  Str8Node n4   = { &s4, nullptr, nullptr };
  Str8Node n5   = { &s5, nullptr, nullptr };

  wapp_str8_list_push_front(&list, &n1);
  wapp_str8_list_push_front(&list, &n2);
  wapp_str8_list_push_front(&list, &n3);
  wapp_str8_list_push_front(&list, &n4);
  wapp_str8_list_push_front(&list, &n5);

  Str8Node *node = wapp_str8_list_pop_back(&list);
  result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4;

  node = wapp_str8_list_pop_back(&list);
  result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3;

  node = wapp_str8_list_pop_back(&list);
  result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2;

  node = wapp_str8_list_pop_back(&list);
  result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1;

  node = wapp_str8_list_pop_back(&list);
  result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0;

  return wapp_tester_result(result);
}

TestFuncResult test_str8_list_remove(void) {
  b8 result;

  Str8 s1 = wapp_str8_lit("1");
  Str8 s2 = wapp_str8_lit("2");
  Str8 s3 = wapp_str8_lit("3");
  Str8 s4 = wapp_str8_lit("4");
  Str8 s5 = wapp_str8_lit("5");

  Str8List list = {};
  Str8Node n1   = { &s1, nullptr, nullptr };
  Str8Node n2   = { &s2, nullptr, nullptr };
  Str8Node n3   = { &s3, nullptr, nullptr };
  Str8Node n4   = { &s4, nullptr, nullptr };
  Str8Node n5   = { &s5, nullptr, nullptr };

  wapp_str8_list_push_back(&list, &n1);
  wapp_str8_list_push_back(&list, &n2);
  wapp_str8_list_push_back(&list, &n3);
  wapp_str8_list_push_back(&list, &n4);
  wapp_str8_list_push_back(&list, &n5);

  Str8Node *node = wapp_str8_list_remove(&list, 0);
  result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4;

  node = wapp_str8_list_remove(&list, 0);
  result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3;

  node = wapp_str8_list_remove(&list, 0);
  result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2;

  node = wapp_str8_list_remove(&list, 0);
  result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1;

  node = wapp_str8_list_remove(&list, 0);
  result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0;

  return wapp_tester_result(result);
}

TestFuncResult test_str8_list_empty(void) {
  b8 result;

  Str8List list = {};

  Str8Node hello = wapp_str8_node_from_cstr("Hello");
  wapp_str8_list_push_back(&list, &hello);

  Str8Node from = wapp_str8_node_from_cstr("from");
  wapp_str8_list_push_back(&list, &from);

  Str8Node wizapp = wapp_str8_node_from_cstr("wizapp");
  wapp_str8_list_push_back(&list, &wizapp);

  Str8Node stdlib = wapp_str8_node_from_cstr("stdlib");
  wapp_str8_list_push_back(&list, &stdlib);

  wapp_str8_list_empty(&list);

  result = list.first == NULL && list.last == NULL && list.node_count == 0 && wapp_str8_list_total_size(&list) == 0;

  return wapp_tester_result(result);
}
