Skip to content

Commit b9b702c

Browse files
committed
Update util to validate, fix bad test (sigh)
1 parent b733b08 commit b9b702c

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);
@@ -40,6 +40,29 @@ static ecsact_async_error validate_instructions(
4040
return ECSACT_ASYNC_OK;
4141
}
4242

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

302325
if(options.adds.size() > 0) {
303-
error = validate_instructions(options.adds);
326+
error = validate_entity_instructions(options.adds);
304327
if(error != ECSACT_ASYNC_OK) {
305328
return error;
306329
}
307330
}
308331

309332
if(options.updates.size() > 0) {
310-
error = validate_instructions(options.updates);
333+
error = validate_entity_instructions(options.updates);
311334
if(error != ECSACT_ASYNC_OK) {
312335
return error;
313336
}
314337
}
315338

316339
if(options.removes.size() > 0) {
317-
error = validate_instructions(options.removes);
340+
error = validate_entity_instructions(options.removes);
318341
if(error != ECSACT_ASYNC_OK) {
319342
return error;
320343
}
321344
}
322345

346+
if(options.create_entities.size() > 0) {
347+
for(auto& entity_to_create : options.create_entities) {
348+
error = validate_instructions(entity_to_create.components);
349+
if(error != ECSACT_ASYNC_OK) {
350+
return error;
351+
}
352+
}
353+
}
354+
323355
if(options.destroy_entities.size() > 0) {
324356
auto entities = std::views::all(options.destroy_entities);
325357
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)