43 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "ring_buffer.h"
 | |
| 
 | |
| #include <gmock/gmock.h>
 | |
| #include <gtest/gtest.h>
 | |
| 
 | |
| #include <array>
 | |
| 
 | |
| using namespace ::testing;
 | |
| 
 | |
| TEST(RingBuffer, BasicStoreLoad) {
 | |
|     std::array<char, 20> arr;
 | |
|     RingBuffer rb{.buffer = std::as_writable_bytes(std::span{arr})};
 | |
| 
 | |
|     std::array<char, 10> a0 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 | |
|     std::array<char, 10> a1 = {};
 | |
| 
 | |
|     ASSERT_TRUE(rb.Store(std::as_bytes(std::span{a0})));
 | |
|     ASSERT_TRUE(rb.Load(std::as_writable_bytes(std::span{a1})));
 | |
| 
 | |
|     EXPECT_THAT(a1, ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
 | |
| }
 | |
| 
 | |
| TEST(RingBuffer, StoreOverflow) {
 | |
|     std::array<char, 10> arr;
 | |
|     RingBuffer rb{.buffer = std::as_writable_bytes(std::span{arr})};
 | |
| 
 | |
|     std::array<char, 8> a0 = {0, 1, 2, 3, 4, 5, 6, 7};
 | |
|     std::array<char, 8> a1 = {};
 | |
| 
 | |
|     ASSERT_TRUE(rb.Store(std::as_bytes(std::span{a0})));
 | |
|     ASSERT_FALSE(rb.Store(std::as_bytes(std::span{a0})));
 | |
| 
 | |
|     EXPECT_EQ(rb.AvailableData(), 8);
 | |
| 
 | |
|     ASSERT_TRUE(rb.Pop(7));
 | |
|     EXPECT_EQ(rb.AvailableData(), 1);
 | |
| 
 | |
|     EXPECT_TRUE(rb.Store(std::as_bytes(std::span{a0})));
 | |
|     ASSERT_TRUE(rb.Load(std::as_writable_bytes(std::span{a1})));
 | |
|     // one byte leftover from first run, then 7 more
 | |
|     EXPECT_THAT(a1, ElementsAre(7, 0, 1, 2, 3, 4, 5, 6));
 | |
| }
 |