1 #include "ktest.h"
2 #include "ktest_utils.h"
3 #include <common/kfifo.h>
4 #include <common/kprint.h>
5 #include <mm/slab.h>
6 
ktest_kfifo_case0_1(uint64_t arg0,uint64_t arg1)7 static long ktest_kfifo_case0_1(uint64_t arg0, uint64_t arg1)
8 {
9     const int fifo_size = 256;
10     // 创建kfifo(由kfifo申请内存)
11     struct kfifo_t fifo;
12     if (arg0 == 0)
13         assert(kfifo_alloc(&fifo, fifo_size, 0) == 0);
14     else
15     {
16         void *buf = kmalloc(fifo_size, 0);
17         kfifo_init(&fifo, buf, fifo_size);
18     }
19 
20     assert(fifo.buffer != NULL);
21     assert(fifo.total_size == fifo_size);
22     assert(kfifo_total_size(&fifo) == fifo_size);
23     assert(fifo.size == 0);
24     assert(kfifo_size(&fifo) == 0);
25     assert(fifo.in_offset == 0);
26     assert(fifo.out_offset == 0);
27     assert(kfifo_empty(&fifo) == 1);
28     assert(kfifo_full(&fifo) == 0);
29 
30     // 循环增加10个uint64_t
31     for (int i = 1; i <= 10; ++i)
32     {
33         uint64_t tmp = i;
34         assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
35     }
36     assert(fifo.in_offset == 10 * sizeof(uint64_t));
37     assert(fifo.out_offset == 0);
38     assert(fifo.size == 10 * sizeof(uint64_t));
39     assert(fifo.total_size == fifo_size);
40 
41     // 循环删除这10个uint64_t
42     for (int i = 1; i <= 10; ++i)
43     {
44         uint64_t tmp = 0;
45         assert(kfifo_out(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
46         assert(tmp == i);
47         assert(fifo.size == (10 - i) * sizeof(uint64_t));
48         assert(fifo.in_offset == 10 * sizeof(uint64_t));
49         assert(fifo.out_offset == i * sizeof(uint64_t));
50     }
51 
52     assert(fifo.in_offset == 10 * sizeof(uint64_t));
53     assert(fifo.out_offset == 10 * sizeof(uint64_t));
54     assert(fifo.in_offset == fifo.out_offset);
55     assert(kfifo_empty(&fifo) == 1);
56 
57     // reset
58     kfifo_reset(&fifo);
59     assert(fifo.in_offset == 0);
60     assert(fifo.out_offset == 0);
61     assert(fifo.size == 0);
62 
63     // 测试插入31个元素
64     for (int i = 1; i <= 31; ++i)
65     {
66         uint64_t tmp = i;
67         assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
68     }
69 
70     assert(fifo.size == 31 * sizeof(uint64_t));
71     assert(fifo.in_offset == 31 * sizeof(uint64_t));
72     assert(fifo.out_offset == 0);
73 
74     // 然后再尝试插入一个大小为2*sizeof(uint64_t)的元素
75     {
76         __int128_t tmp = 100;
77         assert(kfifo_in(&fifo, &tmp, sizeof(__int128_t)) == 0);
78         assert(fifo.size == 31 * sizeof(uint64_t));
79         assert(fifo.in_offset == 31 * sizeof(uint64_t));
80         assert(fifo.out_offset == 0);
81     }
82     // 插入一个uint64, 队列满
83     {
84         uint64_t tmp = 32;
85         assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
86         assert(kfifo_full(&fifo));
87         assert(kfifo_empty(&fifo) == 0);
88         assert(fifo.size == fifo.total_size);
89         assert(fifo.in_offset == fifo_size);
90         assert(fifo.out_offset == 0);
91     }
92 
93     // 取出之前的20个元素
94     for (int i = 1; i <= 20; ++i)
95     {
96         uint64_t tmp = 0;
97         assert(kfifo_out(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
98     }
99     assert(fifo.size == (fifo.total_size - 20 * sizeof(uint64_t)));
100     assert(fifo.in_offset == fifo_size);
101     assert(fifo.out_offset == 20 * sizeof(uint64_t));
102 
103     // 插入10个元素,剩余10个空位
104     {
105         uint64_t tmp = 99;
106 
107         assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
108         assert(fifo.in_offset == 1 * sizeof(uint64_t));
109 
110         for (int i = 1; i <= 9; ++i)
111         {
112             assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
113         }
114         assert(fifo.in_offset == 10 * sizeof(uint64_t));
115         assert(fifo.size == 22 * sizeof(uint64_t));
116     }
117 
118     {
119         // 取出20个
120         char tmp[20 * sizeof(uint64_t)];
121         assert(kfifo_out(&fifo, &tmp, 20 * sizeof(uint64_t)) == 20 * sizeof(uint64_t));
122         assert(fifo.out_offset == 8 * sizeof(uint64_t));
123         assert(fifo.size == 2 * (sizeof(uint64_t)));
124     }
125 
126     {
127         // 插入25个
128         char tmp[25 * sizeof(uint64_t)];
129         assert(kfifo_in(&fifo, &tmp, 25 * sizeof(uint64_t)) == 25 * sizeof(uint64_t));
130         assert(fifo.out_offset == 8 * sizeof(uint64_t));
131         assert(fifo.size == 27 * sizeof(uint64_t));
132         assert(fifo.in_offset == 3 * sizeof(uint64_t));
133     }
134 
135     // 测试reset out
136     uint32_t prev_in_offset = fifo.in_offset;
137     kfifo_reset_out(&fifo);
138     assert(fifo.size == 0);
139     assert(fifo.total_size == fifo_size);
140     assert(fifo.in_offset == prev_in_offset);
141     assert(fifo.out_offset == prev_in_offset);
142 
143     // 测试释放
144     if (arg0 == 0)
145     {
146         kfifo_free_alloc(&fifo);
147         assert(fifo.buffer == NULL);
148     }
149     return 0;
150 }
151 
152 static ktest_case_table kt_kfifo_func_table[] = {
153     ktest_kfifo_case0_1,
154 };
155 
ktest_test_kfifo(void * arg)156 int ktest_test_kfifo(void* arg)
157 {
158     kTEST("Testing kfifo...");
159     for (int i = 0; i < sizeof(kt_kfifo_func_table) / sizeof(ktest_case_table); ++i)
160     {
161         kTEST("Testing case %d", i);
162         kt_kfifo_func_table[i](i, 0);
163     }
164     kTEST("kfifo Test done.");
165     return 0;
166 }
167