00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "common/test/run.hpp"
00025 #include "common/util.hpp"
00026
00027 #include "proc/mobject/session/defsregistry.hpp"
00028 #include "common/factory.hpp"
00029 #include "common/query.hpp"
00030 #include "common/p.hpp"
00031
00032 #include "../common/query/querydiagnostics.hpp"
00033
00034 #include <boost/scoped_ptr.hpp>
00035 #include <boost/format.hpp>
00036 #include <cstdlib>
00037 #include <map>
00038
00039 using lumiera::P;
00040 using lumiera::Query;
00041 using lumiera::query::test::garbage_query;
00042 using util::isnil;
00043
00044 using boost::scoped_ptr;
00045 using boost::format;
00046 using std::string;
00047 using std::rand;
00048 using std::map;
00049
00050
00051
00052 namespace mobject
00053 {
00054 namespace session
00055 {
00056 namespace test
00057 {
00058
00059 format typePatt ("Dummy<%2i>");
00060 format instancePatt ("obj_%s_%i");
00061 format predicatePatt ("%s_%2i( %s )");
00062
00063
00064
00066 string
00067 newID (string prefix)
00068 {
00069 return str (instancePatt % prefix % rand());
00070 }
00071
00072
00074 template<int I>
00075 struct Dummy
00076 {
00077 static string name;
00078 string instanceID;
00079 operator string () const { return instanceID; }
00080 bool operator== (const Dummy& odu) const { return this == &odu; }
00081
00082
00083 Dummy () : instanceID (newID (name)) {}
00084 };
00085
00086 template<int I>
00087 string Dummy<I>::name = str (typePatt % I);
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 class DefsRegistryImpl_test : public Test
00102 {
00103 scoped_ptr<DefsRegistry> reg_;
00104
00105 typedef lumiera::P<Dummy<13> > O;
00106 typedef lumiera::P<Dummy<23> > P;
00107
00108 typedef Query<Dummy<13> > Q13;
00109 typedef Query<Dummy<23> > Q23;
00110
00111 typedef DefsRegistry::Iter<Dummy<13> > Iter13;
00112 typedef DefsRegistry::Iter<Dummy<23> > Iter23;
00113
00114
00115
00116 lumiera::factory::RefcountPtr<Dummy<13> > oFac;
00117 lumiera::factory::RefcountPtr<Dummy<23> > pFac;
00118
00119 O o1, o2, o3;
00120 Q13 q1, q2, q3, q4, q5;
00121 map<Q23, P> ps;
00122
00123 public:
00124 DefsRegistryImpl_test ()
00125 : o1 (oFac()), o2 (oFac()), o3 (oFac()),
00126 q1 (garbage_query (1)),
00127 q2 (garbage_query (2)),
00128 q3 (garbage_query (3)),
00129 q4 (garbage_query (4)),
00130 q5 (garbage_query (5))
00131 { }
00132
00133
00134 virtual void run(Arg arg)
00135 {
00136 this->reg_.reset (new DefsRegistry);
00137
00138 fill_table ();
00139 check_query ();
00140 check_remove ();
00141 }
00142
00143
00144
00145
00146 void fill_table ()
00147 {
00148
00149
00150 ASSERT ( ! *(reg_->candidates(Q13 ("something"))) );
00151
00152 reg_->put (o1, q5);
00153 reg_->put (o2, q4);
00154 reg_->put (o3, q3);
00155 reg_->put (o3, q2);
00156 reg_->put (o2, q1);
00157 reg_->put (o1, Q13());
00158
00159 ps.clear();
00160 for (int i=0; i<100; ++i)
00161 {
00162 P px (pFac());
00163 Q23 qx (garbage_query());
00164 ps[qx] = px;
00165 reg_->put (px, qx);
00166 px->instanceID = qx;
00167 }
00168 }
00169
00170
00171 void check_query ()
00172 {
00173 Iter13 i (reg_->candidates(Q13 ("irrelevant query")));
00174 ASSERT ( i.hasNext());
00175 ASSERT ( *i++ == o1);
00176 ASSERT ( *i++ == o2);
00177 ASSERT ( *i++ == o3);
00178 ASSERT ( *i++ == o3);
00179 ASSERT ( *i++ == o2);
00180 ASSERT ( *i == o1);
00181 ASSERT (!i.hasNext());
00182 ASSERT (! *++i );
00183
00184 i = reg_->candidates(q3);
00185 ASSERT ( *i++ == o3);
00186 ASSERT ( *i++ == o1);
00187 ASSERT ( *i++ == o2);
00188 ASSERT ( *i++ == o3);
00189 ASSERT ( *i++ == o3);
00190 ASSERT ( *i++ == o2);
00191 ASSERT ( *i++ == o1);
00192 ASSERT (!i.hasNext());
00193
00194 i = reg_->candidates(Q13());
00195 ASSERT ( *i++ == o1);
00196 ASSERT ( *i++ == o1);
00197 ASSERT ( *i++ == o2);
00198 ASSERT ( *i++ == o3);
00199 ASSERT ( *i++ == o3);
00200 ASSERT ( *i++ == o2);
00201 ASSERT ( *i++ == o1);
00202 ASSERT (!i.hasNext());
00203
00204 uint d=0;
00205 uint d_prev=0;
00206 Iter23 j = reg_->candidates(Q23 ("some crap"));
00207 for ( ; *j ; ++j )
00208 {
00209 ASSERT ( *j );
00210 Q23 qx ((*j)->instanceID);
00211 ASSERT ( ps[qx] == (*j));
00212 d = lumiera::query::countPraed (qx);
00213 ASSERT ( d_prev <= d );
00214 d_prev = d;
00215 }
00216 ASSERT (!j.hasNext());
00217
00218
00219
00220 j = reg_->candidates(ps.begin()->first);
00221 ASSERT ( *j == ps.begin()->second);
00222
00223 }
00224
00225
00226 void check_remove ()
00227 {
00228 reg_->forget (o2);
00229
00230 Iter13 i (reg_->candidates(q4));
00231 ASSERT ( i.hasNext());
00232 ASSERT ( *i++ == o1);
00233
00234 ASSERT ( *i++ == o3);
00235 ASSERT ( *i++ == o3);
00236
00237 ASSERT ( *i == o1);
00238 ASSERT (!i.hasNext());
00239
00240 o3.reset();
00241
00242
00243 i = reg_->candidates(Q13 ("something"));
00244 ASSERT ( i.hasNext());
00245 ASSERT ( *i++ == o1);
00246
00247 ASSERT ( *i == o1);
00248 ASSERT (!i.hasNext());
00249
00250 ASSERT ( reg_->put (o1, q5));
00251
00252 i = reg_->candidates(q5);
00253 ASSERT ( *i++ == o1);
00254 ASSERT ( *i++ == o1);
00255 ASSERT ( *i++ == o1);
00256 ASSERT (!i.hasNext());
00257
00258 ASSERT (!reg_->put (o2, q5));
00259
00260 i = reg_->candidates(q5);
00261 ASSERT ( *i++ == o1);
00262 ASSERT ( *i++ == o1);
00263 ASSERT ( *i++ == o1);
00264 ASSERT (!i.hasNext());
00265
00266 ASSERT ( reg_->put (o2, q2));
00267 i = reg_->candidates(q2);
00268 ASSERT ( *i++ == o2);
00269 ASSERT ( *i++ == o1);
00270 ASSERT ( *i++ == o2);
00271 ASSERT ( *i++ == o1);
00272 ASSERT (!i.hasNext());
00273
00274 ASSERT ( reg_->forget (o1));
00275 ASSERT (!reg_->forget (o1));
00276 ASSERT ( reg_->forget (o2));
00277
00278 o3 = oFac();
00279
00280 i = reg_->candidates(q2);
00281 ASSERT (! (*i));
00282 }
00283
00284 };
00285
00286
00288 LAUNCHER (DefsRegistryImpl_test, "function session");
00289
00290
00291
00292 }
00293
00294 }
00295
00296 }