![](/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@119:ee6a53069455, 2015-04-30 (annotated)
- Committer:
- klauss
- Date:
- Thu Apr 30 15:42:41 2015 +0000
- Revision:
- 119:ee6a53069455
- Parent:
- 117:e9facba9db27
- Child:
- 135:2f4290590e51
funcionalidade - clock "ogro" - implementada
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 72:895ca792c647 | 1 | #include "ring_buffer.h" |
klauss | 72:895ca792c647 | 2 | |
klauss | 72:895ca792c647 | 3 | ring_buffer * ring_buffer_init( ring_buffer * rb ){ |
klauss | 72:895ca792c647 | 4 | if( rb == NULL ){ |
klauss | 72:895ca792c647 | 5 | rb = ( ring_buffer * )malloc( sizeof( ring_buffer ) ); |
klauss | 72:895ca792c647 | 6 | rb->begin = 0; |
klauss | 72:895ca792c647 | 7 | rb->end = 0; |
klauss | 72:895ca792c647 | 8 | rb->size = 0; |
klauss | 119:ee6a53069455 | 9 | rb->data = ( uint8_t * )malloc( sizeof( uint8_t ) * CB_BUFFER_SIZE * RING_BUFFER_SIZE ); |
klauss | 72:895ca792c647 | 10 | return( rb ); |
klauss | 72:895ca792c647 | 11 | }else return( rb ); |
klauss | 72:895ca792c647 | 12 | } |
klauss | 72:895ca792c647 | 13 | |
klauss | 72:895ca792c647 | 14 | void ring_buffer_delete( ring_buffer **rb ){ |
klauss | 72:895ca792c647 | 15 | if( (*rb) != NULL ){ |
klauss | 72:895ca792c647 | 16 | if( (*rb)->data != NULL ) free( (*rb)->data ); |
klauss | 72:895ca792c647 | 17 | free( *rb ); |
klauss | 72:895ca792c647 | 18 | } |
klauss | 72:895ca792c647 | 19 | *rb = NULL; |
klauss | 72:895ca792c647 | 20 | return; |
klauss | 72:895ca792c647 | 21 | } |
klauss | 72:895ca792c647 | 22 | |
klauss | 72:895ca792c647 | 23 | uint8_t ring_buffer_add( ring_buffer * rb, uint8_t * data ){ |
klauss | 72:895ca792c647 | 24 | if( rb == NULL ) return 0x99; |
klauss | 72:895ca792c647 | 25 | |
klauss | 72:895ca792c647 | 26 | if( data == NULL ) return 0x98; |
klauss | 72:895ca792c647 | 27 | |
klauss | 99:e80850c51106 | 28 | if( rb->size >= RING_BUFFER_SIZE ) return 0x01; |
klauss | 72:895ca792c647 | 29 | |
klauss | 72:895ca792c647 | 30 | else{ |
klauss | 119:ee6a53069455 | 31 | rb_xmemcpy( rb->data + ( CB_BUFFER_SIZE * rb->end ), data, CB_BUFFER_SIZE ); |
klauss | 99:e80850c51106 | 32 | if( ++( rb->end ) >= RING_BUFFER_SIZE ) rb->end = 0; |
klauss | 72:895ca792c647 | 33 | rb->size++; |
klauss | 72:895ca792c647 | 34 | } |
klauss | 72:895ca792c647 | 35 | |
klauss | 72:895ca792c647 | 36 | return( 0x00 ); |
klauss | 72:895ca792c647 | 37 | } |
klauss | 72:895ca792c647 | 38 | |
klauss | 72:895ca792c647 | 39 | uint8_t * ring_buffer_get_next( ring_buffer * rb ){ |
klauss | 72:895ca792c647 | 40 | if( rb == NULL ) return( NULL ); |
klauss | 72:895ca792c647 | 41 | |
klauss | 72:895ca792c647 | 42 | if( rb->size == 0 ) return( NULL ); |
klauss | 72:895ca792c647 | 43 | |
klauss | 119:ee6a53069455 | 44 | uint8_t * ret = rb->data + ( rb->begin * CB_BUFFER_SIZE ); |
klauss | 99:e80850c51106 | 45 | |
klauss | 99:e80850c51106 | 46 | if( ++( rb->begin ) >= RING_BUFFER_SIZE ) rb->begin = 0; |
klauss | 72:895ca792c647 | 47 | |
klauss | 99:e80850c51106 | 48 | if( rb->size > 0 ) rb->size--; |
klauss | 72:895ca792c647 | 49 | |
klauss | 99:e80850c51106 | 50 | return( ret ); |
klauss | 72:895ca792c647 | 51 | } |
klauss | 72:895ca792c647 | 52 | |
klauss | 72:895ca792c647 | 53 | bool unit_test_01(){ |
klauss | 72:895ca792c647 | 54 | /* next on empty ring_buffer */ |
klauss | 72:895ca792c647 | 55 | |
klauss | 72:895ca792c647 | 56 | ring_buffer * rb = ring_buffer_init( NULL ); |
klauss | 72:895ca792c647 | 57 | |
klauss | 72:895ca792c647 | 58 | uint8_t * data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 59 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 60 | |
klauss | 72:895ca792c647 | 61 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 62 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 63 | |
klauss | 72:895ca792c647 | 64 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 65 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 66 | |
klauss | 72:895ca792c647 | 67 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 68 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 69 | |
klauss | 72:895ca792c647 | 70 | ring_buffer_delete( &rb ); |
klauss | 72:895ca792c647 | 71 | |
klauss | 72:895ca792c647 | 72 | return( rb == NULL ) ? true : false; |
klauss | 72:895ca792c647 | 73 | } |
klauss | 72:895ca792c647 | 74 | |
klauss | 72:895ca792c647 | 75 | bool unit_test_02(){ |
klauss | 72:895ca792c647 | 76 | /* add 1 next 4 */ |
klauss | 72:895ca792c647 | 77 | ring_buffer * rb = ring_buffer_init( NULL ); |
klauss | 72:895ca792c647 | 78 | |
klauss | 119:ee6a53069455 | 79 | uint8_t data_buff[ CB_BUFFER_SIZE ], *data = NULL; |
klauss | 72:895ca792c647 | 80 | |
klauss | 119:ee6a53069455 | 81 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xab; |
klauss | 72:895ca792c647 | 82 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 83 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 84 | |
klauss | 72:895ca792c647 | 85 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 86 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 87 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 88 | |
klauss | 72:895ca792c647 | 89 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 90 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 91 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 92 | |
klauss | 72:895ca792c647 | 93 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 94 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 95 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 96 | |
klauss | 72:895ca792c647 | 97 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 98 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 99 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 100 | |
klauss | 72:895ca792c647 | 101 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 102 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 103 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 104 | |
klauss | 72:895ca792c647 | 105 | ring_buffer_delete( &rb ); |
klauss | 72:895ca792c647 | 106 | return( rb == NULL ) ? true : false; |
klauss | 72:895ca792c647 | 107 | |
klauss | 72:895ca792c647 | 108 | } |
klauss | 72:895ca792c647 | 109 | |
klauss | 72:895ca792c647 | 110 | bool unit_test_03(){ |
klauss | 72:895ca792c647 | 111 | /* ad 1, next 1, cmp, add 2, next one, cmp, next one, cmp, next one, cmp */ |
klauss | 72:895ca792c647 | 112 | |
klauss | 72:895ca792c647 | 113 | ring_buffer * rb = ring_buffer_init( NULL ); |
klauss | 72:895ca792c647 | 114 | |
klauss | 119:ee6a53069455 | 115 | uint8_t data_buff[ CB_BUFFER_SIZE ], *data = NULL; |
klauss | 72:895ca792c647 | 116 | |
klauss | 72:895ca792c647 | 117 | bool flag = true; |
klauss | 72:895ca792c647 | 118 | |
klauss | 119:ee6a53069455 | 119 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xab; |
klauss | 72:895ca792c647 | 120 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 121 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 122 | |
klauss | 72:895ca792c647 | 123 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 124 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 125 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 126 | flag = true; |
klauss | 119:ee6a53069455 | 127 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xab ) flag = false; |
klauss | 72:895ca792c647 | 128 | assert( flag == true ); |
klauss | 72:895ca792c647 | 129 | |
klauss | 119:ee6a53069455 | 130 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xcc; |
klauss | 72:895ca792c647 | 131 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 132 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 133 | |
klauss | 119:ee6a53069455 | 134 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xdb; |
klauss | 72:895ca792c647 | 135 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 136 | assert( rb->size == 2 ); |
klauss | 72:895ca792c647 | 137 | |
klauss | 72:895ca792c647 | 138 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 139 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 140 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 141 | flag = true; |
klauss | 119:ee6a53069455 | 142 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xcc ) flag = false; |
klauss | 72:895ca792c647 | 143 | assert( flag == true ); |
klauss | 72:895ca792c647 | 144 | |
klauss | 72:895ca792c647 | 145 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 146 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 147 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 148 | flag = true; |
klauss | 119:ee6a53069455 | 149 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xdb ) flag = false; |
klauss | 72:895ca792c647 | 150 | assert( flag == true ); |
klauss | 72:895ca792c647 | 151 | |
klauss | 72:895ca792c647 | 152 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 153 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 154 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 155 | |
klauss | 72:895ca792c647 | 156 | ring_buffer_delete( &rb ); |
klauss | 72:895ca792c647 | 157 | |
klauss | 72:895ca792c647 | 158 | return( rb == NULL ) ? true : false; |
klauss | 72:895ca792c647 | 159 | } |
klauss | 72:895ca792c647 | 160 | |
klauss | 72:895ca792c647 | 161 | bool unit_test_04(){ |
klauss | 72:895ca792c647 | 162 | /* add 1, next 1, add 2, next one, add 1, next 2, add 3, next 1, add 1, next 3 */ |
klauss | 72:895ca792c647 | 163 | |
klauss | 72:895ca792c647 | 164 | ring_buffer * rb = ring_buffer_init( NULL ); |
klauss | 72:895ca792c647 | 165 | |
klauss | 119:ee6a53069455 | 166 | uint8_t data_buff[ CB_BUFFER_SIZE ], *data = NULL; |
klauss | 72:895ca792c647 | 167 | |
klauss | 72:895ca792c647 | 168 | bool flag = true; |
klauss | 72:895ca792c647 | 169 | |
klauss | 119:ee6a53069455 | 170 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xab; |
klauss | 72:895ca792c647 | 171 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 172 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 173 | |
klauss | 72:895ca792c647 | 174 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 175 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 176 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 177 | flag = true; |
klauss | 119:ee6a53069455 | 178 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xab ) flag = false; |
klauss | 72:895ca792c647 | 179 | assert( flag == true ); |
klauss | 72:895ca792c647 | 180 | |
klauss | 119:ee6a53069455 | 181 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xcc; |
klauss | 72:895ca792c647 | 182 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 183 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 184 | |
klauss | 119:ee6a53069455 | 185 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xdb; |
klauss | 72:895ca792c647 | 186 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 187 | assert( rb->size == 2 ); |
klauss | 72:895ca792c647 | 188 | |
klauss | 72:895ca792c647 | 189 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 190 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 191 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 192 | flag = true; |
klauss | 119:ee6a53069455 | 193 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xcc ) flag = false; |
klauss | 72:895ca792c647 | 194 | assert( flag == true ); |
klauss | 72:895ca792c647 | 195 | |
klauss | 119:ee6a53069455 | 196 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xdd; |
klauss | 72:895ca792c647 | 197 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 198 | assert( rb->size == 2 ); |
klauss | 72:895ca792c647 | 199 | |
klauss | 72:895ca792c647 | 200 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 201 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 202 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 203 | flag = true; |
klauss | 119:ee6a53069455 | 204 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xdb ) flag = false; |
klauss | 72:895ca792c647 | 205 | assert( flag == true ); |
klauss | 72:895ca792c647 | 206 | |
klauss | 72:895ca792c647 | 207 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 208 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 209 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 210 | flag = true; |
klauss | 119:ee6a53069455 | 211 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xdd ) flag = false; |
klauss | 72:895ca792c647 | 212 | assert( flag == true ); |
klauss | 72:895ca792c647 | 213 | |
klauss | 119:ee6a53069455 | 214 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xaa; |
klauss | 72:895ca792c647 | 215 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 216 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 217 | |
klauss | 119:ee6a53069455 | 218 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xab; |
klauss | 72:895ca792c647 | 219 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 220 | assert( rb->size == 2 ); |
klauss | 72:895ca792c647 | 221 | |
klauss | 119:ee6a53069455 | 222 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xac; |
klauss | 72:895ca792c647 | 223 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 224 | assert( rb->size == 3 ); |
klauss | 72:895ca792c647 | 225 | |
klauss | 72:895ca792c647 | 226 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 227 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 228 | assert( rb->size == 2 ); |
klauss | 72:895ca792c647 | 229 | flag = true; |
klauss | 72:895ca792c647 | 230 | |
klauss | 119:ee6a53069455 | 231 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xaa ) flag = false; |
klauss | 72:895ca792c647 | 232 | assert( flag == true ); |
klauss | 72:895ca792c647 | 233 | |
klauss | 72:895ca792c647 | 234 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 235 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 236 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 237 | flag = true; |
klauss | 119:ee6a53069455 | 238 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xab ) flag = false; |
klauss | 72:895ca792c647 | 239 | assert( flag == true ); |
klauss | 72:895ca792c647 | 240 | |
klauss | 72:895ca792c647 | 241 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 242 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 243 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 244 | flag = true; |
klauss | 119:ee6a53069455 | 245 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xac ) flag = false; |
klauss | 72:895ca792c647 | 246 | assert( flag == true ); |
klauss | 72:895ca792c647 | 247 | |
klauss | 119:ee6a53069455 | 248 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ){ |
klauss | 72:895ca792c647 | 249 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 250 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 251 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 252 | } |
klauss | 72:895ca792c647 | 253 | |
klauss | 72:895ca792c647 | 254 | ring_buffer_delete( &rb ); |
klauss | 72:895ca792c647 | 255 | |
klauss | 72:895ca792c647 | 256 | return( rb == NULL ) ? true : false; |
klauss | 72:895ca792c647 | 257 | } |
klauss | 72:895ca792c647 | 258 | |
klauss | 72:895ca792c647 | 259 | bool unit_test_05(){ |
klauss | 72:895ca792c647 | 260 | /* add 3 add 300, next 1, add 301, next 300, add 300, next 300 */ |
klauss | 72:895ca792c647 | 261 | |
klauss | 72:895ca792c647 | 262 | ring_buffer * rb = ring_buffer_init( NULL ); |
klauss | 119:ee6a53069455 | 263 | uint8_t data_buff[ CB_BUFFER_SIZE ], *data = NULL; |
klauss | 72:895ca792c647 | 264 | bool flag = true; |
klauss | 72:895ca792c647 | 265 | |
klauss | 72:895ca792c647 | 266 | // add 3 |
klauss | 72:895ca792c647 | 267 | uint8_t value = 0x00; |
klauss | 72:895ca792c647 | 268 | for( uint8_t count = 0; count < 3; count++ ){ |
klauss | 119:ee6a53069455 | 269 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = value; |
klauss | 72:895ca792c647 | 270 | ring_buffer_add( rb, data_buff ); |
klauss | 72:895ca792c647 | 271 | assert( rb->size == ( count + 1 ) ); |
klauss | 72:895ca792c647 | 272 | value++; |
klauss | 72:895ca792c647 | 273 | } |
klauss | 72:895ca792c647 | 274 | // add 300 |
klauss | 119:ee6a53069455 | 275 | for( uint16_t count = 0; count < CB_BUFFER_SIZE; count++ ){ |
klauss | 119:ee6a53069455 | 276 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = value; |
klauss | 72:895ca792c647 | 277 | assert( ring_buffer_add( rb, data_buff ) == 0x01 ) ; |
klauss | 72:895ca792c647 | 278 | assert( rb->size == RING_BUFFER_SIZE ); |
klauss | 72:895ca792c647 | 279 | value++; |
klauss | 72:895ca792c647 | 280 | } |
klauss | 72:895ca792c647 | 281 | // next 1 |
klauss | 72:895ca792c647 | 282 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 283 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 284 | assert( rb->size == 2 ); |
klauss | 72:895ca792c647 | 285 | flag = true; |
klauss | 119:ee6a53069455 | 286 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0x00 ) flag = false; |
klauss | 72:895ca792c647 | 287 | assert( flag == true ); |
klauss | 72:895ca792c647 | 288 | |
klauss | 72:895ca792c647 | 289 | // add 1 |
klauss | 119:ee6a53069455 | 290 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = 0xff; |
klauss | 72:895ca792c647 | 291 | assert( ring_buffer_add( rb, data_buff ) == 0x00 ); |
klauss | 72:895ca792c647 | 292 | |
klauss | 72:895ca792c647 | 293 | // add 300 |
klauss | 72:895ca792c647 | 294 | value = 0xee; |
klauss | 119:ee6a53069455 | 295 | for( uint16_t count = 0; count < CB_BUFFER_SIZE; count++ ){ |
klauss | 119:ee6a53069455 | 296 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = value; |
klauss | 72:895ca792c647 | 297 | assert( ring_buffer_add( rb, data_buff ) == 0x01 ); |
klauss | 72:895ca792c647 | 298 | assert( rb->size == RING_BUFFER_SIZE ); |
klauss | 72:895ca792c647 | 299 | value++; |
klauss | 72:895ca792c647 | 300 | } |
klauss | 72:895ca792c647 | 301 | |
klauss | 72:895ca792c647 | 302 | // next 1 |
klauss | 72:895ca792c647 | 303 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 304 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 305 | assert( rb->size == 2 ); |
klauss | 72:895ca792c647 | 306 | flag = true; |
klauss | 119:ee6a53069455 | 307 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0x01 ) flag = false; |
klauss | 72:895ca792c647 | 308 | assert( flag == true ); |
klauss | 72:895ca792c647 | 309 | |
klauss | 72:895ca792c647 | 310 | //next 1 |
klauss | 72:895ca792c647 | 311 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 312 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 313 | assert( rb->size == 1 ); |
klauss | 72:895ca792c647 | 314 | flag = true; |
klauss | 119:ee6a53069455 | 315 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0x02 ) flag = false; |
klauss | 72:895ca792c647 | 316 | assert( flag == true ); |
klauss | 72:895ca792c647 | 317 | |
klauss | 72:895ca792c647 | 318 | //next 1 |
klauss | 72:895ca792c647 | 319 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 320 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 321 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 322 | flag = true; |
klauss | 119:ee6a53069455 | 323 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != 0xff ) flag = false; |
klauss | 72:895ca792c647 | 324 | assert( flag == true ); |
klauss | 72:895ca792c647 | 325 | |
klauss | 72:895ca792c647 | 326 | // next 300 - 3 |
klauss | 119:ee6a53069455 | 327 | for( uint16_t count = 0; count < CB_BUFFER_SIZE - 3; count++ ){ |
klauss | 72:895ca792c647 | 328 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 329 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 330 | } |
klauss | 72:895ca792c647 | 331 | |
klauss | 72:895ca792c647 | 332 | // add 300 |
klauss | 72:895ca792c647 | 333 | value = 0xbb; |
klauss | 119:ee6a53069455 | 334 | for( uint16_t count = 0; count < CB_BUFFER_SIZE; count++ ){ |
klauss | 119:ee6a53069455 | 335 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) data_buff[ i ] = value; |
klauss | 72:895ca792c647 | 336 | |
klauss | 72:895ca792c647 | 337 | if( rb->size != RING_BUFFER_SIZE ) assert( ring_buffer_add( rb, data_buff ) == 0x00 ); |
klauss | 72:895ca792c647 | 338 | |
klauss | 72:895ca792c647 | 339 | else assert( ring_buffer_add( rb, data_buff ) == 0x01 ); |
klauss | 72:895ca792c647 | 340 | |
klauss | 72:895ca792c647 | 341 | value++; |
klauss | 72:895ca792c647 | 342 | } |
klauss | 72:895ca792c647 | 343 | // next 3 |
klauss | 72:895ca792c647 | 344 | value = 0xbb; |
klauss | 72:895ca792c647 | 345 | for( uint8_t count = 0; count < 3; count++ ){ |
klauss | 72:895ca792c647 | 346 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 347 | assert( data != NULL ); |
klauss | 72:895ca792c647 | 348 | assert( rb->size == ( RING_BUFFER_SIZE - 1 - count ) ); |
klauss | 72:895ca792c647 | 349 | flag = true; |
klauss | 119:ee6a53069455 | 350 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) if( data[ i ] != value ) flag = false; |
klauss | 72:895ca792c647 | 351 | assert( flag == true ); |
klauss | 72:895ca792c647 | 352 | value++; |
klauss | 72:895ca792c647 | 353 | } |
klauss | 72:895ca792c647 | 354 | |
klauss | 119:ee6a53069455 | 355 | for( uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ){ |
klauss | 72:895ca792c647 | 356 | data = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 357 | assert( data == NULL ); |
klauss | 72:895ca792c647 | 358 | assert( rb->size == 0 ); |
klauss | 72:895ca792c647 | 359 | } |
klauss | 72:895ca792c647 | 360 | |
klauss | 72:895ca792c647 | 361 | ring_buffer_delete( &rb ); |
klauss | 72:895ca792c647 | 362 | |
klauss | 72:895ca792c647 | 363 | return( rb == NULL ) ? true : false; |
klauss | 72:895ca792c647 | 364 | |
klauss | 72:895ca792c647 | 365 | } |
klauss | 72:895ca792c647 | 366 | |
klauss | 72:895ca792c647 | 367 | /* |
klauss | 72:895ca792c647 | 368 | int main(){ |
klauss | 72:895ca792c647 | 369 | assert( 1 == unit_test_01() ); |
klauss | 72:895ca792c647 | 370 | printf("unit_test 01 -- ok\n\r"); |
klauss | 72:895ca792c647 | 371 | |
klauss | 72:895ca792c647 | 372 | assert( 1 == unit_test_02() ); |
klauss | 72:895ca792c647 | 373 | printf("unit_test 02 -- ok\n\r"); |
klauss | 72:895ca792c647 | 374 | |
klauss | 72:895ca792c647 | 375 | assert( 1 == unit_test_03() ); |
klauss | 72:895ca792c647 | 376 | printf("unit_test 03 -- ok\n\r"); |
klauss | 72:895ca792c647 | 377 | |
klauss | 72:895ca792c647 | 378 | assert( 1 == unit_test_04() ); |
klauss | 72:895ca792c647 | 379 | printf("unit_test 04 -- ok\n\r"); |
klauss | 72:895ca792c647 | 380 | |
klauss | 72:895ca792c647 | 381 | assert( 1 == unit_test_05() ); |
klauss | 72:895ca792c647 | 382 | printf("unit_test 05 -- ok\n\r"); |
klauss | 72:895ca792c647 | 383 | |
klauss | 72:895ca792c647 | 384 | return( 0 ); |
klauss | 72:895ca792c647 | 385 | } |
klauss | 72:895ca792c647 | 386 | */ |
klauss | 72:895ca792c647 | 387 | |
klauss | 72:895ca792c647 | 388 | void rb_xmemcpy(uint8_t * dest, uint8_t * src, uint16_t size){ |
klauss | 72:895ca792c647 | 389 | while (size--) *dest++ = *src++; |
klauss | 72:895ca792c647 | 390 | } |