| 1 |
#include <iostream> |
| 2 |
#include <cassert> |
| 3 |
#include <cstring> |
| 4 |
#include <memory> |
| 5 |
#include "jwt/string_view.hpp" |
| 6 |
|
| 7 |
using string_view = jwt::basic_string_view<char>; |
| 8 |
|
| 9 |
void basic_cons() |
| 10 |
{ |
| 11 |
// Default construction |
| 12 |
string_view sv{}; |
| 13 |
assert (sv.length() == 0 && "Size must be zero for default constructor"); |
| 14 |
|
| 15 |
// Construction from string literal |
| 16 |
string_view sv2{"Arun Muralidharan"}; |
| 17 |
assert (sv2.length() == strlen("Arun Muralidharan") && "Lengths must match"); |
| 18 |
|
| 19 |
const char* haystack = "some really big data with infinite objects...."; |
| 20 |
|
| 21 |
// Construct using part of data |
| 22 |
string_view sv3{haystack, 4}; |
| 23 |
assert (sv3.length() == 4 && "Partial construction is not ok"); |
| 24 |
assert (sv3.to_string() == "some" && "Partial strings are not equal"); |
| 25 |
|
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
void iterator_test() |
| 30 |
{ |
| 31 |
string_view sv{"Arun Muralidharan"}; |
| 32 |
for (auto c : sv) std::cout << c; |
| 33 |
std::cout << std::endl; |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
void str_operations() |
| 38 |
{ |
| 39 |
string_view sv{"Arun Muralidharan"}; |
| 40 |
string_view tmp = sv; |
| 41 |
sv.remove_prefix(5); |
| 42 |
assert (sv.to_string() == "Muralidharan" && "Remove prefix failed"); |
| 43 |
|
| 44 |
sv = tmp; |
| 45 |
sv.remove_suffix(strlen("Muralidharan")); |
| 46 |
assert (sv.to_string() == "Arun " && "Remove suffix failed"); |
| 47 |
|
| 48 |
sv=tmp; |
| 49 |
{ |
| 50 |
std::unique_ptr<char[]> dst{new char[32]}; |
| 51 |
sv.copy(dst.get(), 6, 0); |
| 52 |
dst[6] = '\0'; |
| 53 |
assert (strlen(dst.get()) == 6 && "Copy Failed-1"); |
| 54 |
assert (std::string{dst.get()} == "Arun M" && "Copy Failed-2"); |
| 55 |
|
| 56 |
sv.copy(dst.get(), 8, 4); |
| 57 |
dst[8] = '\0'; |
| 58 |
assert (strlen(dst.get()) == 8 && "Middle copy failed-1"); |
| 59 |
assert (std::string{dst.get()} == " Muralid" && "Middle copy failed-2"); |
| 60 |
} |
| 61 |
|
| 62 |
{ |
| 63 |
auto ss1 = sv.substr(0, 4); |
| 64 |
assert (ss1.to_string() == "Arun" && "Substr failed - 1"); |
| 65 |
|
| 66 |
auto ss2 = sv.substr(1, 3); |
| 67 |
assert (ss2.to_string() == "run" && "Substr failed - 2"); |
| 68 |
|
| 69 |
auto ss3 = sv.substr(0); |
| 70 |
assert (ss3.length() == sv.length() && "Substr failed - 3"); |
| 71 |
} |
| 72 |
|
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
void find_oper() |
| 77 |
{ |
| 78 |
string_view sv{"Arun Muralidharan"}; |
| 79 |
auto pos = sv.find("Arun", 0, 4); |
| 80 |
assert (pos == 0 && "Arun not found in sv"); |
| 81 |
|
| 82 |
pos = sv.find("arun", 0, 4); |
| 83 |
assert (pos == string_view::npos && "arun is not there in sv"); |
| 84 |
|
| 85 |
sv = "This has a, in it."; |
| 86 |
pos = sv.find_first_of(",", 0, 1); |
| 87 |
assert (pos != string_view::npos); |
| 88 |
assert (pos == 10 && "Comma not found at correct place"); |
| 89 |
|
| 90 |
pos = sv.find_first_of(",", 10, 1); |
| 91 |
assert (pos != string_view::npos); |
| 92 |
assert (pos == 10 && "Comma not found at correct place"); |
| 93 |
|
| 94 |
pos = sv.find_first_of(":", 10, 1); |
| 95 |
assert (pos == string_view::npos); |
| 96 |
|
| 97 |
pos = sv.find_last_of(",", 5, 1); |
| 98 |
assert (pos == string_view::npos); |
| 99 |
|
| 100 |
pos = sv.find_last_of(",", sv.length() - 1, 1); |
| 101 |
assert (pos != string_view::npos); |
| 102 |
assert (pos == 10 && "Comma not found at correct place"); |
| 103 |
|
| 104 |
pos = sv.find_first_of(".", 0, 1); |
| 105 |
assert (pos == sv.length() - 1 && "Dot not found at correct place"); |
| 106 |
|
| 107 |
pos = sv.find_last_of(".", sv.length() - 2, 1); |
| 108 |
assert (pos == string_view::npos); |
| 109 |
|
| 110 |
pos = sv.find_last_of(".", sv.length() - 1, 1); |
| 111 |
assert (pos == sv.length() - 1); |
| 112 |
|
| 113 |
sv = "Some string :<> with some ??? pattern --**"; |
| 114 |
|
| 115 |
pos = sv.rfind("???", sv.length() - 1, 3); |
| 116 |
assert (pos != string_view::npos && "??? not found"); |
| 117 |
assert (pos == 26 && "??? not found at the correct place"); |
| 118 |
|
| 119 |
sv = "ATCGTTCACGRRRTCGGGGACGTC"; |
| 120 |
|
| 121 |
pos = sv.find_first_not_of("ATCG"); |
| 122 |
assert (pos != string_view::npos); |
| 123 |
assert (pos == 10); |
| 124 |
|
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
void conversions() |
| 129 |
{ |
| 130 |
auto c2sv = [](int num) -> string_view { |
| 131 |
switch (num) { |
| 132 |
case 1: return "one"; |
| 133 |
case 2: return "two"; |
| 134 |
case 3: return "three"; |
| 135 |
default: return "many"; |
| 136 |
}; |
| 137 |
}; |
| 138 |
|
| 139 |
auto res = c2sv(2); |
| 140 |
assert (res.to_string() == "two"); |
| 141 |
|
| 142 |
auto s2sv = [](std::string s) { |
| 143 |
return s; |
| 144 |
}; |
| 145 |
|
| 146 |
s2sv(static_cast<std::string>(res)); |
| 147 |
} |
| 148 |
|
| 149 |
void comparisons() |
| 150 |
{ |
| 151 |
string_view s1{"Apple"}; |
| 152 |
string_view s2{"Orange"}; |
| 153 |
|
| 154 |
assert (s1 != s2 && "Two string views are not equal"); |
| 155 |
assert (s2 > s1 && "Orange is lexicographically bigger than Apple"); |
| 156 |
|
| 157 |
s2 = "Apples"; |
| 158 |
assert (s2 > s1 && "Because Apples is plural"); |
| 159 |
} |
| 160 |
|
| 161 |
int main() { |
| 162 |
basic_cons(); |
| 163 |
iterator_test(); |
| 164 |
str_operations(); |
| 165 |
find_oper(); |
| 166 |
conversions(); |
| 167 |
comparisons(); |
| 168 |
return 0; |
| 169 |
}; |