Skip to content

Commit

Permalink
#124 add more observer tests, fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Sep 18, 2021
1 parent 87cd503 commit 75641bc
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 13 deletions.
15 changes: 11 additions & 4 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,7 @@ bool flecs_filter_match_table(
const ecs_filter_t *filter,
const ecs_table_t *table,
ecs_type_t type,
int32_t offset,
ecs_id_t *ids,
int32_t *columns,
ecs_type_t *types,
Expand Down Expand Up @@ -17133,6 +17134,7 @@ static
bool populate_from_column(
ecs_world_t *world,
const ecs_table_t *table,
int32_t offset,
ecs_id_t id,
int32_t column,
ecs_entity_t source,
Expand Down Expand Up @@ -17182,6 +17184,10 @@ bool populate_from_column(
ecs_column_t *col = &data->columns[column];
*ptr_out = ecs_vector_first_t(
col->data, col->size, col->alignment);

if (*ptr_out && offset) {
*ptr_out = ECS_OFFSET(*ptr_out, col->size * offset);
}
}
} else {
*ptr_out = NULL;
Expand Down Expand Up @@ -17220,6 +17226,7 @@ bool flecs_filter_match_table(
const ecs_filter_t *filter,
const ecs_table_t *table,
ecs_type_t type,
int32_t offset,
ecs_id_t *ids,
int32_t *columns,
ecs_type_t *types,
Expand Down Expand Up @@ -17306,7 +17313,7 @@ bool flecs_filter_match_table(
int32_t t_i = term->index;

void **ptr = ptrs ? &ptrs[t_i] : NULL;
populate_from_column(world, table, term->id, column,
populate_from_column(world, table, offset, term->id, column,
source, &ids[t_i], &types[t_i], &subjects[t_i], &sizes[t_i],
ptr);

Expand Down Expand Up @@ -17497,7 +17504,7 @@ bool ecs_term_next(
it->entities = ecs_vector_first(data->entities, ecs_entity_t);
it->is_valid = true;

bool has_data = populate_from_column(world, table, term->id, tr->column,
bool has_data = populate_from_column(world, table, 0, term->id, tr->column,
source, &iter->id, &iter->type, &iter->subject, &iter->size,
&iter->ptr);

Expand Down Expand Up @@ -17635,7 +17642,7 @@ bool ecs_filter_next(

table = tr->table;
match = flecs_filter_match_table(world, filter, table, table->type,
it->ids, it->columns, it->types, it->subjects, it->sizes,
0, it->ids, it->columns, it->types, it->subjects, it->sizes,
it->ptrs);
} while (!match);

Expand Down Expand Up @@ -17674,7 +17681,7 @@ void observer_callback(ecs_iter_t *it) {

ecs_iter_init(&user_it);

if (flecs_filter_match_table(world, &o->filter, table, type,
if (flecs_filter_match_table(world, &o->filter, table, type, it->offset,
user_it.ids, user_it.columns, user_it.types, user_it.subjects,
user_it.sizes, user_it.ptrs))
{
Expand Down
12 changes: 9 additions & 3 deletions src/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ static
bool populate_from_column(
ecs_world_t *world,
const ecs_table_t *table,
int32_t offset,
ecs_id_t id,
int32_t column,
ecs_entity_t source,
Expand Down Expand Up @@ -698,6 +699,10 @@ bool populate_from_column(
ecs_column_t *col = &data->columns[column];
*ptr_out = ecs_vector_first_t(
col->data, col->size, col->alignment);

if (*ptr_out && offset) {
*ptr_out = ECS_OFFSET(*ptr_out, col->size * offset);
}
}
} else {
*ptr_out = NULL;
Expand Down Expand Up @@ -736,6 +741,7 @@ bool flecs_filter_match_table(
const ecs_filter_t *filter,
const ecs_table_t *table,
ecs_type_t type,
int32_t offset,
ecs_id_t *ids,
int32_t *columns,
ecs_type_t *types,
Expand Down Expand Up @@ -822,7 +828,7 @@ bool flecs_filter_match_table(
int32_t t_i = term->index;

void **ptr = ptrs ? &ptrs[t_i] : NULL;
populate_from_column(world, table, term->id, column,
populate_from_column(world, table, offset, term->id, column,
source, &ids[t_i], &types[t_i], &subjects[t_i], &sizes[t_i],
ptr);

Expand Down Expand Up @@ -1013,7 +1019,7 @@ bool ecs_term_next(
it->entities = ecs_vector_first(data->entities, ecs_entity_t);
it->is_valid = true;

bool has_data = populate_from_column(world, table, term->id, tr->column,
bool has_data = populate_from_column(world, table, 0, term->id, tr->column,
source, &iter->id, &iter->type, &iter->subject, &iter->size,
&iter->ptr);

Expand Down Expand Up @@ -1151,7 +1157,7 @@ bool ecs_filter_next(

table = tr->table;
match = flecs_filter_match_table(world, filter, table, table->type,
it->ids, it->columns, it->types, it->subjects, it->sizes,
0, it->ids, it->columns, it->types, it->subjects, it->sizes,
it->ptrs);
} while (!match);

Expand Down
2 changes: 1 addition & 1 deletion src/observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void observer_callback(ecs_iter_t *it) {

ecs_iter_init(&user_it);

if (flecs_filter_match_table(world, &o->filter, table, type,
if (flecs_filter_match_table(world, &o->filter, table, type, it->offset,
user_it.ids, user_it.columns, user_it.types, user_it.subjects,
user_it.sizes, user_it.ptrs))
{
Expand Down
1 change: 1 addition & 0 deletions src/private_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ bool flecs_filter_match_table(
const ecs_filter_t *filter,
const ecs_table_t *table,
ecs_type_t type,
int32_t offset,
ecs_id_t *ids,
int32_t *columns,
ecs_type_t *types,
Expand Down
4 changes: 4 additions & 0 deletions test/api/include/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ typedef struct Color {
float a;
} Color;

typedef struct Self {
ecs_entity_t value;
} Self;

void probe_system_w_ctx(
ecs_iter_t *it,
Probe *ctx);
Expand Down
2 changes: 2 additions & 0 deletions test/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,8 @@
"2_wildcard_pair_terms_w_on_add_2_matching",
"2_wildcard_pair_terms_w_on_add_3_matching",
"2_wildcard_pair_terms_w_on_remove",
"on_set_n_entities",
"on_set_n_entities_2_comp",
"wildcard_pair_w_pred_component",
"wildcard_pair_w_obj_component",
"2_terms_1_not_w_on_add",
Expand Down
128 changes: 128 additions & 0 deletions test/api/src/Observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ void Observer_w_value(ecs_iter_t *it) {
}
}

static
void Observer_w_self(ecs_iter_t *it) {
probe_system_w_ctx(it, it->ctx);

test_int(it->count, 1);
test_assert(it->entities != NULL);
test_assert(it->entities[0] != 0);

Self *s = ecs_term(it, Self, 1);
test_assert(s != NULL);
test_int(s->value, it->entities[0]);

if (it->column_count > 1) {
Self *s_2 = ecs_term(it, Self, 2);
test_int(s_2->value, it->entities[0]);
}
}

static bool dummy_called = false;

static
Expand Down Expand Up @@ -526,6 +544,116 @@ void Observer_2_wildcard_pair_terms_w_on_remove() {
ecs_fini(world);
}

void Observer_on_set_n_entities() {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Self);

Probe ctx = {0};
ecs_entity_t o = ecs_observer_init(world, &(ecs_observer_desc_t){
.filter.terms = {{ecs_id(Self)}},
.events = {EcsOnSet},
.callback = Observer_w_self,
.ctx = &ctx
});
test_assert(o != 0);

ecs_entity_t e_1 = ecs_new_id(world);
test_int(ctx.invoked, 0);
ecs_set(world, e_1, Self, {e_1});

test_int(ctx.invoked, 1);
test_int(ctx.count, 1);
test_int(ctx.system, o);
test_int(ctx.event, EcsOnSet);
test_int(ctx.column_count, 1);
test_null(ctx.param);
test_int(ctx.e[0], e_1);
test_int(ctx.c[0][0], ecs_id(Self));

ctx = (Probe){0};

ecs_entity_t e_2 = ecs_new_id(world);
test_int(ctx.invoked, 0);
ecs_set(world, e_2, Self, {e_2});

test_int(ctx.invoked, 1);
test_int(ctx.count, 1);
test_int(ctx.system, o);
test_int(ctx.event, EcsOnSet);
test_int(ctx.column_count, 1);
test_null(ctx.param);
test_int(ctx.e[0], e_2);
test_int(ctx.c[0][0], ecs_id(Self));

ctx = (Probe){0};

ecs_entity_t e_3 = ecs_new_id(world);
test_int(ctx.invoked, 0);
ecs_set(world, e_3, Self, {e_3});

test_int(ctx.invoked, 1);
test_int(ctx.count, 1);
test_int(ctx.system, o);
test_int(ctx.event, EcsOnSet);
test_int(ctx.column_count, 1);
test_null(ctx.param);
test_int(ctx.e[0], e_3);
test_int(ctx.c[0][0], ecs_id(Self));

ecs_fini(world);
}

void Observer_on_set_n_entities_2_comp() {
ecs_world_t *world = ecs_init();

ECS_TAG(world, Obj);
ECS_COMPONENT(world, Self);

Probe ctx = {0};
ecs_entity_t o = ecs_observer_init(world, &(ecs_observer_desc_t){
.filter.terms = {{ecs_id(Self)}, {ecs_pair(ecs_id(Self), EcsWildcard)}},
.events = {EcsOnSet},
.callback = Observer_w_self,
.ctx = &ctx
});
test_assert(o != 0);

ecs_entity_t e_1 = ecs_new_id(world);
ecs_set(world, e_1, Self, {e_1});
test_int(ctx.invoked, 0);
ecs_set_pair(world, e_1, Self, Obj, {e_1});

test_int(ctx.invoked, 1);
test_int(ctx.count, 1);
test_int(ctx.system, o);
test_int(ctx.event, EcsOnSet);
test_int(ctx.column_count, 2);
test_null(ctx.param);
test_int(ctx.e[0], e_1);
test_int(ctx.c[0][0], ecs_id(Self));
test_int(ctx.c[0][1], ecs_pair(ecs_id(Self), Obj));

ctx = (Probe){0};

ecs_entity_t e_2 = ecs_new_id(world);
ecs_set(world, e_2, Self, {e_2});
test_int(ctx.invoked, 0);
ecs_set_pair(world, e_2, Self, Obj, {e_2});

test_int(ctx.invoked, 1);
test_int(ctx.count, 1);
test_int(ctx.system, o);
test_int(ctx.event, EcsOnSet);
test_int(ctx.column_count, 2);
test_null(ctx.param);
test_int(ctx.e[0], e_2);
test_int(ctx.c[0][0], ecs_id(Self));
test_int(ctx.c[0][1], ecs_pair(ecs_id(Self), Obj));

ecs_fini(world);
}

void Observer_wildcard_pair_w_pred_component() {
ecs_world_t *world = ecs_init();

Expand Down
4 changes: 0 additions & 4 deletions test/api/src/Trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -1750,10 +1750,6 @@ void Trigger_trigger_w_named_entity() {
ecs_fini(world);
}

typedef struct Self {
ecs_entity_t value;
} Self;

void RemoveSelf(ecs_iter_t *it) {
Self *s = ecs_term(it, Self, 1);
ecs_id_t ecs_id(Self) = ecs_term_id(it, 1);
Expand Down
12 changes: 11 additions & 1 deletion test/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,8 @@ void Observer_2_wildcard_pair_terms_w_on_add(void);
void Observer_2_wildcard_pair_terms_w_on_add_2_matching(void);
void Observer_2_wildcard_pair_terms_w_on_add_3_matching(void);
void Observer_2_wildcard_pair_terms_w_on_remove(void);
void Observer_on_set_n_entities(void);
void Observer_on_set_n_entities_2_comp(void);
void Observer_wildcard_pair_w_pred_component(void);
void Observer_wildcard_pair_w_obj_component(void);
void Observer_2_terms_1_not_w_on_add(void);
Expand Down Expand Up @@ -6482,6 +6484,14 @@ bake_test_case Observer_testcases[] = {
"2_wildcard_pair_terms_w_on_remove",
Observer_2_wildcard_pair_terms_w_on_remove
},
{
"on_set_n_entities",
Observer_on_set_n_entities
},
{
"on_set_n_entities_2_comp",
Observer_on_set_n_entities_2_comp
},
{
"wildcard_pair_w_pred_component",
Observer_wildcard_pair_w_pred_component
Expand Down Expand Up @@ -9878,7 +9888,7 @@ static bake_test_suite suites[] = {
"Observer",
NULL,
NULL,
28,
30,
Observer_testcases
},
{
Expand Down

0 comments on commit 75641bc

Please sign in to comment.