Testing
Unit Tests (Mocked)
The tests/ directory contains host-only tests that replace the low-level lw_* functions with mocks. They exercise buffer semantics, repeated-start flows, timeout handling, and the strict scanner behavior.
Running locally
cmake -S . -B build -DBUILD_TESTING=ON
cmake --build build
ctest --test-dir build --output-on-failure
Tests covered:
- Plain
requestFromusinglw_read - Repeated-start reads via
lw_ioctl_read - Internal register helper (
requestFrom(addr, qty, iaddress, isize, sendStop)) - Timeout flag propagation when
lw_readreportsETIMEDOUT - Deferred write flushing when
endTransmission(false)is not followed by a read - Strict scanner write failures (logging suppressed in that binary)
- Basic negative-path checks for the C API (
lw_open_bus,lw_write,lw_ioctl_write, etc.)
Hardware Tests
Mock tests catch logic regressions, but you should still validate on real hardware before tagging releases:
- Setup: Attach an I2C peripheral to
/dev/i2c-1. You can use the Arduino companion sketch in examples, which exposes a simple slave at0x40. Ensurei2c-devis loaded (sudo modprobe i2c-dev). - Scanner: From
build/, runsudo ./i2c_scanner. Confirm your peripheral shows up (the Arduino sketch will report at0x40). - Writer: Run
sudo ./master_writer(or pass--helpto adjust bus/address/register). Watch the Arduino serial log to confirm the byte was received. - Reader / Repeated Start: Run
sudo ./master_reader. It performsendTransmission(false)followed byrequestFrom(), verifying the repeated-start path. - Extended exercise:
sudo ./master_multiplierwrites a rolling pattern, then reads it back, providing a simple stress test. - Custom tests: Integrate
linux_wireinto your application and validate against the target device(s).
Continuous Integration
.github/workflows/ci.yml runs on ubuntu-22.04 for every push/PR targeting main. Steps:
- Checkout
- Install
cmake,ninja-build,gcc,g++ cmake -S . -B build -G Ninja -DBUILD_TESTING=ONcmake --build buildctest --test-dir build --output-on-failure
CI ensures both the library and tests build cleanly on a baseline similar to Raspberry Pi OS. Always run the hardware checks above before releasing.***