-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtest_invertable_hal.cpp
More file actions
88 lines (68 loc) · 2.52 KB
/
Copy pathtest_invertable_hal.cpp
File metadata and controls
88 lines (68 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// JLed unit tests for the InvertableHal<Hal> adapter (run on host).
// Copyright 2026 Jan Delgado jdelgado@gmx.net
#include <Arduino.h>
#include <arduino_hal.h> // NOLINT
#include <invertable_hal.h> // NOLINT
#include "catch2/catch_amalgamated.hpp"
using jled::ArduinoHal;
using jled::InvertableHal;
// A plain HAL with the single-argument analogWrite(val) contract InvertableHal
// adapts, i.e. a HAL with no native inversion capability of its own.
class SingleArgHal {
public:
using PinType = uint8_t;
explicit SingleArgHal(PinType pin) : pin_(pin) {}
template<typename Brightness>
void analogWrite(Brightness val) const {
val_ = static_cast<uint16_t>(val);
}
uint16_t Value() const { return val_; }
private:
PinType pin_;
mutable uint16_t val_ = 0;
};
TEST_CASE("InvertableHal<SingleArgHal> analogWrite", "[invertable_hal]") {
InvertableHal<SingleArgHal> hal(1);
SECTION("passthrough when invert is false (uint8_t)") {
hal.analogWrite<uint8_t>(123, false);
REQUIRE(hal.Value() == 123);
}
SECTION("passthrough when invert is false (uint16_t)") {
hal.analogWrite<uint16_t>(12345, false);
REQUIRE(hal.Value() == 12345);
}
SECTION("inverts uint8_t value when invert is true") {
hal.analogWrite<uint8_t>(0, true);
REQUIRE(hal.Value() == 255);
hal.analogWrite<uint8_t>(255, true);
REQUIRE(hal.Value() == 0);
hal.analogWrite<uint8_t>(100, true);
REQUIRE(hal.Value() == 155);
}
SECTION("inverts uint16_t value when invert is true") {
hal.analogWrite<uint16_t>(0, true);
REQUIRE(hal.Value() == 65535);
hal.analogWrite<uint16_t>(65535, true);
REQUIRE(hal.Value() == 0);
}
}
struct ArduinoMockFixture {
ArduinoState mock{};
ArduinoMockFixture() { arduinoMockSetInstance(&mock); }
~ArduinoMockFixture() { arduinoMockSetInstance(nullptr); }
};
TEST_CASE_METHOD(ArduinoMockFixture, "InvertableHal<ArduinoHal<8>> analogWrite",
"[invertable_hal]") {
constexpr auto kPin = 10;
InvertableHal<ArduinoHal<8>> hal(kPin);
SECTION("passes value through unchanged when invert is false") {
hal.analogWrite<uint8_t>(123, false);
REQUIRE(mock.getPinState(kPin) == 123);
}
SECTION("inverts value before forwarding when invert is true") {
hal.analogWrite<uint8_t>(0, true);
REQUIRE(mock.getPinState(kPin) == 255);
hal.analogWrite<uint8_t>(255, true);
REQUIRE(mock.getPinState(kPin) == 0);
}
}