-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mystring.cpp
More file actions
117 lines (100 loc) · 3.25 KB
/
Copy pathtest_mystring.cpp
File metadata and controls
117 lines (100 loc) · 3.25 KB
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
#include <cassert>
#include <sstream>
#include <stdexcept>
#include <string_view>
#include <utility>
#include "MyString.h"
static void test_construction_and_access() {
MyString empty;
assert(empty.empty());
assert(empty.size() == 0);
assert(empty.c_str()[0] == '\0');
MyString value("hello");
assert(value.size() == 5);
assert(value.length() == 5);
assert(value.front() == 'h');
assert(value.back() == 'o');
value[0] = 'H';
assert(value == MyString("Hello"));
assert(value.at(1) == 'e');
bool threw = false;
try { (void)value.at(99); } catch (const std::out_of_range&) { threw = true; }
assert(threw);
}
static void test_rule_of_five() {
MyString original("resource");
MyString copied(original);
copied[0] = 'R';
assert(original == MyString("resource"));
assert(copied == MyString("Resource"));
MyString assigned;
assigned = original;
assert(assigned == original);
MyString moved(std::move(copied));
assert(moved == MyString("Resource"));
assert(copied.empty());
assert(copied.c_str()[0] == '\0');
MyString move_assigned;
move_assigned = std::move(moved);
assert(move_assigned == MyString("Resource"));
assert(moved.empty());
}
static void test_modification_and_capacity() {
MyString value("ab");
value.reserve(32);
assert(value.capacity() >= 32);
value.append('c').append("def");
assert(value == MyString("abcdef"));
value += MyString("!");
assert(value == MyString("abcdef!"));
value += value;
assert(value == MyString("abcdef!abcdef!"));
const std::size_t old_capacity = value.capacity();
value.clear();
assert(value.empty());
assert(value.capacity() == old_capacity);
}
static void test_search_and_substrings() {
MyString value("banana bread");
assert(value.find('b') == 0);
assert(value.find('x') == MyString::npos);
assert(value.find(MyString("bread")) == 7);
assert(value.contains(MyString("ana")));
assert(!value.contains(MyString("cake")));
assert(value.starts_with(MyString("ban")));
assert(value.ends_with(MyString("bread")));
assert(value.substr(7) == MyString("bread"));
assert(value.substr(0, 6) == MyString("banana"));
assert(value.reversed() == MyString("daerb ananab"));
}
static void test_comparison_and_streams() {
assert(MyString("abc") < MyString("abd"));
assert(MyString("abc") <= MyString("abc"));
assert(MyString("abd") > MyString("abc"));
assert(MyString("abc") != MyString("abd"));
std::stringstream input("hello world");
MyString first;
MyString second;
input >> first >> second;
assert(first == MyString("hello"));
assert(second == MyString("world"));
std::stringstream output;
output << first << ' ' << second;
assert(output.str() == "hello world");
}
static void test_embedded_nulls() {
const char raw[] = {'a', '\0', 'b'};
MyString value(std::string_view(raw, 3));
assert(value.size() == 3);
assert(value[1] == '\0');
assert(value.substr(1, 2).size() == 2);
}
int main() {
test_construction_and_access();
test_rule_of_five();
test_modification_and_capacity();
test_search_and_substrings();
test_comparison_and_streams();
test_embedded_nulls();
return 0;
}