Skip to contentSkip to conversation

bindings: cxx: tests: use the builder pattern for the GPIO simulator

PATCH In review Bartosz Golaszewski 2 participants 4 messages Sep 15, 2022 – Sep 16, 2022 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bindings: cxx: tests: use the builder pattern for the GPIO simulator

In order to make the interface more elegant use the Rust-like builder pattern for the GPIO simulator class.

---
 bindings/cxx/tests/gpiosim.cpp            | 261 ++++++++++++----------
 bindings/cxx/tests/gpiosim.hpp            |  65 ++++--
 bindings/cxx/tests/tests-chip-info.cpp    |  22 +-
 bindings/cxx/tests/tests-chip.cpp         |  63 +++---
 bindings/cxx/tests/tests-edge-event.cpp   |  25 ++-
 bindings/cxx/tests/tests-info-event.cpp   |  11 +-
 bindings/cxx/tests/tests-line-info.cpp    |  50 ++---
 bindings/cxx/tests/tests-line-request.cpp |  45 ++--
 bindings/cxx/tests/tests-misc.cpp         |   6 +-
 9 files changed, 316 insertions(+), 232 deletions(-)
bindings/cxx/tests/gpiosim.cpp +140 -121
index 402e1f0..0a29efe 100644
@@ -1,15 +1,13 @@
 /* SPDX-License-Identifier: LGPL-3.0-or-later */
 /* SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> */
 
-#include <functional>
 #include <map>
 #include <system_error>
+#include <utility>
 
 #include "gpiosim.h"
 #include "gpiosim.hpp"
 
