mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-17 10:40:35 +08:00
Compile time BitLenToByteLen computation; marshal type util tests
This commit is contained in:
@@ -20,6 +20,10 @@ template <>
|
||||
struct IntegerBitLen<0> { enum { Result = 0 }; };
|
||||
|
||||
|
||||
template <unsigned long BitLen>
|
||||
struct BitLenToByteLen { enum { Result = (BitLen + 7) / 8 }; };
|
||||
|
||||
|
||||
template <typename T, typename Enable = void>
|
||||
struct StorageType { typedef T Type; };
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <uavcan/internal/lazy_constructor.hpp>
|
||||
#include <uavcan/internal/transport/transfer_sender.hpp>
|
||||
#include <uavcan/internal/marshal/scalar_codec.hpp>
|
||||
#include <uavcan/internal/marshal/types.hpp>
|
||||
|
||||
namespace uavcan
|
||||
{
|
||||
@@ -54,8 +55,7 @@ private:
|
||||
|
||||
IMarshalBuffer* getBuffer()
|
||||
{
|
||||
const int size = (DataType::MaxBitLen + 7) / 8;
|
||||
return buffer_provider_.getBuffer(size);
|
||||
return buffer_provider_.getBuffer(BitLenToByteLen<DataType::MaxBitLen>::Result);
|
||||
}
|
||||
|
||||
int genericSend(const DataType& message, TransferType transfer_type, NodeID dst_node_id,
|
||||
|
||||
@@ -76,18 +76,6 @@ struct CustomType
|
||||
};
|
||||
|
||||
|
||||
TEST(Array, IntegerBitLen)
|
||||
{
|
||||
using uavcan::IntegerBitLen;
|
||||
|
||||
ASSERT_EQ(0, IntegerBitLen<0>::Result);
|
||||
ASSERT_EQ(1, IntegerBitLen<1>::Result);
|
||||
ASSERT_EQ(6, IntegerBitLen<42>::Result);
|
||||
ASSERT_EQ(8, IntegerBitLen<232>::Result);
|
||||
ASSERT_EQ(32, IntegerBitLen<0x81234567>::Result);
|
||||
}
|
||||
|
||||
|
||||
TEST(Array, Basic)
|
||||
{
|
||||
typedef Array<IntegerSpec<8, SignednessSigned, CastModeTruncate>, ArrayModeStatic, 4> A1;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <uavcan/internal/marshal/types.hpp>
|
||||
|
||||
|
||||
TEST(MarshalTypeUtil, IntegerBitLen)
|
||||
{
|
||||
using uavcan::IntegerBitLen;
|
||||
|
||||
ASSERT_EQ(0, IntegerBitLen<0>::Result);
|
||||
ASSERT_EQ(1, IntegerBitLen<1>::Result);
|
||||
ASSERT_EQ(6, IntegerBitLen<42>::Result);
|
||||
ASSERT_EQ(8, IntegerBitLen<232>::Result);
|
||||
ASSERT_EQ(32, IntegerBitLen<0x81234567>::Result);
|
||||
}
|
||||
|
||||
|
||||
TEST(MarshalTypeUtil, BitLenToByteLen)
|
||||
{
|
||||
using uavcan::BitLenToByteLen;
|
||||
|
||||
ASSERT_EQ(2, BitLenToByteLen<16>::Result);
|
||||
ASSERT_EQ(1, BitLenToByteLen<8>::Result);
|
||||
ASSERT_EQ(1, BitLenToByteLen<7>::Result);
|
||||
ASSERT_EQ(1, BitLenToByteLen<1>::Result);
|
||||
ASSERT_EQ(2, BitLenToByteLen<9>::Result);
|
||||
}
|
||||
Reference in New Issue
Block a user