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
|
#include <iostream>
#include <cassert>
#include <cstring>
#include <memory>
#include "jwt/string_view.hpp"
using string_view = jwt::basic_string_view<char>;
void basic_cons()
{
// Default construction
string_view sv{};
assert (sv.length() == 0 && "Size must be zero for default constructor");
// Construction from string literal
string_view sv2{"Arun Muralidharan"};
assert (sv2.length() == strlen("Arun Muralidharan") && "Lengths must match");
const char* haystack = "some really big data with infinite objects....";
// Construct using part of data
string_view sv3{haystack, 4};
assert (sv3.length() == 4 && "Partial construction is not ok");
assert (sv3.to_string() == "some" && "Partial strings are not equal");
return;
}
void iterator_test()
{
string_view sv{"Arun Muralidharan"};
for (auto c : sv) std::cout << c;
std::cout << std::endl;
return;
}
void str_operations()
{
string_view sv{"Arun Muralidharan"};
string_view tmp = sv;
sv.remove_prefix(5);
assert (sv.to_string() == "Muralidharan" && "Remove prefix failed");
sv = tmp;
sv.remove_suffix(strlen("Muralidharan"));
assert (sv.to_string() == "Arun " && "Remove suffix failed");
sv=tmp;
{
std::unique_ptr<char[]> dst{new char[32]};
sv.copy(dst.get(), 6, 0);
dst[6] = '\0';
assert (strlen(dst.get()) == 6 && "Copy Failed-1");
assert (std::string{dst.get()} == "Arun M" && "Copy Failed-2");
sv.copy(dst.get(), 8, 4);
dst[8] = '\0';
assert (strlen(dst.get()) == 8 && "Middle copy failed-1");
assert (std::string{dst.get()} == " Muralid" && "Middle copy failed-2");
}
{
auto ss1 = sv.substr(0, 4);
assert (ss1.to_string() == "Arun" && "Substr failed - 1");
auto ss2 = sv.substr(1, 3);
assert (ss2.to_string() == "run" && "Substr failed - 2");
auto ss3 = sv.substr(0);
assert (ss3.length() == sv.length() && "Substr failed - 3");
}
return;
}
void find_oper()
{
string_view sv{"Arun Muralidharan"};
auto pos = sv.find("Arun", 0, 4);
assert (pos == 0 && "Arun not found in sv");
pos = sv.find("arun", 0, 4);
assert (pos == string_view::npos && "arun is not there in sv");
sv = "This has a, in it.";
pos = sv.find_first_of(",", 0, 1);
assert (pos != string_view::npos);
assert (pos == 10 && "Comma not found at correct place");
pos = sv.find_first_of(",", 10, 1);
assert (pos != string_view::npos);
assert (pos == 10 && "Comma not found at correct place");
pos = sv.find_first_of(":", 10, 1);
assert (pos == string_view::npos);
pos = sv.find_last_of(",", 5, 1);
assert (pos == string_view::npos);
pos = sv.find_last_of(",", sv.length() - 1, 1);
assert (pos != string_view::npos);
assert (pos == 10 && "Comma not found at correct place");
pos = sv.find_first_of(".", 0, 1);
assert (pos == sv.length() - 1 && "Dot not found at correct place");
pos = sv.find_last_of(".", sv.length() - 2, 1);
assert (pos == string_view::npos);
pos = sv.find_last_of(".", sv.length() - 1, 1);
assert (pos == sv.length() - 1);
sv = "Some string :<> with some ??? pattern --**";
pos = sv.rfind("???", sv.length() - 1, 3);
assert (pos != string_view::npos && "??? not found");
assert (pos == 26 && "??? not found at the correct place");
sv = "ATCGTTCACGRRRTCGGGGACGTC";
pos = sv.find_first_not_of("ATCG");
assert (pos != string_view::npos);
assert (pos == 10);
return;
}
void conversions()
{
auto c2sv = [](int num) -> string_view {
switch (num) {
case 1: return "one";
case 2: return "two";
case 3: return "three";
default: return "many";
};
};
auto res = c2sv(2);
assert (res.to_string() == "two");
auto s2sv = [](std::string s) {
return s;
};
s2sv(static_cast<std::string>(res));
}
void comparisons()
{
string_view s1{"Apple"};
string_view s2{"Orange"};
assert (s1 != s2 && "Two string views are not equal");
assert (s2 > s1 && "Orange is lexicographically bigger than Apple");
s2 = "Apples";
assert (s2 > s1 && "Because Apples is plural");
}
int main() {
basic_cons();
iterator_test();
str_operations();
find_oper();
conversions();
comparisons();
return 0;
};
|