#include "TestMaybe.h" #include "Maybe.h" #include Q_DECLARE_METATYPE(int*); void TestMaybe::testMaybe() { Maybe tryOk = just(42); Maybe tryFail = nothing(); Maybe tryZero = just(0); QVERIFY(tryOk); QVERIFY(!tryFail); QVERIFY(tryZero); QCOMPARE(tryZero.value(),0); QCOMPARE(tryOk.value(),42); } void TestMaybe::testMaybeCast() { Maybe fromCStr = just("hello world"); Maybe fromNothing = nothing(); QVERIFY(!fromNothing); QCOMPARE(fromCStr.value(),QString("hello world")); Maybe maybeShort = just(42); QVERIFY(maybeShort); QCOMPARE(maybeShort.value(),'*'); } struct BigStruct { double array[42]; }; void TestMaybe::testMaybeSize() { Maybe maybeInt; Maybe maybeDouble; Maybe maybeIntPtr; Maybe maybeQString; Maybe 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 nullPtr = just(bar); Maybe nullPtr2 = nothing(); Maybe notNullPtr = just(&foo); QVERIFY(!nullPtr); QVERIFY(!nullPtr2); QVERIFY(notNullPtr); } void TestMaybe::testEither() { Either eitherInt = some(42); Either eitherBool = some(true); QVERIFY(eitherInt.is()); QVERIFY(eitherInt.is1st()); QVERIFY(!eitherInt.is()); QVERIFY(!eitherInt.is2nd()); QCOMPARE(eitherInt.as(),42); QCOMPARE(eitherInt.as1st(),42); QVERIFY(eitherBool.is()); QVERIFY(eitherBool.is2nd()); QVERIFY(!eitherBool.is()); QVERIFY(!eitherBool.is1st()); QCOMPARE(eitherBool.as(),true); QCOMPARE(eitherBool.as2nd(),true); } QTEST_APPLESS_MAIN(TestMaybe)