1 | #include <iostream> |
---|
2 | #include "test_base.h" |
---|
3 | #include "../primitive.h" |
---|
4 | #include "../r.h" |
---|
5 | |
---|
6 | class A |
---|
7 | { |
---|
8 | public: |
---|
9 | A(Int i) : i(i) { /*std::cout << "A(" << i << ") ctor" << std::endl;*/ } |
---|
10 | ~A() { /*std::cout << "A(" << i << ") dtor" << std::endl;*/ } |
---|
11 | Int i; |
---|
12 | }; |
---|
13 | |
---|
14 | Int bc = 0L; |
---|
15 | Int bd = 0L; |
---|
16 | Int cc = 0L; |
---|
17 | Int cd = 0L; |
---|
18 | |
---|
19 | class B |
---|
20 | { |
---|
21 | public: |
---|
22 | B(int i) : i(i) { ++bc; } |
---|
23 | virtual ~B() { ++bd; } |
---|
24 | virtual Int getI() { return i; } |
---|
25 | protected: |
---|
26 | Int i; |
---|
27 | }; |
---|
28 | |
---|
29 | class C : public B |
---|
30 | { |
---|
31 | public: |
---|
32 | C(int i) : B(i) { ++cc; } |
---|
33 | virtual ~C() { ++cd; } |
---|
34 | virtual Int getI() { return i + 1; } |
---|
35 | }; |
---|
36 | |
---|
37 | void test(); |
---|
38 | |
---|
39 | int main() |
---|
40 | { |
---|
41 | int retVal = -1; |
---|
42 | try |
---|
43 | { |
---|
44 | std::cout << "**** Testing references ****" << std::endl; |
---|
45 | test(); |
---|
46 | std::cout << "Test SUCCEEDED!" << std::endl; |
---|
47 | retVal = 0; |
---|
48 | } |
---|
49 | catch (TestFailed& ex) |
---|
50 | { |
---|
51 | std::cout << "Failed" << std::endl << "Test FAILED: " << ex.what() << std::endl; |
---|
52 | } |
---|
53 | catch (...) |
---|
54 | { |
---|
55 | std::cout << "Failed" << std::endl << "Test FAILED with unknown exception" << std::endl; |
---|
56 | } |
---|
57 | return retVal; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | void test() |
---|
62 | { |
---|
63 | StringStream ss; |
---|
64 | std::cout << "Basic tests"; |
---|
65 | R<A> a = Ref::mk<A>(5); |
---|
66 | auto fun = [&ss](R<A> a2, Int refCount, Int val) { |
---|
67 | ss.empty() << "a2->i (with val " << val << ") == " << a2->i << ", should be " << val << "!"; |
---|
68 | assertEq(a2->i, val, ss.str()); |
---|
69 | |
---|
70 | Int refc = a2.refCount(); |
---|
71 | ss.empty() << "a2.refCount() (with val " << val << ") == " << refc <<" should be " << refCount << "!"; |
---|
72 | assertEq(a2.refCount(), refc, ss.str()); |
---|
73 | }; |
---|
74 | |
---|
75 | Int deref = a->i; |
---|
76 | ss.empty() << "a->i (with val " << 5l << ") == " << deref << ", should be " << 5l << "!"; |
---|
77 | assertEq(deref, 5l, ss.str()); |
---|
78 | |
---|
79 | Int refc = a.refCount(); |
---|
80 | ss.empty() << "a.refCount() (with val " << 5l << ") == " << refc <<" should be " << 1l << "!"; |
---|
81 | assertEq(refc, 1l, ss.str()); |
---|
82 | |
---|
83 | fun(Ref::mk<A>(7), 1, 7); |
---|
84 | fun(a, 2, 5); |
---|
85 | |
---|
86 | deref = a->i; |
---|
87 | ss.empty() << "a->i (with val " << 5l << ") == " << deref << ", should be " << 5l << "!"; |
---|
88 | assertEq(deref, 5l, ss.str()); |
---|
89 | |
---|
90 | refc = a.refCount(); |
---|
91 | ss.empty() << "a.refCount() (with val " << 5l << ") == " << refc <<" should be " << 1l << "!"; |
---|
92 | assertEq(refc, 1l, ss.str()); |
---|
93 | |
---|
94 | std::cout << "Success" << std::endl << "null reference tests"; |
---|
95 | R<A> b; |
---|
96 | assertEq(b.isNull(), true, "b.isNull returned false instead of true"); |
---|
97 | auto funDeref = [&b] () { |
---|
98 | A& a = *b; |
---|
99 | }; |
---|
100 | auto funRefCount = [&b] () { |
---|
101 | Int i = b.refCount(); |
---|
102 | }; |
---|
103 | assertException<NullReference>::thrown(funDeref, "Dereferencing null ref did not throw exception"); |
---|
104 | assertException<NullReference>::thrown(funRefCount, "Getting reference count for null ref did not throw exception"); |
---|
105 | |
---|
106 | std::cout << "Success" << std::endl << "operator= tests"; |
---|
107 | { |
---|
108 | R<A> c; |
---|
109 | c = a; |
---|
110 | |
---|
111 | deref = a->i; |
---|
112 | ss.empty() << "a->i (with val " << 5l << ") == " << deref << ", should be " << 5l << "!"; |
---|
113 | assertEq(deref, 5l, ss.str()); |
---|
114 | |
---|
115 | refc = a.refCount(); |
---|
116 | ss.empty() << "a.refCount() (in subblock, with val " << 5l << ") == " << refc <<" should be " << 2l << "!"; |
---|
117 | assertEq(refc, 2l, ss.str()); |
---|
118 | |
---|
119 | deref = c->i; |
---|
120 | ss.empty() << "c->i (with val " << 5l << ") == " << deref << ", should be " << 5l << "!"; |
---|
121 | assertEq(deref, 5l, ss.str()); |
---|
122 | |
---|
123 | refc = c.refCount(); |
---|
124 | ss.empty() << "c.refCount() (with val " << 5l << ") == " << refc <<" should be " << 2l << "!"; |
---|
125 | assertEq(refc, 2l, ss.str()); |
---|
126 | |
---|
127 | c->i = 20l; |
---|
128 | } |
---|
129 | deref = a->i; |
---|
130 | ss.empty() << "a->i (with val " << 20l << ") == " << deref << ", should be " << 20l << "!"; |
---|
131 | assertEq(deref, 20l, ss.str()); |
---|
132 | |
---|
133 | refc = a.refCount(); |
---|
134 | ss.empty() << "a.refCount() (with val " << 5l << ") == " << refc <<" should be " << 1l << "!"; |
---|
135 | assertEq(refc, 1l, ss.str()); |
---|
136 | |
---|
137 | std::cout << "Success" << std::endl << "plimorphism tests"; |
---|
138 | |
---|
139 | { |
---|
140 | R<B> c = Ref::mk<C>(10L); |
---|
141 | assertEq(bc, 1L, "B should be created but it is not"); |
---|
142 | assertEq(cc, 1L, "C should be created but it is not"); |
---|
143 | deref = c->getI(); |
---|
144 | ss.empty() << "c->getI() should return 11L but it returns " << deref; |
---|
145 | assertEq(deref, 11L, ss.str()); |
---|
146 | } |
---|
147 | assertEq(bd, 1L, "B should be destroyed but it is not"); |
---|
148 | assertEq(cd, 1L, "C should be destroyed but it is not"); |
---|
149 | |
---|
150 | /*std::cout << "Success" << std::endl << "self-referencing tests"; |
---|
151 | R<Int> d = d; |
---|
152 | auto funDerefSelf = [&d]() |
---|
153 | { |
---|
154 | Int val = d.deref(); |
---|
155 | }; |
---|
156 | auto funRefCountSelf = [&d]() |
---|
157 | { |
---|
158 | Int refCount = d.refCount(); |
---|
159 | }; |
---|
160 | assertException<NullReference>::thrown(funRefCountSelf, "Reference counting of self-initialized ref did not throw exception"); |
---|
161 | assertException<NullReference>::thrown(funDerefSelf, "Dereferencing self-initialized ref did not throw exception"); |
---|
162 | d = Ref::mk<Int, Int>(2); |
---|
163 | |
---|
164 | deref = d.deref(); |
---|
165 | ss.empty() << "d.deref() (with val " << 2l << ") == " << deref << ", should be " << 2l << "!"; |
---|
166 | assertEq(deref, 2l, ss.str()); |
---|
167 | |
---|
168 | refc = d.refCount(); |
---|
169 | ss.empty() << "d.refCount() (with val " << 2l << ") == " << refc <<" should be " << 1l << "!"; |
---|
170 | assertEq(refc, 1l, ss.str());*/ |
---|
171 | |
---|
172 | std::cout << "Success" << std::endl; |
---|
173 | } |
---|