LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
eithertest.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 "eithertest.h"
10#include <QtTest>
11#include <either.h>
12#include <curry.h>
13#include <void.h>
14#include <functor.h>
15#include <monad.h>
16
17QTEST_MAIN (LC::Util::EitherTest)
18
19namespace LC
20{
21namespace Util
22{
24
25 void EitherTest::testBasicLeft ()
26 {
27 const auto& left = SomeEither_t::Left (1);
28 QCOMPARE (left.IsLeft (), true);
29 QCOMPARE (left.IsRight (), false);
30 QCOMPARE (left.GetLeft (), 1);
31
32 bool hadCaught = false;
33 try
34 {
35 left.GetRight ();
36 }
37 catch (const std::exception&)
38 {
39 hadCaught = true;
40 }
41 QCOMPARE (hadCaught, true);
42 }
43
44 void EitherTest::testBasicRight ()
45 {
46 const auto& right = SomeEither_t::Right ("foo");
47 QCOMPARE (right.IsLeft (), false);
48 QCOMPARE (right.IsRight (), true);
49 QCOMPARE (right.GetRight (), QString { "foo" });
50
51 bool hadCaught = false;
52 try
53 {
54 right.GetLeft ();
55 }
56 catch (const std::exception&)
57 {
58 hadCaught = true;
59 }
60 QCOMPARE (hadCaught, true);
61 }
62
63 void EitherTest::testFMapLeft ()
64 {
65 const auto& left = SomeEither_t::Left (1);
66 const auto& fmapped = Fmap (left, [] (const QString& str) { return str + "_mapped"; });
67 QCOMPARE (fmapped, left);
68 }
69
70 void EitherTest::testFMapRight ()
71 {
72 const auto& right = SomeEither_t::Right ("foo");
73 const auto& fmapped = Fmap (right, [] (const QString& str) { return str + "_mapped"; });
74 QCOMPARE (fmapped.GetRight (), QString { "foo_mapped" });
75 }
76
77 void EitherTest::testFMapRightChangeType ()
78 {
79 const auto& right = SomeEither_t::Right ("foo");
80 const auto& fmapped = Fmap (right, [] (const QString& str) { return static_cast<long> (str.size ()); });
81 QCOMPARE (fmapped.GetRight (), static_cast<long> (right.GetRight ().size ()));
82 }
83
84 void EitherTest::testPure ()
85 {
86 const auto& pure = Pure<Either, int> (QString { "foo" });
87 QCOMPARE (pure, SomeEither_t::Right ("foo"));
88 }
89
90 void EitherTest::testGSL ()
91 {
92 const auto& pure = Pure<Either, int> ([] (const QString& s) { return s + "_pure"; });
93 const auto& app = pure * Pure<Either, int> (QString { "foo" });
94 QCOMPARE (app, SomeEither_t::Right ("foo_pure"));
95 }
96
97 void EitherTest::testGSLLeft ()
98 {
99 const auto& pure = Pure<Either, int> ([] (const QString& s) { return s + "_pure"; });
100 const auto& value = SomeEither_t::Left (2);
101 const auto& app = pure * value;
102 QCOMPARE (app, value);
103 }
104
105 void EitherTest::testGSLCurry ()
106 {
107 const auto& summer = Pure<Either, int> (Curry ([] (const QString& a, const QString& b) { return a + b; }));
108 const auto& s1 = Pure<Either, int> (QString { "foo" });
109 const auto& s2 = Pure<Either, int> (QString { "bar" });
110 const auto& app = summer * s1 * s2;
111 QCOMPARE (app, SomeEither_t::Right ("foobar"));
112 }
113
114 void EitherTest::testGSLCurryLeft ()
115 {
116 const auto& summer = Pure<Either, int> (Curry ([] (const QString& a, const QString& b) { return a + b; }));
117 const auto& s1 = SomeEither_t::Left (2);
118 const auto& s2 = Pure<Either, int> (QString { "bar" });
119 const auto& app = summer * s1 * s2;
120 QCOMPARE (app, s1);
121 }
122
123 void EitherTest::testBind ()
124 {
125 const auto& res = Return<Either, int> (QString { "foo" }) >>
126 [] (const QString& right) { return SomeEither_t::Right (right + "_bound"); };
127 QCOMPARE (res, SomeEither_t::Right ("foo_bound"));
128 }
129
130 void EitherTest::testBindLeft ()
131 {
132 const auto& value = SomeEither_t::Left (2);
133 const auto& res = value >>
134 [] (const QString& right) { return SomeEither_t::Right (right + "_bound"); };
135 QCOMPARE (res, value);
136 }
137
139 {
140 NoDefaultCtor () = delete;
141 NoDefaultCtor (const QString&)
142 {
143 }
144
145 bool operator== (const NoDefaultCtor&) const
146 {
147 return true;
148 }
149 };
150
151 void EitherTest::testBindLeftNotConstructed ()
152 {
153 const auto& value = Either<NoDefaultCtor, Void>::Right ({});
154 const auto& expected = Either<NoDefaultCtor, int>::Right (5);
155 const auto res = value >>
156 [&expected] (const auto&) { return expected; };
157 QCOMPARE (res, expected);
158 }
159}
160}
static Either Left(const L &l)
Definition either.h:119
static Either Right(R &&r)
Definition either.h:124
FmapResult_t< T, F > Fmap(const T &functor, const F &function)
Apply the function f to the elements in functor.
Definition functor.h:128
auto Return(const V &v)
Definition monad.h:24
CurryImpl< std::decay_t< F >, Args... > Curry(F &&f, Args &&... args)
Definition curry.h:72
auto Pure(const T &v)
Definition applicative.h:25
Either< int, QString > SomeEither_t
Definition constants.h:15
bool operator==(const NoDefaultCtor &) const
NoDefaultCtor(const QString &)