-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetSet_Experiments.cpp
126 lines (92 loc) · 2.91 KB
/
GetSet_Experiments.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
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
118
119
120
121
122
123
124
125
126
#pragma once
#include "GetSet.h"
#include<string>
#include<iostream>
using namespace std;
//*
int main() {
GetSet_Experiments::testAll();
std::cout << "Press [ENTER} to exit" << std::endl;
string ret;
std::cin >> ret;
return 0;
}
//*/
class GetSet_Experiments {
public:
static void testAll() {
testInteger();
testString();
testMove();
testCopy();
}
static void testInteger() {
std::cout << "-----------------" << std::endl;
std::cout << "START testInteger" << std::endl;
GetSet<int> t(3);
cout << t << endl;//expected 3
t = 5;
cout << t << endl;//expected 5
t.setGetter([](int& currentItem)-> int {return currentItem * 10; });
cout << t << endl;//expected 5*10=50
t.setSetter([](int& currentItem, int newValue) {currentItem = newValue * 3; });
t = 6;
cout << t << endl;//expected 6*10*3=180
t.setGetter(nullptr);
cout << t << endl;//expected 6*3=18
std::cout << "END testInteger" << std::endl << std::endl;
}
static void testString() {
std::cout << "-----------------" << std::endl;
std::cout << "START testString" << std::endl;
GetSet<string> t("aaa");
cout << t << endl;//expected 'aaa'
t.setSetter([](auto& currItem, auto newItem) {currItem = "(Setter) " + newItem; });
t.setGetter([](auto& currItem) ->auto {return currItem + " (Getter)"; });
t = "bbb";
cout << t << endl;//expected '(Setter) bbb (Getter)'
cout << ((string)t) << endl;//expected '(Setter) bbb (Getter)'
std::cout << "END testString" << std::endl << std::endl;
}
static void testMove() {
std::cout << "-----------------" << std::endl;
std::cout << "START testMove" << std::endl;
GetSet<int> t(3);
t.setGetter([](int& currentItem)-> int {return currentItem * 10; });
cout << t << endl;//expected 30
GetSet<int> t2 = std::move(t);
cout << t2 << endl;//expected 30
std::cout << "END testMove" << std::endl << std::endl;
}
static void testCopy() {
std::cout << "-----------------" << std::endl;
std::cout << "START testCopy" << std::endl;
GetSet<int> t(3);
t.setGetter([](int& currentItem)-> int {return currentItem * 10; });
cout << t << endl;//expected 30
// GetSet<int> t2 = std::move(t); : this is NOT allowed
GetSet<int> t2 = (int)t;//I'm only interested about value(returned through getter)
cout << t2 << endl;//expected 3*10
GetSet<int> t3 = t.factory_copyValue();//I'm only interested about value
cout << t3 << endl;//expected 3
GetSet<int> t4 = t.factory_copyValueGetterSetter();//I'm interested about value and getter/setter
cout << t4 << endl;//expected 30
std::cout << "END testCopy" << std::endl << std::endl;
}
/* class TempClass {
public:
int a;
};
void testClass() {
std::cout << "-----------------" << std::endl;
std::cout << "START testString" << std::endl;
TempClass *a = new TempClass();
a->a = 5;
GetSet<TempClass> gs(*a);
TempClass &b = gs;
b.a = 6;
TempClass c = gs;
cout << c.a << endl;
cout << ((TempClass)gs).a;
}*/
};