/* test the ring buffer */ void testRingAroundBuffer( Test * pTest, RING_BUFFER * test_buffer, uint8_t * data_element, unsigned element_size, unsigned element_count) { uint8_t *test_data; unsigned index; unsigned data_index; unsigned count; unsigned dummy; bool status; ct_test(pTest, Ringbuf_Empty(test_buffer)); /* test the ring around the buffer */ for (index = 0; index < element_count; index++) { for (count = 1; count < 4; count++) { dummy = index * count; for (data_index = 0; data_index < element_size; data_index++) { data_element[data_index] = dummy; } status = Ringbuf_Put(test_buffer, data_element); ct_test(pTest, status == true); } for (count = 1; count < 4; count++) { dummy = index * count; test_data = Ringbuf_Get_Front(test_buffer); ct_test(pTest, test_data); if (test_data) { for (data_index = 0; data_index < element_size; data_index++) { ct_test(pTest, test_data[data_index] == dummy); } } test_data = Ringbuf_Pop_Front(test_buffer); ct_test(pTest, test_data); if (test_data) { for (data_index = 0; data_index < element_size; data_index++) { ct_test(pTest, test_data[data_index] == dummy); } } } } ct_test(pTest, Ringbuf_Empty(test_buffer)); }
void testRingBuf( Test * pTest) { RING_BUFFER test_buffer; char data_store[RING_BUFFER_DATA_SIZE * RING_BUFFER_COUNT]; char data[RING_BUFFER_DATA_SIZE]; char *test_data; unsigned index; unsigned data_index; unsigned count; unsigned dummy; bool status; Ringbuf_Init(&test_buffer, data_store, RING_BUFFER_DATA_SIZE, RING_BUFFER_COUNT); ct_test(pTest, Ringbuf_Empty(&test_buffer)); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { data[data_index] = data_index; } status = Ringbuf_Put(&test_buffer, data); ct_test(pTest, status == true); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); test_data = Ringbuf_Get_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == data[data_index]); } ct_test(pTest, !Ringbuf_Empty(&test_buffer)); test_data = Ringbuf_Pop_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == data[data_index]); } ct_test(pTest, Ringbuf_Empty(&test_buffer)); /* fill to max */ for (index = 0; index < RING_BUFFER_COUNT; index++) { for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { data[data_index] = index; } status = Ringbuf_Put(&test_buffer, data); ct_test(pTest, status == true); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); } /* verify actions on full buffer */ for (index = 0; index < RING_BUFFER_COUNT; index++) { for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { data[data_index] = index; } status = Ringbuf_Put(&test_buffer, data); ct_test(pTest, status == false); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); } /* check buffer full */ for (index = 0; index < RING_BUFFER_COUNT; index++) { test_data = Ringbuf_Get_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == index); } test_data = Ringbuf_Pop_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == index); } } ct_test(pTest, Ringbuf_Empty(&test_buffer)); /* test the ring around the buffer */ for (index = 0; index < RING_BUFFER_COUNT; index++) { for (count = 1; count < 4; count++) { dummy = index * count; for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { data[data_index] = dummy; } status = Ringbuf_Put(&test_buffer, data); ct_test(pTest, status == true); } for (count = 1; count < 4; count++) { dummy = index * count; test_data = Ringbuf_Get_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == dummy); } test_data = Ringbuf_Pop_Front(&test_buffer); for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == dummy); } } } ct_test(pTest, Ringbuf_Empty(&test_buffer)); return; }
/* test the ring buffer */ void testRingBuf( Test * pTest, uint8_t * data_store, uint8_t * data_element, unsigned element_size, unsigned element_count) { RING_BUFFER test_buffer; uint8_t *test_data; unsigned index; unsigned data_index; bool status; Ringbuf_Init(&test_buffer, data_store, element_size, element_count); ct_test(pTest, Ringbuf_Empty(&test_buffer)); for (data_index = 0; data_index < element_size; data_index++) { data_element[data_index] = data_index; } status = Ringbuf_Put(&test_buffer, data_element); ct_test(pTest, status == true); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); test_data = Ringbuf_Get_Front(&test_buffer); for (data_index = 0; data_index < element_size; data_index++) { ct_test(pTest, test_data[data_index] == data_element[data_index]); } ct_test(pTest, !Ringbuf_Empty(&test_buffer)); test_data = Ringbuf_Pop_Front(&test_buffer); for (data_index = 0; data_index < element_size; data_index++) { ct_test(pTest, test_data[data_index] == data_element[data_index]); } ct_test(pTest, Ringbuf_Empty(&test_buffer)); /* fill to max */ for (index = 0; index < element_count; index++) { for (data_index = 0; data_index < element_size; data_index++) { data_element[data_index] = index; } status = Ringbuf_Put(&test_buffer, data_element); ct_test(pTest, status == true); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); } /* verify actions on full buffer */ for (index = 0; index < element_count; index++) { for (data_index = 0; data_index < element_size; data_index++) { data_element[data_index] = index; } status = Ringbuf_Put(&test_buffer, data_element); ct_test(pTest, status == false); ct_test(pTest, !Ringbuf_Empty(&test_buffer)); } /* check buffer full */ for (index = 0; index < element_count; index++) { test_data = Ringbuf_Get_Front(&test_buffer); ct_test(pTest, test_data); if (test_data) { for (data_index = 0; data_index < element_size; data_index++) { ct_test(pTest, test_data[data_index] == index); } } test_data = Ringbuf_Pop_Front(&test_buffer); ct_test(pTest, test_data); if (test_data) { for (data_index = 0; data_index < element_size; data_index++) { ct_test(pTest, test_data[data_index] == index); } } } ct_test(pTest, Ringbuf_Empty(&test_buffer)); testRingAroundBuffer(pTest, &test_buffer, data_element, element_size, element_count); /* adjust the internal index of Ringbuf to test unsigned wrapping */ test_buffer.head = UINT_MAX - 1; test_buffer.tail = UINT_MAX - 1; testRingAroundBuffer(pTest, &test_buffer, data_element, element_size, element_count); return; }