#include "list.h"

#include "test.h"

#define TEST_LIST_SIZE 10

DEF_LIST(int);

// Iterate over a list.
TEST_CASE(list_traverse) {
  int_list list = make_list(int);
  for (int i = 0; i < TEST_LIST_SIZE; ++i) {
    list_push(list, i + 1);
  }

  int count = 0;
  int sum   = 0;
  list_foreach(list, {
    count++;
    sum += *value;
  });

  TEST_EQUAL(count, TEST_LIST_SIZE);
  TEST_EQUAL(sum, TEST_LIST_SIZE * (TEST_LIST_SIZE + 1) / 2);

  del_list(list);
}

// Delete by value.
TEST_CASE(list_remove_by_value) {
  int_list list = make_list(int);
  for (int i = 0; i < TEST_LIST_SIZE; ++i) {
    list_push(list, i + 1);
  }

  list_remove(list, 5);

  int count = 0;
  int sum   = 0;
  list_foreach(list, {
    count++;
    sum += *value;
  });

  TEST_EQUAL(count, TEST_LIST_SIZE - 1);
  TEST_EQUAL(sum, (TEST_LIST_SIZE * (TEST_LIST_SIZE + 1) / 2) - 5);

  del_list(list);
}

// Delete by address.
TEST_CASE(list_remove_by_address) {
  const int N = 3;

  int* ptrs[3] = {0};

  int_list list = make_list(int);
  for (int i = 0; i < N; ++i) {
    list_push(list, i + 1);
    ptrs[i] = &list.head->val;
  }

  list_remove_ptr(list, ptrs[1]); // Value 2.

  int count = 0;
  int sum   = 0;
  list_foreach(list, {
    count++;
    sum += *value;
  });

  TEST_EQUAL(count, 2);
  TEST_EQUAL(sum, 1 + 3);

  del_list(list);
}

int main() { return 0; }