feat(scheduler-sim-decision-trace): move units and value formatting helpers into repl_fmt.c

Task 1.2: translate_units_mode, repl_format_numeric_resource_value,
repl_format_consumable_value_and_used and repl_current_units_mode move
unchanged from repl_info.c and are declared in repl_internal.h so the
trace renderer can reuse them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Rongsong Shen 2026-10-08 07:30:42 +00:00
parent ce757eaaa1
commit 7f40ff27dc
Signed by untrusted user: rshen
GPG key ID: 80533536B0EB651C
5 changed files with 269 additions and 127 deletions

View file

@ -89,6 +89,17 @@
#include <unity/unity.h>
/* scheduler-sim-decision-trace spec, task 1.2 (Req 7.7): the units-mode
* translation, numeric/consumable value formatters and current-units
* lookup moved from repl_info.c (file-static) to repl_fmt.c, declared in
* repl_internal.h so other REPL files (repl_trace_render.c) can reuse them.
* Included here -- a translation unit separate from repl_info.c/repl_fmt.c,
* which this target links as objects -- to prove the declarations are
* reachable from another file. */
#include "repl_internal.h"
#include "sim.h"
#include "utils.h"
/*
* repl_is_bytes_valued_resource / repl_format_bytes_human (task 7.2, NOT YET
* IMPLEMENTED -- see this file's own header comment above for why these are
@ -311,6 +322,90 @@ static void test_swp_value_converted_mb_to_bytes_renders_on_same_adaptive_scale(
"scale: PASS\n");
}
/* ---------------------------------------------------------------------- */
/* scheduler-sim-decision-trace task 1.2 (Req 7.7): the shared units/value */
/* formatting helpers are callable from a separate translation unit through */
/* repl_internal.h, with their pre-move behaviour unchanged. */
/* ---------------------------------------------------------------------- */
static void test_shared_translate_units_mode_maps_each_repl_mode(void)
{
TEST_ASSERT_EQUAL_INT(XCE_UNITS_RAW,
translate_units_mode(XCE_REPL_UNITS_RAW));
TEST_ASSERT_EQUAL_INT(XCE_UNITS_HUMAN,
translate_units_mode(XCE_REPL_UNITS_HUMAN));
TEST_ASSERT_EQUAL_INT(XCE_UNITS_BOTH,
translate_units_mode(XCE_REPL_UNITS_BOTH));
}
static void test_shared_numeric_resource_value_formats_per_units_mode(void)
{
char buf[128];
char want[128];
/* Unitless: whole number has no ".00"; fraction keeps two decimals. */
repl_format_numeric_resource_value(XCE_REPL_UNITS_BOTH, "r1m", 3.0, buf,
sizeof(buf));
TEST_ASSERT_EQUAL_STRING("3", buf);
repl_format_numeric_resource_value(XCE_REPL_UNITS_RAW, "r1m", 2.5, buf,
sizeof(buf));
TEST_ASSERT_EQUAL_STRING("2.50", buf);
/* Bytes-valued: delegated to xce_format_units_value() per mode. */
repl_format_numeric_resource_value(XCE_REPL_UNITS_HUMAN, "mem", 1024.0,
buf, sizeof(buf));
TEST_ASSERT_EQUAL_STRING("1.00 KB", buf);
repl_format_numeric_resource_value(XCE_REPL_UNITS_RAW, "mem", 1024.0, buf,
sizeof(buf));
TEST_ASSERT_EQUAL_STRING("1024.000000", buf);
repl_format_numeric_resource_value(XCE_REPL_UNITS_BOTH, "mem", 1024.0,
buf, sizeof(buf));
TEST_ASSERT_EQUAL_STRING("1024.000000 (1.00 KB)", buf);
/* swp: megabytes on the wire, converted to bytes before formatting. */
repl_format_numeric_resource_value(XCE_REPL_UNITS_BOTH, "swp", 2.0, buf,
sizeof(buf));
xce_format_units_value(XCE_UNITS_BOTH, 2.0 * 1024.0 * 1024.0, want,
sizeof(want));
TEST_ASSERT_EQUAL_STRING(want, buf);
}
static void test_shared_consumable_value_and_used_formats(void)
{
char vbuf[128];
char ubuf[128];
/* Non-bytes consumable keeps the pinned "%.2f"/"%.2f" shape. */
repl_format_consumable_value_and_used(XCE_REPL_UNITS_BOTH, "slots", 4.0,
1.0, vbuf, sizeof(vbuf), ubuf,
sizeof(ubuf));
TEST_ASSERT_EQUAL_STRING("4.00", vbuf);
TEST_ASSERT_EQUAL_STRING("1.00", ubuf);
/* Bytes-valued consumable formats both sides per the units mode. */
repl_format_consumable_value_and_used(XCE_REPL_UNITS_HUMAN, "mem",
2048.0, 1024.0, vbuf, sizeof(vbuf),
ubuf, sizeof(ubuf));
TEST_ASSERT_EQUAL_STRING("2.00 KB", vbuf);
TEST_ASSERT_EQUAL_STRING("1.00 KB", ubuf);
}
static void test_shared_current_units_mode_reads_state_or_defaults(void)
{
xce_sched_sim_driver_t sim;
xce_sched_repl_state_t st;
memset(&sim, 0, sizeof(sim));
memset(&st, 0, sizeof(st));
/* No REPL state: falls back to the documented BOTH default. */
TEST_ASSERT_EQUAL_INT(XCE_REPL_UNITS_BOTH, repl_current_units_mode(&sim));
st.units_mode = XCE_REPL_UNITS_RAW;
sim.repl_state = &st;
TEST_ASSERT_EQUAL_INT(XCE_REPL_UNITS_RAW, repl_current_units_mode(&sim));
}
int main(void)
{
UNITY_BEGIN();
@ -322,6 +417,10 @@ int main(void)
RUN_TEST(test_format_bytes_human_fractional_values_render_two_decimals);
RUN_TEST(
test_swp_value_converted_mb_to_bytes_renders_on_same_adaptive_scale);
RUN_TEST(test_shared_translate_units_mode_maps_each_repl_mode);
RUN_TEST(test_shared_numeric_resource_value_formats_per_units_mode);
RUN_TEST(test_shared_consumable_value_and_used_formats);
RUN_TEST(test_shared_current_units_mode_reads_state_or_defaults);
return UNITY_END();
}