Skip to content
Closed
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
57 changes: 57 additions & 0 deletions extras/test/unit/tests/TestModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,63 @@

BOOST_FIXTURE_TEST_SUITE(suite_Module, ModuleFixture)

BOOST_FIXTURE_TEST_CASE(Module_copy_assignment_copies_spi_config_without_mutating_source, ModuleFixture)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No human in the history of the world would ever name a test case like this. Why is Module_copy_assignment insufficient?

{
BOOST_TEST_MESSAGE("--- Test Module copy assignment copies SPI config correctly ---");

Module other(hal, EMULATED_RADIO_NSS_PIN, EMULATED_RADIO_IRQ_PIN, EMULATED_RADIO_RST_PIN, EMULATED_RADIO_GPIO_PIN);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you really want to ensure the source is unchanged, you have to set different pins and check them.


mod->spiConfig.stream = true;
mod->spiConfig.err = 123;
mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ] = 0x42;
mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE] = 0x99;
mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR] = Module::BITS_16;
mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD] = Module::BITS_8;
mod->spiConfig.statusPos = 7;
mod->spiConfig.timeout = 4321;
mod->rfSwitchPins[0] = 1001;
mod->rfSwitchPins[1] = 1002;
mod->rfSwitchTable = reinterpret_cast<const Module::RfSwitchMode_t*>(0x1234);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you reusing the module fixture to do this? It seems prone to breaking in the future if the approach to its setup/teardown changes. Especially since mod->rfSwitchTable is pointing a an arbitrary memory address.


other.spiConfig.stream = false;
other.spiConfig.err = -77;
other.spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ] = 0x11;
other.spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE] = 0x22;
other.spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR] = Module::BITS_24;
other.spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD] = Module::BITS_16;
other.spiConfig.statusPos = 3;
other.spiConfig.timeout = 555;
other.rfSwitchPins[0] = 2001;
other.rfSwitchPins[1] = 2002;
other.rfSwitchTable = reinterpret_cast<const Module::RfSwitchMode_t*>(0x5678);

other = *mod;

BOOST_TEST(other.spiConfig.stream == mod->spiConfig.stream);
BOOST_TEST(other.spiConfig.err == mod->spiConfig.err);
BOOST_TEST(other.spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ] == mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ]);
BOOST_TEST(other.spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE] == mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE]);
BOOST_TEST(other.spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR] == mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]);
BOOST_TEST(other.spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD] == mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]);
BOOST_TEST(other.spiConfig.statusPos == mod->spiConfig.statusPos);
BOOST_TEST(other.spiConfig.timeout == mod->spiConfig.timeout);
BOOST_TEST(other.rfSwitchPins[0] == mod->rfSwitchPins[0]);
BOOST_TEST(other.rfSwitchPins[1] == mod->rfSwitchPins[1]);
BOOST_TEST(other.rfSwitchTable == mod->rfSwitchTable);

BOOST_TEST(mod->spiConfig.stream == true);
BOOST_TEST(mod->spiConfig.err == 123);
BOOST_TEST(mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ] == 0x42);
BOOST_TEST(mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE] == 0x99);
BOOST_TEST(mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR] == Module::BITS_16);
BOOST_TEST(mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD] == Module::BITS_8);
BOOST_TEST(mod->spiConfig.statusPos == 7);
BOOST_TEST(mod->spiConfig.timeout == 4321);
BOOST_TEST(mod->rfSwitchPins[0] == 1001);
BOOST_TEST(mod->rfSwitchPins[1] == 1002);
BOOST_TEST(mod->rfSwitchTable == reinterpret_cast<const Module::RfSwitchMode_t*>(0x1234));
}

BOOST_FIXTURE_TEST_CASE(Module_SPIgetRegValue_reg, ModuleFixture)
{
BOOST_TEST_MESSAGE("--- Test Module::SPIgetRegValue register access ---");
Expand Down
11 changes: 10 additions & 1 deletion src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@ Module::Module(const Module& mod) {
}

Module& Module::operator=(const Module& mod) {
memcpy(reinterpret_cast<void*>(&(const_cast<Module&>(mod)).spiConfig), &this->spiConfig, sizeof(SPIConfig_t));
this->hal = mod.hal;
memcpy(&this->spiConfig, &mod.spiConfig, sizeof(SPIConfig_t));
this->csPin = mod.csPin;
this->irqPin = mod.irqPin;
this->rstPin = mod.rstPin;
this->gpioPin = mod.gpioPin;
memcpy(this->rfSwitchPins, mod.rfSwitchPins, sizeof(this->rfSwitchPins));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is size of this copy not checked against Module::RFSWITCH_MAX_PINS?

this->rfSwitchTable = mod.rfSwitchTable;

#if RADIOLIB_INTERRUPT_TIMING
this->TimerSetupCb = mod.TimerSetupCb;
this->TimerFlag = mod.TimerFlag;
this->prevTimingLen = mod.prevTimingLen;
#endif
return(*this);
}

Expand Down
Loading