Add unit tests for SafetyState

Open Beginner friendly
#257 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
90/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
testing

Research direction

Read src/utils/safety_state.hpp and src/utils/safety_state.cpp to understand SafetyState::evaluate and reasons_to_string, then compare the layout in test/test_utils/test_main.cpp. Make host time controllable in test/host/Arduino.h, add the specified test/test_safety/test_main.cpp suite and Makefile entry, and run make test. Done means every listed case has its own test and the suite passes.

Written by the indexing model from the issue text.

Description

good first issue not started

SafetyState decides whether the motors may move, and it has no tests. Good first task. No hardware needed.

Files

  • src/utils/safety_state.hpp, src/utils/safety_state.cpp
  • src/utils/timing.hpp (Timer, uses micros())
  • test/host/Arduino.h (host stub; micros() always returns 0 today)
  • Makefile

Steps

  1. Read SafetyState::evaluate. It takes four booleans and returns a bitmask of Reason flags. NONE (0) means the motors are armed.
  2. Make time controllable in host tests. In test/host/Arduino.h, in the non-FW_SIMULATION branch, replace the fixed micros() with:
    inline uint32_t host_fake_micros = 0;
    inline uint32_t micros() { return host_fake_micros; }
    
    Run make test to check nothing else broke.
  3. Create test/test_safety/test_main.cpp (copy the layout of test/test_utils/test_main.cpp) and add to the Makefile:
    HOST_TEST_SRC_test_safety := $(TEST_DIR)/test_safety/test_main.cpp src/utils/safety_state.cpp
    
  4. Write one test per case, with a fresh SafetyState and host_fake_micros = 0 at the start of each:
    • Each input on its own sets exactly its own bit (transmitter, not configured, slow loop, gimbal power off).
    • All four bad at once sets all four bits.
    • Gimbal power just turned on: GIMBAL_POWER_SETTLING is set.
    • Gimbal power on for GIMBAL_POWER_SETTLE_US (3 s) or more: settling clears and motors_armed() is true.
    • Power off then on again: the 3 s wait starts over.
    • reasons_to_string: NONE gives "none", two bits give both names separated by one space, and a small buffer (length 5) never overflows and stays null-terminated.

Done when

  • make test passes with the new test_safety suite.
  • Each case above has its own test.
Dominant language
C
Stars
11
Forks
2
Avg merge
18d 3h
Merged PRs (30d)
3

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from CU-Robotics/firmware

All issues in CU-Robotics/firmware

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.