添加ringbuffer的测试

master
zcy 2022-12-30 17:17:25 +08:00
parent 97bdec1f62
commit ae51fee51d
2 changed files with 50 additions and 4 deletions

View File

@ -140,7 +140,6 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
return i; return i;
} }
/* /*
*/ */
@ -159,6 +158,7 @@ int RingBuffer<T>::TakeBack(T *data,uint64_t len){
} }
int i = 0; int i = 0;
for(i = 0;i < len;i++) { for(i = 0;i < len;i++) {
data[len - 1 - i] = (*tail) ; data[len - 1 - i] = (*tail) ;
if((tail == m_head)) { if((tail == m_head)) {
@ -167,8 +167,7 @@ int RingBuffer<T>::TakeBack(T *data,uint64_t len){
} }
if(tail > m_data) { if(tail > m_data) {
tail--; tail--;
} }else if(tail == m_data) {
if(tail == m_data) {
tail = m_data_end; tail = m_data_end;
} }
// next is head stop // next is head stop

View File

@ -1,5 +1,7 @@
#include "loger.h" #include "loger.h"
#include "debug.h" #include "debug.h"
#include "pattern/ringbuffer.hpp"
#include <math.h>
#pragma pack(4) #pragma pack(4)
typedef struct test1{ typedef struct test1{
@ -46,7 +48,52 @@ BYTE_ORDER HostByteOrder(){
} }
} }
RingBuffer<int> pTest(200);
int tmp[200];
int tmp2[200];
int main(){ int main(){
int randlen;
for (int w = 0; w < 200; w++) {
tmp2[w] = w;
}
for(int i = 0;i < 100;i++){
std::cout << "\r\n";
std::cout << "round " << i << std::endl;
randlen = rand() % 100;
std::cout <<"randlen" << randlen << "\r\n";
int z = 0;
for (z = 0; z < randlen; z++) {
pTest.Add(&tmp2[z], 1);
}
std::cout << "check\r\n";
for (z = 0; z < randlen; z++) {
printf("%02d ", pTest.At(z));
//std::cout << pTest.At(z) << " ";
if ((z != 0) && (z % 10 == 0)) {
std::cout << "\r\n";
}
}
std::cout << "\r\n";
std::cout << "take" << pTest.TakeBack(tmp, randlen) << "\r\n" << std::endl;
std::cout << "\r\n";
for (int z = 0; z < randlen; z++) {
printf("%02d ", tmp[z]);
// std::cout << tmp[z]<< " ";
if ((z != 0)&&(z % 10 == 0)){
std::cout<<"\r\n";
}
}
std::cout << "\r\n";
}
std::cout<<"byteorder "<<HostByteOrder()<<std::endl; std::cout<<"byteorder "<<HostByteOrder()<<std::endl;
std::cout<< LimitFloat(1.123,2)<<std::endl; std::cout<< LimitFloat(1.123,2)<<std::endl;
std::cout<<LimitFloat(1.123,3) << std::endl; std::cout<<LimitFloat(1.123,3) << std::endl;