mirror of
https://github.com/fraillt/bitsery.git
synced 2026-06-18 21:29:05 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ab4378bc2 | ||
|
|
bf712bc9e5 |
@@ -15,7 +15,7 @@ It has basic features, serialize arithmetic types, enums, containers and text, a
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <Bitsery.h>
|
||||
#include <bitsery/bitsery.h>
|
||||
|
||||
|
||||
enum class MyEnum { V1,V2,V3 };
|
||||
@@ -73,4 +73,5 @@ int main() {
|
||||
This library was tested on
|
||||
* Windows: Visual Studio 2015, MinGW (gcc 5.2)
|
||||
* Linux: GCC 5.4, GCC 6.2, Clang 3.9
|
||||
* OS X Mavericks: AppleClang 8
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <Bitsery.h>
|
||||
#include <bitsery/bitsery.h>
|
||||
|
||||
|
||||
enum class MyEnum { V1,V2,V3 };
|
||||
|
||||
@@ -25,8 +25,8 @@
|
||||
#define BITSERY_BITSERY_H
|
||||
|
||||
#define BITSERY_MAJOR_VERSION 1
|
||||
#define BITSERY_MINOR_VERSION 0
|
||||
#define BITSERY_PATCH_VERSION 0
|
||||
#define BITSERY_MINOR_VERSION 1
|
||||
#define BITSERY_PATCH_VERSION 1
|
||||
|
||||
#define BITSERY_QUOTE_MACRO(name) #name
|
||||
#define BITSERY_BUILD_VERSION_STR(major,minor, patch) \
|
||||
@@ -38,9 +38,9 @@ BITSERY_QUOTE_MACRO(patch)
|
||||
BITSERY_BUILD_VERSION_STR(BITSERY_MAJOR_VERSION, BITSERY_MINOR_VERSION, BITSERY_PATCH_VERSION)
|
||||
|
||||
|
||||
#include "BufferWriter.h"
|
||||
#include "BufferReader.h"
|
||||
#include "Serializer.h"
|
||||
#include "Deserializer.h"
|
||||
#include "buffer_writer.h"
|
||||
#include "buffer_reader.h"
|
||||
#include "serializer.h"
|
||||
#include "deserializer.h"
|
||||
|
||||
#endif //BITSERY_BITSERY_H
|
||||
@@ -25,7 +25,7 @@
|
||||
#ifndef BITSERY_BUFFER_READER_H
|
||||
#define BITSERY_BUFFER_READER_H
|
||||
|
||||
#include "Common.h"
|
||||
#include "common.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
@@ -36,10 +36,25 @@ namespace bitsery {
|
||||
|
||||
using value_type = uint8_t;
|
||||
|
||||
BufferReader(const std::vector<uint8_t> &buf) : _buf{buf}, _pos{std::begin(buf)} {
|
||||
BufferReader(const std::vector<uint8_t> &buf)
|
||||
: _pos{buf.data()},
|
||||
_end{buf.data() + buf.size()} {
|
||||
|
||||
}
|
||||
|
||||
BufferReader(const uint8_t* data, size_t size)
|
||||
: _pos{data},
|
||||
_end{data + size}
|
||||
{
|
||||
}
|
||||
template <size_t N>
|
||||
BufferReader(const uint8_t (&data)[N])
|
||||
: _pos{data},
|
||||
_end{data + N}
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
template<size_t SIZE, typename T>
|
||||
bool readBytes(T &v) {
|
||||
static_assert(std::is_integral<T>(), "");
|
||||
@@ -78,7 +93,7 @@ namespace bitsery {
|
||||
const auto bytesRequired = bitsCount > m_scratchBits
|
||||
? ((bitsCount - 1 - m_scratchBits) >> 3) + 1u
|
||||
: 0u;
|
||||
if (static_cast<size_t>(std::distance(_pos, std::end(_buf))) < bytesRequired)
|
||||
if (static_cast<size_t>(std::distance(_pos, _end)) < bytesRequired)
|
||||
return false;
|
||||
readBitsInternal(v, bitsCount);
|
||||
return true;
|
||||
@@ -94,18 +109,18 @@ namespace bitsery {
|
||||
}
|
||||
|
||||
bool isCompleted() const {
|
||||
return _pos == std::end(_buf);
|
||||
return _pos == _end;
|
||||
}
|
||||
|
||||
private:
|
||||
const std::vector<value_type> &_buf;
|
||||
decltype(std::begin(_buf)) _pos;
|
||||
const value_type* _pos;
|
||||
const value_type* _end;
|
||||
|
||||
template<typename T>
|
||||
bool directRead(T *v, size_t count) {
|
||||
static_assert(!std::is_const<T>::value, "");
|
||||
const auto bytesCount = sizeof(T) * count;
|
||||
if (static_cast<size_t>(std::distance(_pos, std::end(_buf))) < bytesCount)
|
||||
if (static_cast<size_t>(std::distance(_pos, _end)) < bytesCount)
|
||||
return false;
|
||||
std::copy_n(_pos, bytesCount, reinterpret_cast<value_type *>(v));
|
||||
std::advance(_pos, bytesCount);
|
||||
@@ -25,7 +25,7 @@
|
||||
#ifndef BITSERY_BUFFER_WRITER_H
|
||||
#define BITSERY_BUFFER_WRITER_H
|
||||
|
||||
#include "Common.h"
|
||||
#include "common.h"
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
@@ -25,7 +25,7 @@
|
||||
#ifndef BITSERY_COMMON_H
|
||||
#define BITSERY_COMMON_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <cstdint>
|
||||
|
||||
namespace bitsery {
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
#include <array>
|
||||
#include <stack>
|
||||
#include <algorithm>
|
||||
#include "Deserializer.h"
|
||||
#include "deserializer.h"
|
||||
|
||||
namespace bitsery {
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
#include <array>
|
||||
#include <stack>
|
||||
#include <algorithm>
|
||||
#include "Serializer.h"
|
||||
#include "serializer.h"
|
||||
|
||||
namespace bitsery {
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#ifndef BITSERY_DESERIALIZER_H
|
||||
#define BITSERY_DESERIALIZER_H
|
||||
|
||||
#include "Common.h"
|
||||
#include "common.h"
|
||||
#include <array>
|
||||
#include <utility>
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
#ifndef BITSERY_SERIALIZER_H
|
||||
#define BITSERY_SERIALIZER_H
|
||||
|
||||
#include "Common.h"
|
||||
#include "common.h"
|
||||
#include <array>
|
||||
|
||||
namespace bitsery {
|
||||
@@ -39,33 +39,54 @@ include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||
file(GLOB TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||
|
||||
FOREACH(TEST_PROJECT_FILE ${TEST_SRC_FILES})
|
||||
get_filename_component(TEST_PROJECT_NAME ${TEST_PROJECT_FILE} NAME_WE)
|
||||
get_filename_component(SEPARATE_TEST_NAME ${TEST_PROJECT_FILE} NAME_WE)
|
||||
set(SEPARATE_TEST_NAME TEST_${SEPARATE_TEST_NAME})
|
||||
add_executable(${SEPARATE_TEST_NAME} ${TEST_PROJECT_FILE})
|
||||
add_dependencies(${SEPARATE_TEST_NAME} googletest)
|
||||
|
||||
add_executable(${TEST_PROJECT_NAME} ${TEST_PROJECT_FILE})
|
||||
add_dependencies(${TEST_PROJECT_NAME} googletest)
|
||||
|
||||
set_property(TARGET ${TEST_PROJECT_NAME} PROPERTY CXX_STANDARD 14)
|
||||
set_property(TARGET ${TEST_PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
set_property(TARGET ${SEPARATE_TEST_NAME} PROPERTY CXX_STANDARD 14)
|
||||
set_property(TARGET ${SEPARATE_TEST_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
|
||||
if(NOT WIN32 OR MINGW)
|
||||
FOREACH(LIBNAME ${GTEST_LINK_LIBNAMES})
|
||||
target_link_libraries(${TEST_PROJECT_NAME} ${GTEST_LIBS_DIR}/lib${LIBNAME}.a )
|
||||
target_link_libraries(${SEPARATE_TEST_NAME} ${GTEST_LIBS_DIR}/lib${LIBNAME}.a )
|
||||
ENDFOREACH()
|
||||
else()
|
||||
FOREACH(LIBNAME ${GTEST_LINK_LIBNAMES})
|
||||
target_link_libraries(${TEST_PROJECT_NAME}
|
||||
target_link_libraries(${SEPARATE_TEST_NAME}
|
||||
debug ${GTEST_LIBS_DIR}/DebugLibs/${CMAKE_FIND_LIBRARY_PREFIXES}${LIBNAME}${CMAKE_FIND_LIBRARY_SUFFIXES}
|
||||
optimized ${GTEST_LIBS_DIR}/ReleaseLibs/${CMAKE_FIND_LIBRARY_PREFIXES}${LIBNAME}${CMAKE_FIND_LIBRARY_SUFFIXES})
|
||||
ENDFOREACH()
|
||||
endif()
|
||||
|
||||
target_link_libraries(${TEST_PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries(${SEPARATE_TEST_NAME} ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
||||
add_test(NAME ${TEST_PROJECT_NAME} COMMAND $<TARGET_FILE:${TEST_PROJECT_NAME}>)
|
||||
add_test(NAME ${SEPARATE_TEST_NAME} COMMAND $<TARGET_FILE:${SEPARATE_TEST_NAME}>)
|
||||
|
||||
ENDFOREACH()
|
||||
|
||||
#all in one tests for code coverage
|
||||
add_executable(${TEST_PROJECT_NAME} ${TEST_SRC_FILES})
|
||||
add_dependencies(${TEST_PROJECT_NAME} googletest)
|
||||
|
||||
set_property(TARGET ${TEST_PROJECT_NAME} PROPERTY CXX_STANDARD 14)
|
||||
set_property(TARGET ${TEST_PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
|
||||
if(NOT WIN32 OR MINGW)
|
||||
FOREACH(LIBNAME ${GTEST_LINK_LIBNAMES})
|
||||
target_link_libraries(${TEST_PROJECT_NAME} ${GTEST_LIBS_DIR}/lib${LIBNAME}.a )
|
||||
ENDFOREACH()
|
||||
else()
|
||||
FOREACH(LIBNAME ${GTEST_LINK_LIBNAMES})
|
||||
target_link_libraries(${TEST_PROJECT_NAME}
|
||||
debug ${GTEST_LIBS_DIR}/DebugLibs/${CMAKE_FIND_LIBRARY_PREFIXES}${LIBNAME}${CMAKE_FIND_LIBRARY_SUFFIXES}
|
||||
optimized ${GTEST_LIBS_DIR}/ReleaseLibs/${CMAKE_FIND_LIBRARY_PREFIXES}${LIBNAME}${CMAKE_FIND_LIBRARY_SUFFIXES})
|
||||
ENDFOREACH()
|
||||
endif()
|
||||
|
||||
target_link_libraries(${TEST_PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "BufferWriter.h"
|
||||
#include "BufferReader.h"
|
||||
#include <bitsery/buffer_writer.h>
|
||||
#include <bitsery/buffer_reader.h>
|
||||
#include <list>
|
||||
#include <bitset>
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "BufferWriter.h"
|
||||
#include "BufferReader.h"
|
||||
#include <bitsery/buffer_writer.h>
|
||||
#include <bitsery/buffer_reader.h>
|
||||
#include <list>
|
||||
#include <bitset>
|
||||
|
||||
@@ -41,9 +41,7 @@ struct IntegralTypes {
|
||||
int8_t f[2];
|
||||
};
|
||||
|
||||
|
||||
TEST(BufferBytesOperations, WriteAndReadBytes) {
|
||||
//setup data
|
||||
IntegralTypes getInitializedIntegralTypes() {
|
||||
IntegralTypes data;
|
||||
data.a = -4894541654564;
|
||||
data.b = 94545646;
|
||||
@@ -52,11 +50,10 @@ TEST(BufferBytesOperations, WriteAndReadBytes) {
|
||||
data.e = -98;
|
||||
data.f[0] = 43;
|
||||
data.f[1] = -45;
|
||||
return data;
|
||||
}
|
||||
|
||||
//create and write to buffer
|
||||
std::vector<uint8_t> buf{};
|
||||
BufferWriter bw{buf};
|
||||
|
||||
void writeIntegralTypesToBuffer(BufferWriter& bw, const IntegralTypes& data) {
|
||||
bw.writeBytes<4>(data.b);
|
||||
bw.writeBytes<1>(data.f[0]);
|
||||
bw.writeBytes<2>(data.c);
|
||||
@@ -64,6 +61,17 @@ TEST(BufferBytesOperations, WriteAndReadBytes) {
|
||||
bw.writeBytes<8>(data.a);
|
||||
bw.writeBytes<1>(data.e);
|
||||
bw.writeBytes<1>(data.f[1]);
|
||||
}
|
||||
|
||||
|
||||
TEST(BufferBytesOperations, WriteAndReadBytes) {
|
||||
//setup data
|
||||
auto data =getInitializedIntegralTypes();
|
||||
//create and write to buffer
|
||||
std::vector<uint8_t> buf{};
|
||||
BufferWriter bw{buf};
|
||||
writeIntegralTypesToBuffer(bw, data);
|
||||
|
||||
EXPECT_THAT(std::distance(buf.begin(), buf.end()), Eq(18));
|
||||
//read from buffer
|
||||
BufferReader br{buf};
|
||||
@@ -86,6 +94,68 @@ TEST(BufferBytesOperations, WriteAndReadBytes) {
|
||||
|
||||
}
|
||||
|
||||
TEST(BufferBytesOperations, BufferReaderUsingDataPlusSizeCtor) {
|
||||
//setup data
|
||||
auto data =getInitializedIntegralTypes();
|
||||
//create and write to buffer
|
||||
std::vector<uint8_t> buf{};
|
||||
BufferWriter bw{buf};
|
||||
writeIntegralTypesToBuffer(bw, data);
|
||||
|
||||
EXPECT_THAT(std::distance(buf.begin(), buf.end()), Eq(18));
|
||||
//read from buffer
|
||||
BufferReader br{buf.data(), buf.size()};
|
||||
IntegralTypes res{};
|
||||
EXPECT_THAT(br.readBytes<4>(res.b), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<1>(res.f[0]), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<2>(res.c), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<1>(res.d), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<8>(res.a), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<1>(res.e), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<1>(res.f[1]), Eq(true));
|
||||
//assert results
|
||||
|
||||
EXPECT_THAT(data.a, Eq(res.a));
|
||||
EXPECT_THAT(data.b, Eq(res.b));
|
||||
EXPECT_THAT(data.c, Eq(res.c));
|
||||
EXPECT_THAT(data.d, Eq(res.d));
|
||||
EXPECT_THAT(data.e, Eq(res.e));
|
||||
EXPECT_THAT(data.f, ContainerEq(res.f));
|
||||
}
|
||||
|
||||
TEST(BufferBytesOperations, BufferReaderUsingCArrayCtor) {
|
||||
//setup data
|
||||
auto data =getInitializedIntegralTypes();
|
||||
//create and write to buffer
|
||||
std::vector<uint8_t> buf{};
|
||||
BufferWriter bw{buf};
|
||||
writeIntegralTypesToBuffer(bw, data);
|
||||
|
||||
ASSERT_THAT(std::distance(buf.begin(), buf.end()), Eq(18));
|
||||
uint8_t cArrBuf[18];
|
||||
std::copy(buf.begin(), buf.end(), cArrBuf);
|
||||
//read from buffer
|
||||
BufferReader br{cArrBuf};
|
||||
IntegralTypes res{};
|
||||
EXPECT_THAT(br.readBytes<4>(res.b), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<1>(res.f[0]), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<2>(res.c), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<1>(res.d), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<8>(res.a), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<1>(res.e), Eq(true));
|
||||
EXPECT_THAT(br.readBytes<1>(res.f[1]), Eq(true));
|
||||
//assert results
|
||||
|
||||
EXPECT_THAT(data.a, Eq(res.a));
|
||||
EXPECT_THAT(data.b, Eq(res.b));
|
||||
EXPECT_THAT(data.c, Eq(res.c));
|
||||
EXPECT_THAT(data.d, Eq(res.d));
|
||||
EXPECT_THAT(data.e, Eq(res.e));
|
||||
EXPECT_THAT(data.f, ContainerEq(res.f));
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(BufferBytesOperations, ReadReturnsFalseIfNotEnoughBufferSize) {
|
||||
//setup data
|
||||
uint8_t a = 111;
|
||||
@@ -146,5 +216,4 @@ TEST(BufferBytesOperations, ReadIsCompletedWhenAllBytesAreRead) {
|
||||
EXPECT_THAT(br1.readBytes<1>(res.d), Eq(true));
|
||||
EXPECT_THAT(br1.isCompleted(), Eq(true));
|
||||
|
||||
|
||||
}
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "SerializationTestUtils.h"
|
||||
#include "serialization_test_utils.h"
|
||||
|
||||
using testing::Eq;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "SerializationTestUtils.h"
|
||||
#include "serialization_test_utils.h"
|
||||
#include <numeric>
|
||||
#include <deque>
|
||||
#include <list>
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "SerializationTestUtils.h"
|
||||
#include "serialization_test_utils.h"
|
||||
#include <type_traits>
|
||||
|
||||
using testing::ContainerEq;
|
||||
@@ -22,10 +22,10 @@
|
||||
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "SerializationTestUtils.h"
|
||||
#include "serialization_test_utils.h"
|
||||
|
||||
#include "DeltaSerializer.h"
|
||||
#include "DeltaDeserializer.h"
|
||||
#include <bitsery/delta_serializer.h>
|
||||
#include <bitsery/delta_deserializer.h>
|
||||
|
||||
#include <list>
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "SerializationTestUtils.h"
|
||||
#include "serialization_test_utils.h"
|
||||
using namespace testing;
|
||||
using bitsery::RangeSpec;
|
||||
using bitsery::BitsConstraint;
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "SerializationTestUtils.h"
|
||||
#include "serialization_test_utils.h"
|
||||
|
||||
using testing::Eq;
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "SerializationTestUtils.h"
|
||||
#include "serialization_test_utils.h"
|
||||
using namespace testing;
|
||||
|
||||
TEST(SerializeSubstitution, WhenSubstitutedThenOnlyWriteIndexUsingMinRequiredBits) {
|
||||
@@ -24,10 +24,7 @@
|
||||
#ifndef BITSERY_SERIALIZERTESTS_H
|
||||
#define BITSERY_SERIALIZERTESTS_H
|
||||
|
||||
#include <Deserializer.h>
|
||||
#include <BufferReader.h>
|
||||
#include "BufferWriter.h"
|
||||
#include "Serializer.h"
|
||||
#include <bitsery/bitsery.h>
|
||||
#include <memory>
|
||||
|
||||
struct MyStruct1 {
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "SerializationTestUtils.h"
|
||||
#include "serialization_test_utils.h"
|
||||
using namespace testing;
|
||||
|
||||
TEST(SerializeText, BasicString) {
|
||||
@@ -21,7 +21,7 @@
|
||||
//SOFTWARE.
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "SerializationTestUtils.h"
|
||||
#include "serialization_test_utils.h"
|
||||
|
||||
using testing::Eq;
|
||||
|
||||
Reference in New Issue
Block a user