File: tests-line-settings.cpp

package info (click to toggle)
libgpiod 2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,108 kB
  • sloc: ansic: 26,612; sh: 7,554; cpp: 4,944; python: 2,426; makefile: 811; xml: 49
file content (188 lines) | stat: -rw-r--r-- 5,724 bytes parent folder | download | duplicates (2)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>

#include <catch2/catch_all.hpp>
#include <gpiod.hpp>

#include "helpers.hpp"

using value = ::gpiod::line::value;
using direction = ::gpiod::line::direction;
using edge = ::gpiod::line::edge;
using bias = ::gpiod::line::bias;
using drive = ::gpiod::line::drive;
using clock_type = ::gpiod::line::clock;
using value = ::gpiod::line::value;

using namespace ::std::chrono_literals;

namespace {

TEST_CASE("line_settings constructor works", "[line-settings]")
{
	::gpiod::line_settings settings;

	REQUIRE(settings.direction() == direction::AS_IS);
	REQUIRE(settings.edge_detection() == edge::NONE);
	REQUIRE(settings.bias() == bias::AS_IS);
	REQUIRE(settings.drive() == drive::PUSH_PULL);
	REQUIRE_FALSE(settings.active_low());
	REQUIRE(settings.debounce_period() == 0us);
	REQUIRE(settings.event_clock() == clock_type::MONOTONIC);
	REQUIRE(settings.output_value() == value::INACTIVE);
}

TEST_CASE("line_settings mutators work", "[line-settings]")
{
	::gpiod::line_settings settings;

	SECTION("direction")
	{
		settings.set_direction(direction::INPUT);
		REQUIRE(settings.direction() == direction::INPUT);
		settings.set_direction(direction::AS_IS);
		REQUIRE(settings.direction() == direction::AS_IS);
		settings.set_direction(direction::OUTPUT);
		REQUIRE(settings.direction() == direction::OUTPUT);
		REQUIRE_THROWS_AS(settings.set_direction(static_cast<direction>(999)),
				  ::std::invalid_argument);
	}

	SECTION("edge detection")
	{
		settings.set_edge_detection(edge::BOTH);
		REQUIRE(settings.edge_detection() == edge::BOTH);
		settings.set_edge_detection(edge::NONE);
		REQUIRE(settings.edge_detection() == edge::NONE);
		settings.set_edge_detection(edge::FALLING);
		REQUIRE(settings.edge_detection() == edge::FALLING);
		settings.set_edge_detection(edge::RISING);
		REQUIRE(settings.edge_detection() == edge::RISING);
		REQUIRE_THROWS_AS(settings.set_edge_detection(static_cast<edge>(999)),
				  ::std::invalid_argument);
	}

	SECTION("bias")
	{
		settings.set_bias(bias::DISABLED);
		REQUIRE(settings.bias() == bias::DISABLED);
		settings.set_bias(bias::AS_IS);
		REQUIRE(settings.bias() == bias::AS_IS);
		settings.set_bias(bias::PULL_DOWN);
		REQUIRE(settings.bias() == bias::PULL_DOWN);
		settings.set_bias(bias::PULL_UP);
		REQUIRE(settings.bias() == bias::PULL_UP);
		REQUIRE_THROWS_AS(settings.set_bias(static_cast<bias>(999)), ::std::invalid_argument);
		REQUIRE_THROWS_AS(settings.set_bias(bias::UNKNOWN), ::std::invalid_argument);
	}

	SECTION("drive")
	{
		settings.set_drive(drive::OPEN_DRAIN);
		REQUIRE(settings.drive() == drive::OPEN_DRAIN);
		settings.set_drive(drive::PUSH_PULL);
		REQUIRE(settings.drive() == drive::PUSH_PULL);
		settings.set_drive(drive::OPEN_SOURCE);
		REQUIRE(settings.drive() == drive::OPEN_SOURCE);
		REQUIRE_THROWS_AS(settings.set_drive(static_cast<drive>(999)), ::std::invalid_argument);
	}

	SECTION("active-low")
	{
		settings.set_active_low(true);
		REQUIRE(settings.active_low());
		settings.set_active_low(false);
		REQUIRE_FALSE(settings.active_low());
	}

	SECTION("debounce period")
	{
		settings.set_debounce_period(2000us);
		REQUIRE(settings.debounce_period() == 2000us);
	}

	SECTION("event clock")
	{
		settings.set_event_clock(clock_type::REALTIME);
		REQUIRE(settings.event_clock() == clock_type::REALTIME);
		settings.set_event_clock(clock_type::MONOTONIC);
		REQUIRE(settings.event_clock() == clock_type::MONOTONIC);
		settings.set_event_clock(clock_type::HTE);
		REQUIRE(settings.event_clock() == clock_type::HTE);
		REQUIRE_THROWS_AS(settings.set_event_clock(static_cast<clock_type>(999)),
				  ::std::invalid_argument);
	}

	SECTION("output value")
	{
		settings.set_output_value(value::ACTIVE);
		REQUIRE(settings.output_value() == value::ACTIVE);
		settings.set_output_value(value::INACTIVE);
		REQUIRE(settings.output_value() == value::INACTIVE);
		REQUIRE_THROWS_AS(settings.set_output_value(static_cast<value>(999)),
				  ::std::invalid_argument);
	}
}

TEST_CASE("line_settings can be moved and copied", "[line-settings]")
{
	::gpiod::line_settings settings;

	settings
		.set_direction(direction::INPUT)
		.set_edge_detection(edge::BOTH);

	SECTION("copy constructor works")
	{
		auto copy(settings);
		settings.set_direction(direction::OUTPUT);
		settings.set_edge_detection(edge::NONE);
		REQUIRE(copy.direction() == direction::INPUT);
		REQUIRE(copy.edge_detection() == edge::BOTH);
	}

	SECTION("assignment operator works")
	{
		::gpiod::line_settings copy;
		copy = settings;
		settings.set_direction(direction::OUTPUT);
		settings.set_edge_detection(edge::NONE);
		REQUIRE(copy.direction() == direction::INPUT);
		REQUIRE(copy.edge_detection() == edge::BOTH);
	}

	SECTION("move constructor works")
	{
		auto copy(::std::move(settings));
		REQUIRE(copy.direction() == direction::INPUT);
		REQUIRE(copy.edge_detection() == edge::BOTH);
	}

	SECTION("move assignment operator works")
	{
		::gpiod::line_settings copy;
		copy = ::std::move(settings);
		REQUIRE(copy.direction() == direction::INPUT);
		REQUIRE(copy.edge_detection() == edge::BOTH);
	}
}

TEST_CASE("line_settings stream insertion operator works", "[line-settings]")
{
	::gpiod::line_settings settings;

	REQUIRE_THAT(settings
		.set_active_low(true)
		.set_direction(direction::INPUT)
		.set_edge_detection(edge::BOTH)
		.set_bias(bias::PULL_DOWN)
		.set_event_clock(clock_type::REALTIME),
		stringify_matcher<::gpiod::line_settings>(
			"gpiod::line_settings(direction=INPUT, edge_detection=BOTH_EDGES, "
			"bias=PULL_DOWN, drive=PUSH_PULL, active-low, debounce_period=0, "
			"event_clock=REALTIME, output_value=INACTIVE)"
		)
	);
}

} /* namespace */