Skip to content

Commit 2aaf209

Browse files
Andrea Righitorvalds
authored andcommitted
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> Cc: Stefani Seibold <stefani@seibold.net> Cc: Greg KH <greg@kroah.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5ddf839 commit 2aaf209

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

‎samples/kfifo/bytestream-example.c‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,17 @@ static struct kfifo test;
4444
static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
4545
#endif
4646

47+
static unsigned char expected_result[FIFO_SIZE] = {
48+
3, 4, 5, 6, 7, 8, 9, 0,
49+
1, 20, 21, 22, 23, 24, 25, 26,
50+
27, 28, 29, 30, 31, 32, 33, 34,
51+
35, 36, 37, 38, 39, 40, 41, 42,
52+
};
53+
4754
static int __init testfunc(void)
4855
{
4956
unsigned char buf[6];
50-
unsigned char i;
57+
unsigned char i, j;
5158
unsigned int ret;
5259

5360
printk(KERN_INFO "byte stream fifo test start\n");
@@ -83,10 +90,19 @@ static int __init testfunc(void)
8390

8491
printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
8592

86-
/* print out all values in the fifo */
87-
while (kfifo_get(&test, &i))
88-
printk("%d ", i);
89-
printk("\n");
93+
/* check the correctness of all values in the fifo */
94+
j = 0;
95+
while (kfifo_get(&test, &i)) {
96+
if (i != expected_result[j++]) {
97+
printk(KERN_WARNING "value mismatch: test failed\n");
98+
return -EIO;
99+
}
100+
}
101+
if (j != ARRAY_SIZE(expected_result)) {
102+
printk(KERN_WARNING "size mismatch: test failed\n");
103+
return -EIO;
104+
}
105+
printk(KERN_INFO "test passed\n");
90106

91107
return 0;
92108
}
@@ -142,7 +158,12 @@ static int __init example_init(void)
142158
#else
143159
INIT_KFIFO(test);
144160
#endif
145-
testfunc();
161+
if (testfunc() < 0) {
162+
#ifdef DYNAMIC
163+
kfifo_free(&test);
164+
#endif
165+
return -EIO;
166+
}
146167

147168
if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
148169
#ifdef DYNAMIC

0 commit comments

Comments
 (0)