Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Primitive Array Access #48

Merged
merged 2 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ros_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const RosValue::Pointer RosValue::at(const size_t idx) const {
return RosValue::Pointer(
primitive_array_info_.element_type,
primitive_array_info_.message_buffer,
primitive_array_info_.offset + idx
primitive_array_info_.offset + idx * primitiveTypeToSize(primitive_array_info_.element_type)
);
} else {
throw std::runtime_error("Value is not an array or object");
Expand Down
2 changes: 1 addition & 1 deletion python/adapters.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ py::object getField(Embag::RosValue::Pointer &v, const std::string field_name) {
}

py::object getIndex(Embag::RosValue::Pointer &v, const size_t index) {
if (v->getType() != Embag::RosValue::Type::array) {
if (v->getType() != Embag::RosValue::Type::array && v->getType() != Embag::RosValue::Type::primitive_array) {
throw std::runtime_error("Can only getIndex on an array");
}

Expand Down
Binary file modified test/array_test.bag
Binary file not shown.
24 changes: 23 additions & 1 deletion test/embag_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,26 @@ TEST_F(ArraysTest, ArrayReading) {
ASSERT_EQ(static_bool_array[inner_index]->as<bool>(), index == inner_index);
}

// Tests a dynamically sized array of doubles
inner_index = 0;
const auto dynamic_uint64_array = message->data()["index_multiples_as_dynamic_uint64_array"];
for (auto item = dynamic_uint64_array->beginValues<Embag::RosValue::Pointer>(); item != dynamic_uint64_array->endValues<Embag::RosValue::Pointer>(); item++) {
ASSERT_EQ((*item)->as<uint64_t>(), (uint64_t) index * inner_index++);
}
for (inner_index = 0; inner_index < 20; inner_index++) {
ASSERT_EQ(dynamic_uint64_array[inner_index]->as<uint64_t>(), (uint64_t) index * inner_index);
}

// Tests a statically sized array of doubles
inner_index = 0;
const auto static_uint64_array = message->data()["index_multiples_as_static_uint64_array"];
for (auto item = static_uint64_array->beginValues<Embag::RosValue::Pointer>(); item != static_uint64_array->endValues<Embag::RosValue::Pointer>(); item++) {
ASSERT_EQ((*item)->as<uint64_t>(), (uint64_t) index * inner_index++);
}
for (inner_index = 0; inner_index < 20; inner_index++) {
ASSERT_EQ(static_uint64_array[inner_index]->as<uint64_t>(), (uint64_t) index * inner_index);
}

// Tests an array of strings
inner_index = 0;
const auto string_array = message->data()["index_as_string_array"];
Expand All @@ -296,7 +316,7 @@ TEST_F(ArraysTest, ArrayReading) {
ASSERT_EQ(string_array[inner_index]->as<std::string>(), index == inner_index ? "true" : "false");
}

// Tests an array of std/Bool objects
// Tests an array of std_msgs/Bool objects
inner_index = 0;
const auto bool_object_array = message->data()["index_as_bool_object_array"];
for (auto item = bool_object_array->beginValues<Embag::RosValue::Pointer>(); item != bool_object_array->endValues<Embag::RosValue::Pointer>(); item++) {
Expand All @@ -308,6 +328,8 @@ TEST_F(ArraysTest, ArrayReading) {

index++;
}

ASSERT_EQ(index, 20);
}

// TODO: test multi-bag message sorting