Permalink
Browse files

Merge pull request #17 from steinwurf/read-as-to-as

read_as to as
  • Loading branch information...
jpihl committed Oct 24, 2017
2 parents 1a9633a + 8f932d9 commit 17d0cd37cbbe1a916b153cbb633cc2060a3f3544
@@ -6,7 +6,7 @@ detailed list of every change, see the Git log.
Latest
------
* tbd
* Major: Renamed ``read_as`` to ``as``.
3.0.0
-----
@@ -110,10 +110,10 @@ LSB 0 mode
auto reader = bitter::lsb0_reader<bitter::u32, 8, 8, 8, 8>(0x12345678);
uint8_t value0 = reader.field<0>().read_as<uint8_t>(); // Read bits 0-7
uint8_t value1 = reader.field<1>().read_as<uint8_t>(); // Read bits 8-15
uint8_t value2 = reader.field<2>().read_as<uint8_t>(); // Read bits 16-23
uint8_t value3 = reader.field<3>().read_as<uint8_t>(); // Read bits 24-31
uint8_t value0 = reader.field<0>().as<uint8_t>(); // Read bits 0-7
uint8_t value1 = reader.field<1>().as<uint8_t>(); // Read bits 8-15
uint8_t value2 = reader.field<2>().as<uint8_t>(); // Read bits 16-23
uint8_t value3 = reader.field<3>().as<uint8_t>(); // Read bits 24-31
assert(value0 == 0x78);
assert(value1 == 0x56);
@@ -130,10 +130,10 @@ MSB 0 mode
auto reader = bitter::msb0_reader<bitter::u32, 8, 8, 8, 8>(0x12345678);
uint8_t value0 = reader.field<0>().read_as<uint8_t>(); // Read bits 0-7
uint8_t value1 = reader.field<1>().read_as<uint8_t>(); // Read bits 8-15
uint8_t value2 = reader.field<2>().read_as<uint8_t>(); // Read bits 16-23
uint8_t value3 = reader.field<3>().read_as<uint8_t>(); // Read bits 24-31
uint8_t value0 = reader.field<0>().as<uint8_t>(); // Read bits 0-7
uint8_t value1 = reader.field<1>().as<uint8_t>(); // Read bits 8-15
uint8_t value2 = reader.field<2>().as<uint8_t>(); // Read bits 16-23
uint8_t value3 = reader.field<3>().as<uint8_t>(); // Read bits 24-31
assert(value0 == 0x12);
assert(value1 == 0x34);
@@ -14,16 +14,16 @@ int main()
uint32_t value = 0x8028041U;
auto reader = bitter::lsb0_reader<bitter::u32, 1, 7, 8, 16>(value);
auto first_field = reader.field<0>().read_as<bool>();
auto first_field = reader.field<0>().as<bool>();
assert(first_field == true);
auto second_field = reader.field<1>().read_as<uint8_t>();
auto second_field = reader.field<1>().as<uint8_t>();
assert(second_field == 32U);
auto third_field = reader.field<2>().read_as<uint8_t>();
auto third_field = reader.field<2>().as<uint8_t>();
assert(third_field == 128U);
auto fourth_field = reader.field<3>().read_as<uint16_t>();;
auto fourth_field = reader.field<3>().as<uint16_t>();;
assert(fourth_field == 2050U);
return 0;
@@ -29,7 +29,7 @@ class bit_field
/// @return The bit field value cast to the type of ReturnType
template<typename ReturnType>
ReturnType read_as() const
ReturnType as() const
{
// Check if the size provide can fit into ReturnType
static_assert(Size <= size_in_bits<ReturnType>(), "There are not "
@@ -45,14 +45,14 @@ class reader
template<uint32_t Index>
bit_field_type<Index> field() const
{
return bit_field_type<Index>(read<Index>());
return bit_field_type<Index>(get<Index>());
}
private:
/// @brief Function used as a wrapper, used for retrieving a field
/// based on the Index provide
template<uint32_t Index>
typename DataType::type read() const
typename DataType::type get() const
{
return field_get<DataType, BitNumbering, Index, Sizes...>(m_value);
}
@@ -13,17 +13,17 @@ TEST(test_bit_field, bit_field_read_as)
{
auto field = bitter::bit_field<uint8_t, 1U>(1U);
EXPECT_TRUE(field.read_as<bool>());
EXPECT_TRUE(field.as<bool>());
}
TEST(test_bit_field, bit_field_read_as1)
{
auto field = bitter::bit_field<uint8_t, 1U>(0U);
EXPECT_FALSE(field.read_as<bool>());
EXPECT_FALSE(field.as<bool>());
}
TEST(test_bit_field, bit_field_read_as2)
{
auto field = bitter::bit_field<uint8_t, 8U>(255U);
EXPECT_EQ(255U, field.read_as<uint8_t>());
EXPECT_EQ(255U, field.as<uint8_t>());
}
@@ -18,26 +18,26 @@ TEST(test_bit_reader, read_bit)
{
auto reader = bitter::lsb0_reader<bitter::u32, 8, 8, 8, 8>(input);
auto value = reader.field<0>().read_as<uint8_t>();
auto value = reader.field<0>().as<uint8_t>();
EXPECT_EQ(0x00U, value);
value = reader.field<1>().read_as<uint8_t>();
value = reader.field<1>().as<uint8_t>();
EXPECT_EQ(0xFFU, value);
value = reader.field<2>().read_as<uint8_t>();
value = reader.field<2>().as<uint8_t>();
EXPECT_EQ(0xF0U, value);
value = reader.field<3>().read_as<uint8_t>();
value = reader.field<3>().as<uint8_t>();
EXPECT_EQ(0x0FU, value);
}
{
auto reader = bitter::msb0_reader<bitter::u32, 8, 8, 8, 8>(input);
auto value = reader.field<0>().read_as<uint8_t>();
auto value = reader.field<0>().as<uint8_t>();
EXPECT_EQ(0x0FU, value);
value = reader.field<1>().read_as<uint8_t>();
value = reader.field<1>().as<uint8_t>();
EXPECT_EQ(0xF0U, value);
value = reader.field<2>().read_as<uint8_t>();
value = reader.field<2>().as<uint8_t>();
EXPECT_EQ(0xFFU, value);
value = reader.field<3>().read_as<uint8_t>();
value = reader.field<3>().as<uint8_t>();
EXPECT_EQ(0x00U, value);
}
@@ -51,18 +51,18 @@ TEST(test_bit_reader, read_bit1)
{
auto reader = bitter::lsb0_reader<bitter::u32, 16, 16>(input);
auto value = reader.field<0>().read_as<uint16_t>();
auto value = reader.field<0>().as<uint16_t>();
EXPECT_EQ(0xFF00U, value);
value = reader.field<1>().read_as<uint16_t>();
value = reader.field<1>().as<uint16_t>();
EXPECT_EQ(0x0FF0U, value);
}
{
auto reader = bitter::msb0_reader<bitter::u32, 16, 16>(input);
auto value = reader.field<0>().read_as<uint16_t>();
auto value = reader.field<0>().as<uint16_t>();
EXPECT_EQ(0x0FF0U, value);
value = reader.field<1>().read_as<uint16_t>();
value = reader.field<1>().as<uint16_t>();
EXPECT_EQ(0xFF00U, value);
}
}
@@ -75,18 +75,18 @@ TEST(test_bit_reader, read_bit_u24)
{
auto reader = bitter::lsb0_reader<bitter::u24, 16, 8>(input);
auto value = reader.field<0>().read_as<uint16_t>();
auto value = reader.field<0>().as<uint16_t>();
EXPECT_EQ(0xFF00U, value);
value = reader.field<1>().read_as<uint8_t>();
value = reader.field<1>().as<uint8_t>();
EXPECT_EQ(0xF0U, value);
}
{
auto reader = bitter::msb0_reader<bitter::u24, 16, 8>(input);
auto value = reader.field<0>().read_as<uint16_t>();
auto value = reader.field<0>().as<uint16_t>();
EXPECT_EQ(0xF0FFU, value);
value = reader.field<1>().read_as<uint8_t>();
value = reader.field<1>().as<uint8_t>();
EXPECT_EQ(0x00, value);
}
}
@@ -99,18 +99,18 @@ TEST(test_bit_reader, read_bit3)
{
auto reader = bitter::lsb0_reader<bitter::u64, 32, 32>(input);
auto value = reader.field<0>().read_as<uint32_t>();
auto value = reader.field<0>().as<uint32_t>();
EXPECT_EQ(0x0FF0FF00U, value);
value = reader.field<1>().read_as<uint32_t>();
value = reader.field<1>().as<uint32_t>();
EXPECT_EQ(0xA50FF0A5U, value);
}
{
auto reader = bitter::msb0_reader<bitter::u64, 32, 32>(input);
auto value = reader.field<0>().read_as<uint32_t>();
auto value = reader.field<0>().as<uint32_t>();
EXPECT_EQ(0xA50FF0A5U, value);
value = reader.field<1>().read_as<uint32_t>();
value = reader.field<1>().as<uint32_t>();
EXPECT_EQ(0x0FF0FF00U, value);
}
}
@@ -121,13 +121,13 @@ TEST(test_bit_reader, read_bit4)
{
auto reader = bitter::lsb0_reader<bitter::u64, 64>(input);
auto value = reader.field<0>().read_as<uint64_t>();
auto value = reader.field<0>().as<uint64_t>();
EXPECT_EQ(0xA50FF0A50FF0FF00U, value);
}
{
auto reader = bitter::msb0_reader<bitter::u64, 64>(input);
auto value = reader.field<0>().read_as<uint64_t>();
auto value = reader.field<0>().as<uint64_t>();
EXPECT_EQ(0xA50FF0A50FF0FF00U, value);
}
}
@@ -138,19 +138,19 @@ TEST(test_bit_reader, read_bit5)
{
auto reader = bitter::lsb0_reader<bitter::u8, 1, 7>(input);
auto value = reader.field<0>().read_as<bool>();
auto value = reader.field<0>().as<bool>();
EXPECT_TRUE(value);
auto second_value = reader.field<1>().read_as<uint8_t>();
auto second_value = reader.field<1>().as<uint8_t>();
EXPECT_EQ(0b1111000, second_value);
}
{
auto reader = bitter::msb0_reader<bitter::u8, 1, 7>(input);
auto value = reader.field<0>().read_as<bool>();
auto value = reader.field<0>().as<bool>();
EXPECT_TRUE(value);
auto second_value = reader.field<1>().read_as<uint8_t>();
auto second_value = reader.field<1>().as<uint8_t>();
EXPECT_EQ(0b1110001, second_value);
}
}
@@ -161,20 +161,20 @@ TEST(test_bit_reader, read_bit6)
uint8_t input = 0b10000001;
auto reader = bitter::lsb0_reader<bitter::u8, 1, 7>(input);
auto value = reader.field<0>().read_as<bool>();
auto value = reader.field<0>().as<bool>();
EXPECT_TRUE(value);
auto second_value = reader.field<1>().read_as<uint8_t>();
auto second_value = reader.field<1>().as<uint8_t>();
EXPECT_EQ(0b1000000, second_value);
}
{
uint8_t input = 0b10000001;
auto reader = bitter::msb0_reader<bitter::u8, 1, 7>(input);
auto value = reader.field<0>().read_as<bool>();
auto value = reader.field<0>().as<bool>();
EXPECT_TRUE(value);
auto second_value = reader.field<1>().read_as<uint8_t>();
auto second_value = reader.field<1>().as<uint8_t>();
EXPECT_EQ(0b0000001, second_value);
}
}
@@ -186,57 +186,57 @@ TEST(test_bit_reader, read_bit9)
auto reader = bitter::lsb0_reader<
bitter::u8, 1, 1, 1, 1, 1, 1, 1, 1>(input);
auto value = reader.field<0>().read_as<bool>();
auto value = reader.field<0>().as<bool>();
EXPECT_TRUE(value);
value = reader.field<1>().read_as<bool>();
value = reader.field<1>().as<bool>();
EXPECT_FALSE(value);
value = reader.field<2>().read_as<bool>();
value = reader.field<2>().as<bool>();
EXPECT_TRUE(value);
value = reader.field<3>().read_as<bool>();
value = reader.field<3>().as<bool>();
EXPECT_FALSE(value);
value = reader.field<4>().read_as<bool>();
value = reader.field<4>().as<bool>();
EXPECT_TRUE(value);
value = reader.field<5>().read_as<bool>();
value = reader.field<5>().as<bool>();
EXPECT_TRUE(value);
value = reader.field<6>().read_as<bool>();
value = reader.field<6>().as<bool>();
EXPECT_FALSE(value);
value = reader.field<7>().read_as<bool>();
value = reader.field<7>().as<bool>();
EXPECT_TRUE(value);
}
{
uint8_t input = 0b10110101;
auto reader = bitter::msb0_reader<
bitter::u8, 1, 1, 1, 1, 1, 1, 1, 1>(input);
auto value = reader.field<0>().read_as<bool>();
auto value = reader.field<0>().as<bool>();
EXPECT_TRUE(value);
value = reader.field<1>().read_as<bool>();
value = reader.field<1>().as<bool>();
EXPECT_FALSE(value);
value = reader.field<2>().read_as<bool>();
value = reader.field<2>().as<bool>();
EXPECT_TRUE(value);
value = reader.field<3>().read_as<bool>();
value = reader.field<3>().as<bool>();
EXPECT_TRUE(value);
value = reader.field<4>().read_as<bool>();
value = reader.field<4>().as<bool>();
EXPECT_FALSE(value);
value = reader.field<5>().read_as<bool>();
value = reader.field<5>().as<bool>();
EXPECT_TRUE(value);
value = reader.field<6>().read_as<bool>();
value = reader.field<6>().as<bool>();
EXPECT_FALSE(value);
value = reader.field<7>().read_as<bool>();
value = reader.field<7>().as<bool>();
EXPECT_TRUE(value);
}
}
@@ -249,23 +249,23 @@ TEST(test_bit_reader, test_const)
bitter::u8, 1, 1, 1, 1, 1, 1, 1, 1>(input);
const auto field1 = reader.field<0>();
auto value1 = field1.read_as<bool>();
auto value1 = field1.as<bool>();
EXPECT_TRUE(value1);
const auto field2 = reader.field<7>();
auto value2 = field2.read_as<bool>();
auto value2 = field2.as<bool>();
EXPECT_FALSE(value2);
}
{
const auto reader = bitter::msb0_reader<
bitter::u8, 1, 1, 1, 1, 1, 1, 1, 1>(input);
const auto field1 = reader.field<0>();
auto value1 = field1.read_as<bool>();
auto value1 = field1.as<bool>();
EXPECT_FALSE(value1);
const auto field2 = reader.field<7>();
auto value2 = field2.read_as<bool>();
auto value2 = field2.as<bool>();
EXPECT_TRUE(value2);
}
}
Oops, something went wrong.

0 comments on commit 17d0cd3

Please sign in to comment.