PVData C++ 8.0.7
Loading...
Searching...
No Matches
pvUnitTest.h
1/*
2 * Copyright information and license terms for this software can be
3 * found in the file LICENSE that is included with the distribution
4 */
5#ifndef PVUNITTEST_H
6#define PVUNITTEST_H
7
8#include <sstream>
9#include <typeinfo>
10
11#include <epicsUnitTest.h>
12
13#include <pv/sharedPtr.h>
14#include <pv/epicsException.h>
15#include <pv/pvData.h>
16
17namespace detail {
18
19template<class C, void (C::*M)()>
20void test_method(const char *kname, const char *mname)
21{
22 try {
23 testDiag("------- %s::%s --------", kname, mname);
24 C inst;
25 (inst.*M)();
26 } catch(std::exception& e) {
27 PRINT_EXCEPTION(e);
28 testAbort("unexpected exception: %s", e.what());
29 }
30}
31
32class epicsShareClass testPassx
33{
34 std::ostringstream strm;
35 const bool dotest, pass;
36 bool alive;
37public:
38 testPassx() :dotest(false), pass(false), alive(true) {}
39 explicit testPassx(bool r) :dotest(true), pass(r), alive(true) {}
40 ~testPassx();
41 template<typename T>
42 inline testPassx& operator<<(const T& v) {
43 strm<<v;
44 return *this;
45 }
46
47 // allow testPassx to be returned
48 // move ctor masquerading as copy ctor
50private:
52};
53
54template<typename LHS, typename RHS>
55inline testPassx testEqualx(const char *nLHS, const char *nRHS, const LHS& l, const RHS& r)
56{
57 return testPassx(l==r)<<nLHS<<" ("<<l<<") == "<<nRHS<<" ("<<r<<")";
58}
59
60template<typename LHS, typename RHS>
61inline testPassx testNotEqualx(const char *nLHS, const char *nRHS, const LHS& l, const RHS& r)
62{
63 return testPassx(l!=r)<<nLHS<<" ("<<l<<") != "<<nRHS<<" ("<<r<<")";
64}
65
66}//namespace detail
67
98#define TEST_METHOD(klass, method) ::detail::test_method<klass, &klass::method>(#klass, #method)
99
110#define testEqual(LHS, RHS) ::detail::testEqualx(#LHS, #RHS, LHS, RHS)
111
112#define testNotEqual(LHS, RHS) ::detail::testNotEqualx(#LHS, #RHS, LHS, RHS)
113
124#define testTrue(B) ::detail::testPassx(!!(B))<<#B
125
132#define testThrows(EXC, CODE) try{ CODE; testFail("unexpected success of " #CODE); }catch(EXC& e){testPass("catch expected exception: %s", e.what());}
133
140#define testShow() ::detail::testPassx()
141
149template<typename PVD>
151testFieldEqual(const std::tr1::shared_ptr<const epics::pvData::PVStructure>& val, const char *name, typename PVD::value_type expect)
152{
153 if(!val) {
154 return ::detail::testPassx(false)<<" null structure pointer";
155 }
156 typename PVD::const_shared_pointer fval(val->getSubField<PVD>(name));
157 if(!fval) {
158 epics::pvData::PVUnion::const_shared_pointer uval(val->getSubField<epics::pvData::PVUnion>(name));
159 if(uval)
160 fval = uval->get<PVD>();
161 }
162 if(!fval) {
163 return ::detail::testPassx(false)<<" field '"<<name<<"' with type "<<typeid(PVD).name()<<" does not exist";
164 } else {
165 typename PVD::value_type actual(fval->get());
166 return ::detail::testPassx(actual==expect)<<name<<" ("<<actual<<") == "<<expect;
167 }
168}
169
170template<typename PVD>
172testFieldEqual(const std::tr1::shared_ptr<const epics::pvData::PVStructure>& val, const char *name, typename PVD::const_svector expect)
173{
174 if(!val) {
175 return ::detail::testPassx(false)<<" null structure pointer";
176 }
177 typename PVD::const_shared_pointer fval(val->getSubField<PVD>(name));
178 if(!fval) {
179 return ::detail::testPassx(false)<<" field '"<<name<<"' with type "<<typeid(PVD).name()<<" does not exist";
180 } else {
181 typename PVD::const_svector actual(fval->view());
182 return ::detail::testPassx(actual==expect)<<name<<" ("<<actual<<") == "<<expect;
183 }
184}
185
188#endif // PVUNITTEST_H
constexpr complex< _Tp > & operator=(const _Tp &)
basic_ostream< _CharT, _Traits > & operator<<(basic_ostream< _CharT, _Traits > &__os, const basic_string< _CharT, _Traits, _Alloc > &__str)
virtual const char * what() const noexcept
PVUnion has a single subfield.
Definition pvData.h:941
::detail::testPassx testFieldEqual(const std::tr1::shared_ptr< const epics::pvData::PVStructure > &val, const char *name, typename PVD::value_type expect)
Definition pvUnitTest.h:151
const char * name(Type type)