ringbuffer单元测试
parent
2aef9ca853
commit
d628355dcd
|
@ -17,6 +17,7 @@ public:
|
|||
uint32_t CanReadCount();
|
||||
uint32_t CanWriteCount();
|
||||
uint32_t Size();
|
||||
uint32_t Length();
|
||||
|
||||
~RingBuffer();
|
||||
|
||||
|
@ -57,6 +58,10 @@ template<typename T>
|
|||
uint32_t RingBuffer<T>::Size(){
|
||||
return mSize;
|
||||
}
|
||||
template<typename T>
|
||||
uint32_t RingBuffer<T>::Length(){
|
||||
return mSize - mRemain;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T RingBuffer<T>::At(uint64_t pos){
|
||||
|
@ -84,6 +89,7 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
|
|||
if(mCurrentHead == 0){
|
||||
memcpy(mData,data,bytes_write*sizeof(T));
|
||||
mCurrentTail = bytes_write;
|
||||
return bytes_write;
|
||||
}else{
|
||||
/// |start |...| head also tail|...|end|
|
||||
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";
|
||||
memcpy(&mData[0],&data[mSize - mCurrentTail],(mCurrentTail + bytes_write)%mSize*sizeof(T));
|
||||
mCurrentTail = (mCurrentTail + bytes_write)%mSize;
|
||||
this->mRemain -= bytes_write;
|
||||
return bytes_write;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// |start|head|.....|tail|end|
|
||||
if(mCurrentTail > mCurrentHead){
|
||||
uint16_t off = mCurrentTail + bytes_write;
|
||||
|
@ -105,43 +114,87 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
|
|||
sizeof(T)*(mSize - mCurrentTail));
|
||||
memcpy(mData,data + sizeof(T)*(mSize - mCurrentTail),
|
||||
(bytes_write - (mSize - mCurrentTail))*sizeof(T));
|
||||
mRemain -= bytes_write;
|
||||
mCurrentTail = off - mSize;
|
||||
return bytes_write;
|
||||
}
|
||||
if(off <= mSize){
|
||||
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|
|
||||
if(mCurrentHead > mCurrentTail){
|
||||
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>
|
||||
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>
|
||||
uint32_t RingBuffer<T>::CanReadCount(){
|
||||
if(mCurrentHead > mCurrentTail){
|
||||
return mSize - (mCurrentHead - mCurrentTail);
|
||||
}else{
|
||||
return mCurrentTail - mCurrentHead;
|
||||
}
|
||||
return mSize - mRemain;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
uint32_t RingBuffer<T>::CanWriteCount(){
|
||||
if (mCurrentTail > mCurrentHead){
|
||||
return mSize - (mCurrentTail - mCurrentHead);
|
||||
}
|
||||
if(mCurrentHead > mCurrentTail){
|
||||
return mCurrentHead - mCurrentTail;
|
||||
}
|
||||
return mRemain;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -17,11 +17,26 @@ void TestRingBuffer(){
|
|||
}
|
||||
|
||||
x.SetEmpty();
|
||||
ret = x.Add(in,1024);
|
||||
ret = x.Add(in,516);
|
||||
std::cout<<"recv "<<ret<<"\r\n"<<std::endl;
|
||||
for(uint32_t i = 0;i < x.Size();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]);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue