pseq: Add format_utils namespace
Add a namespace containing utility functions for formatting data.
The initial utility function converts a span of container elements into
a string.
This function is a temporary solution until g++ implements the C++23
std::format() support for formatting ranges.
fmt::format() supports formatting spans, but the OpenBMC project is
converting to use of std::format() instead.
Tested:
* Ran all new and existing gtests.
Change-Id: Ie84b59db35bb49e3f1bb529490aaa05af434b6fd
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
diff --git a/phosphor-power-sequencer/test/format_utils_tests.cpp b/phosphor-power-sequencer/test/format_utils_tests.cpp
new file mode 100644
index 0000000..794630f
--- /dev/null
+++ b/phosphor-power-sequencer/test/format_utils_tests.cpp
@@ -0,0 +1,62 @@
+/**
+ * Copyright © 2024 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "format_utils.hpp"
+
+#include <array>
+#include <span>
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::sequencer::format_utils;
+
+TEST(FormatUtilsTests, toString)
+{
+ // Test with spans from integer vector
+ {
+ std::vector<int> vec{1, 3, 5, 7, 9, 11};
+ std::span<int> spn{vec};
+ EXPECT_EQ(toString(spn), "[1, 3, 5, 7, 9, 11]");
+ EXPECT_EQ(toString(spn.subspan(0, 4)), "[1, 3, 5, 7]");
+ EXPECT_EQ(toString(spn.subspan(3, 3)), "[7, 9, 11]");
+ EXPECT_EQ(toString(spn.subspan(5, 1)), "[11]");
+ EXPECT_EQ(toString(spn.subspan(0, 0)), "[]");
+ }
+
+ // Test with spans from double vector
+ {
+ std::vector<double> vec{2.1, -3.9, 21.03};
+ std::span<double> spn{vec};
+ EXPECT_EQ(toString(spn), "[2.1, -3.9, 21.03]");
+ }
+
+ // Test with span from empty vector
+ {
+ std::vector<int> vec{};
+ std::span<int> spn{vec};
+ EXPECT_EQ(toString(spn), "[]");
+ }
+
+ // Test with spans from string array
+ {
+ std::array<std::string, 3> ary{"cow", "horse", "zebra"};
+ std::span<std::string> spn{ary};
+ EXPECT_EQ(toString(spn), "[cow, horse, zebra]");
+ EXPECT_EQ(toString(spn.subspan(1, 2)), "[horse, zebra]");
+ }
+}
diff --git a/phosphor-power-sequencer/test/meson.build b/phosphor-power-sequencer/test/meson.build
index 6bce451..9d1547b 100644
--- a/phosphor-power-sequencer/test/meson.build
+++ b/phosphor-power-sequencer/test/meson.build
@@ -2,6 +2,7 @@
executable('phosphor-power-sequencer-tests',
'config_file_parser_error_tests.cpp',
'config_file_parser_tests.cpp',
+ 'format_utils_tests.cpp',
'rail_tests.cpp',
dependencies: [
gmock,