Debian Codegraph @

bindings/cxx/tests/tests-line-info.cpp

raw symbols SCIP
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 #include <string>
7
8 #include "helpers.hpp"
9 #include "gpiosim.hpp"
10
11 using ::gpiosim::make_sim;
12 using hog_dir = ::gpiosim::chip_builder::direction;
13 using direction = ::gpiod::line::direction;
14 using edge = ::gpiod::line::edge;
15 using bias = ::gpiod::line::bias;
16 using drive = ::gpiod::line::drive;
17 using event_clock = ::gpiod::line::clock;
18
19 using namespace ::std::chrono_literals;
20
21 namespace {
22
23 TEST_CASE("get_line_info() works", "[chip][line-info]")
24 {
25 auto sim = make_sim()
26 .set_num_lines(8)
27 .set_line_name(0, "foobar")
28 .set_hog(0, "hog", hog_dir::OUTPUT_HIGH)
29 .build();
30
31 ::gpiod::chip chip(sim.dev_path());
32
33 SECTION("line_info can be retrieved from chip")
34 {
35 auto info = chip.get_line_info(0);
36
37 REQUIRE(info.offset() == 0);
38 REQUIRE_THAT(info.name(), Catch::Matchers::Equals("foobar"));
39 REQUIRE(info.used());
40 REQUIRE_THAT(info.consumer(), Catch::Matchers::Equals("hog"));
41 REQUIRE(info.direction() == ::gpiod::line::direction::OUTPUT);
42 REQUIRE_FALSE(info.active_low());
43 REQUIRE(info.bias() == ::gpiod::line::bias::UNKNOWN);
44 REQUIRE(info.drive() == ::gpiod::line::drive::PUSH_PULL);
45 REQUIRE(info.edge_detection() == ::gpiod::line::edge::NONE);
46 REQUIRE(info.event_clock() == ::gpiod::line::clock::MONOTONIC);
47 REQUIRE_FALSE(info.debounced());
48 REQUIRE(info.debounce_period() == 0us);
49 }
50
51 SECTION("offset out of range")
52 {
53 REQUIRE_THROWS_AS(chip.get_line_info(8), ::std::invalid_argument);
54 }
55 }
56
57 TEST_CASE("line properties can be retrieved", "[line-info]")
58 {
59 auto sim = make_sim()
60 .set_num_lines(8)
61 .set_line_name(1, "foo")
62 .set_line_name(2, "bar")
63 .set_line_name(4, "baz")
64 .set_line_name(5, "xyz")
65 .set_hog(3, "hog3", hog_dir::OUTPUT_HIGH)
66 .set_hog(4, "hog4", hog_dir::OUTPUT_LOW)
67 .build();
68
69 ::gpiod::chip chip(sim.dev_path());
70
71 SECTION("basic properties")
72 {
73 auto info4 = chip.get_line_info(4);
74 auto info6 = chip.get_line_info(6);
75
76 REQUIRE(info4.offset() == 4);
77 REQUIRE_THAT(info4.name(), Catch::Matchers::Equals("baz"));
78 REQUIRE(info4.used());
79 REQUIRE_THAT(info4.consumer(), Catch::Matchers::Equals("hog4"));
80 REQUIRE(info4.direction() == direction::OUTPUT);
81 REQUIRE(info4.edge_detection() == edge::NONE);
82 REQUIRE_FALSE(info4.active_low());
83 REQUIRE(info4.bias() == bias::UNKNOWN);
84 REQUIRE(info4.drive() == drive::PUSH_PULL);
85 REQUIRE(info4.event_clock() == event_clock::MONOTONIC);
86 REQUIRE_FALSE(info4.debounced());
87 REQUIRE(info4.debounce_period() == 0us);
88 }
89 }
90
91 TEST_CASE("line_info can be copied and moved")
92 {
93 auto sim = make_sim()
94 .set_num_lines(4)
95 .set_line_name(2, "foobar")
96 .build();
97
98 ::gpiod::chip chip(sim.dev_path());
99 auto info = chip.get_line_info(2);
100
101 SECTION("copy constructor works")
102 {
103 auto copy(info);
104 REQUIRE(copy.offset() == 2);
105 REQUIRE_THAT(copy.name(), Catch::Matchers::Equals("foobar"));
106 /* info can still be used */
107 REQUIRE(info.offset() == 2);
108 REQUIRE_THAT(info.name(), Catch::Matchers::Equals("foobar"));
109 }
110
111 SECTION("assignment operator works")
112 {
113 auto copy = chip.get_line_info(0);
114 copy = info;
115 REQUIRE(copy.offset() == 2);
116 REQUIRE_THAT(copy.name(), Catch::Matchers::Equals("foobar"));
117 /* info can still be used */
118 REQUIRE(info.offset() == 2);
119 REQUIRE_THAT(info.name(), Catch::Matchers::Equals("foobar"));
120 }
121
122 SECTION("move constructor works")
123 {
124 auto copy(::std::move(info));
125 REQUIRE(copy.offset() == 2);
126 REQUIRE_THAT(copy.name(), Catch::Matchers::Equals("foobar"));
127 }
128
129 SECTION("move assignment operator works")
130 {
131 auto copy = chip.get_line_info(0);
132 copy = ::std::move(info);
133 REQUIRE(copy.offset() == 2);
134 REQUIRE_THAT(copy.name(), Catch::Matchers::Equals("foobar"));
135 }
136 }
137
138 TEST_CASE("line_info stream insertion operator works")
139 {
140 auto sim = make_sim()
141 .set_line_name(0, "foo")
142 .set_hog(0, "hogger", hog_dir::OUTPUT_HIGH)
143 .build();
144
145 ::gpiod::chip chip(sim.dev_path());
146
147 auto info = chip.get_line_info(0);
148
149 REQUIRE_THAT(info, stringify_matcher<::gpiod::line_info>(
150 "gpiod::line_info(offset=0, name='foo', used=true, consumer='hogger', direction=OUTPUT, "
151 "active_low=false, bias=UNKNOWN, drive=PUSH_PULL, edge_detection=NONE, event_clock=MONOTONIC, debounced=false)"));
152 }
153
154 } /* namespace */