LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
monadtest.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "monadtest.h"
31 #include <QtTest>
32 #include <monad.h>
33 #include <typelist.h>
34 
35 QTEST_MAIN (LeechCraft::Util::MonadTest)
36 
37 namespace LeechCraft
38 {
39 namespace Util
40 {
41  void MonadTest::testBoostOptionalReturn ()
42  {
43  const auto& pure = Return<boost::optional> (2);
44  QCOMPARE (pure, boost::optional<int> { 2 });
45  }
46 
47  void MonadTest::testBoostOptionalBind ()
48  {
49  const auto& pure = Return<boost::optional> (2);
50  const auto& result = Bind (pure, [] (int value) { return boost::optional<int> { ++value }; });
51  QCOMPARE (result, boost::optional<int> { 3 });
52  }
53 
54  void MonadTest::testBoostOptionalBindEmpty ()
55  {
56  const auto& result = Bind (boost::optional<int> {}, [] (int value) { return boost::optional<int> { ++value }; });
57  QCOMPARE (result, boost::optional<int> {});
58  }
59 
60  void MonadTest::testBoostOptionalBindOperator ()
61  {
62  const auto& pure = Return<boost::optional> (2);
63  const auto& result = pure >> [] (int value) { return boost::optional<int> { ++value }; };
64  QCOMPARE (result, boost::optional<int> { 3 });
65  }
66 
67  void MonadTest::testBoostOptionalBindEmptyOperator ()
68  {
69  const auto& result = boost::optional<int> {} >> [] (int value) { return boost::optional<int> { ++value }; };
70  QCOMPARE (result, boost::optional<int> {});
71  }
72 
73  void MonadTest::testBoostOptionalDo ()
74  {
75  const auto& result = Do (boost::optional<int> { 2 },
76  [] (int a) -> boost::optional<int> { return a * 2; },
77  [] (int a) -> boost::optional<int> { return a + 1; },
78  [] (int a) -> boost::optional<int> { return a * 3; });
79  QCOMPARE (result, boost::optional<int> { 15 });
80  }
81 
82  void MonadTest::testBoostOptionalDoEmpty ()
83  {
84  bool called = false;
85  const auto& result = Do (boost::optional<int> { 2 },
86  [] (int a) -> boost::optional<int> { return a * 2; },
87  [] (int) -> boost::optional<int> { return {}; },
88  [&called] (int a) -> boost::optional<int> { called = true; return a * 3; });
89 
90  QCOMPARE (result, boost::optional<int> {});
91  QCOMPARE (called, false);
92  }
93 
94  void MonadTest::testCompatibilitySingle ()
95  {
96  constexpr auto result = detail::IsCompatibleMonad<
97  Typelist<QString>,
98  Typelist<QString>
99  > ();
100  QCOMPARE (result, true);
101  }
102 
103  void MonadTest::testCompatibilitySingleDif ()
104  {
105  constexpr auto result = detail::IsCompatibleMonad<
106  Typelist<QString>,
107  Typelist<QByteArray>
108  > ();
109  QCOMPARE (result, true);
110  }
111 
112  void MonadTest::testCompatibilityMulti ()
113  {
114  constexpr auto result = detail::IsCompatibleMonad<
115  Typelist<int, float, QString>,
116  Typelist<int, float, QString>
117  > ();
118  QCOMPARE (result, true);
119  }
120 
121  void MonadTest::testCompatibilityMultiDifEnd ()
122  {
123  constexpr auto result = detail::IsCompatibleMonad<
124  Typelist<int, float, QString>,
125  Typelist<int, float, QByteArray>
126  > ();
127  QCOMPARE (result, true);
128  }
129 
130  void MonadTest::testInCompatibilityMulti ()
131  {
132  constexpr auto result = detail::IsCompatibleMonad<
133  Typelist<int, float, QString>,
134  Typelist<int, double, QString>
135  > ();
136  QCOMPARE (result, false);
137  }
138 
139  void MonadTest::testInCompatibilityMultiStart ()
140  {
141  constexpr auto result = detail::IsCompatibleMonad<
142  Typelist<int, float, QString>,
143  Typelist<QByteArray, float, QString>
144  > ();
145  QCOMPARE (result, false);
146  }
147 }
148 }
constexpr bool IsCompatibleMonad()
Definition: monad.h:76
auto Do(const MV &value)
Definition: monad.h:97
BindResult_t< MV, F > Bind(const MV &value, const F &f)
Definition: monad.h:83