-#define NORETURN __attribute__((noreturn))
-
 namespace gpiosim {
 
 namespace {
@@ -19,10 +17,10 @@ const ::std::map<chip::pull, int> pull_mapping = {
 	{ chip::pull::PULL_DOWN,	GPIOSIM_PULL_DOWN }
 };
 
-const ::std::map<chip::hog_direction, int> hog_dir_mapping = {
-	{ chip::hog_direction::INPUT,		GPIOSIM_HOG_DIR_INPUT },
-	{ chip::hog_direction::OUTPUT_HIGH,	GPIOSIM_HOG_DIR_OUTPUT_HIGH },
-	{ chip::hog_direction::OUTPUT_LOW,	GPIOSIM_HOG_DIR_OUTPUT_LOW }
+const ::std::map<chip_builder::hog_direction, int> hog_dir_mapping = {
+	{ chip_builder::hog_direction::INPUT,		GPIOSIM_HOG_DIR_INPUT },
+	{ chip_builder::hog_direction::OUTPUT_HIGH,	GPIOSIM_HOG_DIR_OUTPUT_HIGH },
+	{ chip_builder::hog_direction::OUTPUT_LOW,	GPIOSIM_HOG_DIR_OUTPUT_LOW }
 };
 
 const ::std::map<int, chip::value> value_mapping = {
@@ -82,45 +80,13 @@ bank_ptr make_sim_bank(const dev_ptr& dev)
 	return bank;
 }
 
-NORETURN void throw_invalid_type()
-{
-	throw ::std::logic_error("invalid type for property");
-}
-
-unsigned any_to_unsigned_int(const ::std::any& val)
-{
-	if (val.type() == typeid(int)) {
-		auto num_lines = ::std::any_cast<int>(val);
-		if (num_lines < 0)
-			throw ::std::invalid_argument("negative value not accepted");
-
-		   return static_cast<unsigned int>(num_lines);
-	} else if (val.type() == typeid(unsigned int)) {
-		return ::std::any_cast<unsigned int>(val);
-	}
-
-	throw_invalid_type();
-}
-
-::std::string any_to_string(const ::std::any& val)
-{
-	if (val.type() == typeid(::std::string))
-		return ::std::any_cast<::std::string>(val);
-	else if (val.type() == typeid(const char*))
-		return ::std::any_cast<const char*>(val);
-
-	throw_invalid_type();
-}
-
 } /* namespace */
 
 struct chip::impl
 {
 	impl()
 		: dev(make_sim_dev()),
-		  bank(make_sim_bank(this->dev)),
-		  has_num_lines(false),
-		  has_label(false)
+		  bank(make_sim_bank(this->dev))
 	{
 
 	}
@@ -131,99 +97,32 @@ struct chip::impl
 	impl& operator=(const impl& other) = delete;
 	impl& operator=(impl&& other) = delete;
 
-	static const ::std::map<chip::property,
-				::std::function<void (impl&,
-						      const ::std::any&)>> setter_mapping;
-
-	void set_property(chip::property prop, const ::std::any& val)
-	{
-		setter_mapping.at(prop)(*this, val);
-	}
-
-	void set_num_lines(const ::std::any& val)
-	{
-		if (this->has_num_lines)
-			throw ::std::logic_error("number of lines can be set at most once");
-
-		int ret = ::gpiosim_bank_set_num_lines(this->bank.get(), any_to_unsigned_int(val));
-		if (ret)
-			throw ::std::system_error(errno, ::std::system_category(),
-						  "failed to set the number of lines");
-
-		this->has_num_lines = true;
-	}
-
-	void set_label(const ::std::any& val)
-	{
-		if (this->has_label)
-			throw ::std::logic_error("label can be set at most once");
-
-		int ret = ::gpiosim_bank_set_label(this->bank.get(),
-						   any_to_string(val).c_str());
-		if (ret)
-			throw ::std::system_error(errno, ::std::system_category(),
-						  "failed to set the chip label");
-
-		this->has_label = true;
-	}
-
-	void set_line_name(const ::std::any& val)
-	{
-		auto name = ::std::any_cast<line_name>(val);
-
-		int ret = ::gpiosim_bank_set_line_name(this->bank.get(),
-						       ::std::get<0>(name),
-						       ::std::get<1>(name).c_str());
-		if (ret)
-			throw ::std::system_error(errno, ::std::system_category(),
-						  "failed to set simulated line name");
-	}
-
-	void set_line_hog(const ::std::any& val)
-	{
-		auto hog = ::std::any_cast<line_hog>(val);
-
-		int ret = ::gpiosim_bank_hog_line(this->bank.get(),
-						  ::std::get<0>(hog),
-						  ::std::get<1>(hog).c_str(),
-						  hog_dir_mapping.at(::std::get<2>(hog)));
-		if (ret)
-			throw ::std::system_error(errno, ::std::system_category(),
-						  "failed to hog a simulated line");
-	}
-
 	dev_ptr dev;
 	bank_ptr bank;
-	bool has_num_lines;
-	bool has_label;
 };
 
-const ::std::map<chip::property,
-		 ::std::function<void (chip::impl&,
-				       const ::std::any&)>> chip::impl::setter_mapping = {
-	{ chip::property::NUM_LINES,	&chip::impl::set_num_lines },
-	{ chip::property::LABEL,	&chip::impl::set_label },
-	{ chip::property::LINE_NAME,	&chip::impl::set_line_name },
-	{ chip::property::HOG,		&chip::impl::set_line_hog }
-};
-
-chip::chip(const properties& args)
+chip::chip()
 	: _m_priv(new impl)
 {
-	int ret;
 
-	for (const auto& arg: args)
-		this->_m_priv.get()->set_property(arg.first, arg.second);
+}
+
+chip::chip(chip&& other)
+	: _m_priv(::std::move(other._m_priv))
+{
 
-	ret = ::gpiosim_dev_enable(this->_m_priv->dev.get());
-	if (ret)
-		throw ::std::system_error(errno, ::std::system_category(),
-					  "failed to enable the simulated GPIO chip");
 }
 
 chip::~chip()
 {
-	this->_m_priv.reset(nullptr);
+
+}
+
+chip& chip::operator=(chip&& other)
+{
+	this->_m_priv = ::std::move(other._m_priv);
+
+	return *this;
 }
 
 ::std::filesystem::path chip::dev_path() const
@@ -255,4 +154,124 @@ void chip::set_pull(unsigned int offset, pull pull)
 					  "failed to set the pull of simulated GPIO line");
 }
 
