libcruft-util/test/string.cpp

37 lines
1.1 KiB
C++
Raw Normal View History

2016-04-05 11:06:01 +10:00
#include "tap.hpp"
#include "string.hpp"
#include "types.hpp"
2017-09-15 15:22:29 +10:00
#include "iterator.hpp"
#include <vector>
2016-03-17 18:13:19 +11:00
int
main (int, char**)
{
util::TAP::logger tap;
// the string_literal prefix is required to (easily) construct a string
// with an internal null character.
using namespace std::literals::string_literals;
const std::string csv = "\0,a,123,,this is a test,"s;
2017-09-15 15:22:29 +10:00
// expected test data must be a std::string so we can check embedded
// nulls (which are ambiguous when using a cstr).
struct foo {
const std::string value;
const char *message;
} TESTS[] = {
2017-11-23 17:24:11 +11:00
{ "\0"s, "null" },
2017-09-15 15:22:29 +10:00
{ "a", "single letter" },
{ "123", "three digits" },
{ "", "empty string" },
{ "this is a test", "string with spaces" },
{ "", "trailing empty" }
2016-03-17 18:13:19 +11:00
};
2017-09-15 15:22:29 +10:00
for (const auto &[tok, expected]: util::zip (util::make_tokeniser (csv, ','), TESTS))
tap.expect_eq (tok, expected.value, "%s", expected.message);
2016-03-17 18:13:19 +11:00
return tap.status ();
}