initial commit, copy everything to new repository

This commit is contained in:
ado
2020-12-10 19:26:56 +01:00
parent 2c0a35acf9
commit 9d45ff7b62
12 changed files with 8334 additions and 0 deletions

6205
test/doctest.h Normal file

File diff suppressed because it is too large Load Diff

26
test/makefile Normal file
View File

@@ -0,0 +1,26 @@
CXX=clang++
CXXFLAGS=-Wall -Wextra -std=c++17 -lstdc++fs
TESTS=test_parser test_converter test_extractions
all: $(TESTS)
# pattern rule, replacing built-in implicit .cpp-suffix rule
%: %.cpp
$(CXX) $(CXXFLAGS) $< -o $@
debug: CXXFLAGS += -g
debug: all
clean:
@$(RM) -fv $(TESTS)
@$(RM) *.csv
test:
@for i in $(TESTS); do \
./$$i; \
done
# don't use any implicit rules
.SUFFIXES:
# these rules won't actually build the targets they're named after
.PHONY: all clean run debug

288
test/test_converter.cpp Normal file
View File

@@ -0,0 +1,288 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "../include/ss/converter.hpp"
#include "doctest.h"
#include <algorithm>
TEST_CASE("testing split") {
ss::converter c;
for (const auto& [s, expected, delim] :
// clang-format off
{std::tuple{"a,b,c,d", std::vector{"a", "b", "c", "d"}, ","},
{"", {}, " "},
{"a,b,c", {"a", "b", "c"}, ""},
{" x x x x | x ", {" x x x x ", " x "}, "|"},
{"a::b::c::d", {"a", "b", "c", "d"}, "::"},
{"x\t-\ty", {"x", "y"}, "\t-\t"},
{"x", {"x"}, ","}} // clang-format on
) {
auto split = c.split(s, delim);
CHECK(split.size() == expected.size());
for (size_t i = 0; i < split.size(); ++i) {
auto s = std::string(split[i].first, split[i].second);
CHECK(s == expected[i]);
}
}
}
TEST_CASE("testing valid conversions") {
ss::converter c;
{
auto tup = c.convert<int>("5");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<int, void>("5,junk");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<void, int>("junk,5");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<int, void, void>("5\njunk\njunk", "\n");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<void, int, void>("junk 5 junk", " ");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<void, void, int>("junk\tjunk\t5", "\t");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<int, double, void>("5,6.6,junk");
REQUIRE(c.valid());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup = c.convert<int, void, double>("5,junk,6.6");
REQUIRE(c.valid());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup = c.convert<void, int, double>("junk;5;6.6", ";");
REQUIRE(c.valid());
CHECK(tup == std::tuple{5, 6.6});
}
}
TEST_CASE("testing invalid conversions") {
ss::converter c;
c.convert<int>("");
REQUIRE(!c.valid());
c.convert<int, void>("");
REQUIRE(!c.valid());
c.convert<int, void>(",junk");
REQUIRE(!c.valid());
c.convert<void, int>("junk,");
REQUIRE(!c.valid());
c.convert<int>("x");
REQUIRE(!c.valid());
c.convert<int, void>("x");
REQUIRE(!c.valid());
c.convert<int, void>("x,junk");
REQUIRE(!c.valid());
c.convert<void, int>("junk,x");
REQUIRE(!c.valid());
}
TEST_CASE("testing ss:ax restriction (all except)") {
ss::converter c;
c.convert<ss::ax<int, 0>>("0");
REQUIRE(!c.valid());
c.convert<ss::ax<int, 0, 1, 2>>("1");
REQUIRE(!c.valid());
c.convert<void, char, ss::ax<int, 0, 1, 2>>("junk,c,1");
REQUIRE(!c.valid());
c.convert<ss::ax<int, 1>, char>("1,c");
REQUIRE(!c.valid());
{
int tup = c.convert<ss::ax<int, 1>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
std::tuple<char, int> tup =
c.convert<char, ss::ax<int, 1>>("c,3");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 3});
}
{
std::tuple<int, char> tup =
c.convert<ss::ax<int, 1>, char>("3,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{3, 'c'});
}
}
TEST_CASE("testing ss:nx restriction (none except)") {
ss::converter c;
c.convert<ss::nx<int, 1>>("3");
REQUIRE(!c.valid());
c.convert<char, ss::nx<int, 1, 2, 69>>("c,3");
REQUIRE(!c.valid());
c.convert<ss::nx<int, 1>, char>("3,c");
REQUIRE(!c.valid());
{
auto tup = c.convert<ss::nx<int, 3>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
auto tup = c.convert<ss::nx<int, 0, 1, 2>>("2");
REQUIRE(c.valid());
CHECK(tup == 2);
}
{
auto tup =
c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 1});
}
{
auto tup = c.convert<ss::nx<int, 1>, char>("1,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{1, 'c'});
}
}
TEST_CASE("testing ss:ir restriction (in range)") {
ss::converter c;
c.convert<ss::ir<int, 0, 2>>("3");
REQUIRE(!c.valid());
c.convert<char, ss::ir<int, 4, 69>>("c,3");
REQUIRE(!c.valid());
c.convert<ss::ir<int, 1, 2>, char>("3,c");
REQUIRE(!c.valid());
{
auto tup = c.convert<ss::ir<int, 1, 5>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
auto tup = c.convert<ss::ir<int, 0, 2>>("2");
REQUIRE(c.valid());
CHECK(tup == 2);
}
{
auto tup = c.convert<char, void, ss::ir<int, 0, 1>>("c,junk,1");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 1});
}
{
auto tup = c.convert<ss::ir<int, 1, 20>, char>("1,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{1, 'c'});
}
}
TEST_CASE("testing ss:oor restriction (out of range)") {
ss::converter c;
c.convert<ss::oor<int, 1, 5>>("3");
REQUIRE(!c.valid());
c.convert<ss::oor<int, 0, 2>>("2");
REQUIRE(!c.valid());
c.convert<char, ss::oor<int, 0, 1>, void>("c,1,junk");
REQUIRE(!c.valid());
c.convert<ss::oor<int, 1, 20>, char>("1,c");
REQUIRE(!c.valid());
{
auto tup = c.convert<ss::oor<int, 0, 2>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
auto tup =
c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 3});
}
{
auto tup = c.convert<ss::oor<int, 1, 2>, char>("3,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{3, 'c'});
}
}
const std::vector<int> extracted_vector = {1, 2, 3};
// custom extract
template <>
inline bool ss::extract(const char* begin, const char* end,
std::vector<int>& value) {
if (begin == end) {
return false;
}
value = extracted_vector;
return true;
}
TEST_CASE("testing ss:ne restriction (not empty)") {
ss::converter c;
c.convert<ss::ne<std::string>>("");
REQUIRE(!c.valid());
c.convert<int, ss::ne<std::string>>("3,");
REQUIRE(!c.valid());
c.convert<ss::ne<std::string>, int>(",3");
REQUIRE(!c.valid());
c.convert<void, ss::ne<std::string>, int>("junk,,3");
REQUIRE(!c.valid());
c.convert<ss::ne<std::vector<int>>>("");
REQUIRE(!c.valid());
{
auto tup = c.convert<ss::ne<std::string>>("s");
REQUIRE(c.valid());
CHECK(tup == "s");
}
{
auto tup = c.convert<int, ss::ne<std::string>>("1,s");
REQUIRE(c.valid());
CHECK(tup == std::tuple{1, "s"});
}
{
auto tup = c.convert<ss::ne<std::vector<int>>>("{1 2 3}");
REQUIRE(c.valid());
CHECK(tup == extracted_vector);
}
}

