Skip to content

Commit

Permalink
Add std::span to tests (currently not supported)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiusTheBest committed Oct 5, 2023
1 parent f45f5ca commit f8e62e9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
68 changes: 47 additions & 21 deletions test/StdContainers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,96 +8,106 @@
#include <map>
#include <string>

SCENARIO("std containers")
#ifdef __has_include
# if __has_include(<version>)
# include <version>
# endif
#endif

#ifdef __cpp_lib_span
# include <span>
#endif

SCENARIO("std containers")
{
GIVEN("logger is initialised")
GIVEN("logger is initialised")
{
plog::TestAppender testAppender;
plog::Logger<PLOG_DEFAULT_INSTANCE_ID> logger(plog::verbose);
logger.addAppender(&testAppender);

WHEN("empty collection")
WHEN("empty collection")
{
std::vector<int> vectorOfInts;
PLOGI << vectorOfInts;

THEN("the result is as expected")
THEN("the result is as expected")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[]"));
}
}

WHEN("std::vector")
WHEN("std::vector")
{
std::vector<int> vectorOfInts;
vectorOfInts.push_back(1);
vectorOfInts.push_back(2);
vectorOfInts.push_back(3);
PLOGI << vectorOfInts;

THEN("the result is as expected")
THEN("the result is as expected")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[1, 2, 3]"));
}
}

WHEN("std::deque")
WHEN("std::deque")
{
std::deque<std::string> dequeOfStrings;
dequeOfStrings.push_back("one");
dequeOfStrings.push_back("two");
dequeOfStrings.push_back("three");
PLOGI << dequeOfStrings;

THEN("the result is as expected")
THEN("the result is as expected")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[one, two, three]"));
}
}

WHEN("std::list")
WHEN("std::list")
{
std::list<const char*> listOfCharPointers;
listOfCharPointers.push_back("one");
listOfCharPointers.push_back("two");
listOfCharPointers.push_back(NULL);
PLOGI << listOfCharPointers;

THEN("the result is as expected")
THEN("the result is as expected")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[one, two, (null)]"));
}
}

WHEN("std::set")
WHEN("std::set")
{
std::set<int> setOfInts;
setOfInts.insert(10);
setOfInts.insert(20);
setOfInts.insert(30);
PLOGI << setOfInts;

THEN("the result is as expected")
THEN("the result is as expected")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[10, 20, 30]"));
}
}

WHEN("std::map")
WHEN("std::map")
{
std::map<std::string, int> mapStringToInt;
mapStringToInt["red"] = 1;
mapStringToInt["green"] = 2;
mapStringToInt["blue"] = 4;
PLOGI << mapStringToInt;

THEN("the result is as expected")
THEN("the result is as expected")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[blue:4, green:2, red:1]"));
}
}

WHEN("std::multimap")
WHEN("std::multimap")
{
std::multimap<int, std::string> multimapIntToString;
multimapIntToString.insert(std::make_pair(1, "one"));
Expand All @@ -106,13 +116,13 @@ SCENARIO("std containers")
multimapIntToString.insert(std::make_pair(2, "due"));
PLOGI << multimapIntToString;

THEN("the result is as expected")
THEN("the result is as expected")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[1:one, 1:uno, 2:two, 2:due]"));
}
}

WHEN("std::vector of std::vector")
WHEN("std::vector of std::vector")
{
std::vector<std::vector<int> > vectorOfVectorsOfInts(3);
vectorOfVectorsOfInts[0].push_back(1);
Expand All @@ -121,21 +131,37 @@ SCENARIO("std containers")
vectorOfVectorsOfInts[1].push_back(-2);
PLOGI << vectorOfVectorsOfInts;

THEN("the result is as expected")
THEN("the result is as expected")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[[1, 2], [-1, -2], []]"));
}
}

WHEN("std::pair")
WHEN("std::pair")
{
std::pair<int, int> pairOfInts(5, 10);
PLOGI << pairOfInts;

THEN("the result is as expected")
THEN("the result is as expected")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("5:10"));
}
}

#if 0 // std::span is not supported yet as it has no const_iterator till c++23
#ifdef __cpp_lib_span
WHEN("std::span")
{
int arr[] = {1, 2, 3};
std::span<int> spanOfInts(std::begin(arr), std::end(arr));
PLOGI << spanOfInts;

THEN("the result is as expected")
{
CHECK_EQ(testAppender.getMessage(), PLOG_NSTR("[1, 2, 3]"));
}
}
#endif
#endif
}
}
}
8 changes: 7 additions & 1 deletion test/StringTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
#include <plog/Log.h>
#include "TestAppender.h"

#ifdef __has_include
# if __has_include(<version>)
# include <version>
# endif
#endif

#ifdef __cpp_lib_string_view
#include <string_view>
# include <string_view>
#endif

SCENARIO("string types")
Expand Down

0 comments on commit f8e62e9

Please sign in to comment.