Commit 2aaf2092c168fc02df0645415f524b357ee7ec2e
Committed by
Linus Torvalds
1 parent
5ddf83912c
Exists in
master
and in
7 other branches
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>
Showing 1 changed file with 27 additions and 6 deletions Side-by-side Diff
samples/kfifo/bytestream-example.c
... | ... | @@ -44,10 +44,17 @@ |
44 | 44 | static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE); |
45 | 45 | #endif |
46 | 46 | |
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 | + | |
47 | 54 | static int __init testfunc(void) |
48 | 55 | { |
49 | 56 | unsigned char buf[6]; |
50 | - unsigned char i; | |
57 | + unsigned char i, j; | |
51 | 58 | unsigned int ret; |
52 | 59 | |
53 | 60 | printk(KERN_INFO "byte stream fifo test start\n"); |
... | ... | @@ -83,10 +90,19 @@ |
83 | 90 | |
84 | 91 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&test)); |
85 | 92 | |
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"); | |
90 | 106 | |
91 | 107 | return 0; |
92 | 108 | } |
... | ... | @@ -142,7 +158,12 @@ |
142 | 158 | #else |
143 | 159 | INIT_KFIFO(test); |
144 | 160 | #endif |
145 | - testfunc(); | |
161 | + if (testfunc() < 0) { | |
162 | +#ifdef DYNAMIC | |
163 | + kfifo_free(&test); | |
164 | +#endif | |
165 | + return -EIO; | |
166 | + } | |
146 | 167 | |
147 | 168 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { |
148 | 169 | #ifdef DYNAMIC |