177
test/test_extractions.cpp Normal file
View File

@@ -0,0 +1,177 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "../include/ss/extract.hpp"
#include "doctest.h"
#include <algorithm>
constexpr auto eps = 0.000001;
using ld = long double;
#define CHECK_FLOATING_CONVERSION(input, type) \
{ \
std::string s = #input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(std::abs(t.value() - type(input)) < eps); \
} \
{ \
/* check negative too */ \
auto s = std::string("-") + #input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(std::abs(t.value() - type(-input)) < eps); \
}
TEST_CASE("testing extract functions for floating point values") {
CHECK_FLOATING_CONVERSION(123.456, float);
CHECK_FLOATING_CONVERSION(123.456, double);
CHECK_FLOATING_CONVERSION(123.456, ld);
CHECK_FLOATING_CONVERSION(69, float);
CHECK_FLOATING_CONVERSION(69, double);
CHECK_FLOATING_CONVERSION(69, ld);
CHECK_FLOATING_CONVERSION(420., float);
CHECK_FLOATING_CONVERSION(420., double);
CHECK_FLOATING_CONVERSION(420., ld);
CHECK_FLOATING_CONVERSION(0.123, float);
CHECK_FLOATING_CONVERSION(0.123, double);
CHECK_FLOATING_CONVERSION(0.123, ld);
CHECK_FLOATING_CONVERSION(123e4, float);
CHECK_FLOATING_CONVERSION(123e4, double);
CHECK_FLOATING_CONVERSION(123e4, ld);
}
#define CHECK_DECIMAL_CONVERSION(input, type) \
{ \
std::string s = #input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(t.value() == type(input)); \
} \
{ \
/* check negative too */ \
if (std::is_signed_v<type>) { \
auto s = std::string("-") + #input; \
auto t = \
ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(t.value() == type(-input)); \
} \
}
using us = unsigned short;
using ui = unsigned int;
using ul = unsigned long;
using ll = long long;
using ull = unsigned long long;
TEST_CASE("testing extract functions for decimal values") {
CHECK_DECIMAL_CONVERSION(1234, short);
CHECK_DECIMAL_CONVERSION(1234, us);
CHECK_DECIMAL_CONVERSION(1234, int);
CHECK_DECIMAL_CONVERSION(1234, ui);
CHECK_DECIMAL_CONVERSION(1234, long);
CHECK_DECIMAL_CONVERSION(1234, ul);
CHECK_DECIMAL_CONVERSION(1234, ll);
CHECK_DECIMAL_CONVERSION(1234567891011, ull);
}
#define CHECK_INVALID_CONVERSION(input, type) \
{ \
std::string s = input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
}
TEST_CASE("testing extract functions for numbers with invalid inputs") {
// negative unsigned value
CHECK_INVALID_CONVERSION("-1234", ul);
// floating pint for int
CHECK_INVALID_CONVERSION("123.4", int);
// random input for float
CHECK_INVALID_CONVERSION("xxx1", float);
// random input for int
CHECK_INVALID_CONVERSION("xxx1", int);
// empty field for int
CHECK_INVALID_CONVERSION("", int);
}
#define CHECK_OUT_OF_RANGE_CONVERSION(type) \
{ \
std::string s = \
std::to_string(std::numeric_limits<type>::max()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(t.has_value()); \
for (auto& i : s) { \
if (i != '9' && i != '.') { \
i = '9'; \
break; \
} \
} \
t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
} \
{ \
std::string s = \
std::to_string(std::numeric_limits<type>::min()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(t.has_value()); \
for (auto& i : s) { \
if (std::is_signed_v<type> && i != '9' && i != '.') { \
i = '9'; \
break; \
} else if (std::is_unsigned_v<type>) { \
s = "-1"; \
break; \
} \
} \
t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
}
TEST_CASE("testing extract functions for numbers with out of range inputs") {
CHECK_OUT_OF_RANGE_CONVERSION(short);
CHECK_OUT_OF_RANGE_CONVERSION(us);
CHECK_OUT_OF_RANGE_CONVERSION(int);
CHECK_OUT_OF_RANGE_CONVERSION(ui);
CHECK_OUT_OF_RANGE_CONVERSION(long);
CHECK_OUT_OF_RANGE_CONVERSION(ul);
CHECK_OUT_OF_RANGE_CONVERSION(ll);
CHECK_OUT_OF_RANGE_CONVERSION(ull);
}
TEST_CASE("testing extract functions for boolean values") {
for (const auto& [b, s] : {std::pair<bool, std::string>{true, "1"},
{false, "0"},
{true, "true"},
{false, "false"}}) {
bool v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
CHECK(v == b);
}
for (const std::string& s : {"2", "tru", "truee", "xxx", ""}) {
bool v;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
}
}
TEST_CASE("testing extract functions for char values") {
for (const auto& [c, s] :
{std::pair<char, std::string>{'a', "a"}, {'x', "x"}, {' ', " "}}) {
char v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
CHECK(v == c);
}
for (const std::string& s : {"aa", "xxx", ""}) {
char v;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
}
}

234
test/test_parser.cpp Normal file
View File

@@ -0,0 +1,234 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "../include/ss/parser.hpp"
#include "doctest.h"
#include <algorithm>
#include <filesystem>
struct unique_file_name {
const std::string name;
unique_file_name() : name{std::tmpnam(nullptr)} {
}
~unique_file_name() {
std::filesystem::remove(name);
}
};
struct X {
constexpr static auto delim = ",";
int i;
double d;
std::string s;
std::string to_string() const {
return std::to_string(i)
.append(delim)
.append(std::to_string(d))
.append(delim)
.append(s);
}
auto tied() const {
return std::tie(i, d, s);
}
};
template <typename T>
std::enable_if_t<ss::has_m_tied_t<T>, bool> operator==(const T& lhs,
const T& rhs) {
return lhs.tied() == rhs.tied();
}
template <typename T>
static void make_and_write(const std::string& file_name,
const std::vector<T>& data) {
std::ofstream out{file_name};
for (const auto& i : data) {
out << i.to_string() << std::endl;
}
}
TEST_CASE("testing parser") {
unique_file_name f;
std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"}};
make_and_write(f.name, data);
{
ss::parser p{f.name, ","};
std::vector<X> i;
while (!p.eof()) {
auto a = p.get_next<int, double, std::string>();
i.emplace_back(ss::to_struct<X>(a));
}
CHECK(std::equal(i.begin(), i.end(), data.begin()));
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
p.ignore_next();
while (!p.eof()) {
auto a = p.get_next<int, double, std::string>();
i.emplace_back(ss::to_struct<X>(a));
}
CHECK(std::equal(i.begin(), i.end(), data.begin() + 1));
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
while (!p.eof()) {
i.push_back(
p.get_struct<X, int, double, std::string>());
}
CHECK(std::equal(i.begin(), i.end(), data.begin()));
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
while (!p.eof()) {
i.push_back(p.get_next<X>());
}
CHECK(std::equal(i.begin(), i.end(), data.begin()));
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
while (!p.eof()) {
auto a = p.get_struct<X, ss::ax<int, 3>, double,
std::string>();
if (p.valid()) {
i.push_back(a);
}
}
std::vector<X> expected = {{1, 2, "x"}, {5, 6, "z"}};
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
while (!p.eof()) {
auto a = p.get_struct<X, ss::nx<int, 3>, double,
std::string>();
if (p.valid()) {
i.push_back(a);
}
}
std::vector<X> expected = {{3, 4, "y"}};
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
}
{
unique_file_name empty_f;
std::vector<X> empty_data = {};
make_and_write(empty_f.name, empty_data);
ss::parser p{empty_f.name, ","};
std::vector<X> i;
while (!p.eof()) {
i.push_back(p.get_next<X>());
}
CHECK(i.empty());
}
}
size_t move_called = 0;
size_t copy_called = 0;
struct my_string {
char* data{nullptr};
my_string() = default;
~my_string() {
delete[] data;
}
my_string(const my_string&) {
copy_called++;
}
my_string(my_string&& other) : data{other.data} {
move_called++;
other.data = nullptr;
}
};
template <>
inline bool ss::extract(const char* begin, const char* end, my_string& s) {
size_t size = end - begin;
s.data = new char[size + 1];
strncpy(s.data, begin, size);
s.data[size] = '\0';
return true;
}
struct Y {
my_string x;
my_string y;
my_string z;
auto tied() {
return std::tie(x, y, z);
}
};
TEST_CASE("testing the moving of parsed values") {
size_t move_called_one_col;
{
unique_file_name f;
std::ofstream out{f.name};
out << "x" << std::endl;
ss::parser p{f.name, ","};
auto x = p.get_next<my_string>();
CHECK(copy_called == 0);
CHECK(move_called < 3);
move_called_one_col = move_called;
move_called = copy_called = 0;
}
unique_file_name f;
{
std::ofstream out{f.name};
out << "a,b,c" << std::endl;
}
{
ss::parser p{f.name, ","};
auto x = p.get_next<my_string, my_string, my_string>();
CHECK(copy_called == 0);
CHECK(move_called == 3 * move_called_one_col);
move_called = copy_called = 0;
}
{
ss::parser p{f.name, ","};
auto x = p.get_struct<Y, my_string, my_string, my_string>();
CHECK(copy_called == 0);
CHECK(move_called == 6 * move_called_one_col);
move_called = copy_called = 0;
}
{
ss::parser p{f.name, ","};
auto x = p.get_next<Y>();
CHECK(copy_called == 0);
CHECK(move_called == 6 * move_called_one_col);
move_called = copy_called = 0;
}
}