![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
ring_buffer.cpp
- Committer:
- klauss
- Date:
- 2015-01-19
- Revision:
- 99:e80850c51106
- Parent:
- 72:895ca792c647
- Child:
- 105:a930035b6556
File content as of revision 99:e80850c51106:
#include "ring_buffer.h" ring_buffer * ring_buffer_init( ring_buffer * rb ){ if( rb == NULL ){ rb = ( ring_buffer * )malloc( sizeof( ring_buffer ) ); rb->begin = 0; rb->end = 0; rb->size = 0; rb->data = ( uint8_t * )malloc( sizeof( uint8_t ) * __CB_BUFFER_SIZE__ * RING_BUFFER_SIZE ); return( rb ); }else return( rb ); } void ring_buffer_delete( ring_buffer **rb ){ if( (*rb) != NULL ){ if( (*rb)->data != NULL ) free( (*rb)->data ); free( *rb ); } *rb = NULL; return; } uint8_t ring_buffer_add( ring_buffer * rb, uint8_t * data ){ if( rb == NULL ) return 0x99; if( data == NULL ) return 0x98; if( rb->size >= RING_BUFFER_SIZE ) return 0x01; else{ rb_xmemcpy( rb->data + ( __CB_BUFFER_SIZE__ * rb->end ), data, __CB_BUFFER_SIZE__ ); if( ++( rb->end ) >= RING_BUFFER_SIZE ) rb->end = 0; rb->size++; } return( 0x00 ); } uint8_t * ring_buffer_get_next( ring_buffer * rb ){ if( rb == NULL ) return( NULL ); if( rb->size == 0 ) return( NULL ); uint8_t * ret = rb->data + ( rb->begin * __CB_BUFFER_SIZE__ ); if( ++( rb->begin ) >= RING_BUFFER_SIZE ) rb->begin = 0; if( rb->size > 0 ) rb->size--; return( ret ); } bool unit_test_01(){ /* next on empty ring_buffer */ ring_buffer * rb = ring_buffer_init( NULL ); uint8_t * data = ring_buffer_get_next( rb ); assert( data == NULL ); data = ring_buffer_get_next( rb ); assert( data == NULL ); data = ring_buffer_get_next( rb ); assert( data == NULL ); data = ring_buffer_get_next( rb ); assert( data == NULL ); ring_buffer_delete( &rb ); return( rb == NULL ) ? true : false; } bool unit_test_02(){ /* add 1 next 4 */ ring_buffer * rb = ring_buffer_init( NULL ); uint8_t data_buff[ __CB_BUFFER_SIZE__ ], *data = NULL; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xab; ring_buffer_add( rb, data_buff ); assert( rb->size == 1 ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 0 ); data = ring_buffer_get_next( rb ); assert( data == NULL ); assert( rb->size == 0 ); data = ring_buffer_get_next( rb ); assert( data == NULL ); assert( rb->size == 0 ); data = ring_buffer_get_next( rb ); assert( data == NULL ); assert( rb->size == 0 ); data = ring_buffer_get_next( rb ); assert( data == NULL ); assert( rb->size == 0 ); ring_buffer_delete( &rb ); return( rb == NULL ) ? true : false; } bool unit_test_03(){ /* ad 1, next 1, cmp, add 2, next one, cmp, next one, cmp, next one, cmp */ ring_buffer * rb = ring_buffer_init( NULL ); uint8_t data_buff[ __CB_BUFFER_SIZE__ ], *data = NULL; bool flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xab; ring_buffer_add( rb, data_buff ); assert( rb->size == 1 ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 0 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xab ) flag = false; assert( flag == true ); for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xcc; ring_buffer_add( rb, data_buff ); assert( rb->size == 1 ); for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xdb; ring_buffer_add( rb, data_buff ); assert( rb->size == 2 ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 1 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xcc ) flag = false; assert( flag == true ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 0 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xdb ) flag = false; assert( flag == true ); data = ring_buffer_get_next( rb ); assert( data == NULL ); assert( rb->size == 0 ); ring_buffer_delete( &rb ); return( rb == NULL ) ? true : false; } bool unit_test_04(){ /* add 1, next 1, add 2, next one, add 1, next 2, add 3, next 1, add 1, next 3 */ ring_buffer * rb = ring_buffer_init( NULL ); uint8_t data_buff[ __CB_BUFFER_SIZE__ ], *data = NULL; bool flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xab; ring_buffer_add( rb, data_buff ); assert( rb->size == 1 ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 0 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xab ) flag = false; assert( flag == true ); for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xcc; ring_buffer_add( rb, data_buff ); assert( rb->size == 1 ); for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xdb; ring_buffer_add( rb, data_buff ); assert( rb->size == 2 ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 1 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xcc ) flag = false; assert( flag == true ); for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xdd; ring_buffer_add( rb, data_buff ); assert( rb->size == 2 ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 1 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xdb ) flag = false; assert( flag == true ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 0 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xdd ) flag = false; assert( flag == true ); for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xaa; ring_buffer_add( rb, data_buff ); assert( rb->size == 1 ); for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xab; ring_buffer_add( rb, data_buff ); assert( rb->size == 2 ); for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xac; ring_buffer_add( rb, data_buff ); assert( rb->size == 3 ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 2 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xaa ) flag = false; assert( flag == true ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 1 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xab ) flag = false; assert( flag == true ); data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 0 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xac ) flag = false; assert( flag == true ); for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){ data = ring_buffer_get_next( rb ); assert( data == NULL ); assert( rb->size == 0 ); } ring_buffer_delete( &rb ); return( rb == NULL ) ? true : false; } bool unit_test_05(){ /* add 3 add 300, next 1, add 301, next 300, add 300, next 300 */ ring_buffer * rb = ring_buffer_init( NULL ); uint8_t data_buff[ __CB_BUFFER_SIZE__ ], *data = NULL; bool flag = true; // add 3 uint8_t value = 0x00; for( uint8_t count = 0; count < 3; count++ ){ for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = value; ring_buffer_add( rb, data_buff ); assert( rb->size == ( count + 1 ) ); value++; } // add 300 for( uint16_t count = 0; count < __CB_BUFFER_SIZE__; count++ ){ for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = value; assert( ring_buffer_add( rb, data_buff ) == 0x01 ) ; assert( rb->size == RING_BUFFER_SIZE ); value++; } // next 1 data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 2 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0x00 ) flag = false; assert( flag == true ); // add 1 for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = 0xff; assert( ring_buffer_add( rb, data_buff ) == 0x00 ); // add 300 value = 0xee; for( uint16_t count = 0; count < __CB_BUFFER_SIZE__; count++ ){ for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = value; assert( ring_buffer_add( rb, data_buff ) == 0x01 ); assert( rb->size == RING_BUFFER_SIZE ); value++; } // next 1 data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 2 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0x01 ) flag = false; assert( flag == true ); //next 1 data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 1 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0x02 ) flag = false; assert( flag == true ); //next 1 data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == 0 ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != 0xff ) flag = false; assert( flag == true ); // next 300 - 3 for( uint16_t count = 0; count < __CB_BUFFER_SIZE__ - 3; count++ ){ data = ring_buffer_get_next( rb ); assert( data == NULL ); } // add 300 value = 0xbb; for( uint16_t count = 0; count < __CB_BUFFER_SIZE__; count++ ){ for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) data_buff[ i ] = value; if( rb->size != RING_BUFFER_SIZE ) assert( ring_buffer_add( rb, data_buff ) == 0x00 ); else assert( ring_buffer_add( rb, data_buff ) == 0x01 ); value++; } // next 3 value = 0xbb; for( uint8_t count = 0; count < 3; count++ ){ data = ring_buffer_get_next( rb ); assert( data != NULL ); assert( rb->size == ( RING_BUFFER_SIZE - 1 - count ) ); flag = true; for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) if( data[ i ] != value ) flag = false; assert( flag == true ); value++; } for( uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){ data = ring_buffer_get_next( rb ); assert( data == NULL ); assert( rb->size == 0 ); } ring_buffer_delete( &rb ); return( rb == NULL ) ? true : false; } /* int main(){ assert( 1 == unit_test_01() ); printf("unit_test 01 -- ok\n\r"); assert( 1 == unit_test_02() ); printf("unit_test 02 -- ok\n\r"); assert( 1 == unit_test_03() ); printf("unit_test 03 -- ok\n\r"); assert( 1 == unit_test_04() ); printf("unit_test 04 -- ok\n\r"); assert( 1 == unit_test_05() ); printf("unit_test 05 -- ok\n\r"); return( 0 ); } */ void rb_xmemcpy(uint8_t * dest, uint8_t * src, uint16_t size){ while (size--) *dest++ = *src++; }