1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#define CAF_SUITE string_view
#include "caf/string_view.hpp"
#include "core-test.hpp"
using namespace caf;
using namespace caf::literals;
CAF_TEST(default construction) {
string_view x;
string_view y;
CHECK(x.empty());
CHECK_EQ(x.size(), 0u);
CHECK_EQ(x.data(), nullptr);
CHECK_EQ(y, y);
}
CAF_TEST(cstring conversion) {
auto x = "abc"_sv;
CHECK_EQ(x.size(), 3u);
CHECK_EQ(x[0], 'a');
CHECK_EQ(x[1], 'b');
CHECK_EQ(x[2], 'c');
CHECK_EQ(x, "abc");
x = "def";
CHECK_NE(x, "abc");
CHECK_EQ(x, "def");
}
CAF_TEST(string conversion) {
std::string x = "abc";
string_view y;
y = x;
CHECK_EQ(x, y);
auto f = [&](string_view z) { CHECK_EQ(x, z); };
f(x);
}
CAF_TEST(substrings) {
auto without_prefix = [](string_view str, size_t n) {
str.remove_prefix(n);
return str;
};
auto without_suffix = [](string_view str, size_t n) {
str.remove_suffix(n);
return str;
};
auto x = "abcdefghi"_sv;
CHECK_EQ(without_prefix(x, 3), "defghi");
CHECK_EQ(without_suffix(x, 3), "abcdef");
CHECK_EQ(x.substr(3, 3), "def");
CHECK_EQ(without_prefix(x, 9), "");
CHECK_EQ(without_suffix(x, 9), "");
CHECK_EQ(x.substr(9), "");
CHECK_EQ(x.substr(0, 0), "");
}
CAF_TEST(compare) {
// testees
auto x = "abc"_sv;
auto y = "bcd"_sv;
auto z = "cde"_sv;
// x.compare full strings
CHECK(x.compare("abc") == 0);
CHECK(x.compare(y) < 0);
CHECK(x.compare(z) < 0);
// y.compare full strings
CHECK(y.compare(x) > 0);
CHECK(y.compare("bcd") == 0);
CHECK(y.compare(z) < 0);
// z.compare full strings
CHECK(z.compare(x) > 0);
CHECK(z.compare(y) > 0);
CHECK(z.compare("cde") == 0);
// x.compare substrings
CHECK(x.compare(0, 3, "abc") == 0);
CHECK(x.compare(1, 2, y, 0, 2) == 0);
CHECK(x.compare(2, 1, z, 0, 1) == 0);
// make sure substrings aren't equal
CHECK("a/"_sv != "a/b"_sv);
CHECK(z.compare("cdef"_sv) < 0);
CHECK("cdef"_sv.compare(z) > 0);
}
CAF_TEST(copy) {
char buf[10];
auto str = "hello"_sv;
auto n = str.copy(buf, str.size());
CHECK_EQ(n, 5u);
buf[n] = '\0';
CHECK_EQ(str, string_view(buf, n));
CHECK(strcmp("hello", buf) == 0);
n = str.copy(buf, 10, 3);
buf[n] = '\0';
CHECK_EQ(string_view(buf, n), "lo");
CHECK(strcmp("lo", buf) == 0);
}
CAF_TEST(find) {
// Check whether string_view behaves exactly like std::string.
auto x = "abcdef"_sv;
std::string y = "abcdef";
CHECK_EQ(x.find('a'), y.find('a'));
CHECK_EQ(x.find('b'), y.find('b'));
CHECK_EQ(x.find('g'), y.find('g'));
CHECK_EQ(x.find('a', 1), y.find('a', 1));
CHECK_EQ(x.find("a"), y.find("a"));
CHECK_EQ(x.find("bc"), y.find("bc"));
CHECK_EQ(x.find("ce"), y.find("ce"));
CHECK_EQ(x.find("bc", 1), y.find("bc", 1));
CHECK_EQ(x.find("bc", 1, 0), y.find("bc", 1, 0));
CHECK_EQ(x.find("bc", 0, 1), y.find("bc", 0, 1));
CHECK_EQ(x.find("bc", 2, 2), y.find("bc", 2, 2));
}
CAF_TEST(rfind) {
// Check whether string_view behaves exactly like std::string.
auto x = "abccba"_sv;
std::string y = "abccba";
CHECK_EQ(x.rfind('a'), y.rfind('a'));
CHECK_EQ(x.rfind('b'), y.rfind('b'));
CHECK_EQ(x.rfind('g'), y.rfind('g'));
CHECK_EQ(x.rfind('a', 1), y.rfind('a', 1));
CHECK_EQ(x.rfind("a"), y.rfind("a"));
CHECK_EQ(x.rfind("bc"), y.rfind("bc"));
CHECK_EQ(x.rfind("ce"), y.rfind("ce"));
CHECK_EQ(x.rfind("bc", 1), y.rfind("bc", 1));
CHECK_EQ(x.rfind("bc", 1, 0), y.rfind("bc", 1, 0));
CHECK_EQ(x.rfind("bc", 0, 1), y.rfind("bc", 0, 1));
CHECK_EQ(x.rfind("bc", 2, 2), y.rfind("bc", 2, 2));
}
CAF_TEST(find_first_of) {
// Check whether string_view behaves exactly like std::string.
auto x = "abcdef"_sv;
std::string y = "abcdef";
CHECK_EQ(x.find_first_of('a'), y.find_first_of('a'));
CHECK_EQ(x.find_first_of('b'), y.find_first_of('b'));
CHECK_EQ(x.find_first_of('g'), y.find_first_of('g'));
CHECK_EQ(x.find_first_of('a', 1), y.find_first_of('a', 1));
CHECK_EQ(x.find_first_of("a"), y.find_first_of("a"));
CHECK_EQ(x.find_first_of("bc"), y.find_first_of("bc"));
CHECK_EQ(x.find_first_of("ce"), y.find_first_of("ce"));
CHECK_EQ(x.find_first_of("bc", 1), y.find_first_of("bc", 1));
CHECK_EQ(x.find_first_of("bc", 1, 0), y.find_first_of("bc", 1, 0));
CHECK_EQ(x.find_first_of("bc", 0, 1), y.find_first_of("bc", 0, 1));
CHECK_EQ(x.find_first_of("bc", 2, 2), y.find_first_of("bc", 2, 2));
}
CAF_TEST(find_last_of) {
// Check whether string_view behaves exactly like std::string.
auto x = "abcdef"_sv;
std::string y = "abcdef";
CHECK_EQ(x.find_last_of('a'), y.find_last_of('a'));
CHECK_EQ(x.find_last_of('b'), y.find_last_of('b'));
CHECK_EQ(x.find_last_of('g'), y.find_last_of('g'));
CHECK_EQ(x.find_last_of('a', 1), y.find_last_of('a', 1));
CHECK_EQ(x.find_last_of("a"), y.find_last_of("a"));
CHECK_EQ(x.find_last_of("bc"), y.find_last_of("bc"));
CHECK_EQ(x.find_last_of("ce"), y.find_last_of("ce"));
CHECK_EQ(x.find_last_of("bc", 1), y.find_last_of("bc", 1));
CHECK_EQ(x.find_last_of("bc", 1, 0), y.find_last_of("bc", 1, 0));
CHECK_EQ(x.find_last_of("bc", 0, 1), y.find_last_of("bc", 0, 1));
CHECK_EQ(x.find_last_of("bc", 2, 2), y.find_last_of("bc", 2, 2));
}
CAF_TEST(find_first_not_of) {
// Check whether string_view behaves exactly like std::string.
auto x = "abcdef"_sv;
std::string y = "abcdef";
CHECK_EQ(x.find_first_not_of('a'), y.find_first_not_of('a'));
CHECK_EQ(x.find_first_not_of('b'), y.find_first_not_of('b'));
CHECK_EQ(x.find_first_not_of('g'), y.find_first_not_of('g'));
CHECK_EQ(x.find_first_not_of('a', 1), y.find_first_not_of('a', 1));
CHECK_EQ(x.find_first_not_of("a"), y.find_first_not_of("a"));
CHECK_EQ(x.find_first_not_of("bc"), y.find_first_not_of("bc"));
CHECK_EQ(x.find_first_not_of("ce"), y.find_first_not_of("ce"));
CHECK_EQ(x.find_first_not_of("bc", 1), y.find_first_not_of("bc", 1));
CHECK_EQ(x.find_first_not_of("bc", 1, 0), y.find_first_not_of("bc", 1, 0));
CHECK_EQ(x.find_first_not_of("bc", 0, 1), y.find_first_not_of("bc", 0, 1));
CHECK_EQ(x.find_first_not_of("bc", 2, 2), y.find_first_not_of("bc", 2, 2));
}
CAF_TEST(find_last_not_of) {
// Check whether string_view behaves exactly like std::string.
auto x = "abcdef"_sv;
std::string y = "abcdef";
CHECK_EQ(x.find_last_not_of('a'), y.find_last_not_of('a'));
CHECK_EQ(x.find_last_not_of('b'), y.find_last_not_of('b'));
CHECK_EQ(x.find_last_not_of('g'), y.find_last_not_of('g'));
CHECK_EQ(x.find_last_not_of('a', 1), y.find_last_not_of('a', 1));
CHECK_EQ(x.find_last_not_of("a"), y.find_last_not_of("a"));
CHECK_EQ(x.find_last_not_of("bc"), y.find_last_not_of("bc"));
CHECK_EQ(x.find_last_not_of("ce"), y.find_last_not_of("ce"));
CHECK_EQ(x.find_last_not_of("bc", 1), y.find_last_not_of("bc", 1));
CHECK_EQ(x.find_last_not_of("bc", 1, 0), y.find_last_not_of("bc", 1, 0));
CHECK_EQ(x.find_last_not_of("bc", 0, 1), y.find_last_not_of("bc", 0, 1));
CHECK_EQ(x.find_last_not_of("bc", 2, 2), y.find_last_not_of("bc", 2, 2));
}
|