1 | #include <iostream> |
---|
2 | #include "test_base.h" |
---|
3 | #include "../list.h" |
---|
4 | #include "fib.h" |
---|
5 | #include "../lazy_op.h" |
---|
6 | |
---|
7 | void test(); |
---|
8 | |
---|
9 | int main() |
---|
10 | { |
---|
11 | try |
---|
12 | { |
---|
13 | std::cout << "**** Testing lazy lists ****" << std::endl; |
---|
14 | test(); |
---|
15 | std::cout << "Test SUCCEEDED!" << std::endl; |
---|
16 | } |
---|
17 | catch (TestFailed& ex) |
---|
18 | { |
---|
19 | std::cout << "Failed" << std::endl << "Test FAILED: " << ex.what() << std::endl; |
---|
20 | } |
---|
21 | catch (std::exception& ex) |
---|
22 | { |
---|
23 | std::cout << "Failed" << std::endl << "Test FAILED with std::exception: " << ex.what() << std::endl; |
---|
24 | } |
---|
25 | catch (...) |
---|
26 | { |
---|
27 | std::cout << "Failed" << std::endl << "Test FAILED with unknown exception" << std::endl; |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | typedef Lazy<Int> (*IFun)(Lazy<Int>); |
---|
32 | typedef Lazy<Int> (*AFun)(Lazy<Int>, Lazy<Int>); |
---|
33 | |
---|
34 | Lazy<Int> add(Lazy<Int> a, Lazy<Int> b) |
---|
35 | { |
---|
36 | return mkLazy(mkLFun([](Lazy<Int> a, Lazy<Int> b) { return Ref::mk (*a + *b); }, a, b)); |
---|
37 | } |
---|
38 | |
---|
39 | void test() |
---|
40 | { |
---|
41 | StringStream ss; |
---|
42 | std::cout << "Basic tests"; |
---|
43 | // infinite list |
---|
44 | auto fib = mkFib(); |
---|
45 | |
---|
46 | Int nth20 = *fib.drop(20).head(); |
---|
47 | assertEq(nth20, 6765L, "20th fib must be 6765 and it is not!"); |
---|
48 | Int nth30 = *fib.drop(30).head(); |
---|
49 | assertEq(nth30, 832040L, "30th fib must be 832040 and it is not!"); |
---|
50 | Int nth40 = *fib.drop(40).head(); |
---|
51 | assertEq(nth40, 102334155L, "40th fib must be 102334155 and it is not!"); |
---|
52 | |
---|
53 | // auto sqr = mkPFun1(&LazyOp<Int>::sqr); |
---|
54 | // auto inc = mkPFun1(&LazyOp<Int>::inc); |
---|
55 | // auto ascList = map(sqr, iterate(inc, suspend(0))); |
---|
56 | |
---|
57 | auto ascList = LazyList<Int>::iterate(LazyOp<Int>::inc, 0L); |
---|
58 | nth20 = *ascList.drop(20).head(); |
---|
59 | nth30 = *ascList.drop(30).head(); |
---|
60 | nth40 = *ascList.drop(40).head(); |
---|
61 | assertEq(nth20, 20L, "20th item must be 20 and it is not!"); |
---|
62 | assertEq(nth30, 30L, "30th item must be 30 and it is not!"); |
---|
63 | assertEq(nth40, 40L, "40th item must be 40 and it is not!"); |
---|
64 | |
---|
65 | auto sqrList = ascList.map(LazyOp<Int>::sqr); |
---|
66 | nth20 = *sqrList.drop(20).head(); |
---|
67 | nth30 = *sqrList.drop(30).head(); |
---|
68 | nth40 = *sqrList.drop(40).head(); |
---|
69 | assertEq(nth20, 400L, "20th square must be 400 and it is not!"); |
---|
70 | assertEq(nth30, 900L, "30th square must be 900 and it is not!"); |
---|
71 | assertEq(nth40, 1600L, "40th square must be 1600 and it is not!"); |
---|
72 | |
---|
73 | auto psqrList = ascList.filter(mkLFun1([] (Lazy<Int> i) -> R<bool> { return *i % 2 == 0; })).map(LazyOp<Int>::sqr); |
---|
74 | nth20 = *psqrList.drop(20).head(); |
---|
75 | nth30 = *psqrList.drop(30).head(); |
---|
76 | nth40 = *psqrList.drop(40).head(); |
---|
77 | assertEq(nth20, 1600L, "20th square must be 1600 and it is not!"); |
---|
78 | assertEq(nth30, 3600L, "30th square must be 3600 and it is not!"); |
---|
79 | assertEq(nth40, 6400L, "40th square must be 6400 and it is not!"); |
---|
80 | |
---|
81 | std::cout << "Success" << std::endl; |
---|
82 | } |
---|