This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
75 lines (67 loc) · 2.15 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include "log.h"
#include "my_list.h"
#include "hero.h"
using namespace std;
void test()
{
MyList<Hero> myList;
ifstream file("heros.txt");
copy(istream_iterator<Hero>(file), istream_iterator<Hero>(), back_inserter(myList));
myList.sort([](const Hero &a, const Hero &b) {
return a.power > b.power;
});
cout << "Sorted by power:" << endl;
for (auto i: myList)
cout << i << endl;
myList.sort([](const Hero &a, const Hero &b) {
int scorea = a.strength + a.intelligence + a.power + a.charisma + a.luck;
int scoreb = b.strength + b.intelligence + b.power + b.charisma + b.luck;
return scorea > scoreb;
});
cout << "Sorted by total score:" << endl;
for (auto i: myList)
cout << i << endl;
}
void test2()
{
cout << "Some common memory faults:" << endl;
new int; // leak
delete[] new int; // mismatch
delete new int[2]; // mismatch
delete (int *)0x563D7C; // wild pointer (I like this color :-)
delete (int *)nullptr; // delete nullptr
}
int main()
{
// set up clog with message of loglevel "info" logged outputing to file only
// and message of loglevel "warning" and "error" outputing to both file and original output
std::ofstream logfile("log.txt");
teebuf warningbuf = {
logfile.rdbuf(),
clog.rdbuf()
};
logbuf clogbuf(logfile.rdbuf(), &warningbuf, &warningbuf);
streambuf *origclogbuf = clog.rdbuf();
clog.rdbuf(&clogbuf);
// set up cout with all message outputing to both file and original output
teebuf coutbuf = {
logfile.rdbuf(),
cout.rdbuf()
};
streambuf *origcoutbuf = cout.rdbuf();
cout.rdbuf(&coutbuf);
cout << "Test case #1:" << endl;
MEMORY_LEAK_DETECT_TEST(test());
cout << "\n\nTest case #2:" << endl;
MEMORY_LEAK_DETECT_TEST(test2());
cout << "All tests finished." << endl;
cout.rdbuf(origcoutbuf);
clog.rdbuf(origclogbuf);
logfile.close();
cout << "\nDetailed log is available at log.txt." << endl;
return 0;
}