From 07f82bc0c72045d193476c1dc38a36b2e37425d4 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Tue, 3 Oct 2023 15:26:40 -0700 Subject: [PATCH] Don't throw error when creating dotmember metric with CounterIncrement kind --- flecs.c | 6 +- src/addons/metrics.c | 6 +- test/addons/project.json | 4 +- test/addons/src/Metrics.c | 158 ++++++++++++++++++++++++++++++++++++++ test/addons/src/main.c | 12 ++- 5 files changed, 178 insertions(+), 8 deletions(-) diff --git a/flecs.c b/flecs.c index b0d41dccdb..36eb3ca464 100644 --- a/flecs.c +++ b/flecs.c @@ -29276,13 +29276,13 @@ ecs_entity_t ecs_metric_init( goto error; } - if (kind == EcsCounterIncrement && !desc->member) { + if (kind == EcsCounterIncrement && !desc->member && !desc->dotmember) { ecs_err("CounterIncrement can only be used in combination with member"); goto error; } - if (kind == EcsCounterId && desc->member) { - ecs_err("CounterIncrement cannot be used in combination with member"); + if (kind == EcsCounterId && (desc->member || desc->dotmember)) { + ecs_err("CounterId cannot be used in combination with member"); goto error; } diff --git a/src/addons/metrics.c b/src/addons/metrics.c index 4ad7950a81..51495819ab 100644 --- a/src/addons/metrics.c +++ b/src/addons/metrics.c @@ -728,13 +728,13 @@ ecs_entity_t ecs_metric_init( goto error; } - if (kind == EcsCounterIncrement && !desc->member) { + if (kind == EcsCounterIncrement && !desc->member && !desc->dotmember) { ecs_err("CounterIncrement can only be used in combination with member"); goto error; } - if (kind == EcsCounterId && desc->member) { - ecs_err("CounterIncrement cannot be used in combination with member"); + if (kind == EcsCounterId && (desc->member || desc->dotmember)) { + ecs_err("CounterId cannot be used in combination with member"); goto error; } diff --git a/test/addons/project.json b/test/addons/project.json index a1ce9d3d9d..59effbc260 100644 --- a/test/addons/project.json +++ b/test/addons/project.json @@ -1577,7 +1577,9 @@ "id_count", "id_target_count", "metric_instance_has_doc_name", - "metric_nested_member" + "metric_nested_member", + "metric_nested_member_counter", + "metric_nested_member_counter_increment" ] }, { "id": "Alerts", diff --git a/test/addons/src/Metrics.c b/test/addons/src/Metrics.c index 59ac2c19c7..56edee8739 100644 --- a/test/addons/src/Metrics.c +++ b/test/addons/src/Metrics.c @@ -1889,3 +1889,161 @@ void Metrics_metric_nested_member(void) { ecs_fini(world); } + +void Metrics_metric_nested_member_counter(void) { + typedef struct { + float x, y; + } Point; + + typedef struct { + float dummy; + Point position; + } Position; + + ecs_world_t *world = ecs_init(); + ECS_IMPORT(world, FlecsMetrics); + + ECS_COMPONENT(world, Point); + ECS_COMPONENT(world, Position); + + ecs_struct(world, { + .entity = ecs_id(Point), + .members = { + { "x", ecs_id(ecs_f32_t) }, + { "y", ecs_id(ecs_f32_t) }, + } + }); + + ecs_struct(world, { + .entity = ecs_id(Position), + .members = { + { "dummy", ecs_id(ecs_f32_t) }, + { "position", ecs_id(Point) }, + } + }); + + ecs_entity_t m = ecs_metric(world, { + .entity = ecs_entity(world, { .name = "metrics.position_y" }), + .id = ecs_id(Position), + .dotmember = "position.y", + .kind = EcsCounter + }); + + test_assert(m != 0); + + ecs_entity_t e1 = ecs_new_entity(world, "e1"); + ecs_set(world, e1, Position, {10, {20, 30}}); + + ecs_progress(world, 0); + + ecs_iter_t it = ecs_children(world, m); + test_bool(true, ecs_children_next(&it)); + test_uint(it.count, 1); + { + ecs_entity_t e = it.entities[0]; + test_assert(ecs_has_pair(world, e, EcsMetric, EcsCounter)); + const EcsMetricSource *src = ecs_get(world, e, EcsMetricSource); + const EcsMetricValue *inst = ecs_get(world, e, EcsMetricValue); + test_assert(src != NULL); + test_assert(inst != NULL); + test_uint(src->entity, e1); + test_int(inst->value, 30); + + test_str(ecs_doc_get_name(world, e), "e1"); + } + + test_bool(false, ecs_children_next(&it)); + + ecs_fini(world); +} + +void Metrics_metric_nested_member_counter_increment(void) { + typedef struct { + float x, y; + } Point; + + typedef struct { + float dummy; + Point position; + } Position; + + ecs_world_t *world = ecs_init(); + ECS_IMPORT(world, FlecsMetrics); + + ECS_COMPONENT(world, Point); + ECS_COMPONENT(world, Position); + + ecs_struct(world, { + .entity = ecs_id(Point), + .members = { + { "x", ecs_id(ecs_f32_t) }, + { "y", ecs_id(ecs_f32_t) }, + } + }); + + ecs_struct(world, { + .entity = ecs_id(Position), + .members = { + { "dummy", ecs_id(ecs_f32_t) }, + { "position", ecs_id(Point) }, + } + }); + + ecs_entity_t m = ecs_metric(world, { + .entity = ecs_entity(world, { .name = "metrics.position_y" }), + .id = ecs_id(Position), + .dotmember = "position.y", + .kind = EcsCounterIncrement + }); + + test_assert(m != 0); + + ecs_entity_t e1 = ecs_new_entity(world, "e1"); + ecs_set(world, e1, Position, {10, {20, 30}}); + + ecs_progress(world, 1); + + { + ecs_iter_t it = ecs_children(world, m); + test_bool(true, ecs_children_next(&it)); + test_uint(it.count, 1); + { + ecs_entity_t e = it.entities[0]; + test_assert(ecs_has_pair(world, e, EcsMetric, EcsCounterIncrement)); + const EcsMetricSource *src = ecs_get(world, e, EcsMetricSource); + const EcsMetricValue *inst = ecs_get(world, e, EcsMetricValue); + test_assert(src != NULL); + test_assert(inst != NULL); + test_uint(src->entity, e1); + test_int(inst->value, 30); + + test_str(ecs_doc_get_name(world, e), "e1"); + } + + test_bool(false, ecs_children_next(&it)); + } + + ecs_progress(world, 1); + + { + ecs_iter_t it = ecs_children(world, m); + test_bool(true, ecs_children_next(&it)); + test_uint(it.count, 1); + { + ecs_entity_t e = it.entities[0]; + test_assert(ecs_has_pair(world, e, EcsMetric, EcsCounterIncrement)); + const EcsMetricSource *src = ecs_get(world, e, EcsMetricSource); + const EcsMetricValue *inst = ecs_get(world, e, EcsMetricValue); + test_assert(src != NULL); + test_assert(inst != NULL); + test_uint(src->entity, e1); + test_int(inst->value, 60); + + test_str(ecs_doc_get_name(world, e), "e1"); + } + + test_bool(false, ecs_children_next(&it)); + } + + ecs_fini(world); +} diff --git a/test/addons/src/main.c b/test/addons/src/main.c index 3bd704abe7..39ff22269f 100644 --- a/test/addons/src/main.c +++ b/test/addons/src/main.c @@ -1507,6 +1507,8 @@ void Metrics_id_count(void); void Metrics_id_target_count(void); void Metrics_metric_instance_has_doc_name(void); void Metrics_metric_nested_member(void); +void Metrics_metric_nested_member_counter(void); +void Metrics_metric_nested_member_counter_increment(void); // Testsuite 'Alerts' void Alerts_one_active_alert(void); @@ -7341,6 +7343,14 @@ bake_test_case Metrics_testcases[] = { { "metric_nested_member", Metrics_metric_nested_member + }, + { + "metric_nested_member_counter", + Metrics_metric_nested_member_counter + }, + { + "metric_nested_member_counter_increment", + Metrics_metric_nested_member_counter_increment } }; @@ -7727,7 +7737,7 @@ static bake_test_suite suites[] = { "Metrics", NULL, NULL, - 28, + 30, Metrics_testcases }, {