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