Skip to content

Commit

Permalink
Add formatting for array.
Browse files Browse the repository at this point in the history
  • Loading branch information
lhruby committed Sep 24, 2024
1 parent e21ebd6 commit d2794ff
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
7 changes: 5 additions & 2 deletions modules/types/include/hephaestus/types/type_formatting.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
namespace heph::types {

//=================================================================================================
// Vector
// Array and Vector
//=================================================================================================
template <VectorType T>
template <typename T>
concept ArrayOrVectorType = ArrayType<T> || VectorType<T>;

template <ArrayOrVectorType T>
[[nodiscard]] inline auto toString(const T& vec) -> std::string {
std::stringstream ss;

Expand Down
37 changes: 37 additions & 0 deletions modules/types/tests/type_formatting_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//=================================================================================================

#include <algorithm>
#include <array>
#include <chrono>
#include <cstdint>
#include <string>
Expand All @@ -18,6 +19,42 @@ using namespace ::testing; // NOLINT(google-build-using-namespace)

namespace heph::types::tests {

//=================================================================================================
// Array
//=================================================================================================

TEST(TypeFormattingTests, ConvertEmptyArray) {
const std::array<int, 0> arr = {};
const std::string result = toString(arr);
EXPECT_EQ(result, "");
}

TEST(TypeFormattingTests, ConvertIntArray) {
const std::array<int, 3> arr = { 1, 2, 3 };
const std::string result = toString(arr);
const std::string expected = " Index: 0, Value: 1\n"
" Index: 1, Value: 2\n"
" Index: 2, Value: 3\n";
EXPECT_EQ(result, expected);
}

TEST(TypeFormattingTests, ConvertDoubleArray) {
const std::array<double, 3> arr = { 1.1, 2.2, 3.3 }; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
const std::string result = toString(arr);
const std::string expected = " Index: 0, Value: 1.1\n"
" Index: 1, Value: 2.2\n"
" Index: 2, Value: 3.3\n";
EXPECT_EQ(result, expected);
}

TEST(TypeFormattingTests, ConvertStringArray) {
const std::array<std::string, 3> arr = { "one", "two", "three" };
const std::string result = toString(arr);
const std::string expected = " Index: 0, Value: one\n"
" Index: 1, Value: two\n"
" Index: 2, Value: three\n";
EXPECT_EQ(result, expected);
}
//=================================================================================================
// Vector
//=================================================================================================
Expand Down

0 comments on commit d2794ff

Please sign in to comment.