mirror of
				https://github.com/Telecominfraproject/wlan-cloud-lib-cppkafka.git
				synced 2025-11-03 20:18:06 +00:00 
			
		
		
		
	Support for raw array Buffer constructor
This commit is contained in:
		@@ -127,6 +127,20 @@ public:
 | 
				
			|||||||
    template <typename T, size_t N>
 | 
					    template <typename T, size_t N>
 | 
				
			||||||
    Buffer(std::array<T, N>&& data) = delete;
 | 
					    Buffer(std::array<T, N>&& data) = delete;
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Constructs a buffer from a raw array
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * \param data The the array to be used as input
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    template <typename T, size_t N>
 | 
				
			||||||
 | 
					    Buffer(const T(&data)[N])
 | 
				
			||||||
 | 
					    : Buffer(data, N) {
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					     // Don't allow construction from temporary raw arrays
 | 
				
			||||||
 | 
					    template <typename T, size_t N>
 | 
				
			||||||
 | 
					    Buffer(T(&&data)[N]) = delete;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * \brief Construct a buffer from a const string ref
 | 
					     * \brief Construct a buffer from a const string ref
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,11 +1,13 @@
 | 
				
			|||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <vector>
 | 
					#include <vector>
 | 
				
			||||||
 | 
					#include <array>
 | 
				
			||||||
#include <sstream>
 | 
					#include <sstream>
 | 
				
			||||||
#include <catch.hpp>
 | 
					#include <catch.hpp>
 | 
				
			||||||
#include "cppkafka/buffer.h"
 | 
					#include "cppkafka/buffer.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using std::string;
 | 
					using std::string;
 | 
				
			||||||
using std::vector;
 | 
					using std::vector;
 | 
				
			||||||
 | 
					using std::array;
 | 
				
			||||||
using std::ostringstream;
 | 
					using std::ostringstream;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using namespace cppkafka;
 | 
					using namespace cppkafka;
 | 
				
			||||||
@@ -36,14 +38,32 @@ TEST_CASE("conversions", "[buffer]") {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST_CASE("construction", "[buffer]") {
 | 
					TEST_CASE("construction", "[buffer]") {
 | 
				
			||||||
 | 
					    // From string
 | 
				
			||||||
    const string str_data = "Hello world!";
 | 
					    const string str_data = "Hello world!";
 | 
				
			||||||
    const vector<uint8_t> data(str_data.begin(), str_data.end());
 | 
					    // From vector
 | 
				
			||||||
    const Buffer buffer(data);
 | 
					    const vector<uint8_t> vector_data(str_data.begin(), str_data.end());
 | 
				
			||||||
    const Buffer buffer2(data.begin(), data.end());
 | 
					    // From array
 | 
				
			||||||
    const Buffer buffer3(str_data.data(), str_data.data() + str_data.size());
 | 
					    const array<char,12> array_data{'H','e','l','l','o',' ','w','o','r','l','d','!'};
 | 
				
			||||||
 | 
					    // From raw array
 | 
				
			||||||
 | 
					    const char raw_array[12]{'H','e','l','l','o',' ','w','o','r','l','d','!'};
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // Build buffers
 | 
				
			||||||
 | 
					    const Buffer buffer(vector_data); //vector
 | 
				
			||||||
 | 
					    const Buffer buffer2(vector_data.begin(), vector_data.end()); //iterators
 | 
				
			||||||
 | 
					    const Buffer buffer3(str_data.data(), str_data.data() + str_data.size()); //char iterators
 | 
				
			||||||
 | 
					    const Buffer buffer4(array_data); //arrays
 | 
				
			||||||
 | 
					    const Buffer buffer5(raw_array); //raw arrays
 | 
				
			||||||
 | 
					    const Buffer buffer6(str_data); //string
 | 
				
			||||||
 | 
					    const Buffer buffer7(str_data.data(), str_data.size()); //type + size
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // Test
 | 
				
			||||||
    CHECK(str_data == buffer);
 | 
					    CHECK(str_data == buffer);
 | 
				
			||||||
    CHECK(buffer == buffer2);
 | 
					    CHECK(buffer == buffer2);
 | 
				
			||||||
    CHECK(buffer == buffer3);
 | 
					    CHECK(buffer == buffer3);
 | 
				
			||||||
 | 
					    CHECK(buffer == buffer4);
 | 
				
			||||||
 | 
					    CHECK(buffer == buffer5);
 | 
				
			||||||
 | 
					    CHECK(buffer == buffer6);
 | 
				
			||||||
 | 
					    CHECK(buffer == buffer7);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user