Skip to content

Commit b366976

Browse files
committed
Update util to validate, fix bad test (sigh)
1 parent 80b1236 commit b366976

File tree

2 files changed

+76
-98
lines changed

2 files changed

+76
-98
lines changed

reference/async_reference/util/util.cc

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
using namespace ecsact::async_reference::detail;
88

9-
static ecsact_async_error validate_instructions(
9+
static ecsact_async_error validate_entity_instructions(
1010
const std::vector<types::cpp_execution_component>& components
1111
) {
1212
auto components_range = std::ranges::views::all(components);
@@ -41,6 +41,29 @@ static ecsact_async_error validate_instructions(
4141
return ECSACT_ASYNC_OK;
4242
}
4343

44+
static ecsact_async_error validate_instructions(
45+
const std::vector<types::cpp_component>& components
46+
) {
47+
auto components_range = std::ranges::views::all(components);
48+
49+
for(auto& component : components) {
50+
auto view = std::views::filter(
51+
components_range,
52+
[&components_range, &component](types::cpp_component other_component) {
53+
return component._id == other_component._id;
54+
}
55+
);
56+
int component_count = 0;
57+
for(auto& found_component : view) {
58+
component_count++;
59+
}
60+
if(component_count > 1) {
61+
return ECSACT_ASYNC_ERR_EXECUTION_MERGE_FAILURE;
62+
}
63+
}
64+
return ECSACT_ASYNC_OK;
65+
}
66+
4467
ecsact_async_error validate_merge_instructions(
4568
const std::vector<types::cpp_execution_component>& components,
4669
const std::vector<types::cpp_execution_component>& other_components
@@ -304,26 +327,35 @@ ecsact_async_error util::validate_options(types::cpp_execution_options& options
304327
ecsact_async_error error = ECSACT_ASYNC_OK;
305328

306329
if(options.adds.size() > 0) {
307-
error = validate_instructions(options.adds);
330+
error = validate_entity_instructions(options.adds);
308331
if(error != ECSACT_ASYNC_OK) {
309332
return error;
310333
}
311334
}
312335

313336
if(options.updates.size() > 0) {
314-
error = validate_instructions(options.updates);
337+
error = validate_entity_instructions(options.updates);
315338
if(error != ECSACT_ASYNC_OK) {
316339
return error;
317340
}
318341
}
319342

320343
if(options.removes.size() > 0) {
321-
error = validate_instructions(options.removes);
344+
error = validate_entity_instructions(options.removes);
322345
if(error != ECSACT_ASYNC_OK) {
323346
return error;
324347
}
325348
}
326349

350+
if(options.create_entities.size() > 0) {
351+
for(auto& entity_to_create : options.create_entities) {
352+
error = validate_instructions(entity_to_create.components);
353+
if(error != ECSACT_ASYNC_OK) {
354+
return error;
355+
}
356+
}
357+
}
358+
327359
if(options.destroy_entities.size() > 0) {
328360
auto entities = std::views::all(options.destroy_entities);
329361
auto has_duplicates = util::check_entity_duplicates(entities);

tests/async/async_ref_test.cc

Lines changed: 40 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,11 @@ TEST(AsyncRef, AddUpdateAndRemove) {
159159
cb_info.entity = entity_id;
160160
};
161161

162-
// Declare core execution options
163-
auto options = ecsact::core::execution_options{};
162+
ecsact::core::execution_options options{};
164163

165-
ecsact_async_events_collector entity_async_evc{};
166-
entity_async_evc.async_entity_callback = entity_cb;
167-
entity_async_evc.async_entity_callback_user_data = &cb_info;
164+
auto evc = ecsact_execution_events_collector{};
165+
evc.entity_created_callback = entity_cb;
166+
evc.entity_created_callback_user_data = &cb_info;
168167

169168
auto my_needed_component = async_test::NeededComponent{};
170169

@@ -180,18 +179,18 @@ TEST(AsyncRef, AddUpdateAndRemove) {
180179
ASSERT_LT(tick_diff, 10);
181180
}
182181

182+
// Reset events collector and options
183+
evc.entity_created_callback = {};
184+
evc.entity_created_callback_user_data = nullptr;
183185
options.clear();
184186

185187
// Preparing add component data
186-
// auto my_needed_component = async_test::NeededComponent{};
187188
auto my_update_component = async_test::ComponentUpdate{.value_to_update = 1};
188189

189-
// options.add_component(cb_info.entity, &my_needed_component);
190190
options.add_component(cb_info.entity, &my_update_component);
191191

192192
// Adding components
193193
ecsact_async_enqueue_execution_options(options.c());
194-
options.clear();
195194

196195
// Prepare the events collector for the flush to make sure we got all the
197196
// events we expected.
@@ -225,6 +224,7 @@ TEST(AsyncRef, AddUpdateAndRemove) {
225224

226225
evc.init_callback = {};
227226
evc.init_callback_user_data = nullptr;
227+
options.clear();
228228

229229
evc.update_callback_user_data = &cb_info;
230230
evc.update_callback = //
@@ -250,12 +250,9 @@ TEST(AsyncRef, AddUpdateAndRemove) {
250250
my_update_component.value_to_update += 5;
251251

252252
// Update components
253-
options.update_component<async_test::ComponentUpdate>(
254-
cb_info.entity,
255-
&my_update_component
256-
);
253+
options.update_component(cb_info.entity, &my_update_component);
254+
257255
ecsact_async_enqueue_execution_options(options.c());
258-
options.clear();
259256

260257
start_tick = ecsact_async_get_current_tick();
261258
while(!cb_info.update_happened) {
@@ -265,14 +262,14 @@ TEST(AsyncRef, AddUpdateAndRemove) {
265262
ASSERT_LT(tick_diff, 10);
266263
}
267264

265+
options.clear();
268266
evc.update_callback = {};
269267
evc.update_callback_user_data = nullptr;
270268

271269
// Remove component
272270
options.remove_component<async_test::ComponentUpdate>(cb_info.entity);
273271

274272
ecsact_async_enqueue_execution_options(options.c());
275-
options.clear();
276273

277274
evc.remove_callback_user_data = &cb_info;
278275
evc.remove_callback = //
@@ -307,6 +304,7 @@ TEST(AsyncRef, AddUpdateAndRemove) {
307304
<< " remove = " << cb_info.remove_happened << "\n";
308305
}
309306

307+
options.clear();
310308
ecsact_async_disconnect();
311309
}
312310

@@ -322,17 +320,9 @@ TEST(AsyncRef, TryMergeFailure) {
322320

323321
ecsact_async_connect("good?tick_rate=25");
324322

325-
auto my_needed_component = async_test::NeededComponent{};
326-
auto another_my_needed_component = async_test::NeededComponent{};
327-
328-
ecsact_component needed_component{
329-
.component_id = async_test::NeededComponent::id,
330-
.component_data = &my_needed_component,
331-
};
332-
333-
ecsact_component another_needed_component{
334-
.component_id = async_test::NeededComponent::id,
335-
.component_data = &another_my_needed_component,
323+
struct merge_callback_data {
324+
ecsact_async_request_id request_id;
325+
bool wait;
336326
};
337327

338328
auto entity_cb = //
@@ -348,56 +338,6 @@ TEST(AsyncRef, TryMergeFailure) {
348338
entity_info.entity = entity_id;
349339
};
350340

351-
entity_cb_info cb_info{};
352-
353-
// 1D array
354-
std::array<ecsact_component, 2> entity_components = {
355-
needed_component,
356-
another_needed_component,
357-
};
358-
359-
// 1D array
360-
std::array<ecsact_component, 1> another_entity_components = {
361-
needed_component,
362-
};
363-
364-
// Do I combine both the above arrays to make a 2D array?
365-
auto my_vector = std::vector<ecsact_component*>{};
366-
my_vector.push_back(entity_components.data());
367-
my_vector.push_back(another_entity_components.data());
368-
369-
// Holds above array sizes
370-
std::array<int, 2> entity_component_lengths = {
371-
entity_components.size(),
372-
another_entity_components.size(),
373-
};
374-
375-
ecsact_execution_options entity_options{};
376-
377-
entity_options.create_entities_components = my_vector.data();
378-
entity_options.create_entities_length = entity_component_lengths.size();
379-
entity_options.create_entities_components_length =
380-
entity_component_lengths.data();
381-
382-
ecsact_async_enqueue_execution_options(entity_options);
383-
384-
auto evc = ecsact_execution_events_collector{};
385-
evc.entity_created_callback = entity_cb;
386-
evc.entity_created_callback_user_data = &cb_info;
387-
388-
auto start_tick = ecsact_async_get_current_tick();
389-
while(cb_info.wait != true) {
390-
ecsact_async_flush_events(&evc, nullptr);
391-
auto current_tick = ecsact_async_get_current_tick();
392-
auto tick_diff = current_tick - start_tick;
393-
ASSERT_LT(tick_diff, 10);
394-
}
395-
396-
struct merge_callback_data {
397-
ecsact_async_request_id request_id;
398-
bool wait;
399-
};
400-
401341
auto async_err_cb = //
402342
[](
403343
ecsact_async_error async_err,
@@ -414,36 +354,42 @@ TEST(AsyncRef, TryMergeFailure) {
414354
cb_data.wait = true;
415355
};
416356

417-
std::array entities{cb_info.entity, cb_info.entity};
418-
std::array components{
419-
needed_component,
420-
another_needed_component,
421-
};
357+
entity_cb_info cb_info{};
422358

423-
ecsact_execution_options options{};
424-
options.add_components_entities = entities.data();
425-
options.add_components = components.data();
426-
options.add_components_length = entities.size();
359+
ecsact::core::execution_options options{};
427360

428-
auto options_request = ecsact_async_enqueue_execution_options(options);
361+
auto my_needed_component = async_test::NeededComponent{};
362+
auto another_my_needed_component = async_test::NeededComponent{};
429363

430-
merge_callback_data merge_cb_info{
431-
.request_id = options_request,
432-
.wait = false,
433-
};
364+
options.create_entity()
365+
.add_component(&my_needed_component)
366+
.add_component(&another_my_needed_component);
367+
368+
auto request_id = ecsact_async_enqueue_execution_options(options.c());
369+
370+
auto merge_data = merge_callback_data{};
371+
merge_data.request_id = request_id;
372+
merge_data.wait = false;
373+
374+
auto evc = ecsact_execution_events_collector{};
375+
evc.entity_created_callback = entity_cb;
376+
evc.entity_created_callback_user_data = &cb_info;
377+
378+
auto async_evc = ecsact_async_events_collector{};
434379

435-
ecsact_async_events_collector async_evc{};
436380
async_evc.async_error_callback = async_err_cb;
437-
async_evc.async_error_callback_user_data = &merge_cb_info;
381+
async_evc.async_error_callback_user_data = &merge_data;
438382

439-
start_tick = ecsact_async_get_current_tick();
440-
while(merge_cb_info.wait != true) {
441-
ecsact_async_flush_events(nullptr, &async_evc);
383+
auto start_tick = ecsact_async_get_current_tick();
384+
while(merge_data.wait != true) {
385+
ecsact_async_flush_events(&evc, &async_evc);
442386
auto current_tick = ecsact_async_get_current_tick();
443387
auto tick_diff = current_tick - start_tick;
444388
ASSERT_LT(tick_diff, 10);
445389
}
446390

391+
options.clear();
392+
447393
ecsact_async_disconnect();
448394
}
449395

0 commit comments

Comments
 (0)