LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "monadtest.h"
10#include <QtTest>
11#include <monad.h>
12#include <typelist.h>
13
14QTEST_MAIN (LC::Util::MonadTest)
15
16namespace LC
17{
18namespace Util
19{
20 void MonadTest::testBoostOptionalReturn ()
21 {
22 const auto& pure = Return<std::optional> (2);
23 QCOMPARE (pure, std::optional<int> { 2 });
24 }
25
26 void MonadTest::testBoostOptionalBind ()
27 {
28 const auto& pure = Return<std::optional> (2);
29 const auto& result = Bind (pure, [] (int value) { return std::optional<int> { ++value }; });
30 QCOMPARE (result, std::optional<int> { 3 });
31 }
32
33 void MonadTest::testBoostOptionalBindEmpty ()
34 {
35 const auto& result = Bind (std::optional<int> {}, [] (int value) { return std::optional<int> { ++value }; });
36 QCOMPARE (result, std::optional<int> {});
37 }
38
39 void MonadTest::testBoostOptionalBindOperator ()
40 {
41 const auto& pure = Return<std::optional> (2);
42 const auto& result = pure >> [] (int value) { return std::optional<int> { ++value }; };
43 QCOMPARE (result, std::optional<int> { 3 });
44 }
45
46 void MonadTest::testBoostOptionalBindEmptyOperator ()
47 {
48 const auto& result = std::optional<int> {} >> [] (int value) { return std::optional<int> { ++value }; };
49 QCOMPARE (result, std::optional<int> {});
50 }
51
52 void MonadTest::testBoostOptionalDo ()
53 {
54 const auto& result = Do (std::optional<int> { 2 },
55 [] (int a) -> std::optional<int> { return a * 2; },
56 [] (int a) -> std::optional<int> { return a + 1; },
57 [] (int a) -> std::optional<int> { return a * 3; });
58 QCOMPARE (result, std::optional<int> { 15 });
59 }
60
61 void MonadTest::testBoostOptionalDoEmpty ()
62 {
63 bool called = false;
64 const auto& result = Do (std::optional<int> { 2 },
65 [] (int a) -> std::optional<int> { return a * 2; },
66 [] (int) -> std::optional<int> { return {}; },
67 [&called] (int a) -> std::optional<int> { called = true; return a * 3; });
68
69 QCOMPARE (result, std::optional<int> {});
70 QCOMPARE (called, false);
71 }
72
73 void MonadTest::testCompatibilitySingle ()
74 {
75 constexpr auto result = detail::IsCompatibleMonad<
76 Typelist<QString>,
77 Typelist<QString>
78 > ();
79 QCOMPARE (result, true);
80 }
81
82 void MonadTest::testCompatibilitySingleDif ()
83 {
84 constexpr auto result = detail::IsCompatibleMonad<
85 Typelist<QString>,
86 Typelist<QByteArray>
87 > ();
88 QCOMPARE (result, true);
89 }
90
91 void MonadTest::testCompatibilityMulti ()
92 {
93 constexpr auto result = detail::IsCompatibleMonad<
94 Typelist<int, float, QString>,
95 Typelist<int, float, QString>
96 > ();
97 QCOMPARE (result, true);
98 }
99
100 void MonadTest::testCompatibilityMultiDifEnd ()
101 {
102 constexpr auto result = detail::IsCompatibleMonad<
103 Typelist<int, float, QString>,
104 Typelist<int, float, QByteArray>
105 > ();
106 QCOMPARE (result, true);
107 }
108
109 void MonadTest::testInCompatibilityMulti ()
110 {
111 constexpr auto result = detail::IsCompatibleMonad<
112 Typelist<int, float, QString>,
113 Typelist<int, double, QString>
114 > ();
115 QCOMPARE (result, false);
116 }
117
118 void MonadTest::testInCompatibilityMultiStart ()
119 {
120 constexpr auto result = detail::IsCompatibleMonad<
121 Typelist<int, float, QString>,
122 Typelist<QByteArray, float, QString>
123 > ();
124 QCOMPARE (result, false);
125 }
126}
127}
constexpr bool IsCompatibleMonad()
Definition monad.h:56
auto Do(const MV &value)
Definition monad.h:77
auto Return(const V &v)
Definition monad.h:24
BindResult_t< MV, F > Bind(const MV &value, const F &f)
Definition monad.h:63
Definition constants.h:15