All of lore.kernel.org
 help / color / mirror / Atom feed
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: u-boot@lists.denx.de
Subject: [PATCH 2/2] test: unit tests for print_freq(), print_size()
Date: Thu, 22 Oct 2020 21:45:28 +0200	[thread overview]
Message-ID: <20201022194528.5591-3-xypron.glpk@gmx.de> (raw)
In-Reply-To: <20201022194528.5591-1-xypron.glpk@gmx.de>

Provide unit tests for functions print_freq() and print_size().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
v2:
	add missing add missing test/lib/test_print.c
---
 test/lib/Makefile     |  1 +
 test/lib/test_print.c | 71 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
 create mode 100644 test/lib/test_print.c

diff --git a/test/lib/Makefile b/test/lib/Makefile
index 22236f8587..15cd512506 100644
--- a/test/lib/Makefile
+++ b/test/lib/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_EFI_LOADER) += efi_device_path.o
 obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o
 obj-y += hexdump.o
 obj-y += lmb.o
+obj-y += test_print.o
 obj-$(CONFIG_SSCANF) += sscanf.o
 obj-y += string.o
 obj-$(CONFIG_ERRNO_STR) += test_errno_str.o
diff --git a/test/lib/test_print.c b/test/lib/test_print.c
new file mode 100644
index 0000000000..1d497d0041
--- /dev/null
+++ b/test/lib/test_print.c
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for print functions
+ *
+ * Copyright 2020, Heinrich Schuchadt <xypron.glpk@gmx.de>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <display_options.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)
+{
+	console_record_reset_enable();
+	print_freq(freq, ";\n");
+	gd->flags &= ~GD_FLG_RECORD;
+	console_record_readline(uts->actual_str, sizeof(uts->actual_str));
+	ut_asserteq_str(expected, uts->actual_str);
+	ut_assertok(ut_check_console_end(uts));
+	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, 0);
+
+static int test_print_size(struct unit_test_state *uts,
+			   uint64_t freq, char *expected)
+{
+	console_record_reset_enable();
+	print_size(freq, ";\n");
+	gd->flags &= ~GD_FLG_RECORD;
+	console_record_readline(uts->actual_str, sizeof(uts->actual_str));
+	ut_asserteq_str(expected, uts->actual_str);
+	ut_assertok(ut_check_console_end(uts));
+	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, 1987654321, "1.9 GiB;"));
+	ut_assertok(test_print_size(uts, 54321987654321, "49.4 TiB;"));
+	return 0;
+}
+
+LIB_TEST(lib_test_print_size, 0);
--
2.28.0

  parent reply	other threads:[~2020-10-22 19:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-22 19:45 [PATCH 0/2] lib: print_freq() should output kHz not KHz Heinrich Schuchardt
2020-10-22 19:45 ` [PATCH 1/2] " Heinrich Schuchardt
2020-10-22 19:45 ` Heinrich Schuchardt [this message]
2020-10-24 14:52   ` [PATCH 2/2] test: unit tests for print_freq(), print_size() Tom Rini
  -- strict thread matches above, loose matches on Subject: below --
2020-10-08 20:23 [PATCH 0/2] lib: print_freq() should output kHz not KHz Heinrich Schuchardt
2020-10-08 20:23 ` [PATCH 2/2] test: unit tests for print_freq(), print_size() Heinrich Schuchardt
2020-10-09  1:23   ` Sean Anderson
2020-10-22 13:56   ` Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201022194528.5591-3-xypron.glpk@gmx.de \
    --to=xypron.glpk@gmx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.