diff --git a/c/test/auto_check_sbp_imu_MsgImuComp.c b/c/test/auto_check_sbp_imu_MsgImuComp.c new file mode 100644 index 0000000000..873a23dec0 --- /dev/null +++ b/c/test/auto_check_sbp_imu_MsgImuComp.c @@ -0,0 +1,195 @@ +/* + * Copyright (C) 2015-2021 Swift Navigation Inc. + * Contact: https://support.swiftnav.com + * + * This source is subject to the license found in the file 'LICENSE' which must + * be distributed together with this source. All other rights reserved. + * + * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, + * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. + */ + +// This file was auto-generated from +// spec/tests/yaml/swiftnav/sbp/imu/test_MsgImuComp.yaml by generate.py. Do not +// modify by hand! + +#include +#include +#include +#include // for debugging +#include // for malloc + +static struct { + u32 n_callbacks_logged; + u16 sender_id; + sbp_msg_type_t msg_type; + sbp_msg_t msg; + void *context; +} last_msg; + +static size_t dummy_wr = 0; +static size_t dummy_rd = 0; +static u8 dummy_buff[1024]; +static void *last_io_context; + +static void *DUMMY_MEMORY_FOR_CALLBACKS = (void *)0xdeadbeef; +static void *DUMMY_MEMORY_FOR_IO = (void *)0xdead0000; + +static void dummy_reset() { + dummy_rd = dummy_wr = 0; + memset(dummy_buff, 0, sizeof(dummy_buff)); +} + +static s32 dummy_write(u8 *buff, u32 n, void *context) { + last_io_context = context; + size_t real_n = n; //(dummy_n > n) ? n : dummy_n; + memcpy(dummy_buff + dummy_wr, buff, real_n); + dummy_wr += real_n; + return (s32)real_n; +} + +static s32 dummy_read(u8 *buff, u32 n, void *context) { + last_io_context = context; + size_t real_n = n; //(dummy_n > n) ? n : dummy_n; + memcpy(buff, dummy_buff + dummy_rd, real_n); + dummy_rd += real_n; + return (s32)real_n; +} + +static void logging_reset() { memset(&last_msg, 0, sizeof(last_msg)); } + +static void msg_callback(u16 sender_id, sbp_msg_type_t msg_type, + const sbp_msg_t *msg, void *context) { + last_msg.n_callbacks_logged++; + last_msg.sender_id = sender_id; + last_msg.msg_type = msg_type; + last_msg.msg = *msg; + last_msg.context = context; +} + +START_TEST(test_auto_check_sbp_imu_MsgImuComp) { + static sbp_msg_callbacks_node_t n; + + // State of the SBP message parser. + // Must be statically allocated. + sbp_state_t sbp_state; + + // + // Run tests: + // + // Test successful parsing of a message + { + // SBP parser state must be initialized before sbp_process is called. + // We re-initialize before every test so that callbacks for the same message + // types can be + // allocated multiple times across different tests. + sbp_state_init(&sbp_state); + + sbp_state_set_io_context(&sbp_state, &DUMMY_MEMORY_FOR_IO); + + logging_reset(); + + sbp_callback_register(&sbp_state, 0x905, &msg_callback, + &DUMMY_MEMORY_FOR_CALLBACKS, &n); + + u8 encoded_frame[] = { + 85, 5, 9, 52, 18, 34, 26, 1, 0, 192, 0, 0, 0, 0, + 206, 0, 96, 0, 0, 0, 223, 255, 255, 255, 44, 16, 0, 0, + 60, 0, 0, 0, 208, 254, 255, 255, 238, 255, 255, 255, 79, 86, + }; + + dummy_reset(); + + sbp_msg_t test_msg; + memset(&test_msg, 0, sizeof(test_msg)); + + test_msg.imu_comp.acc_comp_x = 96; + + test_msg.imu_comp.acc_comp_y = -33; + + test_msg.imu_comp.acc_comp_z = 4140; + + test_msg.imu_comp.flags = 206; + + test_msg.imu_comp.gyr_comp_x = 60; + + test_msg.imu_comp.gyr_comp_y = -304; + + test_msg.imu_comp.gyr_comp_z = -18; + + test_msg.imu_comp.time = 3221225754; + + sbp_message_send(&sbp_state, SbpMsgImuComp, 4660, &test_msg, &dummy_write); + + ck_assert_msg(dummy_wr == sizeof(encoded_frame), + "not enough data was written to dummy_buff (expected: %zu, " + "actual: %zu)", + sizeof(encoded_frame), dummy_wr); + ck_assert_msg(memcmp(dummy_buff, encoded_frame, sizeof(encoded_frame)) == 0, + "frame was not encoded properly"); + + while (dummy_rd < dummy_wr) { + ck_assert_msg(sbp_process(&sbp_state, &dummy_read) >= SBP_OK, + "sbp_process threw an error!"); + } + + ck_assert_msg(last_msg.n_callbacks_logged == 1, + "msg_callback: one callback should have been logged"); + ck_assert_msg(last_msg.sender_id == 4660, + "msg_callback: sender_id decoded incorrectly"); + + ck_assert_msg(sbp_message_cmp(SbpMsgImuComp, &last_msg.msg, &test_msg) == 0, + "Sent and received messages did not compare equal"); + + ck_assert_msg(last_msg.msg.imu_comp.acc_comp_x == 96, + "incorrect value for last_msg.msg.imu_comp.acc_comp_x, " + "expected 96, is %" PRId64, + (int64_t)last_msg.msg.imu_comp.acc_comp_x); + + ck_assert_msg(last_msg.msg.imu_comp.acc_comp_y == -33, + "incorrect value for last_msg.msg.imu_comp.acc_comp_y, " + "expected -33, is %" PRId64, + (int64_t)last_msg.msg.imu_comp.acc_comp_y); + + ck_assert_msg(last_msg.msg.imu_comp.acc_comp_z == 4140, + "incorrect value for last_msg.msg.imu_comp.acc_comp_z, " + "expected 4140, is %" PRId64, + (int64_t)last_msg.msg.imu_comp.acc_comp_z); + + ck_assert_msg(last_msg.msg.imu_comp.flags == 206, + "incorrect value for last_msg.msg.imu_comp.flags, expected " + "206, is %" PRId64, + (int64_t)last_msg.msg.imu_comp.flags); + + ck_assert_msg(last_msg.msg.imu_comp.gyr_comp_x == 60, + "incorrect value for last_msg.msg.imu_comp.gyr_comp_x, " + "expected 60, is %" PRId64, + (int64_t)last_msg.msg.imu_comp.gyr_comp_x); + + ck_assert_msg(last_msg.msg.imu_comp.gyr_comp_y == -304, + "incorrect value for last_msg.msg.imu_comp.gyr_comp_y, " + "expected -304, is %" PRId64, + (int64_t)last_msg.msg.imu_comp.gyr_comp_y); + + ck_assert_msg(last_msg.msg.imu_comp.gyr_comp_z == -18, + "incorrect value for last_msg.msg.imu_comp.gyr_comp_z, " + "expected -18, is %" PRId64, + (int64_t)last_msg.msg.imu_comp.gyr_comp_z); + + ck_assert_msg(last_msg.msg.imu_comp.time == 3221225754, + "incorrect value for last_msg.msg.imu_comp.time, expected " + "3221225754, is %" PRId64, + (int64_t)last_msg.msg.imu_comp.time); + } +} +END_TEST + +Suite *auto_check_sbp_imu_MsgImuComp_suite(void) { + Suite *s = + suite_create("SBP generated test suite: auto_check_sbp_imu_MsgImuComp"); + TCase *tc_acq = tcase_create("Automated_Suite_auto_check_sbp_imu_MsgImuComp"); + tcase_add_test(tc_acq, test_auto_check_sbp_imu_MsgImuComp); + suite_add_tcase(s, tc_acq); + return s; +} \ No newline at end of file diff --git a/c/test/check_main.c b/c/test/check_main.c index a3a17a23e6..2ba59d1fcf 100644 --- a/c/test/check_main.c +++ b/c/test/check_main.c @@ -59,6 +59,7 @@ int main(void) { srunner_add_suite(sr, auto_check_sbp_flash_MsgStmUniqueIdReq_suite()); srunner_add_suite(sr, auto_check_sbp_flash_MsgStmUniqueIdResp_suite()); srunner_add_suite(sr, auto_check_sbp_imu_MsgImuAux_suite()); + srunner_add_suite(sr, auto_check_sbp_imu_MsgImuComp_suite()); srunner_add_suite(sr, auto_check_sbp_imu_MsgImuRaw_suite()); srunner_add_suite(sr, auto_check_sbp_integrity_MsgAcknowledge_suite()); srunner_add_suite(sr, auto_check_sbp_integrity_MsgSsrFlagHighLevel_suite()); diff --git a/c/test/check_suites.h b/c/test/check_suites.h index 745a5b3b5e..1122596c72 100644 --- a/c/test/check_suites.h +++ b/c/test/check_suites.h @@ -49,6 +49,7 @@ Suite* auto_check_sbp_flash_MsgStmFlashUnlockSector_suite(void); Suite* auto_check_sbp_flash_MsgStmUniqueIdReq_suite(void); Suite* auto_check_sbp_flash_MsgStmUniqueIdResp_suite(void); Suite* auto_check_sbp_imu_MsgImuAux_suite(void); +Suite* auto_check_sbp_imu_MsgImuComp_suite(void); Suite* auto_check_sbp_imu_MsgImuRaw_suite(void); Suite* auto_check_sbp_integrity_MsgAcknowledge_suite(void); Suite* auto_check_sbp_integrity_MsgSsrFlagHighLevel_suite(void); diff --git a/c/test/cpp/auto_check_sbp_imu_MsgImuComp.cc b/c/test/cpp/auto_check_sbp_imu_MsgImuComp.cc new file mode 100644 index 0000000000..f9a2c3d778 --- /dev/null +++ b/c/test/cpp/auto_check_sbp_imu_MsgImuComp.cc @@ -0,0 +1,812 @@ +/* + * Copyright (C) 2015-2021 Swift Navigation Inc. + * Contact: https://support.swiftnav.com + * + * This source is subject to the license found in the file 'LICENSE' which must + * be distributed together with this source. All other rights reserved. + * + * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, + * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. + */ + +// This file was auto-generated from +// spec/tests/yaml/swiftnav/sbp/imu/test_MsgImuComp.yaml by generate.py. Do not +// modify by hand! + +#include +#include +#include +#include +#include + +namespace { + +template +void assign(T &dest, const U &source) { + dest = static_cast(source); +} +class Testauto_check_sbp_imu_MsgImuComp0 : public ::testing::Test { + public: + Testauto_check_sbp_imu_MsgImuComp0() { + assign(test_msg_.acc_comp_x, 96); + assign(test_msg_.acc_comp_y, -33); + assign(test_msg_.acc_comp_z, 4140); + assign(test_msg_.flags, 206); + assign(test_msg_.gyr_comp_x, 60); + assign(test_msg_.gyr_comp_y, -304); + assign(test_msg_.gyr_comp_z, -18); + assign(test_msg_.time, 3221225754); + } + + class SlowReader final : public sbp::IReader { + public: + SlowReader(const uint8_t *buf, uint32_t len) + : sbp::IReader(), buf_{buf}, len_{len} {} + + s32 read(uint8_t *buf, const uint32_t n) override { + if (n == 0) { + return 0; + } + if (remaining() == 0) { + return -1; + } + skip_next_read = !skip_next_read; + if (skip_next_read) { + return 0; + } + uint32_t real_n = std::min(n, 1u); + memcpy(buf, buf_ + offset_, real_n); + offset_ += real_n; + return static_cast(real_n); + } + + uint32_t remaining() const noexcept { return len_ - offset_; } + + static s32 read_static(uint8_t *buf, uint32_t len, void *ctx) { + return static_cast(ctx)->read(buf, len); + } + + private: + const uint8_t *buf_; + uint32_t len_; + uint32_t offset_{}; + bool skip_next_read{}; + }; + + class Reader final : public sbp::IReader { + public: + Reader(const uint8_t *buf, uint32_t len) + : sbp::IReader(), buf_{buf}, len_{len} {} + + s32 read(uint8_t *buf, const uint32_t n) override { + if (n == 0) { + return 0; + } + uint32_t real_n = std::min(n, remaining()); + if (real_n == 0) { + return -1; + } + memcpy(buf, buf_ + offset_, real_n); + offset_ += real_n; + return static_cast(real_n); + } + + uint32_t remaining() const noexcept { return len_ - offset_; } + + static s32 read_static(uint8_t *buf, uint32_t len, void *ctx) { + return static_cast(ctx)->read(buf, len); + } + + private: + const uint8_t *buf_; + uint32_t len_; + uint32_t offset_{}; + }; + + class SlowWriter final : public sbp::IWriter { + public: + explicit SlowWriter(uint32_t max_len = cMaxLen) + : IWriter(), max_len_{max_len} {} + static constexpr uint32_t cMaxLen = SBP_MAX_FRAME_LEN; + + s32 write(const uint8_t *buf, uint32_t n) override { + if (n == 0) { + return 0; + } + uint32_t real_n = std::min(n, 1u); + if (real_n == 0) { + return -1; + } + memcpy(buf_ + offset_, buf, real_n); + offset_ += real_n; + return static_cast(real_n); + } + + uint32_t remaining() const noexcept { return max_len_ - offset_; } + + const uint8_t *data() const noexcept { return buf_; } + + uint32_t len() const noexcept { return offset_; } + + static s32 write_static(const uint8_t *buf, uint32_t len, void *ctx) { + return static_cast(ctx)->write(buf, len); + } + + static s32 write_c(uint8_t *buf, uint32_t len, void *ctx) { + return static_cast(ctx)->write(buf, len); + } + + private: + uint8_t buf_[cMaxLen]; + uint32_t max_len_; + uint32_t offset_{}; + }; + + class Writer final : public sbp::IWriter { + public: + explicit Writer(uint32_t max_len = cMaxLen) + : IWriter(), max_len_{max_len} {} + static constexpr uint32_t cMaxLen = SBP_MAX_FRAME_LEN; + + s32 write(const uint8_t *buf, uint32_t n) override { + if (n == 0) { + return 0; + } + uint32_t real_n = std::min(n, remaining()); + if (real_n == 0) { + return -1; + } + memcpy(buf_ + offset_, buf, real_n); + offset_ += real_n; + return static_cast(real_n); + } + + uint32_t remaining() const noexcept { return max_len_ - offset_; } + + const uint8_t *data() const noexcept { return buf_; } + + uint32_t len() const noexcept { return offset_; } + + static s32 write_static(const uint8_t *buf, uint32_t len, void *ctx) { + return static_cast(ctx)->write(buf, len); + } + + static s32 write_c(uint8_t *buf, uint32_t len, void *ctx) { + return static_cast(ctx)->write(buf, len); + } + + private: + uint8_t buf_[cMaxLen]; + uint32_t max_len_; + uint32_t offset_{}; + }; + + struct CppHandler final : public sbp::MessageHandler { + using sbp::MessageHandler::MessageHandler; + + struct Output final { + uint16_t sender_id; + sbp_msg_imu_comp_t msg; + }; + + std::vector outputs{}; + + protected: + void handle_sbp_msg(uint16_t sender_id, + const sbp_msg_imu_comp_t &msg) override { + outputs.emplace_back(); + outputs.back().sender_id = sender_id; + memcpy(&outputs.back().msg, &msg, sizeof(msg)); + } + }; + + struct CHandler final { + explicit CHandler(sbp_state_t *state) : state_{state} { + sbp_callback_register(state, SbpMsgImuComp, &CHandler::callback_static, + this, &node_); + } + + ~CHandler() { sbp_remove_callback(state_, &node_); } + + struct Output final { + uint16_t sender_id; + sbp_msg_imu_comp_t msg; + }; + + std::vector outputs{}; + + private: + void callback(uint16_t sender_id, sbp_msg_type_t msg_type, + const sbp_msg_t *msg) { + ASSERT_EQ(msg_type, SbpMsgImuComp); + outputs.emplace_back(); + outputs.back().sender_id = sender_id; + memcpy(&outputs.back().msg, &msg->imu_comp, sizeof(msg->imu_comp)); + } + + static void callback_static(uint16_t sender_id, sbp_msg_type_t msg_type, + const sbp_msg_t *msg, void *ctx) { + static_cast(ctx)->callback(sender_id, msg_type, msg); + } + + sbp_msg_callbacks_node_t node_{}; + sbp_state_t *state_; + }; + + struct TestMsgInfo { + sbp_msg_imu_comp_t test_msg; + sbp_msg_t test_msg_wrapped; + sbp_msg_type_t msg_type; + uint16_t sender_id; + uint8_t preamble; + uint16_t crc; + const uint8_t *encoded_frame; + uint32_t frame_len; + const uint8_t *encoded_payload; + uint8_t payload_len; + + Reader get_frame_reader() const noexcept { + return Reader{encoded_frame, frame_len}; + } + + Reader get_frame_reader(uint32_t max) const noexcept { + assert(max <= frame_len); + return Reader{encoded_frame, max}; + } + + SlowReader get_slow_frame_reader() const noexcept { + return SlowReader{encoded_frame, frame_len}; + } + + Writer get_frame_writer() const noexcept { return Writer{frame_len}; } + + Writer get_frame_writer(uint32_t max) const noexcept { return Writer{max}; } + + SlowWriter get_slow_frame_writer() const noexcept { + return SlowWriter{frame_len}; + } + }; + + TestMsgInfo get_test_msg_info() const noexcept { + TestMsgInfo info; + memcpy(&info.test_msg, &test_msg_, sizeof(test_msg_)); + memcpy(&info.test_msg_wrapped.imu_comp, &test_msg_, sizeof(test_msg_)); + info.msg_type = static_cast(SbpMsgImuComp); + info.sender_id = 4660; + info.preamble = 0x55; + info.crc = 0x564f; + info.encoded_frame = encoded_frame_; + info.frame_len = sizeof(encoded_frame_); + info.encoded_payload = encoded_payload_; + info.payload_len = 34; + + return info; + } + + protected: + void comparison_tests(const sbp_msg_imu_comp_t &lesser, + const sbp_msg_imu_comp_t &greater) { + sbp_msg_t wrapped_lesser = + sbp::MessageTraits::to_sbp_msg(lesser); + sbp_msg_t wrapped_greater = + sbp::MessageTraits::to_sbp_msg(greater); + + EXPECT_EQ(sbp_msg_imu_comp_cmp(&lesser, &lesser), 0); + EXPECT_EQ(sbp_msg_imu_comp_cmp(&greater, &greater), 0); + EXPECT_LE(sbp_msg_imu_comp_cmp(&lesser, &greater), 0); + EXPECT_GT(sbp_msg_imu_comp_cmp(&greater, &lesser), 0); + + EXPECT_EQ(sbp_message_cmp(SbpMsgImuComp, &wrapped_lesser, &wrapped_lesser), + 0); + EXPECT_EQ( + sbp_message_cmp(SbpMsgImuComp, &wrapped_greater, &wrapped_greater), 0); + EXPECT_LE(sbp_message_cmp(SbpMsgImuComp, &wrapped_lesser, &wrapped_greater), + 0); + EXPECT_GT(sbp_message_cmp(SbpMsgImuComp, &wrapped_greater, &wrapped_lesser), + 0); + + // lesser vs lesser + EXPECT_TRUE(lesser == lesser); + EXPECT_FALSE(lesser != lesser); + EXPECT_FALSE(lesser < lesser); + EXPECT_TRUE(lesser <= lesser); + EXPECT_FALSE(lesser > lesser); + EXPECT_TRUE(lesser >= lesser); + + // greater vs greater + EXPECT_TRUE(greater == greater); + EXPECT_FALSE(greater != greater); + EXPECT_FALSE(greater < greater); + EXPECT_TRUE(greater <= greater); + EXPECT_FALSE(greater > greater); + EXPECT_TRUE(greater >= greater); + + // lesser vs greater + EXPECT_FALSE(lesser == greater); + EXPECT_TRUE(lesser != greater); + EXPECT_TRUE(lesser < greater); + EXPECT_TRUE(lesser <= greater); + EXPECT_FALSE(lesser > greater); + EXPECT_FALSE(lesser >= greater); + + // greater vs lesser + EXPECT_FALSE(greater == lesser); + EXPECT_TRUE(greater != lesser); + EXPECT_FALSE(greater < lesser); + EXPECT_FALSE(greater <= lesser); + EXPECT_TRUE(greater > lesser); + EXPECT_TRUE(greater >= lesser); + } + + template ::value, bool> = true> + void make_lesser_greater(T &lesser, T &greater) { + if (lesser > std::numeric_limits::min()) { + lesser--; + } else { + greater++; + } + } + + template ::value, bool> = true> + void make_lesser_greater(T &lesser, T &greater) { + (void)lesser; + greater += static_cast(1.0); + } + + void make_lesser_greater(sbp_string_t &lesser, sbp_string_t &greater) { + if (greater.data[0] == 'z') { + lesser.data[0]--; + } else { + greater.data[0]++; + } + } + + template + void make_lesser_greater(char (&lesser)[N], char (&greater)[N]) { + if (lesser[0] == 'z') { + lesser[0]--; + } else { + greater[0]++; + } + } + + private: + sbp_msg_imu_comp_t test_msg_{}; + uint8_t encoded_frame_[34 + 8] = { + 85, 5, 9, 52, 18, 34, 26, 1, 0, 192, 0, 0, 0, 0, + 206, 0, 96, 0, 0, 0, 223, 255, 255, 255, 44, 16, 0, 0, + 60, 0, 0, 0, 208, 254, 255, 255, 238, 255, 255, 255, 79, 86, + }; + uint8_t encoded_payload_[34] = { + 26, 1, 0, 192, 0, 0, 0, 0, 206, 0, 96, 0, + 0, 0, 223, 255, 255, 255, 44, 16, 0, 0, 60, 0, + 0, 0, 208, 254, 255, 255, 238, 255, 255, 255, + }; +}; + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, EncodedLen) { + auto info = get_test_msg_info(); + EXPECT_EQ(sbp_msg_imu_comp_encoded_len(&info.test_msg), info.payload_len); + + EXPECT_EQ(sbp_message_encoded_len(SbpMsgImuComp, &info.test_msg_wrapped), + info.payload_len); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, EncodeToBuf) { + auto info = get_test_msg_info(); + uint8_t buf[34]; + uint8_t n_written; + + EXPECT_EQ( + sbp_msg_imu_comp_encode(&buf[0], sizeof(buf), &n_written, &info.test_msg), + SBP_OK); + EXPECT_EQ(n_written, 34); + EXPECT_EQ(memcmp(&buf[0], info.encoded_payload, 34), 0); + + memset(&buf[0], 0, sizeof(buf)); + EXPECT_EQ(sbp_message_encode(&buf[0], sizeof(buf), &n_written, SbpMsgImuComp, + &info.test_msg_wrapped), + SBP_OK); + EXPECT_EQ(n_written, 34); + EXPECT_EQ(memcmp(&buf[0], info.encoded_payload, 34), 0); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, EncodeToBufWithoutNwritten) { + auto info = get_test_msg_info(); + uint8_t buf[34]; + + EXPECT_EQ( + sbp_msg_imu_comp_encode(&buf[0], sizeof(buf), nullptr, &info.test_msg), + SBP_OK); + EXPECT_EQ(memcmp(&buf[0], info.encoded_payload, 34), 0); +} +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, EncodedToBufUnderflow) { + auto info = get_test_msg_info(); + uint8_t buf[34]; + + for (uint8_t i = 0; i < 34; i++) { + EXPECT_EQ(sbp_msg_imu_comp_encode(&buf[0], i, nullptr, &info.test_msg), + SBP_ENCODE_ERROR); + } +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, DecodeFromBuf) { + auto info = get_test_msg_info(); + sbp_msg_imu_comp_t msg{}; + uint8_t n_read; + + EXPECT_EQ(sbp_msg_imu_comp_decode(&info.encoded_payload[0], info.payload_len, + &n_read, &msg), + SBP_OK); + EXPECT_EQ(n_read, 34); + EXPECT_EQ(msg, info.test_msg); + + sbp_msg_t wrapped_msg{}; + EXPECT_EQ(sbp_message_decode(&info.encoded_payload[0], info.payload_len, + &n_read, SbpMsgImuComp, &wrapped_msg), + SBP_OK); + EXPECT_EQ(n_read, 34); + EXPECT_EQ(msg, info.test_msg); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, DecodeFromBufWithoutNread) { + auto info = get_test_msg_info(); + sbp_msg_imu_comp_t msg{}; + + EXPECT_EQ(sbp_msg_imu_comp_decode(&info.encoded_payload[0], info.payload_len, + nullptr, &msg), + SBP_OK); + EXPECT_EQ(msg, info.test_msg); +} +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, DecodeFromBufUnderflow) { + auto info = get_test_msg_info(); + sbp_msg_imu_comp_t msg{}; + + for (uint8_t i = 0; i < info.payload_len; i++) { + int expected_return = SBP_DECODE_ERROR; + + EXPECT_EQ( + sbp_msg_imu_comp_decode(&info.encoded_payload[0], i, nullptr, &msg), + expected_return); + } +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, ReceiveThroughSbpState) { + auto info = get_test_msg_info(); + sbp_state_t state; + sbp_state_init(&state); + + auto reader = info.get_frame_reader(); + sbp_state_set_io_context(&state, &reader); + + CHandler handler{&state}; + + while (reader.remaining() > 0) { + EXPECT_GE(sbp_process(&state, &Reader::read_static), SBP_OK); + } + + EXPECT_EQ(handler.outputs.size(), 1); + EXPECT_EQ(handler.outputs[0].sender_id, info.sender_id); + EXPECT_EQ(handler.outputs[0].msg, info.test_msg); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, ReceiveThroughSbpStateUnderflow) { + auto info = get_test_msg_info(); + + for (uint32_t i = 0; i < info.frame_len; i++) { + sbp_state_t state; + sbp_state_init(&state); + + auto reader = info.get_frame_reader(i); + sbp_state_set_io_context(&state, &reader); + + CHandler handler(&state); + + int most_recent_return = sbp_process(&state, &Reader::read_static); + while (most_recent_return == SBP_OK || reader.remaining() > 0) { + most_recent_return = sbp_process(&state, &Reader::read_static); + } + + EXPECT_NE(most_recent_return, SBP_OK); + EXPECT_EQ(reader.remaining(), 0); + + EXPECT_EQ(handler.outputs.size(), 0); + } +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, SlowRead) { + auto info = get_test_msg_info(); + sbp_state_t state; + sbp_state_init(&state); + + auto reader = info.get_slow_frame_reader(); + sbp_state_set_io_context(&state, &reader); + + CHandler handler{&state}; + + while (reader.remaining() > 0) { + EXPECT_GE(sbp_process(&state, &SlowReader::read_static), SBP_OK); + } + + EXPECT_EQ(handler.outputs.size(), 1); + EXPECT_EQ(handler.outputs[0].sender_id, info.sender_id); + EXPECT_EQ(handler.outputs[0].msg, info.test_msg); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, BadCRC) { + auto info = get_test_msg_info(); + uint8_t buf[SBP_MAX_FRAME_LEN]; + memcpy(&buf[0], info.encoded_frame, info.frame_len); + + // Introduce a CRC error which should cause an error return and no callback + buf[info.frame_len - 1]++; + + sbp_state_t state; + sbp_state_init(&state); + + Reader reader{buf, info.frame_len}; + sbp_state_set_io_context(&state, &reader); + + CHandler handler{&state}; + + while (reader.remaining() > 0) { + int res = sbp_process(&state, &Reader::read_static); + EXPECT_EQ(res, reader.remaining() == 0 ? SBP_CRC_ERROR : SBP_OK); + } + + EXPECT_EQ(handler.outputs.size(), 0); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, SendThroughSbpState) { + auto info = get_test_msg_info(); + sbp_state_t state; + sbp_state_init(&state); + + auto writer = info.get_frame_writer(); + sbp_state_set_io_context(&state, &writer); + + EXPECT_EQ(sbp_msg_imu_comp_send(&state, info.sender_id, &info.test_msg, + &Writer::write_c), + SBP_OK); + EXPECT_EQ(writer.len(), info.frame_len); + EXPECT_EQ(memcmp(writer.data(), &info.encoded_frame[0], writer.len()), 0); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, SendWrappedThroughSbpState) { + auto info = get_test_msg_info(); + sbp_state_t state; + sbp_state_init(&state); + + auto writer = info.get_frame_writer(); + sbp_state_set_io_context(&state, &writer); + + EXPECT_EQ(sbp_message_send(&state, SbpMsgImuComp, info.sender_id, + &info.test_msg_wrapped, &Writer::write_c), + SBP_OK); + EXPECT_EQ(writer.len(), info.frame_len); + EXPECT_EQ(memcmp(writer.data(), info.encoded_frame, info.frame_len), 0); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, SendThroughSbpStateUnderflow) { + auto info = get_test_msg_info(); + + for (uint32_t i = 0; i < info.frame_len; i++) { + sbp_state_t state; + sbp_state_init(&state); + + auto writer = info.get_frame_writer(i); + sbp_state_set_io_context(&state, &writer); + + EXPECT_NE(sbp_message_send(&state, SbpMsgImuComp, info.sender_id, + &info.test_msg_wrapped, &Writer::write_c), + SBP_OK); + EXPECT_EQ(writer.len(), i); + EXPECT_EQ(memcmp(writer.data(), info.encoded_frame, i), 0); + } +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, DISABLED_SlowWrite) { + auto info = get_test_msg_info(); + sbp_state_t state; + sbp_state_init(&state); + + auto writer = info.get_slow_frame_writer(); + sbp_state_set_io_context(&state, &writer); + + EXPECT_EQ(sbp_message_send(&state, SbpMsgImuComp, info.sender_id, + &info.test_msg_wrapped, &SlowWriter::write_c), + SBP_OK); + EXPECT_EQ(writer.len(), info.frame_len); + EXPECT_EQ(memcmp(writer.data(), info.encoded_frame, info.frame_len), 0); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, Comparison) { + auto info = get_test_msg_info(); + { + sbp_msg_imu_comp_t lesser = info.test_msg; + sbp_msg_imu_comp_t greater = info.test_msg; + make_lesser_greater(lesser.acc_comp_x, greater.acc_comp_x); + comparison_tests(lesser, greater); + } + { + sbp_msg_imu_comp_t lesser = info.test_msg; + sbp_msg_imu_comp_t greater = info.test_msg; + make_lesser_greater(lesser.acc_comp_y, greater.acc_comp_y); + comparison_tests(lesser, greater); + } + { + sbp_msg_imu_comp_t lesser = info.test_msg; + sbp_msg_imu_comp_t greater = info.test_msg; + make_lesser_greater(lesser.acc_comp_z, greater.acc_comp_z); + comparison_tests(lesser, greater); + } + { + sbp_msg_imu_comp_t lesser = info.test_msg; + sbp_msg_imu_comp_t greater = info.test_msg; + make_lesser_greater(lesser.flags, greater.flags); + comparison_tests(lesser, greater); + } + { + sbp_msg_imu_comp_t lesser = info.test_msg; + sbp_msg_imu_comp_t greater = info.test_msg; + make_lesser_greater(lesser.gyr_comp_x, greater.gyr_comp_x); + comparison_tests(lesser, greater); + } + { + sbp_msg_imu_comp_t lesser = info.test_msg; + sbp_msg_imu_comp_t greater = info.test_msg; + make_lesser_greater(lesser.gyr_comp_y, greater.gyr_comp_y); + comparison_tests(lesser, greater); + } + { + sbp_msg_imu_comp_t lesser = info.test_msg; + sbp_msg_imu_comp_t greater = info.test_msg; + make_lesser_greater(lesser.gyr_comp_z, greater.gyr_comp_z); + comparison_tests(lesser, greater); + } + { + sbp_msg_imu_comp_t lesser = info.test_msg; + sbp_msg_imu_comp_t greater = info.test_msg; + make_lesser_greater(lesser.time, greater.time); + comparison_tests(lesser, greater); + } +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, CppTraitsProperties) { + // EXPECT_EQ(sbp::MessageTraits::id, SbpMsgImuComp); + EXPECT_STREQ(sbp::MessageTraits::name, "MSG_IMU_COMP"); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, CppTraitsFromSbpMsgT) { + auto info = get_test_msg_info(); + + const sbp_msg_t &const_sbp_msg_t = info.test_msg_wrapped; + sbp_msg_t &non_const_sbp_msg_t = info.test_msg_wrapped; + + const sbp_msg_imu_comp_t &const_unwrapped = + sbp::MessageTraits::get(const_sbp_msg_t); + sbp_msg_imu_comp_t &non_const_unwrapped = + sbp::MessageTraits::get(non_const_sbp_msg_t); + + EXPECT_EQ((const void *)&const_sbp_msg_t, (const void *)&const_unwrapped); + EXPECT_EQ((void *)&non_const_sbp_msg_t, (void *)&non_const_unwrapped); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, CppTraitsToSbpMsgT) { + auto info = get_test_msg_info(); + + sbp_msg_t msg1 = + sbp::MessageTraits::to_sbp_msg(info.test_msg); + EXPECT_EQ(msg1.imu_comp, info.test_msg); + + sbp_msg_t msg2; + sbp::MessageTraits::to_sbp_msg(info.test_msg, &msg2); + EXPECT_EQ(msg2.imu_comp, info.test_msg); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, CppTraitsEncodedLen) { + auto info = get_test_msg_info(); + EXPECT_EQ(sbp::MessageTraits::encoded_len(info.test_msg), + info.payload_len); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, CppTraitsSendThroughSbpState) { + auto info = get_test_msg_info(); + sbp_state_t state; + sbp_state_init(&state); + + auto writer = info.get_frame_writer(); + sbp_state_set_io_context(&state, &writer); + + EXPECT_EQ(sbp::MessageTraits::send( + &state, info.sender_id, info.test_msg, &Writer::write_c), + SBP_OK); + EXPECT_EQ(writer.len(), info.frame_len); + EXPECT_EQ(memcmp(writer.data(), &info.encoded_frame[0], writer.len()), 0); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, CppTraitsEncodeToBuf) { + auto info = get_test_msg_info(); + uint8_t buf[34]; + uint8_t n_written; + + EXPECT_EQ(sbp::MessageTraits::encode( + &buf[0], sizeof(buf), &n_written, info.test_msg), + SBP_OK); + EXPECT_EQ(n_written, 34); + EXPECT_EQ(memcmp(&buf[0], info.encoded_payload, 34), 0); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, CppTraitsDecodeFromBuf) { + auto info = get_test_msg_info(); + sbp_msg_imu_comp_t msg{}; + uint8_t n_read; + + EXPECT_EQ(sbp::MessageTraits::decode( + &info.encoded_payload[0], info.payload_len, &n_read, &msg), + SBP_OK); + EXPECT_EQ(n_read, 34); + EXPECT_EQ(msg, info.test_msg); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, ReceiveThroughMessageHandler) { + auto info = get_test_msg_info(); + auto reader = info.get_frame_reader(); + + sbp::State state{}; + state.set_reader(&reader); + + CppHandler handler{&state}; + + while (reader.remaining() > 0) { + EXPECT_GE(state.process(), SBP_OK); + } + + EXPECT_EQ(handler.outputs.size(), 1); + EXPECT_EQ(handler.outputs[0].sender_id, info.sender_id); + EXPECT_EQ(handler.outputs[0].msg, info.test_msg); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, + ProcessSbpMsgTThroughMessageHandler) { + auto info = get_test_msg_info(); + sbp::State state{}; + CppHandler handler(&state); + + state.process_message(info.sender_id, SbpMsgImuComp, &info.test_msg_wrapped); + + EXPECT_EQ(handler.outputs.size(), 1); + EXPECT_EQ(handler.outputs[0].sender_id, info.sender_id); + EXPECT_EQ(handler.outputs[0].msg, info.test_msg); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, SendThroughCppState) { + auto info = get_test_msg_info(); + auto writer = info.get_frame_writer(); + + sbp::State state{}; + state.set_writer(&writer); + + EXPECT_EQ(state.send_message(info.sender_id, info.test_msg), SBP_OK); + EXPECT_EQ(writer.len(), info.frame_len); + EXPECT_EQ(memcmp(writer.data(), info.encoded_frame, info.frame_len), 0); +} + +TEST_F(Testauto_check_sbp_imu_MsgImuComp0, SendWrappedSbpMsgTThroughCppState) { + auto info = get_test_msg_info(); + auto writer = info.get_frame_writer(); + + sbp::State state{}; + state.set_writer(&writer); + + EXPECT_EQ( + state.send_message(info.sender_id, SbpMsgImuComp, info.test_msg_wrapped), + SBP_OK); + EXPECT_EQ(writer.len(), info.frame_len); + EXPECT_EQ(memcmp(writer.data(), info.encoded_frame, info.frame_len), 0); +} + +} // namespace \ No newline at end of file diff --git a/java/test/auto_check_sbp_imu_MsgImuCompTest.java b/java/test/auto_check_sbp_imu_MsgImuCompTest.java new file mode 100644 index 0000000000..78849cd477 --- /dev/null +++ b/java/test/auto_check_sbp_imu_MsgImuCompTest.java @@ -0,0 +1,126 @@ +/* Copyright (C) 2015-2022 Swift Navigation Inc. + * Contact: https://support.swiftnav.com + * + * This source is subject to the license found in the file 'LICENSE' which must + * be distributed together with this source. All other rights reserved. + * + * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, + * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. + */ +package com.swiftnav.sbp.test; + +// This file was auto-generated from spec/tests/yaml/swiftnav/sbp/imu/test_MsgImuComp.yaml by +// generate.py. Do not modify by hand! + + +import com.swiftnav.sbp.SBPMessage; +import com.swiftnav.sbp.imu.MsgImuComp; +import java.math.BigInteger; +import org.json.JSONObject; +import org.junit.Test; + +public class auto_check_sbp_imu_MsgImuCompTest { + + public static boolean debug = false; + private static final double DELTA = 1e-15; + + @Test + public void test1() throws Throwable { + if (debug) System.out.format("%n%s%n", "auto_check_sbp_imu_MsgImuCompTest.test1"); + byte[] payload = + new byte[] { + (byte) 26, (byte) 1, (byte) 0, (byte) 192, (byte) 0, (byte) 0, (byte) 0, + (byte) 0, (byte) 206, (byte) 0, (byte) 96, (byte) 0, (byte) 0, (byte) 0, + (byte) 223, (byte) 255, (byte) 255, (byte) 255, (byte) 44, (byte) 16, (byte) 0, + (byte) 0, (byte) 60, (byte) 0, (byte) 0, (byte) 0, (byte) 208, (byte) 254, + (byte) 255, (byte) 255, (byte) 238, (byte) 255, (byte) 255, (byte) 255, + }; + SBPMessage sbp = new SBPMessage(0x1234, 0x905, payload); + MsgImuComp msg = new MsgImuComp(sbp); + JSONObject json = msg.toJSON(); + Number value; + Number expected; + value = msg.acc_comp_x; + if (value instanceof BigInteger) { + org.junit.Assert.assertTrue( + "'" + msg.acc_comp_x + "' != '" + 96 + "'", + value.equals(BigInteger.valueOf(96L))); + } else { + value = value.longValue(); + expected = 96L; + org.junit.Assert.assertEquals(value, expected); + } + value = msg.acc_comp_y; + if (value instanceof BigInteger) { + org.junit.Assert.assertTrue( + "'" + msg.acc_comp_y + "' != '" + -33 + "'", + value.equals(BigInteger.valueOf(-33L))); + } else { + value = value.longValue(); + expected = -33L; + org.junit.Assert.assertEquals(value, expected); + } + value = msg.acc_comp_z; + if (value instanceof BigInteger) { + org.junit.Assert.assertTrue( + "'" + msg.acc_comp_z + "' != '" + 4140 + "'", + value.equals(BigInteger.valueOf(4140L))); + } else { + value = value.longValue(); + expected = 4140L; + org.junit.Assert.assertEquals(value, expected); + } + value = msg.flags; + if (value instanceof BigInteger) { + org.junit.Assert.assertTrue( + "'" + msg.flags + "' != '" + 206 + "'", value.equals(BigInteger.valueOf(206L))); + } else { + value = value.longValue(); + expected = 206L; + org.junit.Assert.assertEquals(value, expected); + } + value = msg.gyr_comp_x; + if (value instanceof BigInteger) { + org.junit.Assert.assertTrue( + "'" + msg.gyr_comp_x + "' != '" + 60 + "'", + value.equals(BigInteger.valueOf(60L))); + } else { + value = value.longValue(); + expected = 60L; + org.junit.Assert.assertEquals(value, expected); + } + value = msg.gyr_comp_y; + if (value instanceof BigInteger) { + org.junit.Assert.assertTrue( + "'" + msg.gyr_comp_y + "' != '" + -304 + "'", + value.equals(BigInteger.valueOf(-304L))); + } else { + value = value.longValue(); + expected = -304L; + org.junit.Assert.assertEquals(value, expected); + } + value = msg.gyr_comp_z; + if (value instanceof BigInteger) { + org.junit.Assert.assertTrue( + "'" + msg.gyr_comp_z + "' != '" + -18 + "'", + value.equals(BigInteger.valueOf(-18L))); + } else { + value = value.longValue(); + expected = -18L; + org.junit.Assert.assertEquals(value, expected); + } + value = msg.time; + if (value instanceof BigInteger) { + org.junit.Assert.assertTrue( + "'" + msg.time + "' != '" + 3221225754L + "'", + value.equals(new BigInteger("3221225754"))); + } else { + value = value.longValue(); + expected = 3221225754L; + org.junit.Assert.assertEquals(value, expected); + } + + org.junit.Assert.assertNotEquals("", msg.getFriendlyName()); + } +} diff --git a/kaitai/perl/KaitaiSbp/t/auto_check_sbp_imu_MsgImuComp.t b/kaitai/perl/KaitaiSbp/t/auto_check_sbp_imu_MsgImuComp.t new file mode 100644 index 0000000000..4a268cb577 --- /dev/null +++ b/kaitai/perl/KaitaiSbp/t/auto_check_sbp_imu_MsgImuComp.t @@ -0,0 +1,70 @@ +#!/usr/bin/perl -w +# +# Copyright (C) 2015-2023 Swift Navigation Inc. +# Contact: https://support.swiftnav.com +# +# This source is subject to the license found in the file 'LICENSE' which must +# be distributed together with this source. All other rights reserved. +# +# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, +# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. +# +# Test cases automatically generated from spec/tests/yaml/swiftnav/sbp/imu/test_MsgImuComp.yaml +# with generate.py. Do not modify by hand! + +use strict; + +BEGIN { + # add grandparent directory to include path + use Cwd qw(realpath); + use File::Basename; + unshift @INC, realpath(dirname($0))."/../../"; +} + +use KaitaiSbp::Sbp; +use KaitaiSbp::ParseUtils; +use IO::KaitaiStruct; +use Test::More; +use JSON::PP; +use MIME::Base64; +sub test_auto_check_sbp_imu_msg_imu_comp_1() { + my $buf = decode_base64("VQUJNBIiGgEAwAAAAADOAGAAAADf////LBAAADwAAADQ/v//7v///09W"); + my $stream = IO::KaitaiStruct::Stream->new($buf); + my $msg = ParseUtils::get_flattened_msg(Sbp::SbpMessage->new($stream)); + + is($msg->{'crc'}, 0x564f, "crc"); + + is($msg->{'length'}, 34, "length"); + + is($msg->{'msg_type'}, 0x905, "msg_type"); + + is($msg->{'payload'}, "GgEAwAAAAADOAGAAAADf////LBAAADwAAADQ/v//7v///w==", "payload"); + + is($msg->{'preamble'}, 0x55, "preamble"); + + is($msg->{'sender'}, 0x1234, "sender"); + + my $json = JSON::PP->new->convert_blessed->canonical; + + is($msg->acc_comp_x(), 96, "acc_comp_x"); + + is($msg->acc_comp_y(), -33, "acc_comp_y"); + + is($msg->acc_comp_z(), 4140, "acc_comp_z"); + + is($msg->flags(), 206, "flags"); + + is($msg->gyr_comp_x(), 60, "gyr_comp_x"); + + is($msg->gyr_comp_y(), -304, "gyr_comp_y"); + + is($msg->gyr_comp_z(), -18, "gyr_comp_z"); + + is($msg->time(), 3221225754, "time"); + + is($json->encode($msg), $json->encode($json->decode(q{{"acc_comp_x":96,"acc_comp_y":-33,"acc_comp_z":4140,"crc":22095,"flags":206,"gyr_comp_x":60,"gyr_comp_y":-304,"gyr_comp_z":-18,"length":34,"msg_type":2309,"payload":"GgEAwAAAAADOAGAAAADf////LBAAADwAAADQ/v//7v///w==","preamble":85,"sender":4660,"time":3221225754}})), "raw_json"); +} +test_auto_check_sbp_imu_msg_imu_comp_1(); + +done_testing(); \ No newline at end of file diff --git a/kaitai/python/kaitai_sbp/tests/test_auto_check_sbp_imu_MsgImuComp.py b/kaitai/python/kaitai_sbp/tests/test_auto_check_sbp_imu_MsgImuComp.py new file mode 100644 index 0000000000..f1623b232e --- /dev/null +++ b/kaitai/python/kaitai_sbp/tests/test_auto_check_sbp_imu_MsgImuComp.py @@ -0,0 +1,55 @@ +# Copyright (C) 2015-2023 Swift Navigation Inc. +# Contact: https://support.swiftnav.com +# +# This source is subject to the license found in the file 'LICENSE' which must +# be distributed together with this source. All other rights reserved. +# +# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, +# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. +# +# Test cases automatically generated from spec/tests/yaml/swiftnav/sbp/imu/test_MsgImuComp.yaml +# with generate.py. Do not modify by hand! + +import kaitai_sbp.sbp as sbp +from kaitai_sbp.parse_utils import get_flattened_msg +from kaitai_sbp.tests.utils import dictify +from kaitaistruct import KaitaiStream +import io +import base64 + +def test_auto_check_sbp_imu_msg_imu_comp_1(): + buf = base64.standard_b64decode("VQUJNBIiGgEAwAAAAADOAGAAAADf////LBAAADwAAADQ/v//7v///09W") + + stream = KaitaiStream(io.BytesIO(buf)) + msg = get_flattened_msg(sbp.Sbp.SbpMessage(stream)) + + assert msg.crc == 0x564f + + assert msg.length == 34 + + assert msg.msg_type == 0x905 + + assert msg.payload == "GgEAwAAAAADOAGAAAADf////LBAAADwAAADQ/v//7v///w==" + + assert msg.preamble == 0x55 + + assert msg.sender == 0x1234 + + assert dictify(msg.acc_comp_x) == 96 + + assert dictify(msg.acc_comp_y) == -33 + + assert dictify(msg.acc_comp_z) == 4140 + + assert dictify(msg.flags) == 206 + + assert dictify(msg.gyr_comp_x) == 60 + + assert dictify(msg.gyr_comp_y) == -304 + + assert dictify(msg.gyr_comp_z) == -18 + + assert dictify(msg.time) == 3221225754 + + assert dictify(msg) == {'time': 3221225754, 'flags': 206, 'acc_comp_x': 96, 'acc_comp_y': -33, 'acc_comp_z': 4140, 'gyr_comp_x': 60, 'gyr_comp_y': -304, 'gyr_comp_z': -18, 'preamble': 85, 'msg_type': 2309, 'sender': 4660, 'payload': 'GgEAwAAAAADOAGAAAADf////LBAAADwAAADQ/v//7v///w==', 'crc': 22095, 'length': 34} \ No newline at end of file diff --git a/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_comp.rs b/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_comp.rs new file mode 100644 index 0000000000..7a8414592e --- /dev/null +++ b/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_comp.rs @@ -0,0 +1,289 @@ +// +// Copyright (C) 2019-2021 Swift Navigation Inc. +// Contact: https://support.swiftnav.com +// +// This source is subject to the license found in the file 'LICENSE' which must +// be distributed together with this source. All other rights reserved. +// +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, +// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. + +// This file was auto-generated from spec/tests/yaml/swiftnav/sbp/imu/test_MsgImuComp.yaml by generate.py. Do not modify by hand! + +use crate::*; + +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +fn test_auto_check_sbp_imu_msg_imu_comp() { + { + let mut payload = Cursor::new(vec![ + 85, 5, 9, 52, 18, 34, 26, 1, 0, 192, 0, 0, 0, 0, 206, 0, 96, 0, 0, 0, 223, 255, 255, + 255, 44, 16, 0, 0, 60, 0, 0, 0, 208, 254, 255, 255, 238, 255, 255, 255, 79, 86, + ]); + + // Test the round trip payload parsing + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + match &sbp_msg { + sbp::messages::Sbp::MsgImuComp(msg) => { + let msg_type = msg.message_type().unwrap(); + assert_eq!( + msg_type, 0x905, + "Incorrect message type, expected 0x905, is {}", + msg_type + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1234, + "incorrect sender id, expected 0x1234, is {sender_id}" + ); + assert_eq!( + msg.acc_comp_x, 96, + "incorrect value for acc_comp_x, expected 96, is {}", + msg.acc_comp_x + ); + assert_eq!( + msg.acc_comp_y, -33, + "incorrect value for acc_comp_y, expected -33, is {}", + msg.acc_comp_y + ); + assert_eq!( + msg.acc_comp_z, 4140, + "incorrect value for acc_comp_z, expected 4140, is {}", + msg.acc_comp_z + ); + assert_eq!( + msg.flags, 206, + "incorrect value for flags, expected 206, is {}", + msg.flags + ); + assert_eq!( + msg.gyr_comp_x, 60, + "incorrect value for gyr_comp_x, expected 60, is {}", + msg.gyr_comp_x + ); + assert_eq!( + msg.gyr_comp_y, -304, + "incorrect value for gyr_comp_y, expected -304, is {}", + msg.gyr_comp_y + ); + assert_eq!( + msg.gyr_comp_z, -18, + "incorrect value for gyr_comp_z, expected -18, is {}", + msg.gyr_comp_z + ); + assert_eq!( + msg.time, 3221225754, + "incorrect value for time, expected 3221225754, is {}", + msg.time + ); + } + _ => panic!("Invalid message type! Expected a MsgImuComp"), + }; + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} + +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_json2sbp_auto_check_sbp_imu_msg_imu_comp() { + { + let json_input = r#"{"time":3221225754,"flags":206,"acc_comp_x":96,"acc_comp_y":-33,"acc_comp_z":4140,"gyr_comp_x":60,"gyr_comp_y":-304,"gyr_comp_z":-18,"preamble":85,"msg_type":2309,"sender":4660,"payload":"GgEAwAAAAADOAGAAAADf////LBAAADwAAADQ/v//7v///w==","crc":22095,"length":34}"#.as_bytes(); + + let sbp_msg = { + // JSON to SBP message from payload + let mut iter = json2sbp_iter_msg(json_input); + let from_payload = iter + .next() + .expect("no message found") + .expect("failed to parse message"); + + // JSON to SBP message from fields + let mut iter = iter_messages_from_fields(json_input); + let from_fields = iter + .next() + .expect("no message found") + .expect("failed to parse message"); + + assert_eq!(from_fields, from_payload); + from_fields + }; + match &sbp_msg { + sbp::messages::Sbp::MsgImuComp(msg) => { + let msg_type = msg.message_type().unwrap(); + assert_eq!( + msg_type, 0x905, + "Incorrect message type, expected 0x905, is {}", + msg_type + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1234, + "incorrect sender id, expected 0x1234, is {sender_id}" + ); + assert_eq!( + msg.acc_comp_x, 96, + "incorrect value for acc_comp_x, expected 96, is {}", + msg.acc_comp_x + ); + assert_eq!( + msg.acc_comp_y, -33, + "incorrect value for acc_comp_y, expected -33, is {}", + msg.acc_comp_y + ); + assert_eq!( + msg.acc_comp_z, 4140, + "incorrect value for acc_comp_z, expected 4140, is {}", + msg.acc_comp_z + ); + assert_eq!( + msg.flags, 206, + "incorrect value for flags, expected 206, is {}", + msg.flags + ); + assert_eq!( + msg.gyr_comp_x, 60, + "incorrect value for gyr_comp_x, expected 60, is {}", + msg.gyr_comp_x + ); + assert_eq!( + msg.gyr_comp_y, -304, + "incorrect value for gyr_comp_y, expected -304, is {}", + msg.gyr_comp_y + ); + assert_eq!( + msg.gyr_comp_z, -18, + "incorrect value for gyr_comp_z, expected -18, is {}", + msg.gyr_comp_z + ); + assert_eq!( + msg.time, 3221225754, + "incorrect value for time, expected 3221225754, is {}", + msg.time + ); + } + _ => panic!("Invalid message type! Expected a MsgImuComp"), + }; + } +} + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_imu_msg_imu_comp`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_imu_msg_imu_comp() { + { + let mut payload = Cursor::new(vec![ + 85, 5, 9, 52, 18, 34, 26, 1, 0, 192, 0, 0, 0, 0, 206, 0, 96, 0, 0, 0, 223, 255, 255, + 255, 44, 16, 0, 0, 60, 0, 0, 0, 208, 254, 255, 255, 238, 255, 255, 255, 79, 86, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgImuComp( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgImuComp(msg) => { + let msg_type = msg.message_type().unwrap(); + assert_eq!( + msg_type, 0x905, + "Incorrect message type, expected 0x905, is {}", + msg_type + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1234, + "incorrect sender id, expected 0x1234, is {sender_id}" + ); + assert_eq!( + msg.acc_comp_x, 96, + "incorrect value for acc_comp_x, expected 96, is {}", + msg.acc_comp_x + ); + assert_eq!( + msg.acc_comp_y, -33, + "incorrect value for acc_comp_y, expected -33, is {}", + msg.acc_comp_y + ); + assert_eq!( + msg.acc_comp_z, 4140, + "incorrect value for acc_comp_z, expected 4140, is {}", + msg.acc_comp_z + ); + assert_eq!( + msg.flags, 206, + "incorrect value for flags, expected 206, is {}", + msg.flags + ); + assert_eq!( + msg.gyr_comp_x, 60, + "incorrect value for gyr_comp_x, expected 60, is {}", + msg.gyr_comp_x + ); + assert_eq!( + msg.gyr_comp_y, -304, + "incorrect value for gyr_comp_y, expected -304, is {}", + msg.gyr_comp_y + ); + assert_eq!( + msg.gyr_comp_z, -18, + "incorrect value for gyr_comp_z, expected -18, is {}", + msg.gyr_comp_z + ); + assert_eq!( + msg.time, 3221225754, + "incorrect value for time, expected 3221225754, is {}", + msg.time + ); + } + _ => panic!("Invalid message type! Expected a MsgImuComp"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/main.rs b/rust/sbp/tests/integration/main.rs index cfc224e26f..db3f62f8a8 100644 --- a/rust/sbp/tests/integration/main.rs +++ b/rust/sbp/tests/integration/main.rs @@ -44,6 +44,7 @@ mod auto_check_sbp_flash_msg_stm_flash_unlock_sector; mod auto_check_sbp_flash_msg_stm_unique_id_req; mod auto_check_sbp_flash_msg_stm_unique_id_resp; mod auto_check_sbp_imu_msg_imu_aux; +mod auto_check_sbp_imu_msg_imu_comp; mod auto_check_sbp_imu_msg_imu_raw; mod auto_check_sbp_integrity_msg_acknowledge; mod auto_check_sbp_integrity_msg_ssr_flag_high_level;