[RESEND PATCH v3 28/29] test: serial: Add test for putc/puts

Sean Anderson sean.anderson at seco.com
Tue Mar 22 22:19:07 CET 2022


This adds a test to ensure that puts is equivalent to putc called in a
loop. We don't verify the contents of the message to avoid having to
record console output a second time (though that could be added in the
future). The globals are initialized to non-zero values to avoid a
warning; in particular, the character count is off-by-one (but we always
make relative measurements).

Signed-off-by: Sean Anderson <sean.anderson at seco.com>
---

Changes in v3:
- New

 arch/sandbox/include/asm/serial.h |  6 ++++++
 drivers/serial/sandbox.c          | 23 +++++++++++++++++++----
 test/dm/serial.c                  | 19 +++++++++++++++++++
 3 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/arch/sandbox/include/asm/serial.h b/arch/sandbox/include/asm/serial.h
index bc82aebd0e..7bce240268 100644
--- a/arch/sandbox/include/asm/serial.h
+++ b/arch/sandbox/include/asm/serial.h
@@ -16,6 +16,12 @@ struct sandbox_serial_plat {
 	int colour;	/* Text colour to use for output, -1 for none */
 };
 
+/* Total number of characters written; for use by tests */
+extern size_t sandbox_serial_written;
+
+/* Disable serial device output; for use by tests */
+extern bool sandbox_serial_enabled;
+
 /**
  * struct sandbox_serial_priv - Private data for this driver
  *
diff --git a/drivers/serial/sandbox.c b/drivers/serial/sandbox.c
index 50cf2c74a7..6d42d87229 100644
--- a/drivers/serial/sandbox.c
+++ b/drivers/serial/sandbox.c
@@ -23,6 +23,9 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+size_t sandbox_serial_written = 1;
+bool sandbox_serial_enabled = true;
+
 /**
  * output_ansi_colour() - Output an ANSI colour code
  *
@@ -84,11 +87,14 @@ static int sandbox_serial_putc(struct udevice *dev, const char ch)
 {
 	struct sandbox_serial_priv *priv = dev_get_priv(dev);
 
-	sandbox_print_color(dev);
-	os_write(1, &ch, 1);
 	if (ch == '\n')
 		priv->start_of_line = true;
 
+	if (sandbox_serial_enabled) {
+		sandbox_print_color(dev);
+		os_write(1, &ch, 1);
+	}
+	sandbox_serial_written += 1;
 	return 0;
 }
 
@@ -96,12 +102,21 @@ static ssize_t sandbox_serial_puts(struct udevice *dev, const char *s,
 				   size_t len)
 {
 	struct sandbox_serial_priv *priv = dev_get_priv(dev);
+	ssize_t ret;
 
-	sandbox_print_color(dev);
 	if (s[len - 1] == '\n')
 		priv->start_of_line = true;
 
-	return os_write(1, s, len);
+	if (sandbox_serial_enabled) {
+		sandbox_print_color(dev);
+		ret = os_write(1, s, len);
+		if (ret < 0)
+			return ret;
+	} else {
+		ret = len;
+	}
+	sandbox_serial_written += ret;
+	return ret;
 }
 
 static int sandbox_serial_pending(struct udevice *dev, bool input)
diff --git a/test/dm/serial.c b/test/dm/serial.c
index 0662b5f09b..a0f360ba43 100644
--- a/test/dm/serial.c
+++ b/test/dm/serial.c
@@ -7,14 +7,22 @@
 #include <log.h>
 #include <serial.h>
 #include <dm.h>
+#include <asm/serial.h>
 #include <dm/test.h>
 #include <test/test.h>
 #include <test/ut.h>
 
+static const char test_message[] =
+	"This is a test message\n"
+	"consisting of multiple lines\n";
+
 static int dm_test_serial(struct unit_test_state *uts)
 {
+	int i;
 	struct serial_device_info info_serial = {0};
 	struct udevice *dev_serial;
+	size_t start, putc_written;
+
 	uint value_serial;
 
 	ut_assertok(uclass_get_device_by_name(UCLASS_SERIAL, "serial",
@@ -66,6 +74,17 @@ static int dm_test_serial(struct unit_test_state *uts)
 						   SERIAL_8_BITS,
 						   SERIAL_TWO_STOP)));
 
+	/* Verify that putc and puts print the same number of characters */
+	sandbox_serial_enabled = false;
+	start = sandbox_serial_written;
+	for (i = 0; i < sizeof(test_message) - 1; i++)
+		serial_putc(test_message[i]);
+	putc_written = sandbox_serial_written;
+	serial_puts(test_message);
+	sandbox_serial_enabled = true;
+	ut_asserteq(putc_written - start,
+		    sandbox_serial_written - putc_written);
+
 	return 0;
 }
 
-- 
2.25.1



More information about the U-Boot mailing list