Skip to content

Commit

Permalink
Fix eecs_array_resize
Browse files Browse the repository at this point in the history
  • Loading branch information
bullno1 committed Jan 14, 2024
1 parent ba5226a commit c0aad7d
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ If:
PathMatch: eecs\.h
CompileFlags:
Add: [-DEECS_IMPLEMENTATION]
---
If:
PathMatch: tests/.*\.c
CompileFlags:
Add: [-I../]
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "munit"]
path = munit
url = https://github.com/nemequ/munit.git
4 changes: 3 additions & 1 deletion eecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ eecs_realloc(void* memctx, void* ptr, size_t new_size) {
#define eecs_array_pop(array) array[eecs_dynamic_array_pop(array)]

#define eecs_array_resize(allocator, array, length) \
eecs_dynamic_array_resize(allocator, array, length, sizeof(*array))
do { \
array = eecs_dynamic_array_resize(allocator, array, length, sizeof(*array)); \
} while (0)

#define eecs_array_clear(array) \
eecs_dynamic_array_clear(array)
Expand Down
1 change: 1 addition & 0 deletions munit
Submodule munit added at fbbdf1
12 changes: 12 additions & 0 deletions run-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh -ex

cc \
-std=c11 -Wextra -Werror -pedantic \
-fsanitize=undefined,address \
-I. \
-g \
-o test \
munit/munit.c \
tests/*.c

./test "$@"
81 changes: 81 additions & 0 deletions tests/basic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <munit/munit.h>
#include <eecs.h>
#include "components.h"

struct SystemData {
int init_per_world_called;
int cleanup_per_world_called;
};

static void
system_init_per_world(
eecs_world_t* world,
void* userdata
) {
struct SystemData* system_data = userdata;
++system_data->init_per_world_called;
}

static void
system_cleanup_per_world(
eecs_world_t* world,
void* userdata
) {
struct SystemData* system_data = userdata;
++system_data->cleanup_per_world_called;
}

static MunitResult
init(const MunitParameter params[], void* fixture) {
eecs_t* ecs = eecs_create((eecs_options_t) { 0 });

eecs_component_t comp_A = EECS_HANDLE_INIT;
eecs_component_t comp_B = EECS_HANDLE_INIT;
eecs_component_t comp_C = EECS_HANDLE_INIT;
eecs_register_component(ecs, &comp_A, (eecs_component_options_t){
.size = sizeof(struct A),
.alignment = _Alignof(struct A),
});
eecs_register_component(ecs, &comp_B, (eecs_component_options_t){
.size = sizeof(struct B),
.alignment = _Alignof(struct B),
});
eecs_register_component(ecs, &comp_C, (eecs_component_options_t){
.size = sizeof(struct C),
.alignment = _Alignof(struct C),
});

eecs_system_t sys_with_init = EECS_HANDLE_INIT;
struct SystemData sys_with_init_data = { 0 };
eecs_register_system(ecs, &sys_with_init, (eecs_system_options_t){
.init_per_world_fn = system_init_per_world,
.userdata = &sys_with_init_data,
});

eecs_system_t sys_with_cleanup = EECS_HANDLE_INIT;
struct SystemData sys_with_cleanup_data = { 0 };
eecs_register_system(ecs, &sys_with_cleanup, (eecs_system_options_t){
.cleanup_per_world_fn = system_cleanup_per_world,
.userdata = &sys_with_cleanup_data,
});

eecs_world_t* world = eecs_create_world(ecs, (eecs_world_options_t){ 0 });
eecs_destroy_world(world);

munit_assert_int(sys_with_init_data.init_per_world_called, ==, 1);
munit_assert_int(sys_with_init_data.cleanup_per_world_called, ==, 0);

munit_assert_int(sys_with_cleanup_data.init_per_world_called, ==, 0);
munit_assert_int(sys_with_cleanup_data.cleanup_per_world_called, ==, 1);

eecs_destroy(ecs);
return MUNIT_OK;
}

MunitSuite basic = {
.prefix = "/basic",
.tests = (MunitTest[]){
{ .name = "/init", .test = init },
{ 0 },
},
};
19 changes: 19 additions & 0 deletions tests/components.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef COMPONENTS_H
#define COMPONENTS_H

struct A {
float a;
};

struct B {
int b;
long c;
};

struct C {
int b;
long c;
void* d;
};

#endif
16 changes: 16 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <munit/munit.h>
#define EECS_IMPLEMENTATION
#include <eecs.h>

extern MunitSuite basic;

int main (int argc, char* argv[]) {
MunitSuite suites = {
.suites = (MunitSuite[]) {
basic,
{ 0 },
},
};

return munit_suite_main(&suites, NULL, argc, argv);
}

0 comments on commit c0aad7d

Please sign in to comment.