示例#1
0
文件: physics.c 项目: andi2/cgame
Collision *physics_get_collisions(Entity ent)
{
    PhysicsInfo *info = entitypool_get(pool, ent);
    error_assert(info);

    _update_collisions(info);
    return array_begin(info->collisions);
}
示例#2
0
static void array_object_show(void* A)
{
  fprintf(stdout,
      "  show object of array :\n"
      "\t__array__ -> {\n"
      "\t  object=>0x%p,\n"
      "\t  size=>%d,\n"
      "\t  empty=>'%s',\n"
      "\t  begin=>0x%p,\n"
      "\t  end=>0x%p,\n"
      "\t  front=>0x%p,\n"
      "\t  back=>0x%p,\n"
      "\t}\n", 
      A, array_size(A), (array_empty(A) ? "yes" : "no"), 
      array_begin(A), array_end(A), array_front(A), array_back(A)
      );
}
示例#3
0
int main(int argc, const char* argv[]) {
    array_t(int) a = NULL;
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);

    array_alloc(a, 0, destructed_element_count_destructor);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);


    array_append(a, 1);
    test(array_size(a) == 1);
    test(array_capacity(a) >= 1);
    test(a[0] == 1);


    array_append(a, 2);
    test(array_size(a) == 2);
    test(array_capacity(a) >= 2);
    test(a[0] == 1);
    test(a[1] == 2);


    array_append(a, 3);
    test(array_size(a) == 3);
    test(array_capacity(a) >= 3);
    test(a[0] == 1);
    test(a[1] == 2);
    test(a[2] == 3);


    array_insert(a, 0, 0);
    test(array_size(a) == 4);
    test(array_capacity(a) >= 4);
    test(a[0] == 0);
    test(a[1] == 1);
    test(a[2] == 2);
    test(a[3] == 3);


    array_reserve(a, 16);
    test(array_size(a) == 4);
    test(array_capacity(a) == 16);
    test(a[0] == 0);
    test(a[1] == 1);
    test(a[2] == 2);
    test(a[3] == 3);


    array_shrink(a);
    test(array_size(a) == 4);
    test(array_capacity(a) == 4);
    test(a[0] == 0);
    test(a[1] == 1);
    test(a[2] == 2);
    test(a[3] == 3);


    array_remove(a, 0);
    test(array_size(a) == 3);
    test(array_capacity(a) == 4);
    test(a[0] == 1);
    test(a[1] == 2);
    test(a[2] == 3);
    test(destructed_element_count == 1);
    destructed_element_count = 0;


    array_remove_unordered(a,0);
    test(array_size(a) == 2);
    test(array_capacity(a) == 4);
    test(a[0] == 3);
    test(a[1] == 2);
    test(destructed_element_count == 1);
    destructed_element_count = 0;


    array_clear(a);
    test(array_size(a) == 0);
    test(array_capacity(a) >= 0);
    test(destructed_element_count == 2);
    destructed_element_count = 0;


    array_append(a, 0);
    array_append(a, 1);
    array_append(a, 2);
    test(array_size(a) == 3);
    test(array_capacity(a) >= 3);
    test(destructed_element_count == 0);


    array_free(a);
    test(a == NULL);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);
    test(destructed_element_count == 3);
    destructed_element_count = 0;


    enum { TEST_LENGTH = 1024 };


    array_alloc(a, 0, destructed_element_count_destructor);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        array_append(a, i);
        test(a[i] == i);
    }
    test(array_size(a) == TEST_LENGTH);
    test(array_capacity(a) >= TEST_LENGTH);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        test(a[i] == i);
    }
    {
        int i = 0;
        const int* const end = array_end(a);
        for (int* itr = array_begin(a); itr < end; ++itr) {
            test(*itr == i++);
        }
    }
    {
        int i = 0;
        while (array_size(a)) {
            test(a[0] == i++);
            array_remove(a,0);
        }
        test(array_size(a) == 0);
        test(array_capacity(a) >= TEST_LENGTH);
        test(destructed_element_count == TEST_LENGTH);
        destructed_element_count = 0;
    }
    array_free(a);
    test(a == NULL);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);


    array_alloc(a, 0, destructed_element_count_destructor);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        array_insert(a, 0, i);
    }
    test(array_size(a) == TEST_LENGTH);
    test(array_capacity(a) >= TEST_LENGTH);
    for (int i = 0; i < TEST_LENGTH; ++i) {
        test(a[i] == (TEST_LENGTH - 1) - i);
    }
    array_free(a);
    test(a == NULL);
    test(array_size(a) == 0);
    test(array_capacity(a) == 0);


    puts("array tests passed");
}
示例#4
0
void test_array(void)
{
  int i;
  double* d;
  void* A = array_create(0);
  fprintf(stdout, "call function : %s\n", __func__);
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_push_back ===>\n");
  {
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_for_each(A, array_element_destroy, NULL);
    array_object_show(A);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_pop_back ===>\n");
  {
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    d = (double*)array_pop_back(A);
    fprintf(stdout, "  the pop element {object=>0x%p,value=>%.3f}\n", d, *d);
    FREE(d);
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_for_each(A, array_element_destroy, NULL);
    array_object_show(A);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_erase ===>\n");
  {
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    d = (double*)array_erase(A, array_begin(A));
    fprintf(stdout, "  the erased begin element {object=>0x%p,value=>%.3f}\n", d, *d);
    FREE(d);
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);
    d = (double*)array_erase(A, array_end(A));
    fprintf(stdout, "  the erased end element {object=>0x%p,value=>%.3f}\n", d, *d);
    FREE(d);
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_for_each(A, array_element_destroy, NULL);
    array_object_show(A);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_remove ===>\n");
  {
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    d = (double*)array_remove(A, 3);
    fprintf(stdout, "  the removed [3] -> {object=>0x%p,value=>%.3f}\n", d, *d);
    FREE(d);
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_for_each(A, array_element_destroy, NULL);
    array_object_show(A);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_copy ===>\n");
  {
    void* copy_A;
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    copy_A = array_copy(A, 10);
    array_object_show(copy_A);
    array_for_each(copy_A, array_element_display, NULL);
    array_release(&copy_A);

    array_for_each(A, array_element_destroy, NULL);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_resize ===>\n");
  {
    srand((unsigned int)time(0));
    for (i = 0; i < 5; ++i)
    {
      NEW0(d);
      *d = (i + 1) * (i + 1) * rand() % 5555 * 0.123;
      fprintf(stdout, "\tappend into array {object=>0x%p, value=>%.3f}\n", d, *d);
      array_push_back(A, d);
    }
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_resize(A, 20);
    array_object_show(A);
    array_for_each(A, array_element_display, NULL);

    array_for_each(A, array_element_destroy, NULL);
    array_clear(A);
  }
  array_object_show(A);

  fprintf(stdout, "\ntest function - array_release ===>\n");
  array_release(&A);
  array_object_show(A);
}