1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4
5 #include "unity/unity.h"
6
7 static lv_circle_buf_t * circle_buf;
8
9 #define circle_buf_CAPACITY 4
10
setUp(void)11 void setUp(void)
12 {
13 circle_buf = lv_circle_buf_create(circle_buf_CAPACITY, sizeof(int32_t));
14
15 TEST_ASSERT_EQUAL_UINT32(lv_circle_buf_capacity(circle_buf), circle_buf_CAPACITY);
16 TEST_ASSERT_EQUAL_UINT32(0, lv_circle_buf_size(circle_buf));
17
18 /**
19 * Write values to the circle buffer. The max size of the buffer is circle_buf_CAPACITY.
20 * When the buffer is full, the write operation should return LV_RESULT_INVALID.
21 */
22 for(int32_t i = 0; i < circle_buf_CAPACITY * 2; i++) {
23 const lv_result_t res = lv_circle_buf_write(circle_buf, &i);
24
25 if(i < circle_buf_CAPACITY) TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
26 else TEST_ASSERT_EQUAL(LV_RESULT_INVALID, res);
27 }
28
29 /**
30 * After writing values to the buffer, the size of the buffer should be equal to the capacity.
31 */
32 TEST_ASSERT_EQUAL_UINT32(lv_circle_buf_size(circle_buf), circle_buf_CAPACITY);
33 }
34
tearDown(void)35 void tearDown(void)
36 {
37 lv_circle_buf_destroy(circle_buf);
38 circle_buf = NULL;
39 }
40
test_circle_buf_read_write_peek_values(void)41 void test_circle_buf_read_write_peek_values(void)
42 {
43 /**
44 * Read 1 value from the buffer.
45 */
46 {
47 int32_t value;
48 const lv_result_t res = lv_circle_buf_read(circle_buf, &value);
49
50 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
51 TEST_ASSERT_EQUAL_INT32(0, value);
52 }
53
54 /**
55 * Peek values will not advance the read and write cursors.
56 * If the peek index is greater than the size of the buffer, it will returns looply.
57 */
58 for(int32_t i = 0, j = 1; i < circle_buf_CAPACITY * 10; i++, j++) {
59 int32_t value;
60 const lv_result_t res = lv_circle_buf_peek_at(circle_buf, i, &value);
61
62 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
63 TEST_ASSERT_EQUAL_INT32(j, value);
64
65 if(j == 3) j = 0;
66 }
67
68 /**
69 * Read values from the circle buffer. The max size of the buffer is circle_buf_CAPACITY.
70 * When the buffer is empty, the read operation should return LV_RESULT_INVALID.
71 */
72 for(int32_t i = 1; i < circle_buf_CAPACITY * 2; i++) {
73 int32_t value;
74 const lv_result_t res = lv_circle_buf_read(circle_buf, &value);
75
76 if(i < circle_buf_CAPACITY) {
77 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
78 TEST_ASSERT_EQUAL_INT32(i, value);
79 }
80 else {
81 TEST_ASSERT_EQUAL(LV_RESULT_INVALID, res);
82 }
83 }
84
85 /**
86 * After reading values from the buffer, the size of the buffer should be equal to 0.
87 */
88 TEST_ASSERT_EQUAL_INT32(0, lv_circle_buf_size(circle_buf));
89 }
90
test_circle_buf_skip_values(void)91 void test_circle_buf_skip_values(void)
92 {
93 /**
94 * Skip 1 value from the buffer.
95 */
96 {
97 const lv_result_t res = lv_circle_buf_skip(circle_buf);
98
99 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
100 }
101
102 /**
103 * Skip values from the circle buffer. The max size of the buffer is circle_buf_CAPACITY.
104 * When the buffer is empty, the skip operation should return LV_RESULT_INVALID.
105 */
106 for(int32_t i = 1; i < circle_buf_CAPACITY * 2; i++) {
107 const lv_result_t res = lv_circle_buf_skip(circle_buf);
108
109 if(i < circle_buf_CAPACITY) {
110 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
111 }
112 else {
113 TEST_ASSERT_EQUAL(LV_RESULT_INVALID, res);
114 }
115 }
116
117 /**
118 * After skipping values from the buffer, the size of the buffer should be equal to 0.
119 */
120 TEST_ASSERT_EQUAL_INT32(0, lv_circle_buf_size(circle_buf));
121 }
122
test_circle_buf_read_after_read_and_write(void)123 void test_circle_buf_read_after_read_and_write(void)
124 {
125 /**
126 * Read 1 value from the buffer.
127 */
128 {
129 int32_t value;
130 const lv_result_t res = lv_circle_buf_read(circle_buf, &value);
131
132 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
133 TEST_ASSERT_EQUAL_INT32(0, value);
134 }
135
136 /**
137 * Write 1 value to the buffer.
138 */
139 {
140 const int32_t value = 4;
141 const lv_result_t res = lv_circle_buf_write(circle_buf, &value);
142
143 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
144 }
145
146 const int32_t expected[] = {4, 1, 2, 3};
147 TEST_ASSERT_EQUAL_INT32_ARRAY(expected, ((lv_array_t *)circle_buf)->data, 4);
148
149 /**
150 * Read values from the circle buffer. The max size of the buffer is circle_buf_CAPACITY.
151 * When the buffer is empty, the read operation should return LV_RESULT_INVALID.
152 */
153 for(int32_t i = 1; i < circle_buf_CAPACITY * 2; i++) {
154 int32_t value;
155 const lv_result_t res = lv_circle_buf_read(circle_buf, &value);
156
157 if(i <= circle_buf_CAPACITY) {
158 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
159 TEST_ASSERT_EQUAL_INT32(i, value);
160 }
161 else {
162 TEST_ASSERT_EQUAL(LV_RESULT_INVALID, res);
163 }
164 }
165 }
166
167 #endif
168