blob: c7fc50a1de17cd5e4b33ae5decb3d09a21091bf6 [file] [log] [blame]
Heinrich Schuchardtdcc8ab72020-10-22 21:45:28 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for print functions
4 *
5 * Copyright 2020, Heinrich Schuchadt <xypron.glpk@gmx.de>
6 */
7
Heinrich Schuchardtdcc8ab72020-10-22 21:45:28 +02008#include <command.h>
9#include <display_options.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Heinrich Schuchardtdcc8ab72020-10-22 21:45:28 +020011#include <test/lib.h>
12#include <test/test.h>
13#include <test/ut.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17static int test_print_freq(struct unit_test_state *uts,
18 uint64_t freq, char *expected)
19{
Simon Glass4d294a72021-03-15 18:11:13 +130020 ut_silence_console(uts);
Heinrich Schuchardtdcc8ab72020-10-22 21:45:28 +020021 console_record_reset_enable();
22 print_freq(freq, ";\n");
Simon Glass4d294a72021-03-15 18:11:13 +130023 ut_unsilence_console(uts);
Heinrich Schuchardtdcc8ab72020-10-22 21:45:28 +020024 console_record_readline(uts->actual_str, sizeof(uts->actual_str));
25 ut_asserteq_str(expected, uts->actual_str);
26 ut_assertok(ut_check_console_end(uts));
Simon Glass4d294a72021-03-15 18:11:13 +130027
Heinrich Schuchardtdcc8ab72020-10-22 21:45:28 +020028 return 0;
29}
30
31static int lib_test_print_freq(struct unit_test_state *uts)
32{
33 ut_assertok(test_print_freq(uts, 321, "321 Hz;"));
34 ut_assertok(test_print_freq(uts, 4321, "4.32 kHz;"));
35 ut_assertok(test_print_freq(uts, 54321, "54.32 kHz;"));
36 ut_assertok(test_print_freq(uts, 654321, "654.32 kHz;"));
37 ut_assertok(test_print_freq(uts, 7654321, "7.66 MHz;"));
38 ut_assertok(test_print_freq(uts, 87654321, "87.66 MHz;"));
39 ut_assertok(test_print_freq(uts, 987654321, "987.66 MHz;"));
40 ut_assertok(test_print_freq(uts, 1987654321, "1.99 GHz;"));
41 ut_assertok(test_print_freq(uts, 54321987654321, "54321.99 GHz;"));
42 return 0;
43}
44
45LIB_TEST(lib_test_print_freq, 0);
46
47static int test_print_size(struct unit_test_state *uts,
48 uint64_t freq, char *expected)
49{
Simon Glass4d294a72021-03-15 18:11:13 +130050 ut_silence_console(uts);
Heinrich Schuchardtdcc8ab72020-10-22 21:45:28 +020051 console_record_reset_enable();
52 print_size(freq, ";\n");
Simon Glass4d294a72021-03-15 18:11:13 +130053 ut_unsilence_console(uts);
Heinrich Schuchardtdcc8ab72020-10-22 21:45:28 +020054 console_record_readline(uts->actual_str, sizeof(uts->actual_str));
55 ut_asserteq_str(expected, uts->actual_str);
56 ut_assertok(ut_check_console_end(uts));
Simon Glass4d294a72021-03-15 18:11:13 +130057
Heinrich Schuchardtdcc8ab72020-10-22 21:45:28 +020058 return 0;
59}
60
61static int lib_test_print_size(struct unit_test_state *uts)
62{
63 ut_assertok(test_print_size(uts, 321, "321 Bytes;"));
64 ut_assertok(test_print_size(uts, 4321, "4.2 KiB;"));
65 ut_assertok(test_print_size(uts, 54321, "53 KiB;"));
66 ut_assertok(test_print_size(uts, 654321, "639 KiB;"));
67 ut_assertok(test_print_size(uts, 7654321, "7.3 MiB;"));
68 ut_assertok(test_print_size(uts, 87654321, "83.6 MiB;"));
69 ut_assertok(test_print_size(uts, 987654321, "941.9 MiB;"));
Pali Rohárc14fad52022-09-12 21:02:27 +020070 ut_assertok(test_print_size(uts, 1073689395, "1023.9 MiB;"));
71 ut_assertok(test_print_size(uts, 1073689396, "1 GiB;"));
72 ut_assertok(test_print_size(uts, 1073741824, "1 GiB;"));
Heinrich Schuchardtdcc8ab72020-10-22 21:45:28 +020073 ut_assertok(test_print_size(uts, 1987654321, "1.9 GiB;"));
74 ut_assertok(test_print_size(uts, 54321987654321, "49.4 TiB;"));
75 return 0;
76}
77
78LIB_TEST(lib_test_print_size, 0);