mrfioc2  2.3.0
objectTest.cpp
Go to the documentation of this file.
1 #include <vector>
2 #include <algorithm>
3 
4 #include "epicsUnitTest.h"
5 #include "epicsString.h"
6 #include "testMain.h"
7 
8 #include "mrf/object.h"
9 
10 namespace {
11 using namespace mrf;
12 
13 class mine : public ObjectInst<mine>
14 {
15 public:
16  int ival;
17  double dval;
18  std::vector<double> darr;
19  unsigned count;
20 
21  explicit mine(const std::string& n) : ObjectInst<mine>(n), ival(0), dval(0.0), count(0)
22  {}
23 
24  /* no locking needed */
25  virtual void lock() const{};
26  virtual void unlock() const{};
27 
28  int getI() const{return ival;}
29  void setI(int i){ival=i;}
30 
31  double val() const{return dval;}
32  void setVal(double v){dval=v;}
33 
34  epicsUInt32 getdarr(double* v, epicsUInt32 l) const
35  {
36  if (!v) return (epicsUInt32)darr.size();
37  l = (epicsUInt32)std::min((size_t)l, darr.size());
38  std::copy(darr.begin(), darr.begin()+l, v);
39  return l;
40  }
41  void setdarr(const double* v, epicsUInt32 l)
42  {
43  darr.resize(l);
44  std::copy(v, v+l, darr.begin());
45  }
46 
47  void incr() { count++; }
48 };
49 
50 class other : public ObjectInst<other, mine>
51 {
52  typedef ObjectInst<other, mine> base_t;
53 public:
54  explicit other(const std::string& n) : base_t(n) {}
55  virtual ~other() {}
56 
57  int getX() const { return 42;}
58 
59  static Object* buildOne(const std::string& name, const std::string& klass, const Object::create_args_t& args);
60 };
61 
62 Object*
63 other::buildOne(const std::string& name, const std::string& klass, const Object::create_args_t& args)
64 {
65  return new other(name);
66 }
67 
68 
69 void testMine()
70 {
71  testDiag("In testMine()");
72  mine m("test");
73 
74  testOk1(m.getI()==0);
75 
76  mrf::auto_ptr<property<int> > I=m.getProperty<int>("I");
77  testOk1(I.get()!=NULL);
78 
79  if(I.get()) {
80  testOk1(I->get()==0);
81  I->set(42);
82  testOk1(m.ival==42);
83  }
84 
85  Object *o = &m;
86 
87  mrf::auto_ptr<property<double> > V=o->getProperty<double>("val");
88  testOk1(V.get()!=NULL);
89 
90  if(V.get()) {
91  testOk1(V->get()==0.0);
92  V->set(4.2);
93  testOk1(m.dval==4.2);
94  }
95 
96  mrf::auto_ptr<property<int> > I2=o->getProperty<int>("I");
97  testOk1(I2.get()!=NULL);
98  testOk1((*I)==(*I2));
99 
100  if(I2.get())
101  testOk1(I2->get()==42);
102 
103  I2=o->getProperty<int>("val");
104  testOk1(I2.get()!=NULL);
105  testOk1((*I)!=(*I2));
106 
107  if(I2.get())
108  testOk1(I2->get()==42);
109 
110  mrf::auto_ptr<property<double[1]> > A=o->getProperty<double[1]>("darr");
111  testOk1(A.get()!=NULL);
112 
113  const double tst[] = {1.0, 2.0, 3.0};
114  double tst2[3];
115 
116  if(A.get()) {
117  A->set(tst,3);
118  testOk1(m.darr.size()==3);
119  testOk1(A->get(tst2,3)==3);
120  }
121 
122  testOk1(std::equal(tst,tst+3,tst2));
123 
124  V=o->getProperty<double>("other");
125  testOk1(V.get()==NULL);
126 
127  try {
128  mine n("test"); // duplicate name
129  testFail("Duplicate name not prevented");
130  } catch(std::runtime_error& e) {
131  testPass("Duplicate name prevented: %s",e.what());
132  }
133 
134  Object *p=Object::getObject("test");
135  testOk1(p!=NULL);
136  testOk1(p==o);
137 
138  {
139  testOk1(m.count==0);
140  mrf::auto_ptr<property<void> > incr(o->getProperty<void>("incr"));
141  testOk1(incr.get()!=NULL);
142  if(incr.get()) {
143  incr->exec();
144  }
145  testOk1(m.count==1);
146  }
147 }
148 
149 void testOther()
150 {
151  testDiag("In testOther()");
152  other m("foo");
153 
154  mrf::auto_ptr<property<double> > V=m.getProperty<double>("val");
155  testOk1(V.get()!=NULL);
156 
157  mrf::auto_ptr<property<int> > I=m.getProperty<int>("I");
158  testOk1(I.get()!=NULL);
159 
160  mrf::auto_ptr<property<double[1]> > A=m.getProperty<double[1]>("darr");
161  testOk1(A.get()!=NULL);
162 
163  mrf::auto_ptr<property<int> > X=m.getProperty<int>("X");
164  testOk1(X.get()!=NULL);
165 
166  if(X.get())
167  testOk1(X->get()==42);
168  else
169  testSkip(1, "NULL");
170 }
171 
172 void testOther2()
173 {
174  testDiag("In testOther2()");
175  other m("foo");
176  Object *o = &m;
177 
178  mrf::auto_ptr<property<double> > V=o->getProperty<double>("val");
179  testOk1(V.get()!=NULL);
180 
181  mrf::auto_ptr<property<int> > I=o->getProperty<int>("I");
182  testOk1(I.get()!=NULL);
183 
184  mrf::auto_ptr<property<double[1]> > A=o->getProperty<double[1]>("darr");
185  testOk1(A.get()!=NULL);
186 
187  mrf::auto_ptr<property<int> > X=o->getProperty<int>("X");
188  testOk1(X.get()!=NULL);
189 }
190 
191 void testFactory()
192 {
193  testDiag("In testFactory()");
194  other m("HelloWorld");
195 
196  testOk1(Object::getObject("NoOnesHome")==NULL);
197  testOk1(Object::getObject("HelloWorld")==&m);
198 
199  testOk1(Object::getCreateObject("HelloWorld", "other")==&m);
200 
201  Object *built = Object::getCreateObject("AnotherOne", "other");
202  testOk1(built!=NULL);
203 
204  testOk1(built==Object::getObject("AnotherOne"));
205  testOk1(built==Object::getCreateObject("AnotherOne", "other"));
206 }
207 
208 } // namespace
209 
210 OBJECT_BEGIN(mine)
211 OBJECT_PROP2("I", &mine::getI, &mine::setI);
212 OBJECT_PROP2("val", &mine::getI, &mine::setI);
213 OBJECT_PROP2("val", &mine::val, &mine::setVal);
214 OBJECT_PROP2("darr",&mine::getdarr, &mine::setdarr);
215 OBJECT_PROP1("incr", &mine::incr);
216 OBJECT_END(mine)
217 
218 OBJECT_BEGIN2(other, mine)
219 OBJECT_PROP1("X", &other::getX);
220 OBJECT_FACTORY(other::buildOne);
221 OBJECT_END(other)
222 
223 MAIN(objectTest)
224 {
225  testPlan(39);
226  testMine();
227  testOther();
228  testOther2();
229  testFactory();
230  return testDone();
231 }
std::map< std::string, std::string > create_args_t
Definition: object.h:422
static Object * getCreateObject(const std::string &name, const std::string &klass, const create_args_t &args=create_args_t())
Definition: object.cpp:118
OBJECT_FACTORY(other::buildOne)
Definition: evrdump.c:37
OBJECT_PROP1("incr", &mine::incr)
#define OBJECT_BEGIN2(klass, Base)
Definition: object.h:507
MAIN(objectTest)
Definition: objectTest.cpp:223
OBJECT_PROP2("I", &mine::getI, &mine::setI)
Base object inspection.
Definition: object.h:378
static Object * getObject(const std::string &name)
Definition: object.cpp:107
#define OBJECT_BEGIN(klass)
Definition: object.h:513
User implementation hook.
Definition: object.h:459
#define OBJECT_END(klass)
Definition: object.h:523
mrf::auto_ptr< property< P > > getProperty(const char *pname)
Definition: object.h:405
Definition: flash.cpp:23