-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_DSString.cpp
More file actions
90 lines (69 loc) · 2.58 KB
/
Copy pathtest_DSString.cpp
File metadata and controls
90 lines (69 loc) · 2.58 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include "DSString.h"
/* TODO: This file is a start to test your DSString class. Add more tests and add code to check if the results are correct. */
int main()
{
DSString myString = "Hello, World!";
std::cout << myString << "\n";
// this uses the one argument constructor in DSString and then the inherited operator=
myString = "Good bye!";
std::cout << myString << "\n";
std::cout << "substr: " << myString.substring(5, 3) << "\n";
// test some operators (=, ==, [])
DSString a = "test";
DSString b;
b = myString+a;
std::cout << std::boolalpha;
std::cout << (a == b) << "\n";
b[0] = 'T';
std::cout << "a is now: " << a << "\n";
std::cout << (a == b) << "\n";
// use initialization list
std::vector<DSString> strings = {
DSString("bb"),
DSString("aaa"),
DSString("ddd"),
DSString("ee"),
DSString("ccc")};
// find strings
for (const auto &s : strings)
std::cout
<< s << "\n";
// find implements linear search
std::cout << "found ddd: " << (std::find(strings.begin(), strings.end(), DSString("ddd")) != strings.end()) << "\n";
std::cout << "found z: " << (std::find(strings.begin(), strings.end(), DSString("z")) != strings.end()) << "\n";
// sorting using the STL (note thus needs operator= and operator<)
std::sort(strings.begin(), strings.end());
for (const auto &s : strings)
std::cout
<< s << "\n";
// the data structure is sorted. Now we can do more efficient search using STL binary search
std::cout << "found ddd: " << binary_search(strings.begin(), strings.end(), DSString("ddd")) << "\n";
std::cout << "found z: " << binary_search(strings.begin(), strings.end(), DSString("z")) << "\n";
// test for toLower and removeSymbols
myString = "Hel#lo, Wor$ld!jdksfj";
std::cout << myString << "\n";
myString = myString.toLower();
std::cout << myString << "\n";
myString = myString.removeSymbols();
std::cout << myString << "\n";
// test for findCharIndex
myString = "Hello, World!";
size_t i = myString.findCharIndex(',');
std::cout << i << "\n";
// test for toString
myString = "Hello, World!";
string test = myString.toString();
cout << test << "\n";
// test for stemming
myString = "documentation";
myString = myString.stem();
cout << myString << "\n";
myString = "documenting";
myString = myString.stem();
cout << myString << "\n";
return 0;
}