Heinrich Schuchardt | f52e925 | 2020-10-22 21:45:28 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Tests for print functions |
| 4 | * |
| 5 | * Copyright 2020, Heinrich Schuchadt <xypron.glpk@gmx.de> |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
| 10 | #include <display_options.h> |
| 11 | #include <test/lib.h> |
| 12 | #include <test/test.h> |
| 13 | #include <test/ut.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | static int test_print_freq(struct unit_test_state *uts, |
| 18 | uint64_t freq, char *expected) |
| 19 | { |
| 20 | console_record_reset_enable(); |
| 21 | print_freq(freq, ";\n"); |
| 22 | gd->flags &= ~GD_FLG_RECORD; |
| 23 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); |
| 24 | ut_asserteq_str(expected, uts->actual_str); |
| 25 | ut_assertok(ut_check_console_end(uts)); |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | static int lib_test_print_freq(struct unit_test_state *uts) |
| 30 | { |
| 31 | ut_assertok(test_print_freq(uts, 321, "321 Hz;")); |
| 32 | ut_assertok(test_print_freq(uts, 4321, "4.32 kHz;")); |
| 33 | ut_assertok(test_print_freq(uts, 54321, "54.32 kHz;")); |
| 34 | ut_assertok(test_print_freq(uts, 654321, "654.32 kHz;")); |
| 35 | ut_assertok(test_print_freq(uts, 7654321, "7.66 MHz;")); |
| 36 | ut_assertok(test_print_freq(uts, 87654321, "87.66 MHz;")); |
| 37 | ut_assertok(test_print_freq(uts, 987654321, "987.66 MHz;")); |
| 38 | ut_assertok(test_print_freq(uts, 1987654321, "1.99 GHz;")); |
| 39 | ut_assertok(test_print_freq(uts, 54321987654321, "54321.99 GHz;")); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | LIB_TEST(lib_test_print_freq, 0); |
| 44 | |
| 45 | static int test_print_size(struct unit_test_state *uts, |
| 46 | uint64_t freq, char *expected) |
| 47 | { |
| 48 | console_record_reset_enable(); |
| 49 | print_size(freq, ";\n"); |
| 50 | gd->flags &= ~GD_FLG_RECORD; |
| 51 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); |
| 52 | ut_asserteq_str(expected, uts->actual_str); |
| 53 | ut_assertok(ut_check_console_end(uts)); |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int lib_test_print_size(struct unit_test_state *uts) |
| 58 | { |
| 59 | ut_assertok(test_print_size(uts, 321, "321 Bytes;")); |
| 60 | ut_assertok(test_print_size(uts, 4321, "4.2 KiB;")); |
| 61 | ut_assertok(test_print_size(uts, 54321, "53 KiB;")); |
| 62 | ut_assertok(test_print_size(uts, 654321, "639 KiB;")); |
| 63 | ut_assertok(test_print_size(uts, 7654321, "7.3 MiB;")); |
| 64 | ut_assertok(test_print_size(uts, 87654321, "83.6 MiB;")); |
| 65 | ut_assertok(test_print_size(uts, 987654321, "941.9 MiB;")); |
| 66 | ut_assertok(test_print_size(uts, 1987654321, "1.9 GiB;")); |
| 67 | ut_assertok(test_print_size(uts, 54321987654321, "49.4 TiB;")); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | LIB_TEST(lib_test_print_size, 0); |