+struct chip_builder::impl
+{
+	impl()
+		: num_lines(0),
+		  label(),
+		  line_names(),
+		  hogs()
+	{
+
+	}
+
+	::std::size_t num_lines;
+	::std::string label;
+	::std::map<unsigned int, ::std::string> line_names;
+	::std::map<unsigned int, ::std::pair<::std::string, hog_direction>> hogs;
+};
+
+chip_builder::chip_builder()
+	: _m_priv(new impl)
+{
+
+}
+
+chip_builder::chip_builder(chip_builder&& other)
+	: _m_priv(::std::move(other._m_priv))
+{
+
+}
+
+chip_builder::~chip_builder()
+{
+
+}
+
+chip_builder& chip_builder::operator=(chip_builder&& other)
+{
+	this->_m_priv = ::std::move(other._m_priv);
+
+	return *this;
+}
+
+chip_builder& chip_builder::set_num_lines(::std::size_t num_lines)
+{
+	this->_m_priv->num_lines = num_lines;
+
+	return *this;
+}
+
+chip_builder& chip_builder::set_label(const ::std::string& label)
+{
+	this->_m_priv->label = label;
+
+	return *this;
+}
+
+chip_builder& chip_builder::set_line_name(unsigned int offset, const ::std::string& name)
+{
+	this->_m_priv->line_names[offset] = name;
+
+	return *this;
+}
+
+chip_builder& chip_builder::set_hog(unsigned int offset, const ::std::string& name, hog_direction direction)
+{
+	this->_m_priv->hogs[offset] = { name, direction };
+
+	return *this;
+}
+
+chip chip_builder::build()
+{
+	chip sim;
+	int ret;
+
+	if (this->_m_priv->num_lines) {
+		ret = ::gpiosim_bank_set_num_lines(sim._m_priv->bank.get(),
+						   this->_m_priv->num_lines);
+		if (ret)
+			throw ::std::system_error(errno, ::std::system_category(),
+						  "failed to set number of lines");
+	}
+
+	if (!this->_m_priv->label.empty()) {
+		ret = ::gpiosim_bank_set_label(sim._m_priv->bank.get(),
+					       this->_m_priv->label.c_str());
+		if (ret)
+			throw ::std::system_error(errno, ::std::system_category(),
+						  "failed to set the chip label");
+	}
+
+	for (const auto& name: this->_m_priv->line_names) {
+		ret = ::gpiosim_bank_set_line_name(sim._m_priv->bank.get(),
+						 name.first, name.second.c_str());
+		if (ret)
+			throw ::std::system_error(errno, ::std::system_category(),
+						  "failed to set the line name");
+	}
+
+	for (const auto& hog: this->_m_priv->hogs) {
+		ret = ::gpiosim_bank_hog_line(sim._m_priv->bank.get(), hog.first,
+					      hog.second.first.c_str(),
+					      hog_dir_mapping.at(hog.second.second));
+		if (ret)
+			throw ::std::system_error(errno, ::std::system_category(),
+						  "failed to hog the line");
+	}
+
+	ret = ::gpiosim_dev_enable(sim._m_priv->dev.get());
+	if (ret)
+		throw ::std::system_error(errno, ::std::system_category(),
+					  "failed to enable the simulated GPIO device");
+
+	return sim;
+}
+
+chip_builder make_sim()
+{
+	return chip_builder();
+}
+
 } /* namespace gpiosim */
bindings/cxx/tests/gpiosim.hpp +41 -24

105 lines not on this page · raw message

bindings/cxx/tests/tests-chip-info.cpp +15 -7
index a6bb123..717c387 100644
@@ -8,13 +8,17 @@
 #include "gpiosim.hpp"
 #include "helpers.hpp"
 
