-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
73 lines (66 loc) · 2.33 KB
/
main.cpp
File metadata and controls
73 lines (66 loc) · 2.33 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
#include <iostream>
#include "garbage_collection.h"
#include "i_smart_object.h"
#include "smart_binary_tree.h"
#include "smart_classes_with_deriving.h"
#include <stdlib.h>
using namespace std;
bool shouldWeCreateAChild()
{
return rand() % 5;
}
void generateRandomTree(SmartHeapBinaryTree::Node * T, int maxdepth=4, int depth=0)
{
if (depth >= maxdepth || T == nullptr)
return;
if (shouldWeCreateAChild()) {
T->left = new SmartHeapBinaryTree::Node(rand() % 100);
generateRandomTree(T->left, maxdepth, depth + 1);
}
if (shouldWeCreateAChild()) {
T->right = new SmartHeapBinaryTree::Node(rand() % 100);
generateRandomTree(T->right, maxdepth, depth + 1);
}
}
void testBinaryTrees()
{
SmartHeapBinaryTree::Node root(10);
SmartHeapBinaryTree::Node * root2 = new SmartHeapBinaryTree::Node(2);
generateRandomTree(root2, 4);
SmartHeapBinaryTree::Node * root3 = new SmartHeapBinaryTree::Node(3);
generateRandomTree(root3, 4);
root.right = root2;
std::cout << "check trees before collection and after" << std::endl;
ISmartObject * ptrRoot2 = dynamic_cast<ISmartObject *>(root2);
ISmartObject * ptrRoot3 = dynamic_cast<ISmartObject *>(root3);
std::cout << GarbageCollection::getInstance().isPointerSteelAlive(ptrRoot2) << " ";
std::cout << GarbageCollection::getInstance().isPointerSteelAlive(ptrRoot3) << std::endl;
GarbageCollection::getInstance().collectGarbage();
std::cout << GarbageCollection::getInstance().isPointerSteelAlive(ptrRoot2) << " ";
std::cout << GarbageCollection::getInstance().isPointerSteelAlive(ptrRoot3) << std::endl;
}
void test_bad_alloc()
{
/*
* test ONLY IN RELEASE,
* because memory leak detector and address sanitizer will get crazy
*/
new test2::B[1000000000LL];
}
int main()
{
// test_bad_alloc();
testBinaryTrees();
test_smart_classes_with_deriving_1();
test_smart_classes_with_deriving_2();
test_smart_classes_with_deriving_3();
test_smart_classes_with_deriving_4();
test_smart_classes_with_deriving_5();
/*
* demonstrate smart singleton that singleton will be destroyed by himself at the end of program
* also it will collect garbage in the end of his life
*/
SmartHeapBinaryTree::Node root(10);
root.left = new SmartHeapBinaryTree::Node(50);
return 0;
}