aboutsummaryrefslogtreecommitdiffstats
path: root/samples/kfifo/bytestream-example.c
diff options
context:
space:
mode:
Diffstat (limited to 'samples/kfifo/bytestream-example.c')
-rw-r--r--samples/kfifo/bytestream-example.c42
1 files changed, 36 insertions, 6 deletions
diff --git a/samples/kfifo/bytestream-example.c b/samples/kfifo/bytestream-example.c
index 642eef3f6336..178061e87ffe 100644
--- a/samples/kfifo/bytestream-example.c
+++ b/samples/kfifo/bytestream-example.c
@@ -44,10 +44,17 @@ static struct kfifo test;
44static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE); 44static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
45#endif 45#endif
46 46
47static const 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
47static int __init testfunc(void) 54static int __init testfunc(void)
48{ 55{
49 unsigned char buf[6]; 56 unsigned char buf[6];
50 unsigned char i; 57 unsigned char i, j;
51 unsigned int ret; 58 unsigned int ret;
52 59
53 printk(KERN_INFO "byte stream fifo test start\n"); 60 printk(KERN_INFO "byte stream fifo test start\n");
@@ -73,16 +80,34 @@ static int __init testfunc(void)
73 ret = kfifo_in(&test, buf, ret); 80 ret = kfifo_in(&test, buf, ret);
74 printk(KERN_INFO "ret: %d\n", ret); 81 printk(KERN_INFO "ret: %d\n", ret);
75 82
83 /* skip first element of the fifo */
84 printk(KERN_INFO "skip 1st element\n");
85 kfifo_skip(&test);
86
76 /* put values into the fifo until is full */ 87 /* put values into the fifo until is full */
77 for (i = 20; kfifo_put(&test, &i); i++) 88 for (i = 20; kfifo_put(&test, &i); i++)
78 ; 89 ;
79 90
80 printk(KERN_INFO "queue len: %u\n", kfifo_len(&test)); 91 printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
81 92
82 /* print out all values in the fifo */ 93 /* show the first value without removing from the fifo */
83 while (kfifo_get(&test, &i)) 94 if (kfifo_peek(&test, &i))
84 printk("%d ", i); 95 printk(KERN_INFO "%d\n", i);
85 printk("\n"); 96
97 /* check the correctness of all values in the fifo */
98 j = 0;
99 while (kfifo_get(&test, &i)) {
100 printk(KERN_INFO "item = %d\n", i);
101 if (i != expected_result[j++]) {
102 printk(KERN_WARNING "value mismatch: test failed\n");
103 return -EIO;
104 }
105 }
106 if (j != ARRAY_SIZE(expected_result)) {
107 printk(KERN_WARNING "size mismatch: test failed\n");
108 return -EIO;
109 }
110 printk(KERN_INFO "test passed\n");
86 111
87 return 0; 112 return 0;
88} 113}
@@ -138,7 +163,12 @@ static int __init example_init(void)
138#else 163#else
139 INIT_KFIFO(test); 164 INIT_KFIFO(test);
140#endif 165#endif
141 testfunc(); 166 if (testfunc() < 0) {
167#ifdef DYNAMIC
168 kfifo_free(&test);
169#endif
170 return -EIO;
171 }
142 172
143 if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { 173 if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
144#ifdef DYNAMIC 174#ifdef DYNAMIC