-using property = ::gpiosim::chip::property;
+using ::gpiosim::make_sim;
 
 namespace {
 
 TEST_CASE("chip_info properties can be read", "[chip-info][chip]")
 {
-	::gpiosim::chip sim({{ property::NUM_LINES, 8 }, { property::LABEL, "foobar" }});
+	auto sim = make_sim()
+		.set_num_lines(8)
+		.set_label("foobar")
+		.build();
+
 	::gpiod::chip chip(sim.dev_path());
 	auto info = chip.get_info();
 
@@ -36,7 +40,11 @@ TEST_CASE("chip_info properties can be read", "[chip-info][chip]")
 
 TEST_CASE("chip_info can be copied and moved", "[chip-info]")
 {
-	::gpiosim::chip sim({{ property::NUM_LINES, 4 }, { property::LABEL, "foobar" }});
+	auto sim = make_sim()
+		.set_num_lines(4)
+		.set_label("foobar")
+		.build();
+
 	::gpiod::chip chip(sim.dev_path());
 	auto info = chip.get_info();
 
@@ -91,10 +99,10 @@ TEST_CASE("chip_info can be copied and moved", "[chip-info]")
 
 TEST_CASE("stream insertion operator works for chip_info", "[chip-info]")
 {
-	::gpiosim::chip sim({
-		{ property::NUM_LINES, 4 },
-		{ property::LABEL, "foobar" }
-	});
+	auto sim = make_sim()
+		.set_num_lines(4)
+		.set_label("foobar")
+		.build();
 
 	::gpiod::chip chip(sim.dev_path());
 	auto info = chip.get_info();
bindings/cxx/tests/tests-chip.cpp +37 -26

121 lines not on this page · raw message

bindings/cxx/tests/tests-edge-event.cpp +17 -8
index 04eb57b..ca42c42 100644
@@ -11,7 +11,7 @@
 #include "gpiosim.hpp"
 #include "helpers.hpp"
 
-using simprop = ::gpiosim::chip::property;
+using ::gpiosim::make_sim;
 using direction = ::gpiod::line::direction;
 using edge = ::gpiod::line::edge;
 using offsets = ::gpiod::line::offsets;
@@ -40,7 +40,7 @@ TEST_CASE("edge_event_buffer capacity settings work", "[edge-event]")
 
 TEST_CASE("edge_event wait timeout", "[edge-event]")
 {
-	::gpiosim::chip sim;
+	auto sim = make_sim().build();
 	::gpiod::chip chip(sim.dev_path());
 
 	auto request = chip.prepare_request()
@@ -56,7 +56,7 @@ TEST_CASE("edge_event wait timeout", "[edge-event]")
 
 TEST_CASE("output mode and edge detection don't work together", "[edge-event]")
 {
-	::gpiosim::chip sim;
+	auto sim = make_sim().build();
 
 	REQUIRE_THROWS_AS(
 		::gpiod::chip(sim.dev_path())
@@ -91,7 +91,10 @@ void trigger_rising_edge_events_on_two_offsets(::gpiosim::chip& sim,
 
 TEST_CASE("waiting for and reading edge events works", "[edge-event]")
 {
-	::gpiosim::chip sim({{ simprop::NUM_LINES, 8 }});
+	auto sim = make_sim()
+		.set_num_lines(8)
+		.build();
+
 	::gpiod::chip chip(sim.dev_path());
 	::gpiod::edge_event_buffer buffer;
 
@@ -220,7 +223,10 @@ TEST_CASE("waiting for and reading edge events works", "[edge-event]")
 
 TEST_CASE("reading multiple events", "[edge-event]")
 {
-	::gpiosim::chip sim({{ simprop::NUM_LINES, 8 }});
+	auto sim = make_sim()
+		.set_num_lines(8)
+		.build();
+
 	::gpiod::chip chip(sim.dev_path());
 
 	auto request = chip
@@ -268,7 +274,10 @@ TEST_CASE("reading multiple events", "[edge-event]")
 
 TEST_CASE("edge_event_buffer can be moved", "[edge-event]")
 {
-	::gpiosim::chip sim({{ simprop::NUM_LINES, 2 }});
+	auto sim = make_sim()
+		.set_num_lines(2)
+		.build();
+
 	::gpiod::chip chip(sim.dev_path());
 	::gpiod::edge_event_buffer buffer(13);
 
@@ -313,7 +322,7 @@ TEST_CASE("edge_event_buffer can be moved", "[edge-event]")
 
 TEST_CASE("edge_event can be copied and moved", "[edge-event]")
 {
-	::gpiosim::chip sim;
+	auto sim = make_sim().build();
 	::gpiod::chip chip(sim.dev_path());
 	::gpiod::edge_event_buffer buffer;
 
@@ -378,7 +387,7 @@ TEST_CASE("stream insertion operators work for edge_event and edge_event_buffer"
 	 * edge_event_buffer classes.
 	 */
 
-	::gpiosim::chip sim;
+	auto sim = make_sim().build();
 	::gpiod::chip chip(sim.dev_path());
 	::gpiod::edge_event_buffer buffer;
 	::std::stringstream sbuf, expected;
bindings/cxx/tests/tests-info-event.cpp +7 -4
index 788da8f..2ec5176 100644
@@ -11,7 +11,7 @@
 #include "gpiosim.hpp"
 #include "helpers.hpp"
 
-using simprop = ::gpiosim::chip::property;
+using ::gpiosim::make_sim;
 using direction = ::gpiod::line::direction;
 using event_type = ::gpiod::info_event::event_type;
 
@@ -44,7 +44,10 @@ void request_reconfigure_release_line(::gpiod::chip& chip)
 
 TEST_CASE("Lines can be watched", "[info-event][chip]")
 {
-	::gpiosim::chip sim({{ simprop::NUM_LINES, 8 }});
+	auto sim = make_sim()
+		.set_num_lines(8)
+		.build();
+
 	::gpiod::chip chip(sim.dev_path());
 
 	SECTION("watch_line_info() returns line info")
@@ -102,7 +105,7 @@ TEST_CASE("Lines can be watched", "[info-event][chip]")
 
 TEST_CASE("info_event can be copied and moved", "[info-event]")
 {
-	::gpiosim::chip sim;
+	auto sim = make_sim().build();
 	::gpiod::chip chip(sim.dev_path());
 	::std::stringstream buf, expected;
 
@@ -167,7 +170,7 @@ TEST_CASE("info_event stream insertion operator works", "[info-event][line-info]
 	 * and line_info classes.
 	 */
 
-	::gpiosim::chip sim;
+	auto sim = make_sim().build();
 	::gpiod::chip chip(sim.dev_path());
 	::std::stringstream buf, expected;
 
bindings/cxx/tests/tests-line-info.cpp +24 -26

80 lines not on this page · raw message

bindings/cxx/tests/tests-line-request.cpp +32 -13

109 lines not on this page · raw message

bindings/cxx/tests/tests-misc.cpp +3 -3
index 7f6bf29..fe8cdea 100644
@@ -11,7 +11,7 @@
 #include "gpiosim.hpp"
 #include "helpers.hpp"
 
-using property = ::gpiosim::chip::property;
+using ::gpiosim::make_sim;
 
 namespace {
 
@@ -48,14 +48,14 @@ TEST_CASE("is_gpiochip_device() works", "[misc][chip]")
 
 	SECTION("is_gpiochip_device() returns true for a GPIO chip")
 	{
-		::gpiosim::chip sim;
+		auto sim = make_sim().build();
 
 		REQUIRE(::gpiod::is_gpiochip_device(sim.dev_path()));
 	}
 
 	SECTION("is_gpiochip_device() can resolve a symlink")
 	{
-		::gpiosim::chip sim;
+		auto sim = make_sim().build();
 		::std::string link("/tmp/gpiod-cxx-tmp-link.");
 
 		link += ::std::to_string(::getpid());
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · Bartosz Golaszewski

In order to make the interface more elegant use the Rust-like builder pattern for the GPIO simulator class.

---

Was there a v1 for this patch?

[snip]

Quoted 11 lines
bindings/cxx/tests/tests-chip-info.cpp +1 -1
index a6bb123..717c387 100644
@@ -8,13 +8,17 @@
 #include "gpiosim.hpp"
 #include "helpers.hpp"
 
-using property = ::gpiosim::chip::property;
+using ::gpiosim::make_sim;
 

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

Quoted 10 lines
bindings/cxx/tests/tests-chip-info.cpp +5 -1
 namespace {
 
 TEST_CASE("chip_info properties can be read", "[chip-info][chip]")
 {
-	::gpiosim::chip sim({{ property::NUM_LINES, 8 }, { property::LABEL, "foobar" }});
+	auto sim = make_sim()
+		.set_num_lines(8)
+		.set_label("foobar")
+		.build();
+

You do this a few times, so perhaps add a helper to create a simple simulator with a given a number of lines?

(I called the equivalent in my tests a Simpleton, borrowing LinusW's practice of naming things after yourself.
Not that I'm suggesting you use that name.)

And rename make_sim() to make_sim_builder(), as it the build() that returns the sim?

Cheers,
Kent.

#2 lore raw
Quoted 17 lines · Kent Gibson

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

Quoted 12 lines · Bartosz Golaszewski

In order to make the interface more elegant use the Rust-like builder pattern for the GPIO simulator class.

---

bindings/cxx/tests/tests-chip-info.cpp +5 -1
-	::gpiosim::chip sim({{ property::NUM_LINES, 8 }, { property::LABEL, "foobar" }});
+	auto sim = make_sim()
+		.set_num_lines(8)
+		.set_label("foobar")
+		.build();
+

You do this a few times, so perhaps add a helper to create a simple simulator with a given a number of lines?

Quoted 3 lines

(I called the equivalent in my tests a Simpleton, borrowing LinusW's practice of naming things after yourself.
Not that I'm suggesting you use that name.)

Good grief - I meant LinusT. See what I mean?

Cheers,
Kent.

#3 lore raw
Quoted 16 lines · Kent Gibson
Quoted 14 lines · Kent Gibson
Quoted 12 lines · Bartosz Golaszewski

In order to make the interface more elegant use the Rust-like builder pattern for the GPIO simulator class.

---

bindings/cxx/tests/tests-chip-info.cpp +5 -1
-   ::gpiosim::chip sim({{ property::NUM_LINES, 8 }, { property::LABEL, "foobar" }});
+   auto sim = make_sim()
+           .set_num_lines(8)
+           .set_label("foobar")
+           .build();
+
Quoted 9 lines
Quoted 6 lines

You do this a few times, so perhaps add a helper to create a simple simulator with a given a number of lines?

(I called the equivalent in my tests a Simpleton, borrowing LinusW's practice of naming things after yourself.
Not that I'm suggesting you use that name.)

Good grief - I meant LinusT. See what I mean?

Yeah, I was going to say that Linus W - being the nicest person ever - would have never called anyone a Simpleton. :)

And as for the Simpleton itself - I may add it in the future but for now I think I'll just keep this version - it's brief and clear.

Bart