forked from robertknight/qt-maybe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestMaybe.cpp
89 lines (71 loc) · 1.76 KB
/
TestMaybe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "TestMaybe.h"
#include "Maybe.h"
#include <QtTest/QtTest>
Q_DECLARE_METATYPE(int*);
void TestMaybe::testMaybe()
{
Maybe<int> tryOk = just(42);
Maybe<int> tryFail = nothing();
Maybe<int> tryZero = just(0);
QVERIFY(tryOk);
QVERIFY(!tryFail);
QVERIFY(tryZero);
QCOMPARE(tryZero.value(),0);
QCOMPARE(tryOk.value(),42);
}
void TestMaybe::testMaybeCast()
{
Maybe<QString> fromCStr = just("hello world");
Maybe<QString> fromNothing = nothing();
QVERIFY(!fromNothing);
QCOMPARE(fromCStr.value(),QString("hello world"));
Maybe<char> maybeShort = just(42);
QVERIFY(maybeShort);
QCOMPARE(maybeShort.value(),'*');
}
struct BigStruct
{
double array[42];
};
void TestMaybe::testMaybeSize()
{
Maybe<int> maybeInt;
Maybe<double> maybeDouble;
Maybe<int*> maybeIntPtr;
Maybe<QString> maybeQString;
Maybe<BigStruct> maybeBig;
QCOMPARE(sizeof(maybeInt),12U);
QCOMPARE(sizeof(maybeDouble),12U);
QCOMPARE(sizeof(maybeIntPtr),12U);
QCOMPARE(sizeof(maybeQString),12U);
QCOMPARE(sizeof(maybeBig),12U);
}
void TestMaybe::testMaybePtr()
{
int foo = 42;
int* bar = 0;
Maybe<int*> nullPtr = just(bar);
Maybe<int*> nullPtr2 = nothing();
Maybe<int*> notNullPtr = just(&foo);
QVERIFY(!nullPtr);
QVERIFY(!nullPtr2);
QVERIFY(notNullPtr);
}
void TestMaybe::testEither()
{
Either<int,bool> eitherInt = some(42);
Either<int,bool> eitherBool = some(true);
QVERIFY(eitherInt.is<int>());
QVERIFY(eitherInt.is1st());
QVERIFY(!eitherInt.is<bool>());
QVERIFY(!eitherInt.is2nd());
QCOMPARE(eitherInt.as<int>(),42);
QCOMPARE(eitherInt.as1st(),42);
QVERIFY(eitherBool.is<bool>());
QVERIFY(eitherBool.is2nd());
QVERIFY(!eitherBool.is<int>());
QVERIFY(!eitherBool.is1st());
QCOMPARE(eitherBool.as<bool>(),true);
QCOMPARE(eitherBool.as2nd(),true);
}
QTEST_APPLESS_MAIN(TestMaybe)