| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | #include <gpiod.hpp> |
| 6 | |
| 7 | #include "helpers.hpp" |
| 8 | |
| 9 | using value = ::gpiod::line::value; |
| 10 | using direction = ::gpiod::line::direction; |
| 11 | using edge = ::gpiod::line::edge; |
| 12 | using bias = ::gpiod::line::bias; |
| 13 | using drive = ::gpiod::line::drive; |
| 14 | using clock_type = ::gpiod::line::clock; |
| 15 | using value = ::gpiod::line::value; |
| 16 | |
| 17 | using namespace ::std::chrono_literals; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | TEST_CASE("line_settings constructor works", "[line-settings]") |
| 22 | { |
| 23 | ::gpiod::line_settings settings; |
| 24 | |
| 25 | REQUIRE(settings.direction() == direction::AS_IS); |
| 26 | REQUIRE(settings.edge_detection() == edge::NONE); |
| 27 | REQUIRE(settings.bias() == bias::AS_IS); |
| 28 | REQUIRE(settings.drive() == drive::PUSH_PULL); |
| 29 | REQUIRE_FALSE(settings.active_low()); |
| 30 | REQUIRE(settings.debounce_period() == 0us); |
| 31 | REQUIRE(settings.event_clock() == clock_type::MONOTONIC); |
| 32 | REQUIRE(settings.output_value() == value::INACTIVE); |
| 33 | } |
| 34 | |
| 35 | TEST_CASE("line_settings mutators work", "[line-settings]") |
| 36 | { |
| 37 | ::gpiod::line_settings settings; |
| 38 | |
| 39 | SECTION("direction") |
| 40 | { |
| 41 | settings.set_direction(direction::INPUT); |
| 42 | REQUIRE(settings.direction() == direction::INPUT); |
| 43 | settings.set_direction(direction::AS_IS); |
| 44 | REQUIRE(settings.direction() == direction::AS_IS); |
| 45 | settings.set_direction(direction::OUTPUT); |
| 46 | REQUIRE(settings.direction() == direction::OUTPUT); |
| 47 | REQUIRE_THROWS_AS(settings.set_direction(static_cast<direction>(999)), |
| 48 | ::std::invalid_argument); |
| 49 | } |
| 50 | |
| 51 | SECTION("edge detection") |
| 52 | { |
| 53 | settings.set_edge_detection(edge::BOTH); |
| 54 | REQUIRE(settings.edge_detection() == edge::BOTH); |
| 55 | settings.set_edge_detection(edge::NONE); |
| 56 | REQUIRE(settings.edge_detection() == edge::NONE); |
| 57 | settings.set_edge_detection(edge::FALLING); |
| 58 | REQUIRE(settings.edge_detection() == edge::FALLING); |
| 59 | settings.set_edge_detection(edge::RISING); |
| 60 | REQUIRE(settings.edge_detection() == edge::RISING); |
| 61 | REQUIRE_THROWS_AS(settings.set_edge_detection(static_cast<edge>(999)), |
| 62 | ::std::invalid_argument); |
| 63 | } |
| 64 | |
| 65 | SECTION("bias") |
| 66 | { |
| 67 | settings.set_bias(bias::DISABLED); |
| 68 | REQUIRE(settings.bias() == bias::DISABLED); |
| 69 | settings.set_bias(bias::AS_IS); |
| 70 | REQUIRE(settings.bias() == bias::AS_IS); |
| 71 | settings.set_bias(bias::PULL_DOWN); |
| 72 | REQUIRE(settings.bias() == bias::PULL_DOWN); |
| 73 | settings.set_bias(bias::PULL_UP); |
| 74 | REQUIRE(settings.bias() == bias::PULL_UP); |
| 75 | REQUIRE_THROWS_AS(settings.set_bias(static_cast<bias>(999)), ::std::invalid_argument); |
| 76 | REQUIRE_THROWS_AS(settings.set_bias(bias::UNKNOWN), ::std::invalid_argument); |
| 77 | } |
| 78 | |
| 79 | SECTION("drive") |
| 80 | { |
| 81 | settings.set_drive(drive::OPEN_DRAIN); |
| 82 | REQUIRE(settings.drive() == drive::OPEN_DRAIN); |
| 83 | settings.set_drive(drive::PUSH_PULL); |
| 84 | REQUIRE(settings.drive() == drive::PUSH_PULL); |
| 85 | settings.set_drive(drive::OPEN_SOURCE); |
| 86 | REQUIRE(settings.drive() == drive::OPEN_SOURCE); |
| 87 | REQUIRE_THROWS_AS(settings.set_drive(static_cast<drive>(999)), ::std::invalid_argument); |
| 88 | } |
| 89 | |
| 90 | SECTION("active-low") |
| 91 | { |
| 92 | settings.set_active_low(true); |
| 93 | REQUIRE(settings.active_low()); |
| 94 | settings.set_active_low(false); |
| 95 | REQUIRE_FALSE(settings.active_low()); |
| 96 | } |
| 97 | |
| 98 | SECTION("debounce period") |
| 99 | { |
| 100 | settings.set_debounce_period(2000us); |
| 101 | REQUIRE(settings.debounce_period() == 2000us); |
| 102 | } |
| 103 | |
| 104 | SECTION("event clock") |
| 105 | { |
| 106 | settings.set_event_clock(clock_type::REALTIME); |
| 107 | REQUIRE(settings.event_clock() == clock_type::REALTIME); |
| 108 | settings.set_event_clock(clock_type::MONOTONIC); |
| 109 | REQUIRE(settings.event_clock() == clock_type::MONOTONIC); |
| 110 | settings.set_event_clock(clock_type::HTE); |
| 111 | REQUIRE(settings.event_clock() == clock_type::HTE); |
| 112 | REQUIRE_THROWS_AS(settings.set_event_clock(static_cast<clock_type>(999)), |
| 113 | ::std::invalid_argument); |
| 114 | } |
| 115 | |
| 116 | SECTION("output value") |
| 117 | { |
| 118 | settings.set_output_value(value::ACTIVE); |
| 119 | REQUIRE(settings.output_value() == value::ACTIVE); |
| 120 | settings.set_output_value(value::INACTIVE); |
| 121 | REQUIRE(settings.output_value() == value::INACTIVE); |
| 122 | REQUIRE_THROWS_AS(settings.set_output_value(static_cast<value>(999)), |
| 123 | ::std::invalid_argument); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | TEST_CASE("line_settings can be moved and copied", "[line-settings]") |
| 128 | { |
| 129 | ::gpiod::line_settings settings; |
| 130 | |
| 131 | settings |
| 132 | .set_direction(direction::INPUT) |
| 133 | .set_edge_detection(edge::BOTH); |
| 134 | |
| 135 | SECTION("copy constructor works") |
| 136 | { |
| 137 | auto copy(settings); |
| 138 | settings.set_direction(direction::OUTPUT); |
| 139 | settings.set_edge_detection(edge::NONE); |
| 140 | REQUIRE(copy.direction() == direction::INPUT); |
| 141 | REQUIRE(copy.edge_detection() == edge::BOTH); |
| 142 | } |
| 143 | |
| 144 | SECTION("assignment operator works") |
| 145 | { |
| 146 | ::gpiod::line_settings copy; |
| 147 | copy = settings; |
| 148 | settings.set_direction(direction::OUTPUT); |
| 149 | settings.set_edge_detection(edge::NONE); |
| 150 | REQUIRE(copy.direction() == direction::INPUT); |
| 151 | REQUIRE(copy.edge_detection() == edge::BOTH); |
| 152 | } |
| 153 | |
| 154 | SECTION("move constructor works") |
| 155 | { |
| 156 | auto copy(::std::move(settings)); |
| 157 | REQUIRE(copy.direction() == direction::INPUT); |
| 158 | REQUIRE(copy.edge_detection() == edge::BOTH); |
| 159 | } |
| 160 | |
| 161 | SECTION("move assignment operator works") |
| 162 | { |
| 163 | ::gpiod::line_settings copy; |
| 164 | copy = ::std::move(settings); |
| 165 | REQUIRE(copy.direction() == direction::INPUT); |
| 166 | REQUIRE(copy.edge_detection() == edge::BOTH); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | TEST_CASE("line_settings stream insertion operator works", "[line-settings]") |
| 171 | { |
| 172 | ::gpiod::line_settings settings; |
| 173 | |
| 174 | REQUIRE_THAT(settings |
| 175 | .set_active_low(true) |
| 176 | .set_direction(direction::INPUT) |
| 177 | .set_edge_detection(edge::BOTH) |
| 178 | .set_bias(bias::PULL_DOWN) |
| 179 | .set_event_clock(clock_type::REALTIME), |
| 180 | stringify_matcher<::gpiod::line_settings>( |
| 181 | "gpiod::line_settings(direction=INPUT, edge_detection=BOTH_EDGES, " |
| 182 | "bias=PULL_DOWN, drive=PUSH_PULL, active-low, debounce_period=0, " |
| 183 | "event_clock=REALTIME, output_value=INACTIVE)" |
| 184 | ) |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | } /* namespace */ |