2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/tap.hpp>
|
|
|
|
#include <cruft/util/view.hpp>
|
2017-12-20 12:31:52 +11:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-03-17 18:12:34 +11:00
|
|
|
int
|
|
|
|
main (int, char**)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2016-03-17 18:12:34 +11:00
|
|
|
|
2017-12-26 17:29:32 +11:00
|
|
|
tap.expect_eq (
|
2018-08-05 14:42:02 +10:00
|
|
|
std::size (cruft::make_view ("foo")), 3u,
|
2017-12-26 17:29:32 +11:00
|
|
|
"character array view does not include trailing null"
|
|
|
|
);
|
|
|
|
|
2018-01-29 17:17:28 +11:00
|
|
|
{
|
|
|
|
const std::string s = "this is a test string";
|
|
|
|
const std::string t = "not the same string";
|
2016-03-17 18:12:34 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (s, cruft::make_view(s), "string/view equality");
|
|
|
|
tap.expect_neq (s, cruft::make_view(t), "string/view inequality");
|
|
|
|
tap.expect_eq (s.data (), cruft::make_view (s), "c-str/view equality");
|
2018-01-29 17:17:28 +11:00
|
|
|
tap.expect_eq (
|
|
|
|
s,
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::view<const char*> (&*s.cbegin (), &*s.cend ()),
|
2018-01-29 17:17:28 +11:00
|
|
|
"string/pointer-view equality");
|
|
|
|
}
|
2017-12-20 12:31:52 +11:00
|
|
|
|
2018-10-04 14:52:35 +10:00
|
|
|
// construction from a std::array
|
|
|
|
{
|
|
|
|
std::array<std::byte,16> raw;
|
|
|
|
cruft::view obj { raw };
|
|
|
|
tap.expect_eq (obj.size (), raw.size (), "std::array construction size matches");
|
|
|
|
}
|
|
|
|
|
2018-01-29 17:17:28 +11:00
|
|
|
// comparator tests
|
2017-12-20 12:31:52 +11:00
|
|
|
{
|
|
|
|
static const struct {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::view<const char*> a;
|
|
|
|
cruft::view<const char*> b;
|
2017-12-20 12:31:52 +11:00
|
|
|
bool less;
|
|
|
|
} TESTS[] = {
|
|
|
|
{ "b", "b", false },
|
|
|
|
{ "b", "a", false },
|
|
|
|
{ "b", "c", true },
|
|
|
|
{ "foo", "bar", false },
|
|
|
|
{ "bar", "foo", true },
|
|
|
|
{ "banana", "banana", false },
|
|
|
|
{ "banana", "banan", false },
|
|
|
|
{ "banan", "banana", true },
|
|
|
|
};
|
|
|
|
|
|
|
|
tap.expect (std::all_of (std::begin (TESTS), std::end (TESTS), [] (const auto &x) {
|
|
|
|
return (x.a < x.b) == x.less;
|
|
|
|
}), "comparator less-than");
|
|
|
|
};
|
2016-03-17 18:12:34 +11:00
|
|
|
|
2018-01-29 17:17:28 +11:00
|
|
|
// slicing
|
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
const char *init;
|
|
|
|
const char *result;
|
|
|
|
int a;
|
|
|
|
int b;
|
|
|
|
const char *message;
|
|
|
|
} TESTS[] = {
|
|
|
|
{ "01234567", "01234567", 0, 8, "identity, +ve +ve" },
|
|
|
|
{ "01234567", "01234567", 0, -1, "identity, +ve -ve" },
|
|
|
|
{ "01234567", "", -1, -1, "empty, -ve -ve" },
|
|
|
|
{ "01234567", "45", 4, -3, "centre, +ve -ve" },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto &t: TESTS) {
|
2021-04-13 16:05:08 +10:00
|
|
|
tap.expect_eq (cruft::view { t.init }.slice (t.a, t.b), t.result, "slice; {:s}", t.message);
|
2018-01-29 17:17:28 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 18:12:34 +11:00
|
|
|
return tap.status ();
|
|
|
|
}
|