Refactor print_size() to use a new format_size() helper that formats a size into a buffer. This allows callers to get the formatted string without printing it directly. The format_size() function is only exported in U-Boot proper (controlled by CONFIG_LIB_FORMAT_SIZE) to avoid code-size impact in SPL/TPL where it remains static. Co-developed-by: Claude <noreply@anthropic.com> Signed-off-by: Simon Glass <simon.glass@canonical.com>
85 lines
2.7 KiB
C
85 lines
2.7 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/*
|
|
* Tests for print functions
|
|
*
|
|
* Copyright 2020, Heinrich Schuchadt <xypron.glpk@gmx.de>
|
|
*/
|
|
|
|
#include <command.h>
|
|
#include <display_options.h>
|
|
#include <asm/global_data.h>
|
|
#include <test/lib.h>
|
|
#include <test/test.h>
|
|
#include <test/ut.h>
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
static int test_print_freq(struct unit_test_state *uts,
|
|
uint64_t freq, char *expected)
|
|
{
|
|
print_freq(freq, ";\n");
|
|
console_record_readline(uts->actual_str, sizeof(uts->actual_str));
|
|
ut_asserteq_str(expected, uts->actual_str);
|
|
ut_assert_console_end();
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int lib_test_print_freq(struct unit_test_state *uts)
|
|
{
|
|
ut_assertok(test_print_freq(uts, 321, "321 Hz;"));
|
|
ut_assertok(test_print_freq(uts, 4321, "4.32 kHz;"));
|
|
ut_assertok(test_print_freq(uts, 54321, "54.32 kHz;"));
|
|
ut_assertok(test_print_freq(uts, 654321, "654.32 kHz;"));
|
|
ut_assertok(test_print_freq(uts, 7654321, "7.66 MHz;"));
|
|
ut_assertok(test_print_freq(uts, 87654321, "87.66 MHz;"));
|
|
ut_assertok(test_print_freq(uts, 987654321, "987.66 MHz;"));
|
|
ut_assertok(test_print_freq(uts, 1987654321, "1.99 GHz;"));
|
|
ut_assertok(test_print_freq(uts, 54321987654321, "54321.99 GHz;"));
|
|
return 0;
|
|
}
|
|
LIB_TEST(lib_test_print_freq, UTF_CONSOLE);
|
|
|
|
static int test_print_size(struct unit_test_state *uts,
|
|
uint64_t freq, char *expected)
|
|
{
|
|
print_size(freq, ";\n");
|
|
console_record_readline(uts->actual_str, sizeof(uts->actual_str));
|
|
ut_asserteq_str(expected, uts->actual_str);
|
|
ut_assert_console_end();
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int lib_test_print_size(struct unit_test_state *uts)
|
|
{
|
|
ut_assertok(test_print_size(uts, 321, "321 Bytes;"));
|
|
ut_assertok(test_print_size(uts, 4321, "4.2 KiB;"));
|
|
ut_assertok(test_print_size(uts, 54321, "53 KiB;"));
|
|
ut_assertok(test_print_size(uts, 654321, "639 KiB;"));
|
|
ut_assertok(test_print_size(uts, 7654321, "7.3 MiB;"));
|
|
ut_assertok(test_print_size(uts, 87654321, "83.6 MiB;"));
|
|
ut_assertok(test_print_size(uts, 987654321, "941.9 MiB;"));
|
|
ut_assertok(test_print_size(uts, 1073689395, "1023.9 MiB;"));
|
|
ut_assertok(test_print_size(uts, 1073689396, "1 GiB;"));
|
|
ut_assertok(test_print_size(uts, 1073741824, "1 GiB;"));
|
|
ut_assertok(test_print_size(uts, 1987654321, "1.9 GiB;"));
|
|
ut_assertok(test_print_size(uts, 54321987654321, "49.4 TiB;"));
|
|
return 0;
|
|
}
|
|
LIB_TEST(lib_test_print_size, UTF_CONSOLE);
|
|
|
|
static int lib_test_format_size(struct unit_test_state *uts)
|
|
{
|
|
char buf[12];
|
|
|
|
ut_asserteq_str("321 Bytes", format_size(buf, 321));
|
|
ut_asserteq_str("4.2 KiB", format_size(buf, 4321));
|
|
ut_asserteq_str("53 KiB", format_size(buf, 54321));
|
|
ut_asserteq_str("1 GiB", format_size(buf, 1073741824));
|
|
ut_asserteq_str("49.4 TiB", format_size(buf, 54321987654321));
|
|
|
|
return 0;
|
|
}
|
|
LIB_TEST(lib_test_format_size, 0);
|