CallableTest.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "../Callable.h"
00025 #include "Imports.cpp"
00026 
00027 namespace Utils {
00028 namespace Testing {
00029 
00031 
00036 class CallableTest : public CppUnit::TestFixture
00037 {
00038     CPPUNIT_TEST_SUITE (CallableTest);
00039     CPPUNIT_TEST (testNullVoidVoid);
00040     CPPUNIT_TEST (testNullIntVoid);
00041     CPPUNIT_TEST (testCallStaticVoidVoid);
00042     CPPUNIT_TEST (testCallStaticIntVoid);
00043     CPPUNIT_TEST (testCallMemberVoidVoid);
00044     CPPUNIT_TEST (testCallMemberIntVoid);
00045     CPPUNIT_TEST (testCallMemberVoidVoidObjectPointer);
00046     CPPUNIT_TEST (testCallMemberVoidVoidConst);
00047     CPPUNIT_TEST (testCallMemberIntVoidConst);
00048     CPPUNIT_TEST (testCallMemberVoidVoidConstObjectPointer);
00049     CPPUNIT_TEST (testCallMemberVoidInt);
00050     CPPUNIT_TEST_SUITE_END();
00051 
00052     static bool hasCalledStaticVoidVoid;
00053     static void staticVoidVoid()
00054     {
00055         hasCalledStaticVoidVoid = true;
00056     }
00057 
00058     static bool hasCalledStaticIntVoid;
00059     static int staticIntVoid()
00060     {
00061         hasCalledStaticIntVoid = true;
00062         return 3;
00063     }
00064 
00065     bool hasCalledMemberVoidVoid;
00066     void memberVoidVoid()
00067     {
00068         hasCalledMemberVoidVoid = true;
00069     }
00070 
00071     bool hasCalledMemberIntVoid;
00072     int memberIntVoid()
00073     {
00074         hasCalledMemberIntVoid = true;
00075         return 4;
00076     }
00077 
00078     bool hasCalledMemberVoidInt;
00079     void memberVoidInt (int value)
00080     {
00081         hasCalledMemberVoidInt = true;
00082         CPPUNIT_ASSERT (value == 6);
00083     }
00084 
00085     mutable bool hasCalledMemberVoidVoidConst;
00086     void memberVoidVoidConst() const
00087     {
00088         hasCalledMemberVoidVoidConst = true;
00089     }
00090 
00091     mutable bool hasCalledMemberIntVoidConst;
00092     int memberIntVoidConst() const
00093     {
00094         hasCalledMemberIntVoidConst = true;
00095         return 5;
00096     }
00097 
00098 public:
00099     CallableTest()
00100      :  hasCalledMemberVoidVoid (false),
00101         hasCalledMemberIntVoid (false),
00102         hasCalledMemberVoidInt (false),
00103         hasCalledMemberVoidVoidConst (false),
00104         hasCalledMemberIntVoidConst (false)
00105     {
00106     }
00107 
00108     void testNullVoidVoid()
00109     {
00110         Callable <void ()> callable;
00111         callable();
00112     }
00113 
00114     void testNullIntVoid()
00115     {
00116         Callable <int ()> callable;
00117         int returnValue = callable();
00118         CPPUNIT_ASSERT (returnValue == int());
00119     }
00120 
00121     void testCallStaticVoidVoid()
00122     {
00123         hasCalledStaticVoidVoid = false;
00124         Callable <void ()> callable (&staticVoidVoid);
00125         callable();
00126         CPPUNIT_ASSERT (hasCalledStaticVoidVoid == true);
00127     }
00128 
00129     void testCallStaticIntVoid()
00130     {
00131         hasCalledStaticIntVoid = false;
00132         Callable <int ()> callable (&staticIntVoid);
00133         int returnedValue = callable();
00134         CPPUNIT_ASSERT (hasCalledStaticIntVoid == true);
00135         CPPUNIT_ASSERT (returnedValue == 3);
00136     }
00137 
00138     void testCallMemberVoidVoid()
00139     {
00140         hasCalledMemberVoidVoid = false;
00141         Callable <void ()> callable (*this, &CallableTest::memberVoidVoid);
00142         callable();
00143         CPPUNIT_ASSERT (hasCalledMemberVoidVoid == true);
00144     }
00145 
00146     void testCallMemberIntVoid()
00147     {
00148         hasCalledMemberIntVoid = false;
00149         Callable <int ()> callable (*this, &CallableTest::memberIntVoid);
00150         int returnedValue = callable();
00151         CPPUNIT_ASSERT (hasCalledMemberIntVoid == true);
00152         CPPUNIT_ASSERT (returnedValue == 4);
00153     }
00154 
00155     void testCallMemberVoidVoidObjectPointer()
00156     {
00157         hasCalledMemberVoidVoid = false;
00158         Callable <void ()> callable (this, &CallableTest::memberVoidVoid);
00159         callable();
00160         CPPUNIT_ASSERT (hasCalledMemberVoidVoid == true);
00161     }
00162 
00163     void testCallMemberVoidVoidConst()
00164     {
00165         hasCalledMemberVoidVoidConst = false;
00166         Callable <void ()> callable (*this, &CallableTest::memberVoidVoidConst);
00167         callable();
00168         CPPUNIT_ASSERT (hasCalledMemberVoidVoidConst == true);
00169     }
00170 
00171     void testCallMemberIntVoidConst()
00172     {
00173         hasCalledMemberIntVoidConst = false;
00174         Callable <int ()> callable (*this, &CallableTest::memberIntVoidConst);
00175         int returnedValue = callable();
00176         CPPUNIT_ASSERT (hasCalledMemberIntVoidConst == true);
00177         CPPUNIT_ASSERT (returnedValue == 5);
00178     }
00179 
00180     void testCallMemberVoidVoidConstObjectPointer()
00181     {
00182         hasCalledMemberVoidVoidConst = false;
00183         Callable <void ()> callable (this, &CallableTest::memberVoidVoidConst);
00184         callable();
00185         CPPUNIT_ASSERT (hasCalledMemberVoidVoidConst == true);
00186     }
00187 
00188     void testCallMemberVoidInt()
00189     {
00190         hasCalledMemberVoidInt = false;
00191         Callable <void (int)> callable (this, &CallableTest::memberVoidInt);
00192         callable (6);
00193         CPPUNIT_ASSERT (hasCalledMemberVoidInt == true);
00194     }
00195 };
00196 
00197 bool CallableTest::hasCalledStaticVoidVoid = false;
00198 bool CallableTest::hasCalledStaticIntVoid = false;
00199 
00200 CPPUNIT_TEST_SUITE_REGISTRATION(CallableTest);
00201 
00202 } // namespace Testing
00203 } // namespace Utils