ringbuffer单元测试

master
zcy 2021-05-16 01:51:10 +08:00
parent 2aef9ca853
commit d628355dcd
2 changed files with 84 additions and 16 deletions

View File

@ -17,6 +17,7 @@ public:
uint32_t CanReadCount(); uint32_t CanReadCount();
uint32_t CanWriteCount(); uint32_t CanWriteCount();
uint32_t Size(); uint32_t Size();
uint32_t Length();
~RingBuffer(); ~RingBuffer();
@ -57,6 +58,10 @@ template<typename T>
uint32_t RingBuffer<T>::Size(){ uint32_t RingBuffer<T>::Size(){
return mSize; return mSize;
} }
template<typename T>
uint32_t RingBuffer<T>::Length(){
return mSize - mRemain;
}
template<typename T> template<typename T>
T RingBuffer<T>::At(uint64_t pos){ T RingBuffer<T>::At(uint64_t pos){
@ -84,6 +89,7 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
if(mCurrentHead == 0){ if(mCurrentHead == 0){
memcpy(mData,data,bytes_write*sizeof(T)); memcpy(mData,data,bytes_write*sizeof(T));
mCurrentTail = bytes_write; mCurrentTail = bytes_write;
return bytes_write;
}else{ }else{
/// |start |...| head also tail|...|end| /// |start |...| head also tail|...|end|
if(mCurrentTail + bytes_write < mSize){ if(mCurrentTail + bytes_write < mSize){
@ -94,9 +100,12 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
std::cout<<"\r\n"<<(mCurrentTail + bytes_write)%mSize<<"\r\n"; std::cout<<"\r\n"<<(mCurrentTail + bytes_write)%mSize<<"\r\n";
memcpy(&mData[0],&data[mSize - mCurrentTail],(mCurrentTail + bytes_write)%mSize*sizeof(T)); memcpy(&mData[0],&data[mSize - mCurrentTail],(mCurrentTail + bytes_write)%mSize*sizeof(T));
mCurrentTail = (mCurrentTail + bytes_write)%mSize; mCurrentTail = (mCurrentTail + bytes_write)%mSize;
this->mRemain -= bytes_write;
return bytes_write;
} }
} }
} }
/// |start|head|.....|tail|end| /// |start|head|.....|tail|end|
if(mCurrentTail > mCurrentHead){ if(mCurrentTail > mCurrentHead){
uint16_t off = mCurrentTail + bytes_write; uint16_t off = mCurrentTail + bytes_write;
@ -105,43 +114,87 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
sizeof(T)*(mSize - mCurrentTail)); sizeof(T)*(mSize - mCurrentTail));
memcpy(mData,data + sizeof(T)*(mSize - mCurrentTail), memcpy(mData,data + sizeof(T)*(mSize - mCurrentTail),
(bytes_write - (mSize - mCurrentTail))*sizeof(T)); (bytes_write - (mSize - mCurrentTail))*sizeof(T));
mRemain -= bytes_write;
mCurrentTail = off - mSize;
return bytes_write;
} }
if(off <= mSize){ if(off <= mSize){
memcpy(&mData[mCurrentTail],data, memcpy(&mData[mCurrentTail],data,
sizeof(T)*bytes_write); sizeof(T)*bytes_write);
mRemain -= bytes_write;
if(off < mSize)
mCurrentTail += bytes_write;
else
mCurrentTail = 0;
return bytes_write;
} }
} }
/// |start|...|tail|.....|head|...|end| /// |start|...|tail|.....|head|...|end|
if(mCurrentHead > mCurrentTail){ if(mCurrentHead > mCurrentTail){
memcpy(&mData[mCurrentTail],data,sizeof(T)*(bytes_write)); memcpy(&mData[mCurrentTail],data,sizeof(T)*(bytes_write));
mCurrentTail += bytes_write;
mRemain -= bytes_write;
return bytes_write;
} }
mRemain -= bytes_write;
return bytes_write;
} }
template<typename T> template<typename T>
int RingBuffer<T>::Take(T *data,uint64_t len){ int RingBuffer<T>::Take(T *data,uint64_t len){
if(data == nullptr)
return -1;
if(mRemain == mSize)
return 0;
int bytes_read = 0;
if(len > (mSize - mRemain)){
bytes_read = mSize - mRemain;
}else{
bytes_read = len;
}
/// |start|head|.....|tail|end|
if(mCurrentTail >= mCurrentHead){
memcpy(data,&mData[mCurrentHead],sizeof(T)*bytes_read);
mCurrentHead += bytes_read;
mRemain = mCurrentTail - mCurrentHead;
return bytes_read;
}
/// |start|...|tail|.....|head|...|end|
if(mCurrentHead > mCurrentTail){
int off = mCurrentHead + bytes_read;
if(off > mSize){
int tmp = mSize - mCurrentHead;
memcpy(data,&mData[mCurrentHead],(mSize - mCurrentHead)*sizeof(T));
memcpy(data + (mSize - mCurrentHead)*sizeof(T),&mData[0],(bytes_read - tmp)*sizeof(T));
mCurrentHead = off - mSize;
mRemain = mCurrentTail - mCurrentHead;
return bytes_read;
}
if(off <= mSize){
memcpy(data,&mData[mCurrentHead],sizeof(T)*bytes_read);
if(off == mSize){
mCurrentHead = 0;
mRemain = mCurrentTail;
return bytes_read;
}
else{
mCurrentHead += bytes_read;
mRemain = (mSize - mCurrentHead) + mCurrentTail;
return bytes_read;
}
}
}
} }
template<typename T> template<typename T>
uint32_t RingBuffer<T>::CanReadCount(){ uint32_t RingBuffer<T>::CanReadCount(){
if(mCurrentHead > mCurrentTail){ return mSize - mRemain;
return mSize - (mCurrentHead - mCurrentTail);
}else{
return mCurrentTail - mCurrentHead;
}
} }
template<typename T> template<typename T>
uint32_t RingBuffer<T>::CanWriteCount(){ uint32_t RingBuffer<T>::CanWriteCount(){
if (mCurrentTail > mCurrentHead){ return mRemain;
return mSize - (mCurrentTail - mCurrentHead);
}
if(mCurrentHead > mCurrentTail){
return mCurrentHead - mCurrentTail;
}
} }
#endif #endif

View File

@ -17,11 +17,26 @@ void TestRingBuffer(){
} }
x.SetEmpty(); x.SetEmpty();
ret = x.Add(in,1024); ret = x.Add(in,516);
std::cout<<"recv "<<ret<<"\r\n"<<std::endl; std::cout<<"recv "<<ret<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < x.Size();i++){ for(uint32_t i = 0;i < x.Size();i++){
printf("%d ",x.At(i)); printf("%d ",x.At(i));
} }
ret = x.Add(in,20);
std::cout<<"recv "<<ret<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < x.Size();i++){
printf("%d ",x.At(i));
}
ret = x.Take(in,20);
std::cout<<"take "<<ret<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < ret;i++){
printf("%d ",in[i]);
}
ret = x.Take(in,516);
std::cout<<"take "<<ret<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < ret;i++){
printf("%d ",in[i]);
}
} }