signal_slot_unittest.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: signal_slot_unittest.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-images.
00007  *
00008  * uds-plugin-images is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-images is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include <cassert>
00028 #include <tut.h>
00029 #include <tut_reporter.h>
00030 #include "signal_slot.h"
00031 #include "log.h"
00032 
00033 namespace testslot
00034 {
00035 typedef std::vector<int> Integers;
00036 
00037 class SlotA
00038 {
00039 public:
00040     SlotA(){}
00041     ~SlotA(){}
00042 
00043 public:
00044     void on_signal_void()
00045     {
00046         LOGPRINTF("SlotA void ");
00047     }
00048 
00049     // Slot handler 
00050     void on_signal_string(const std::string & name)
00051     {
00052         LOGPRINTF("SlotA %p : on_signal_string value %s", name.c_str());
00053     }
00054 
00055     void on_signal_int(int first, int second)
00056     {
00057         LOGPRINTF("SlotA(%p): on_signal_int first %d second %d\n", 
00058                   this, first, second);
00059     }
00060     
00061     void on_signal_three(const std::string & a, int b, const Integers & c)
00062     {
00063         LOGPRINTF("SlotA(%p): on_signal_three a %s ", this, a.c_str());
00064         LOGPRINTF("%d ", b);
00065         for(Integers::const_iterator it = c.begin(); it != c.end(); ++it)
00066         {
00067             printf("%d ", *it);
00068         }
00069         printf("\n");
00070     }
00071 
00072     void on_signal_four(const std::string & a, int b, const Integers & c, const std::string &d)
00073     {
00074         LOGPRINTF("SlotA(%p): on_signal_four a %s ", this, a.c_str());
00075         LOGPRINTF("%d ", b);
00076         for(Integers::const_iterator it = c.begin(); it != c.end(); ++it)
00077         {
00078             LOGPRINTF("%d ", *it);
00079         }
00080         LOGPRINTF("%s", d.c_str());
00081         LOGPRINTF("\n");
00082     }
00083 
00084     void on_signal_five(const std::string & a, int b, 
00085                         const Integers & c, 
00086                         const std::string &d, 
00087                         const std::string &e)
00088     {
00089         LOGPRINTF("SlotA(%p): on_signal_five a %s ", this, a.c_str());
00090         LOGPRINTF("%d ", b);
00091         for(Integers::const_iterator it = c.begin(); it != c.end(); ++it)
00092         {
00093             LOGPRINTF("%d ", *it);
00094         }
00095         LOGPRINTF("%s ", d.c_str());
00096         LOGPRINTF("%s ", e.c_str());
00097         LOGPRINTF("\n");
00098     }
00099 };
00100 
00101 class SlotB
00102 {
00103 public:
00104     SlotB(){}
00105     ~SlotB(){}
00106 
00107 public:
00108     void on_signal_string(const std::string & name)
00109     {
00110         printf("SlotB(%p): on_signal_string value \"%s\"\n", this, name.c_str());
00111     }
00112 
00113     void on_signal_int(int first, int second)
00114     {
00115         printf("SlotB(%p): on_signal_int first %d second %d\n", this, first, second);
00116     }
00117     
00118     void on_signal_three(const std::string & a, int b, const Integers & c)
00119     {
00120         printf("SlotB(%p): on_signal_three a %s ", this, a.c_str());
00121         printf("%d ", b);
00122         for(Integers::const_iterator it = c.begin(); it != c.end(); ++it)
00123         {
00124             printf("%d ", *it);
00125         }
00126         printf("\n");
00127     }
00128 
00129     void on_signal_four(const std::string & a, int b, const Integers & c, const std::string &d)
00130     {
00131         printf("SlotB(%p): on_signal_four a %s ", this, a.c_str());
00132         printf("%d ", b);
00133         for(Integers::const_iterator it = c.begin(); it != c.end(); ++it)
00134         {
00135             printf("%d ", *it);
00136         }
00137         printf("%s", d.c_str());
00138         printf("\n");
00139     }
00140 
00141     void on_signal_five(const std::string & a, int b, 
00142                         const Integers & c, 
00143                         const std::string &d, 
00144                         const std::string &e)
00145     {
00146         printf("SlotB(%p): on_signal_five a %s ", this, a.c_str());
00147         printf("%d ", b);
00148         for(Integers::const_iterator it = c.begin(); it != c.end(); ++it)
00149         {
00150             printf("%d ", *it);
00151         }
00152         printf("%s ", d.c_str());
00153         printf("%s ", e.c_str());
00154         printf("\n");
00155     }
00156 };
00157 
00158 
00159 };  // anonymous namespace
00160 
00161 namespace tut
00162 {
00163     struct empty
00164     { 
00165     };
00166 
00167     typedef test_group<empty> tf;
00168     typedef tf::object object;
00169     tf signal_slot_group("signal slot test");
00170 
00171     // Test 1: Basic test.
00172     template<>
00173     template<>
00174     void object::test<1>()
00175     {
00176         using namespace utils;
00177         using namespace testslot;
00178         Signal<> xx;
00179         Signal<const std::string &> signal_a;
00180         Signal<int, int> signal_b;
00181         Signal<const std::string &, int, const Integers &> signal_c;
00182         Signal<const std::string &, int, const Integers &, const std::string &> signal_d;
00183         Signal<const std::string &, int, const Integers &, const std::string &, const std::string &> signal_e;
00184         SlotA slot_a1, slot_a2;
00185         SlotB slot_b;
00186 
00187         // prepare parameters;
00188         std::string name = "destroy signal!";
00189         int first = 1, second = 100;
00190         Integers ints;
00191         for(int i = 0; i < 3; ++i)
00192         {
00193             ints.push_back(i);
00194         }
00195 
00196         // xx.add_slot(&slot_a1, &SlotA::on_signal_void);
00197         // xx.broadcast();
00198 
00199 
00200         signal_a.add_slot(&slot_a1, &SlotA::on_signal_string);
00201         signal_a.add_slot(&slot_a2, &SlotA::on_signal_string);
00202         signal_a.add_slot(&slot_b, &SlotB::on_signal_string);
00203         signal_a.broadcast(name);
00204         // signal_a.broadcast();
00205 
00206         signal_b.add_slot(&slot_a1, &SlotA::on_signal_int);
00207         signal_b.add_slot(&slot_a2, &SlotA::on_signal_int);
00208         signal_b.add_slot(&slot_b, &SlotB::on_signal_int);
00209         signal_b.broadcast(first, second);
00210         signal_b.broadcast(first);
00211 
00212         signal_c.add_slot(&slot_a1, &SlotA::on_signal_three);
00213         signal_c.add_slot(&slot_a2, &SlotA::on_signal_three);
00214         signal_c.add_slot(&slot_b, &SlotB::on_signal_three);
00215         signal_c.broadcast(name, first, ints);
00216 
00217         signal_d.add_slot(&slot_a1, &SlotA::on_signal_four);
00218         signal_d.add_slot(&slot_a2, &SlotA::on_signal_four);
00219         signal_d.add_slot(&slot_b, &SlotB::on_signal_four);
00220         signal_d.broadcast(name, first, ints, "another one");
00221 
00222         signal_e.add_slot(&slot_a1, &SlotA::on_signal_five);
00223         signal_e.add_slot(&slot_a2, &SlotA::on_signal_five);
00224         signal_e.add_slot(&slot_b, &SlotB::on_signal_five);
00225         signal_e.broadcast(name, first, ints, "another one", "last one");
00226 
00227 
00228     }
00229 
00230     // Test 2: Test remove.
00231     template<>
00232     template<>
00233     void object::test<2>()
00234     {
00235         using namespace utils;
00236         using namespace testslot;
00237         Signal<const std::string &> signal_a;
00238         Signal<int, int> signal_b;
00239         Signal<const std::string &, int, const Integers &> signal_c;
00240         Signal<const std::string &, int, const Integers &, const std::string &> signal_d;
00241         Signal<const std::string &, int, const Integers &, const std::string &, const std::string &> signal_e;
00242         SlotA slot_a1, slot_a2;
00243         SlotB slot_b;
00244 
00245         // prepare parameters;
00246         std::string name = "destroy signal!";
00247         int first = 1, second = 100;
00248         Integers ints;
00249         for(int i = 0; i < 3; ++i)
00250         {
00251             ints.push_back(i);
00252         }
00253 
00254 
00255         signal_a.add_slot(&slot_a1, &SlotA::on_signal_string);
00256         signal_a.add_slot(&slot_a2, &SlotA::on_signal_string);
00257         signal_a.remove_slot(&slot_a1, &SlotA::on_signal_string);
00258         signal_a.broadcast(name);
00259 
00260         signal_b.add_slot(&slot_a1, &SlotA::on_signal_int);
00261         signal_b.add_slot(&slot_a2, &SlotA::on_signal_int);
00262         signal_b.remove_slot(&slot_a2, &SlotA::on_signal_int);
00263         signal_b.broadcast(first, second);
00264 
00265         signal_c.add_slot(&slot_a1, &SlotA::on_signal_three);
00266         signal_c.add_slot(&slot_a2, &SlotA::on_signal_three);
00267         signal_c.remove_slot(&slot_a1, &SlotA::on_signal_three);
00268         signal_c.broadcast(name, first, ints);
00269 
00270         signal_d.add_slot(&slot_a1, &SlotA::on_signal_four);
00271         signal_d.add_slot(&slot_a2, &SlotA::on_signal_four);
00272         signal_d.remove_slot(&slot_a2, &SlotA::on_signal_four);
00273         signal_d.broadcast(name, first, ints, "another one");
00274 
00275         signal_e.add_slot(&slot_a1, &SlotA::on_signal_five);
00276         signal_e.add_slot(&slot_a2, &SlotA::on_signal_five);
00277         signal_e.remove_slot(&slot_a1, &SlotA::on_signal_five);
00278         signal_e.broadcast(name, first, ints, "another one", "last one");
00279 
00280     }
00281 
00282 
00283     // Test 3: Test repeat adding. TODO.
00284     template<>
00285     template<>
00286     void object::test<3>()
00287     {
00288         using namespace utils;
00289         using namespace testslot;
00290         Signal<const std::string &> signal_a;
00291         Signal<int, int> signal_b;
00292         Signal<const std::string &, int, const Integers &> signal_c;
00293         Signal<const std::string &, int, const Integers &, const std::string &> signal_d;
00294         Signal<const std::string &, int, const Integers &, const std::string &, const std::string &> signal_e;
00295         SlotA slot_a1, slot_a2;
00296         SlotB slot_b;
00297 
00298         // prepare parameters;
00299         std::string name = "destroy signal!";
00300         int first = 1, second = 100;
00301         Integers ints;
00302         for(int i = 0; i < 3; ++i)
00303         {
00304             ints.push_back(i);
00305         }
00306 
00307 
00308         signal_a.add_slot(&slot_a1, &SlotA::on_signal_string);
00309         signal_a.add_slot(&slot_a2, &SlotA::on_signal_string);
00310         signal_a.remove_slot(&slot_a1, &SlotA::on_signal_string);
00311         signal_a.broadcast(name);
00312                
00313         
00314         signal_b.add_slot(&slot_a1, &SlotA::on_signal_int);
00315         signal_b.add_slot(&slot_a2, &SlotA::on_signal_int);
00316         signal_b.remove_slot(&slot_a2, &SlotA::on_signal_int);
00317         signal_b.broadcast(first, second);
00318         signal_b.broadcast(first);
00319 
00320         signal_c.add_slot(&slot_a1, &SlotA::on_signal_three);
00321         signal_c.add_slot(&slot_a2, &SlotA::on_signal_three);
00322         signal_c.remove_slot(&slot_a1, &SlotA::on_signal_three);
00323         signal_c.broadcast(name, first, ints);
00324 
00325         signal_d.add_slot(&slot_a1, &SlotA::on_signal_four);
00326         signal_d.add_slot(&slot_a2, &SlotA::on_signal_four);
00327         signal_d.remove_slot(&slot_a2, &SlotA::on_signal_four);
00328         signal_d.broadcast(name, first, ints, "another one");
00329 
00330         signal_e.add_slot(&slot_a1, &SlotA::on_signal_five);
00331         signal_e.add_slot(&slot_a2, &SlotA::on_signal_five);
00332         signal_e.remove_slot(&slot_a1, &SlotA::on_signal_five);
00333         signal_e.broadcast(name, first, ints, "another one", "last one");
00334 
00335     }
00336 
00337 };
00338 
00339 
00340 using std::exception;
00341 using std::cerr;
00342 using std::endl;
00343 
00344 namespace tut
00345 {
00346     test_runner_singleton runner;
00347 }
00348 
00349 int main()
00350 {
00351     tut::reporter reporter;
00352     tut::runner.get().set_callback(&reporter);
00353 
00354     try
00355     {
00356         tut::runner.get().run_tests();
00357     }
00358     catch (const std::exception& ex)
00359     {
00360         cerr << "tut raised ex: " << ex.what() << endl;
00361         return 1;
00362     }
00363 
00364     return 0;
00365 }
00366 
00367 
00368 
Generated by  doxygen 1.6.2-20100208