Re: [PATCH 2/2] kfifo: add kfifo_skip() testcase

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Andrea Righi
Date: Thursday, August 12, 2010 - 3:28 pm

On Thu, Aug 12, 2010 at 02:22:11PM -0700, Andrew Morton wrote:

Agreed. Something like this is probably more meaningful.

---
kfifo: add explicit error checking in byte stream example

Provide a static array of expected items that kfifo should contain at
the end of the test to validate it.

Signed-off-by: Andrea Righi <arighi@develer.com>
---
 samples/kfifo/bytestream-example.c |   33 +++++++++++++++++++++++++++------
 1 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/samples/kfifo/bytestream-example.c b/samples/kfifo/bytestream-example.c
index 2e3a7a8..a94e694 100644
--- a/samples/kfifo/bytestream-example.c
+++ b/samples/kfifo/bytestream-example.c
@@ -44,10 +44,17 @@ static struct kfifo test;
 static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
 #endif
 
+static unsigned char expected_result[FIFO_SIZE] = {
+	 3,  4,  5,  6,  7,  8,  9,  0,
+	 1, 20, 21, 22, 23, 24, 25, 26,
+	27, 28, 29, 30, 31, 32, 33, 34,
+	35, 36, 37, 38, 39, 40, 41, 42,
+};
+
 static int __init testfunc(void)
 {
 	unsigned char	buf[6];
-	unsigned char	i;
+	unsigned char	i, j;
 	unsigned int	ret;
 
 	printk(KERN_INFO "byte stream fifo test start\n");
@@ -83,10 +90,19 @@ static int __init testfunc(void)
 
 	printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
 
-	/* print out all values in the fifo */
-	while (kfifo_get(&test, &i))
-		printk("%d ", i);
-	printk("\n");
+	/* check the correctness of all values in the fifo */
+	j = 0;
+	while (kfifo_get(&test, &i)) {
+		if (i != expected_result[j++]) {
+			printk(KERN_WARNING "value mismatch: test failed\n");
+			return -EIO;
+		}
+	}
+	if (j != ARRAY_SIZE(expected_result)) {
+		printk(KERN_WARNING "size mismatch: test failed\n");
+		return -EIO;
+	}
+	printk(KERN_INFO "test passed\n");
 
 	return 0;
 }
@@ -142,7 +158,12 @@ static int __init example_init(void)
 #else
 	INIT_KFIFO(test);
 #endif
-	testfunc();
+	if (testfunc() < 0) {
+#ifdef DYNAMIC
+		kfifo_free(&test);
+#endif
+		return -EIO;
+	}
 
 	if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
 #ifdef DYNAMIC

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 1/2] kfifo: implement missing __kfifo_skip_r(), Andrea Righi, (Thu Aug 12, 8:32 am)
[PATCH 2/2] kfifo: add kfifo_skip() testcase, Andrea Righi, (Thu Aug 12, 8:32 am)
Re: [PATCH 1/2] kfifo: implement missing __kfifo_skip_r(), Stefani Seibold, (Thu Aug 12, 1:45 pm)
Re: [PATCH 2/2] kfifo: add kfifo_skip() testcase, Stefani Seibold, (Thu Aug 12, 1:46 pm)
Re: [PATCH 2/2] kfifo: add kfifo_skip() testcase, Andrew Morton, (Thu Aug 12, 2:22 pm)
Re: [PATCH 2/2] kfifo: add kfifo_skip() testcase, Andrea Righi, (Thu Aug 12, 3:28 pm)