UnoATmega328PTMP36laptop · gcc· · ·PASSFAILUnityfake1int tempC(int raw) {2 return raw*500L/1024 - 50;3}4bool needHeat(void) {5 return tempC(read_raw()) < 20;6}<= 20;500/1024 - 50; 1void test_cold(void) {2 fake_raw = 141;3 bool on = needHeat();4 TEST_ASSERT_TRUE(on);5}overflow!raw141144truefalsetruetest
Back: the Uno board (target) and the laptop (host) with a fake sensor and Unity's resultFront: temp.c, the logic, and test_temp.c, one testTokens: firmware, the logic, fake readings and true/false answersThe target (an Arduino Uno R3) and the host (a laptop). The logic in temp.c has no pins, so the laptop's compiler builds it too, and a fake read_raw() stands in for the sensor. test_cold sets fake_raw = 141 (18 degrees) and Unity checks needHeat() is true; test_edge sets 144 (exactly 20) and expects false, which catches a < turned into <=. The laptop's int is 32 bits and the Uno's is 16, so some tests still belong on the board.

Why off the board

  1. To try a change on the board you compile, upload over USB, then watch the serial monitor. A cold room is hard to make on demand. And nothing checks the old cases again for you.

Find the seam

One test

Limits

What happens

;; step 1/10 · slow
edit -> compile -> upload -> watch serial output
every try needs the board, the cable and the sensor
cold, hot, the exact set point: hard to make for real

The cards show only the logic and one test. The test file also includes unity.h and temp.c's header, and has a main() that calls UNITY_BEGIN(), RUN_TEST for each test and UNITY_END(). Unity also wants setUp() and tearDown() functions, which may be empty. bool comes from stdbool.h. Signed int overflow is undefined behaviour in C, and the Arduino reference says the result "is unpredictable". With raw = 66, raw*500 = 33,000 does not fit in 32,767, so writing 500L (a long constant) makes the multiply a long: at least 32 bits on both machines (32 on the Uno, usually 64 on a 64-bit macOS or Linux laptop), so it fits on both.