Lumiera  0.pre.03
»edit your freedom«
typed-id-test.cpp
Go to the documentation of this file.
1 /*
2  TypedID(Test) - verifying registration service for ID to type association
3 
4  Copyright (C) Lumiera.org
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 
31 #include "steam/asset/typed-id.hpp"
32 #include "lib/idi/entry-id.hpp"
33 #include "lib/p.hpp"
34 #include "steam/assetmanager.hpp"
36 //#include "steam/mobject/session/clip.hpp"
37 //#include "steam/mobject/session/fork.hpp"
38 //#include "lib/meta/trait-special.hpp"
39 #include "lib/util-foreach.hpp"
40 #include "lib/format-cout.hpp"
41 #include "lib/symbol.hpp"
42 
43 //#include <unordered_map>
44 //#include <string>
45 
46 //using lib::test::showSizeof;
47 //using lib::test::randStr;
48 //using util::isSameObject;
49 //using util::and_all;
50 using util::for_each;
51 using util::isnil;
52 //using lib::Literal;
53 using lib::Symbol;
54 using lib::P;
55 //using std::string;
56 
57 
58 
59 namespace steam {
60 namespace mobject {
61 namespace session {
62 namespace test {
63 
64  struct DummyEntity { };
65 
66  typedef P<DummyEntity> PDum;
67 
68 }}}}
69 
70 namespace lumiera{
71 namespace query {
72 
78  template<>
79  struct TypeHandlerXX<steam::mobject::session::test::DummyEntity>
80  {
81  static Symbol getID() { return "typed-id-test-dummy"; }
82 
83  };
84 
85 }}
86 
87 
88 
89 namespace lib {
90 namespace idi {
91 namespace test{
92 
95 
96  using DummyID = EntryID<DummyEntity>;
97 
98  namespace { // Test definitions...
99 
100  }
101 
102 
103 
104  /***********************************************************************/
119  class TypedID_test : public Test
120  {
121 
122  virtual void
123  run (Arg)
124  {
125  verifyRegistration();
126  verifyAssetFrontend();
127  verifyInstanceAccess();
128  verifyAutomaticCleanup();
129  }
130 
131 
132  void
133  verifyRegistration()
134  {
135 #if false
136  uint type_cnt = TypedID::type_count();
137  uint elm_cnt = TypedID::element_count();
138 
139  PDum d1 = TypedID::get<DummyEntity> ("top");
140  CHECK (!d1); // doesn't exist
141 
142  // the above request automatically opened a new category
143  CHECK (type_cnt+1 == TypedID::type_count());
144 
145  PDum up (new DummyEntity ("up"));
146  PDum down (new DummyEntity ("down"));
147 
148  CHECK (elm_cnt+2 == TypedID::element_count());
149 
150  DummyID idu = TypedID::getID<DummyEntity> ("up");
151  CHECK (idu);
152  CHECK ("up" == idu.getSym());
153 #endif
154  }
155 
156 
157  void
158  verifyAssetFrontend()
159  {
160 #if false
161  PInv inventory = asset::Meta::create (Category (META,"typed-id-test-dummy"));
162  CHECK (inventory);
163  CHECK (0 == inventory->size());
164 
165  // The Inventory is a first-class Asset
166  CHECK (AssetManager::instance().known (inventory->getID()));
167 
168  PDum up (new DummyEntity ("up"));
169  PDum down (new DummyEntity ("down"));
170 
171  // changes reflected immediately
172  CHECK (2 == inventory->size());
173 
174  for_each (inventory->all(), show<BareEntryID> );
175 #endif
176  }
177 
178  template<typename X>
179  static void
180  show (X& o)
181  {
182  cout << "---" << o << endl;
183  }
184 
185 
186  void
187  verifyInstanceAccess()
188  {
189 #if false
190  PDum top (new DummyEntity ("top"));
191  PDum bot (new DummyEntity ("bottom"));
192 
193  CHECK (1 == top.use_count());
194  CHECK (1 == bot.use_count()); // we hold the only reference
195 
196  VERIFY_ERROR (MISSING_INSTANCE, TypedID::get<DummyEntity> ("top"));
197 
198  // to enable fetching instances, an instance link needs to be provided
199  top->registerInstance (top);
200  TypedID::registerInstance<DummyEntity> (bot);
201 
202  PDum d1 = TypedID::get<DummyEntity> ("top");
203  CHECK (d1);
204  CHECK (d1 == top);
205  CHECK (d1 != bot);
206  CHECK (2 == top.use_count());
207  CHECK (1 == bot.use_count());
208 
209  d1 = TypedID::get<DummyEntity> ("bottom");
210  CHECK (d1);
211  CHECK (d1 != top);
212  CHECK (d1 == bot);
213  CHECK (1 == top.use_count());
214  CHECK (2 == bot.use_count());
215 
216  for_each (TypedID::allInstances<DummyEntity>(), show<DummyEntity> );
217 #endif
218  }
219 
220 
221  void
222  verifyAutomaticCleanup()
223  {
224 #if false
225 
226  PDum cha (new DummyEntity ("charm"));
227  PDum bea (new DummyEntity ("beauty"));
228 
229  CHECK (2 == TypedID::entry_count<DummyEntity>());
230 
231  cha.reset();
232  CHECK (1 == TypedID::entry_count<DummyEntity>());
233  CHECK (bea == *(TypedID::allInstances<DummyEntity>()));
234 
235  bea.reset();
236  CHECK (0 == TypedID::entry_count<DummyEntity>());
237  CHECK (! (TypedID::get<DummyEntity>("beauty")));
238  CHECK (isnil (TypedID::allInstances<DummyEntity>()));
239 #endif
240  }
241  };
242 
243 
244 
246  LAUNCHER (TypedID_test, "unit asset");
247 
248 
249 }}} // namespace lib::idi::test
Adapter for using this type as a primary type within Lumiera&#39;s Steam-Layer.
Definition: typed-id.hpp:74
Steam-Layer Interface: Asset Lookup and Organisation.
Automatically use custom string conversion in C++ stream output.
Exposing an ID registration cluster as a Meta Asset.
Definition: run.hpp:49
typed symbolic and hash ID for asset-like position accounting.
Definition: entry-id.hpp:135
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Frontend for a registration service to associate object identities, symbolic identifiers and types...
Customised refcounting smart pointer.
Steam-Layer implementation namespace root.
Namespace of Session and user visible high-level objects.
Definition: sequence.hpp:74
static lib::Depend< AssetManager > instance
get at the system-wide asset manager instance.
Implementation namespace for support and library code.
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Marker types to indicate a literal string and a Symbol.
Simple test class runner.
void for_each(CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
Accept binding for arbitrary function arguments.
A collection of frequently used helper functions to support unit testing.
Lumiera public interface.
Definition: advice.cpp:113
Bare symbolic and hash ID used for accounting of asset like entries.
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:80
Perform operations "for each element" of a collection.