#include "test_i32_array.h"
#include "wapp.h"

TestFuncResult test_i32_array(void) {
  b8 result;

  I32Array array = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7);
  result = array.count == 7 && array.capacity == 16;

  i32 *item;
  u64 count   = array.count;
  u64 index   = 0;
  b8 running  = true;
  while (running) {
      item   = wapp_array_get(i32, &array, index);
      result = result && item && *item == (i32)(index + 1);

      ++index;
      running = index < count;
  }

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_with_capacity(void) {
  b8 result;

  I32Array array = wapp_array_with_capacity(i32, I32Array, 64);
  result = array.count == 0 && array.capacity == 64;

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_get(void) {
  b8 result = true;

  I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8);

  i32 *item;
  u64 count   = array.count;
  u64 index   = 0;
  b8 running  = true;
  while (running) {
    item   = wapp_array_get(i32, &array, index);
    result = result && item && *item == (i32)index;

    ++index;
    running = index < count;
  }

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_set(void) {
  b8 result = true;

  I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8);

  i32 *item;
  u64 count   = array.count;
  u64 index   = 0;
  b8 running  = true;
  while (running) {
    i32 num = (i32)(index * 2);
    wapp_array_set(i32, &array, index, &num);
    item   = wapp_array_get(i32, &array, index);
    result = result && item && *item == (i32)(index * 2);

    ++index;
    running = index < count;
  }

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_append_capped(void) {
  b8 result;

  I32Array array = wapp_array_with_capacity(i32, I32Array, 64);
  wapp_array_append_capped(i32, &array, &((i32){10}));

  result    = array.count == 1;
  i32 *item = wapp_array_get(i32, &array, 0);
  result = result && item && *item == 10;

  array = wapp_array(i32, I32Array, 1);
  wapp_array_append_capped(i32, &array, &((i32){10}));

  result = result && array.count == 2;

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_extend_capped(void) {
  b8 result;

  I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4);
  I32Array array2 = wapp_array(i32, I32Array, 10, 20);

  result = array1.count == 4 && array2.count == 2;

  wapp_array_extend_capped(i32, &array1, &array2);

  result = result && array1.count == 6;

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_copy_capped(void) {
  b8 result;

  I32Array src  = wapp_array(i32, I32Array, 1, 2, 3, 4, 5);
  I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6);
  I32Array dst2 = wapp_array(i32, I32Array, 1, 2);

  u64 expected_count = 5;
  wapp_array_copy_capped(i32, &dst1, &src);
  result = dst1.count == expected_count;

  u64 index    = 0;
  b8 running  = true;
  while (running) {
    result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst1, index);

    ++index;
    running = index < expected_count;
  }

  expected_count = 4;
  wapp_array_copy_capped(i32, &dst2, &src);
  result = result && dst2.count == expected_count;

  index   = 0;
  running = true;
  while (running) {
    result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst2, index);

    ++index;
    running = index < expected_count;
  }

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_alloc_capacity(void) {
  b8 result;

  Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
  u64 capacity        = 32;
  I32Array *array     = wapp_array_alloc_capacity(i32, I32Array, &allocator, capacity);

  result = array && array->capacity == capacity;

  wapp_mem_arena_allocator_destroy(&allocator);

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_append_alloc(void) {
  b8 result;

  Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
  I32Array array1     = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8);
  I32Array array2     = wapp_array(i32, I32Array, 1, 2);

  I32Array *arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array1, &((i32){10}));
  result = arr_ptr == &array1;

  u64 count    = 4;
  u64 index    = 0;
  b8 running  = true;
  while (running) {
    i32 num = (i32)index;
    arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array2, &num);

    ++index;
    running = index < count;
  }
  result = result && arr_ptr != &array2;

  wapp_mem_arena_allocator_destroy(&allocator);

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_extend_alloc(void) {
  b8 result;

  Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
  I32Array array1     = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8);
  I32Array array2     = wapp_array(i32, I32Array, 1, 2);
  I32Array array3     = wapp_array(i32, I32Array, 1, 2, 3, 4);

  I32Array *arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array1, &array3);
  result = arr_ptr == &array1;

  arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array2, &array3);
  result  = result && arr_ptr != &array2;

  wapp_mem_arena_allocator_destroy(&allocator);

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_copy_alloc(void) {
  b8 result;

  Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
  I32Array src        = wapp_array(i32, I32Array, 1, 2, 3, 4, 5);
  I32Array dst1       = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6);
  I32Array dst2       = wapp_array(i32, I32Array, 1, 2);
  I32Array *array_ptr = NULL;

  u64 expected_count = 5;
  array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst1, &src);
  result = array_ptr->count == expected_count && array_ptr == &dst1;

  u64 index    = 0;
  b8 running  = true;
  while (running) {
    result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index);

    ++index;
    running = index < expected_count;
  }

  expected_count = 5;
  array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst2, &src);
  result = result && array_ptr->count == expected_count && array_ptr != &dst2;

  index   = 0;
  running = true;
  while (running) {
    result = result && *wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index);

    ++index;
    running = index < expected_count;
  }

  wapp_mem_arena_allocator_destroy(&allocator);

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_pop(void) {
  b8 result;

  I32Array array1 = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8);
  I32Array array2 = wapp_array_with_capacity(i32, I32Array, 32);

  i32 item1 = wapp_array_pop(i32, &array1);
  i32 item2 = wapp_array_pop(i32, &array2);

  result = item1 == 8 && item2 == 0;

  return wapp_tester_result(result);
}

TestFuncResult test_i32_array_clear(void) {
  b8 result;

  I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8);
  result = array.count == 9;

  wapp_array_clear(i32, &array);

  result = result && array.count == 0;

  return wapp_tester_result(result);
}
