Skip to content

Commit

Permalink
Fixed float bug #72. 4 and 8 bytes floats are now supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
ihedvall committed Feb 15, 2024
1 parent 16b8f11 commit 03586d7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
11 changes: 7 additions & 4 deletions mdflib/src/cn4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ ChannelDataType Cn4Block::DataType() const {
ChannelType Cn4Block::Type() const { return static_cast<ChannelType>(type_); }

uint64_t Cn4Block::DataBytes() const {
auto value_size = static_cast<uint64_t>((bit_count_) / 8) + (bit_count_ % 8 > 0 ? 1 : 0);
auto value_size = static_cast<uint64_t>((bit_count_) / 8)
+ (bit_count_ % 8 > 0 ? 1 : 0);
/*
auto* channel_array = ChannelArray();
if (channel_array != nullptr) {
Expand All @@ -263,6 +264,7 @@ uint64_t Cn4Block::DataBytes() const {
*/
return value_size;
}

void Cn4Block::Decimals(uint8_t precision) {
precision_ = precision;
flags_ |= CnFlag::PrecisionValid;
Expand Down Expand Up @@ -784,6 +786,7 @@ void Cn4Block::SetByteArray(const std::vector<uint8_t> &value, bool valid) {
IChannel::SetByteArray(value, valid);
}
}

void Cn4Block::Unit(const std::string &unit) {
if (unit.empty()) {
unit_.reset();
Expand Down Expand Up @@ -1006,10 +1009,10 @@ void Cn4Block::PrepareForWriting(size_t offset) {

case ChannelDataType::FloatLe:
case ChannelDataType::FloatBe:
if (DataBytes() == 0) {
if (bit_count_ == 0) {
bit_count_ = 64; // Assume double
} else {
switch (DataBytes()) {
switch (bit_count_) {
case 32:
case 64:
break;
Expand Down Expand Up @@ -1049,7 +1052,7 @@ void Cn4Block::PrepareForWriting(size_t offset) {
if (bit_count_ == 0) {
bit_count_ = 128; // Assume 2 double
} else {
switch (DataBytes()) {
switch (bit_count_) {
case 64: // 2 float
case 128:// 2 double
break;
Expand Down
31 changes: 31 additions & 0 deletions mdflib_test/src/testwrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,11 +1239,42 @@ TEST_F(TestWrite, Mdf4Float) {
const auto* file1 = reader.GetFile();
const auto* header1 = file1->Header();
const auto dg_list = header1->DataGroups();
EXPECT_EQ(dg_list.size(), 1);
for (auto* dg4 : dg_list) {
const auto cg_list = dg4->ChannelGroups();
EXPECT_EQ(cg_list.size(), 1);
for (auto* cg4 : cg_list) {
CreateChannelObserverForChannelGroup(*dg4, *cg4, observer_list);
const auto channel_list = cg4->Channels();
EXPECT_EQ(channel_list.size(), 4);
for (size_t index = 0; index < channel_list.size(); ++index) {
const auto* channel = channel_list[index];
EXPECT_TRUE(channel != nullptr);
switch (index) {
case 0:
EXPECT_EQ(channel->DataBytes(), 4) << channel->Name();
EXPECT_EQ(channel->DataType(), ChannelDataType::FloatLe);
break;

case 1:
EXPECT_EQ(channel->DataBytes(), 8) << channel->Name();
EXPECT_EQ(channel->DataType(), ChannelDataType::FloatLe);
break;

case 2:
EXPECT_EQ(channel->DataBytes(), 4) << channel->Name();
EXPECT_EQ(channel->DataType(), ChannelDataType::FloatBe);
break;

case 3:
EXPECT_EQ(channel->DataBytes(), 8) << channel->Name();
EXPECT_EQ(channel->DataType(), ChannelDataType::FloatBe);
break;
default:
break;
}
}

}
reader.ReadData(*dg4);
}
Expand Down

0 comments on commit 03586d7

Please sign in to comment.