From 260864adbcd5766684ff9625ff96a514629215cf Mon Sep 17 00:00:00 2001 From: Charly Olivier Date: Tue, 26 May 2026 15:56:28 +0200 Subject: [PATCH] test(hts221): Add readOneShot, continuous-no-trigger, and softReset cases. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three test cases were missing from the HTS221 native suite: - test_read_one_shot_returns_calibrated_values exercises the bundled readOneShot() flow (trigger + wait + decode in one call) against the same calibration math used by the read() path. - test_read_does_not_trigger_one_shot_when_continuous guards the symmetric branch of test_read_auto_triggers_when_powered_down: when the device is already streaming, read() must not poke CTRL2. - test_soft_reset_writes_ctrl2_boot mirrors test_reboot_writes_ctrl2_boot for the documented-equivalent softReset() entry point — the two share the same BOOT mechanism today but the contract is part of the public API, so it stays covered if the implementation diverges later. Builds on top of the existing AUTO_INCREMENT-aware mock plumbing and the parameterised preloadMeasurement helper. Closes #22 Co-authored-by: Sébastien NEDJAR --- tests/native/test_hts221/test_main.cpp | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/native/test_hts221/test_main.cpp b/tests/native/test_hts221/test_main.cpp index 7fdd64b..d16eaba 100644 --- a/tests/native/test_hts221/test_main.cpp +++ b/tests/native/test_hts221/test_main.cpp @@ -244,6 +244,52 @@ void test_read_returns_nan_on_timeout(void) { TEST_ASSERT_TRUE_MESSAGE(isnan(r.humidity), "humidity should be NaN on timeout"); } +void test_read_one_shot_returns_calibrated_values(void) { + sensor.begin(); // begin() leaves the device powered down — the bundled + // readOneShot() flow must drive the trigger itself. + preloadMeasurement(15.0f, 60.0f); + + auto r = sensor.readOneShot(); + TEST_ASSERT_FLOAT_WITHIN(0.01f, 15.0f, r.temperature); + TEST_ASSERT_FLOAT_WITHIN(0.1f, 60.0f, r.humidity); +} + +void test_read_does_not_trigger_one_shot_when_continuous(void) { + sensor.begin(); + sensor.setContinuous(HTS221_ODR_1_HZ); + preloadMeasurement(20.0f, 50.0f); + Wire.clearWrites(); + + sensor.read(); + + // In continuous mode the driver must rely on the periodic conversion + // and not poke CTRL2 at all — any write here would either re-arm a + // one-shot or reboot the device, both of which break continuous flow. + for (const auto& w : Wire.getWrites()) { + TEST_ASSERT_NOT_EQUAL_MESSAGE(HTS221_REG_CTRL2, w.reg, + "read() should not touch CTRL2 in continuous mode"); + } +} + +void test_soft_reset_writes_ctrl2_boot(void) { + sensor.begin(); + Wire.clearWrites(); + // Make BOOT bit clear itself on the very first poll so the driver + // doesn't spin against the mock. + Wire.setRegister(ADDR, HTS221_REG_CTRL2, 0); + + sensor.softReset(); + + bool sawBootSet = false; + for (const auto& w : Wire.getWrites()) { + if (w.reg == HTS221_REG_CTRL2 && (w.value & HTS221_CTRL2_BOOT)) { + sawBootSet = true; + break; + } + } + TEST_ASSERT_TRUE(sawBootSet); +} + void test_reboot_writes_ctrl2_boot(void) { sensor.begin(); Wire.clearWrites(); @@ -281,6 +327,9 @@ int main(void) { RUN_TEST(test_set_temperature_offset_shifts_reading); RUN_TEST(test_calibrate_temperature_applies_two_point_correction); RUN_TEST(test_read_returns_nan_on_timeout); + RUN_TEST(test_read_one_shot_returns_calibrated_values); + RUN_TEST(test_read_does_not_trigger_one_shot_when_continuous); + RUN_TEST(test_soft_reset_writes_ctrl2_boot); RUN_TEST(test_reboot_writes_ctrl2_boot); return UNITY_END(); }