1 | #include <iostream> |
---|
2 | #include "test_base.h" |
---|
3 | #include "../primitive.h" |
---|
4 | #include "../r.h" |
---|
5 | #include "../lazy.h" |
---|
6 | #include "../delegate.h" |
---|
7 | |
---|
8 | Int cCount = 0; |
---|
9 | Int dCount = 0; |
---|
10 | |
---|
11 | class A |
---|
12 | { |
---|
13 | public: |
---|
14 | A(Int i) : i(i) { ++cCount; } |
---|
15 | ~A() { ++dCount; } |
---|
16 | Int i; |
---|
17 | }; |
---|
18 | |
---|
19 | class B |
---|
20 | { |
---|
21 | public: |
---|
22 | B(Int i) : i(i) { } |
---|
23 | virtual ~B() { } |
---|
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) { } |
---|
33 | virtual ~C() { } |
---|
34 | virtual Int getI() { return i + 1; } |
---|
35 | }; |
---|
36 | |
---|
37 | void test(); |
---|
38 | |
---|
39 | int main() |
---|
40 | { |
---|
41 | try |
---|
42 | { |
---|
43 | test(); |
---|
44 | std::cout << "Test SUCCEEDED!" << std::endl; |
---|
45 | } |
---|
46 | catch (TestFailed& ex) |
---|
47 | { |
---|
48 | std::cout << "Failed" << std::endl << "Test FAILED: " << ex.what() << std::endl; |
---|
49 | } |
---|
50 | catch (...) |
---|
51 | { |
---|
52 | std::cout << "Failed" << std::endl << "Test FAILED with unknown exception" << std::endl; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | R<A> mkA() |
---|
57 | { |
---|
58 | return Ref::mk<A> (10L); |
---|
59 | } |
---|
60 | |
---|
61 | void test() |
---|
62 | { |
---|
63 | StringStream ss; |
---|
64 | std::cout << "Basic tests"; |
---|
65 | |
---|
66 | auto la = mkLazy<A>(mkFun(&mkA)); |
---|
67 | |
---|
68 | assertEq(0L, cCount, "Object should not have been created, but it has!"); |
---|
69 | R<A> a = la.getR(); |
---|
70 | assertEq(1L, cCount, "Object should have been created, but it has not!"); |
---|
71 | R<A> b = la.getR(); |
---|
72 | assertEq(1L, cCount, "Object should have been created only once, but it has not!"); |
---|
73 | |
---|
74 | auto lb = mkLazy(mkLFun([] { return Ref::mk<A> (20L); })); |
---|
75 | |
---|
76 | assertEq(1L, cCount, "Object should not have been created, but it has!"); |
---|
77 | a = lb.getR(); |
---|
78 | assertEq(2L, cCount, "Object should have been created, but it has not!"); |
---|
79 | b = lb.getR(); |
---|
80 | assertEq(2L, cCount, "Object should have been created only once, but it has not!"); |
---|
81 | |
---|
82 | auto lc = mkLazy<A>([]() { return Ref::mk<A> (30L); }); |
---|
83 | std::cout << "Success" << std::endl << "Polimorphism tests"; |
---|
84 | auto ld = mkLazy<C>([]() { return Ref::mk<C, Int>(40L); }); |
---|
85 | |
---|
86 | R<B> c = ld.getR(); |
---|
87 | Int deref = c->getI(); |
---|
88 | ss.empty() << "c.deref().getI() should return 41L but it returns " << deref; |
---|
89 | assertEq(41L, deref, ss.str()); |
---|
90 | |
---|
91 | std::cout << "Success" << std::endl << "Closure tests"; |
---|
92 | auto fun = mkLFun([] (R<Int> i) { |
---|
93 | ++*i; |
---|
94 | return i; |
---|
95 | }, Ref::mk (1L)); |
---|
96 | auto le = mkLazy(fun); |
---|
97 | auto lf = mkLazy(fun); |
---|
98 | ss.empty() << "*le should return 2L but it returns " << *le; |
---|
99 | assertEq(2L, *le, ss.str()); |
---|
100 | ss.empty() << "*lf should return 3L but it returns " << *lf; |
---|
101 | assertEq(3L, *lf, ss.str()); |
---|
102 | std::cout << "Success" << std::endl << "Lazifying tests"; |
---|
103 | |
---|
104 | auto lfun = lazify([] () { return Ref::mk (20L); }); |
---|
105 | Lazy<Int> lg = lfun(); |
---|
106 | ss.empty() << "*lg should return 20L but it returns " << *lg; |
---|
107 | assertEq(20L, *lg, ss.str()); |
---|
108 | |
---|
109 | auto lfun1 = lazify([] (R<Int> i) { return Ref::mk (*i + 20L); }); |
---|
110 | Lazy<Int> lh = lfun1(lg); |
---|
111 | ss.empty() << "*lh should return 40L but it returns " << *lh; |
---|
112 | assertEq(40L, *lh, ss.str()); |
---|
113 | |
---|
114 | std::cout << "Success" << std::endl; |
---|
115 | } |
---|