utils_unittest.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: utils_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 "utils.h"
00031 
00032 namespace
00033 {
00034 
00035 /// Images interface 
00036 class IPluginA
00037 {
00038 public:
00039     IPluginA(){}
00040     ~IPluginA(){}
00041 
00042 public:
00043     virtual void do_a() = 0;
00044 };
00045 
00046 class IPluginB
00047 {
00048 public:
00049     IPluginB(){}
00050     ~IPluginB(){}
00051 
00052     virtual void do_b() = 0;
00053 };
00054 
00055 class IPluginC
00056 {
00057 public:
00058     IPluginC(){}
00059     ~IPluginC(){}
00060 
00061     virtual void do_c() = 0;
00062 };
00063 
00064 
00065 /// Implement all images interfaces.
00066 class Impl : public IPluginA
00067            , public IPluginB
00068            , public IPluginC
00069 {
00070 public:
00071     Impl(){}
00072     ~Impl(){}
00073 
00074 public:
00075     virtual void do_a() { printf("this %p impl a\n", this); }
00076     virtual void do_b()  { printf("this %p impl b\n", this); }
00077     virtual void do_c()   { printf("this %p impl c\n", this); }
00078 };
00079 
00080 /// template test
00081 template <class T>
00082 class TemplateImpl : public IPluginA
00083                    , public IPluginB
00084                    , public IPluginC
00085 {
00086 public:
00087     TemplateImpl(){data.push_back(10);}
00088     ~TemplateImpl(){}
00089 
00090 public:
00091     virtual void do_a() { printf("this %p impl a\n", this); }
00092     virtual void do_b()  { printf("this %p impl b\n", this); }
00093     virtual void do_c()   { printf("this %p impl c\n", this); }
00094 
00095 private:
00096     std::vector<T> data;
00097 };
00098 
00099 };  // anonymous namespace
00100 
00101 namespace tut
00102 {
00103     struct empty
00104     { 
00105     };
00106 
00107     typedef test_group<empty> tf;
00108     typedef tf::object object;
00109     tf interface_ptr_group("interface utils test");
00110 
00111     // Test 1: Test the query_interface.
00112     template<>
00113     template<>
00114     void object::test<1>()
00115     {
00116         ::Impl instance;
00117         utils::ObjectTable<Impl> objects;
00118 
00119         // The object supports three interfaces.
00120         objects.add_interface<IPluginA>(&instance);
00121         objects.add_interface<IPluginB>(&instance);
00122         objects.add_interface<IPluginC>(&instance);
00123 
00124         IPluginA * pa = 0;
00125         IPluginB * pb = 0;
00126         IPluginC * pc = 0;
00127         objects.query_interface(&instance, "IPluginA", (void **)&pa);
00128         objects.query_interface(&instance, "IPluginB", (void **)&pb);
00129         objects.query_interface(&instance, "IPluginC", (void **)&pc);
00130 
00131         ensure(pa == static_cast<IPluginA *>(&instance));
00132         ensure(pb == static_cast<IPluginB *>(&instance));
00133         ensure(pc == static_cast<IPluginC *>(&instance));
00134     }
00135 
00136     // Test 2: Test the query_interface with template class.
00137     template<>
00138     template<>
00139     void object::test<2>()
00140     {
00141         ::TemplateImpl<int> instance;
00142         utils::ObjectTable<TemplateImpl<int>> objects;
00143 
00144         // The object supports three interfaces.
00145         objects.add_interface<IPluginA>(&instance);
00146         objects.add_interface<IPluginB>(&instance);
00147         objects.add_interface<IPluginC>(&instance);
00148 
00149         IPluginA * pa = 0;
00150         IPluginB * pb = 0;
00151         IPluginC * pc = 0;
00152         objects.query_interface(&instance, "IPluginA", (void **)&pa);
00153         objects.query_interface(&instance, "IPluginB", (void **)&pb);
00154         objects.query_interface(&instance, "IPluginC", (void **)&pc);
00155 
00156         ensure(pa == static_cast<IPluginA *>(&instance));
00157         ensure(pb == static_cast<IPluginB *>(&instance));
00158         ensure(pc == static_cast<IPluginC *>(&instance));
00159 
00160     }
00161 
00162     // Test 3: Test get_object which can retrieve object from interface.
00163     template<>
00164     template<>
00165     void object::test<3>()
00166     {
00167         ::Impl instance;
00168         utils::ObjectTable<Impl> objects;
00169 
00170         objects.add_interface<IPluginA>(&instance);
00171         objects.add_interface<IPluginB>(&instance);
00172         objects.add_interface<IPluginC>(&instance);
00173 
00174         IPluginA * pa = 0;
00175         IPluginB * pb = 0;
00176         IPluginC * pc = 0;
00177         objects.query_interface(&instance, "IPluginA", (void **)&pa);
00178         objects.query_interface(&instance, "IPluginB", (void **)&pb);
00179         objects.query_interface(&instance, "IPluginC", (void **)&pc);
00180 
00181         ensure(&instance == objects.get_object(pa));
00182         ensure(&instance == objects.get_object(pb));
00183         ensure(&instance == objects.get_object(pc));
00184     }
00185 
00186     // Test 4: Test remove which removes object from object table.
00187     template<>
00188     template<>
00189     void object::test<4>()
00190     {
00191         ::Impl instance_a, instance_b;
00192         utils::ObjectTable<Impl> objects;
00193 
00194         objects.add_interface<IPluginA>(&instance_a);
00195         objects.add_interface<IPluginB>(&instance_a);
00196         objects.add_interface<IPluginC>(&instance_a);
00197 
00198         objects.add_interface<IPluginA>(&instance_b);
00199         objects.add_interface<IPluginB>(&instance_b);
00200         objects.add_interface<IPluginC>(&instance_b);
00201 
00202         objects.remove(&instance_a);
00203 
00204         IPluginA * pa = 0;
00205         IPluginB * pb = 0;
00206         IPluginC * pc = 0;
00207         objects.query_interface(&instance_a, "IPluginA", (void **)&pa);
00208         ensure_equals(pa,  0);
00209 
00210         objects.query_interface(&instance_b, "IPluginA", (void **)&pa);
00211         ensure(pa == static_cast<IPluginA *>(&instance_b));
00212 
00213         objects.remove(&instance_b);
00214         objects.query_interface(&instance_b, "IPluginA", (void **)&pa);
00215         ensure_equals(pa,  0);
00216 
00217     }
00218 
00219 };
00220 
00221 
00222 using std::exception;
00223 using std::cerr;
00224 using std::endl;
00225 
00226 namespace tut
00227 {
00228     test_runner_singleton runner;
00229 }
00230 
00231 int main()
00232 {
00233     tut::reporter reporter;
00234     tut::runner.get().set_callback(&reporter);
00235 
00236     try
00237     {
00238         tut::runner.get().run_tests();
00239     }
00240     catch (const std::exception& ex)
00241     {
00242         cerr << "tut raised ex: " << ex.what() << endl;
00243         return 1;
00244     }
00245     while(1);
00246 
00247     return 0;
00248 }
00249 
Generated by  doxygen 1.6.2-20100208