Skip to content

Commit

Permalink
Add tests for overlapping query iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Aug 2, 2023
1 parent 16c5f7a commit 5e87a37
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 3 deletions.
5 changes: 4 additions & 1 deletion test/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,10 @@
"query_next_table_w_populate_skip_first",
"query_next_table_w_populate_skip_last",
"create_query_existing_query_entity",
"query_for_recycled_pair"
"query_for_recycled_pair",
"query_w_singleton_w_rule_iter",
"query_w_singleton_nested_iter",
"query_w_singleton_interleaved_iter"
]
}, {
"id": "Iter",
Expand Down
144 changes: 144 additions & 0 deletions test/api/src/Query.c
Original file line number Diff line number Diff line change
Expand Up @@ -9149,3 +9149,147 @@ void Query_query_for_recycled_pair() {

ecs_fini(world);
}

void Query_query_w_singleton_w_rule_iter() {
ecs_world_t* ecs = ecs_init();

ECS_TAG(ecs, Tag);
ECS_TAG(ecs, Singleton);

ecs_query_t *q = ecs_query(ecs, {
.filter.terms = {
{Singleton, .src.id = Singleton}, {Tag}
}
});

ecs_rule_t *r = ecs_rule(ecs, {
.terms = {{Tag}}
});

ecs_singleton_add(ecs, Singleton);
ecs_entity_t e = ecs_new(ecs, Tag);

ecs_iter_t qit = ecs_query_iter(ecs, q);
test_bool(true, ecs_query_next(&qit));
test_int(1, qit.count);
test_uint(e, qit.entities[0]);

ecs_id_t id = qit.ids[0];
ecs_iter_t rit = ecs_rule_iter(ecs, r);
test_bool(true, ecs_rule_next(&rit));
test_int(1, rit.count);
test_uint(e, rit.entities[0]);
test_bool(false, ecs_rule_next(&rit));
test_assert(id == qit.ids[0]);
test_bool(false, ecs_query_next(&qit));

ecs_rule_fini(r);

ecs_fini(ecs);
}

void Query_query_w_singleton_nested_iter() {
ecs_world_t* ecs = ecs_init();

ECS_TAG(ecs, TagA);
ECS_TAG(ecs, TagB);
ECS_TAG(ecs, Singleton);

ecs_query_t *qa = ecs_query(ecs, {
.filter.terms = {
{TagA}, {Singleton, .src.id = Singleton}
}
});

ecs_query_t *qb = ecs_query(ecs, {
.filter.terms = {
{TagB}, {Singleton, .src.id = Singleton}
}
});

ecs_singleton_add(ecs, Singleton);

ecs_entity_t ea = ecs_new(ecs, TagA);
ecs_entity_t eb = ecs_new(ecs, TagB);

ecs_iter_t qita = ecs_query_iter(ecs, qa);
ecs_iter_t qitb = ecs_query_iter(ecs, qb);

test_bool(ecs_query_next(&qita), true);
test_int(1, qita.count);
test_uint(ea, qita.entities[0]);

test_bool(ecs_query_next(&qitb), true);
test_int(1, qitb.count);
test_uint(eb, qitb.entities[0]);

test_uint(TagA, qita.ids[0]);
test_uint(TagB, qitb.ids[0]);

test_bool(ecs_query_next(&qita), false);
test_bool(ecs_query_next(&qitb), false);

ecs_fini(ecs);
}

void Query_query_w_singleton_interleaved_iter() {
ecs_world_t* ecs = ecs_init();

ECS_TAG(ecs, TagA);
ECS_TAG(ecs, TagB);
ECS_TAG(ecs, TagC);
ECS_TAG(ecs, Singleton);

ecs_query_t *qa = ecs_query(ecs, {
.filter.terms = {
{TagA}, {Singleton, .src.id = Singleton}
}
});

ecs_query_t *qb = ecs_query(ecs, {
.filter.terms = {
{TagB}, {Singleton, .src.id = Singleton}
}
});

ecs_query_t *qc = ecs_query(ecs, {
.filter.terms = {
{TagC}, {Singleton, .src.id = Singleton}
}
});

ecs_singleton_add(ecs, Singleton);

ecs_entity_t ea = ecs_new(ecs, TagA);
ecs_entity_t eb = ecs_new(ecs, TagB);
ecs_entity_t ec = ecs_new(ecs, TagC);

ecs_iter_t qita = ecs_query_iter(ecs, qa);
ecs_iter_t qitb = ecs_query_iter(ecs, qb);

test_bool(ecs_query_next(&qita), true);
test_int(1, qita.count);
test_uint(ea, qita.entities[0]);

test_bool(ecs_query_next(&qitb), true);
test_int(1, qitb.count);
test_uint(eb, qitb.entities[0]);

test_uint(TagA, qita.ids[0]);
test_uint(TagB, qitb.ids[0]);

test_bool(ecs_query_next(&qita), false);

ecs_iter_t qitc = ecs_query_iter(ecs, qc);
test_bool(ecs_query_next(&qitc), true);
test_int(1, qitc.count);
test_uint(ec, qitc.entities[0]);

test_uint(TagB, qitb.ids[0]);
test_uint(TagC, qitc.ids[0]);

test_bool(ecs_query_next(&qitb), false);
test_bool(ecs_query_next(&qitc), false);

ecs_fini(ecs);
}
2 changes: 1 addition & 1 deletion test/api/src/StackAlloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,4 @@ void StackAlloc_multiple_overlapping_cursors() {
flecs_stack_fini(&stack);
ecs_fini(world);

}
}
17 changes: 16 additions & 1 deletion test/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,9 @@ void Query_query_next_table_w_populate_skip_first(void);
void Query_query_next_table_w_populate_skip_last(void);
void Query_create_query_existing_query_entity(void);
void Query_query_for_recycled_pair(void);
void Query_query_w_singleton_w_rule_iter(void);
void Query_query_w_singleton_nested_iter(void);
void Query_query_w_singleton_interleaved_iter(void);

// Testsuite 'Iter'
void Iter_page_iter_0_0(void);
Expand Down Expand Up @@ -8664,6 +8667,18 @@ bake_test_case Query_testcases[] = {
{
"query_for_recycled_pair",
Query_query_for_recycled_pair
},
{
"query_w_singleton_w_rule_iter",
Query_query_w_singleton_w_rule_iter
},
{
"query_w_singleton_nested_iter",
Query_query_w_singleton_nested_iter
},
{
"query_w_singleton_interleaved_iter",
Query_query_w_singleton_interleaved_iter
}
};

Expand Down Expand Up @@ -12766,7 +12781,7 @@ static bake_test_suite suites[] = {
"Query",
NULL,
NULL,
221,
224,
Query_testcases
},
{
Expand Down

0 comments on commit 5e87a37

Please sign in to comment.