| 1 |
#include <sstream> |
| 2 |
#include <cstring> |
| 3 |
|
| 4 |
#include "UnitTest++.h" |
| 5 |
|
| 6 |
#include "geners/ClassId.hh" |
| 7 |
#include "geners/IOException.hh" |
| 8 |
#include "geners/BooleanString.hh" |
| 9 |
|
| 10 |
using namespace gs; |
| 11 |
using namespace std; |
| 12 |
|
| 13 |
namespace { |
| 14 |
static int flag = 0; |
| 15 |
|
| 16 |
class DummyClass1 |
| 17 |
{ |
| 18 |
public: |
| 19 |
static const char* classname() {return "DummyClass1";} |
| 20 |
static unsigned version() {return 1U;} |
| 21 |
}; |
| 22 |
|
| 23 |
class DummyClass2 |
| 24 |
{ |
| 25 |
public: |
| 26 |
DummyClass2() : clid_(ClassId::invalidId()), haveClid_(false) {} |
| 27 |
|
| 28 |
inline const ClassId& classId() const |
| 29 |
{ |
| 30 |
flag = 2; |
| 31 |
if (!haveClid_) |
| 32 |
{ |
| 33 |
haveClid_ = true; |
| 34 |
clid_ = ClassId(*this); |
| 35 |
} |
| 36 |
return clid_; |
| 37 |
} |
| 38 |
static const char* classname() {return "DummyClass2";} |
| 39 |
static unsigned version() {return 1U;} |
| 40 |
|
| 41 |
private: |
| 42 |
mutable ClassId clid_; |
| 43 |
mutable bool haveClid_; |
| 44 |
}; |
| 45 |
|
| 46 |
class DummyClass3 |
| 47 |
{ |
| 48 |
public: |
| 49 |
virtual ~DummyClass3() {} |
| 50 |
|
| 51 |
inline virtual ClassId classId() const |
| 52 |
{ |
| 53 |
flag = 3; |
| 54 |
return ClassId(*this); |
| 55 |
} |
| 56 |
static const char* classname() {return "DummyClass3";} |
| 57 |
static unsigned version() {return 1U;} |
| 58 |
}; |
| 59 |
|
| 60 |
class DummyClass4 : public DummyClass3 |
| 61 |
{ |
| 62 |
public: |
| 63 |
virtual ~DummyClass4() {} |
| 64 |
|
| 65 |
inline virtual ClassId classId() const |
| 66 |
{ |
| 67 |
flag = 4; |
| 68 |
return ClassId(*this); |
| 69 |
} |
| 70 |
static const char* classname() {return "DummyClass4";} |
| 71 |
static unsigned version() {return 1U;} |
| 72 |
}; |
| 73 |
|
| 74 |
template <class T> |
| 75 |
class DummyClass5 |
| 76 |
{ |
| 77 |
public: |
| 78 |
inline const ClassId classId() const {return ClassId(*this);} |
| 79 |
static const char* classname() {return classname_.c_str();} |
| 80 |
static unsigned version() {return 1U;} |
| 81 |
|
| 82 |
private: |
| 83 |
static std::string classname_; |
| 84 |
}; |
| 85 |
|
| 86 |
template<class T> |
| 87 |
std::string DummyClass5<T>::classname_ = |
| 88 |
template_class_name<T>("DummyClass5"); |
| 89 |
|
| 90 |
template <class T1, class T2> |
| 91 |
class DummyClass6 |
| 92 |
{ |
| 93 |
public: |
| 94 |
inline const ClassId classId() const {return ClassId(*this);} |
| 95 |
static const char* classname() {return classname_.c_str();} |
| 96 |
static unsigned version() {return 1U;} |
| 97 |
|
| 98 |
private: |
| 99 |
static std::string classname_; |
| 100 |
}; |
| 101 |
|
| 102 |
template<class T1, class T2> |
| 103 |
std::string DummyClass6<T1,T2>::classname_ = |
| 104 |
template_class_name<T1,T2>("DummyClass6"); |
| 105 |
|
| 106 |
TEST(ClassId) |
| 107 |
{ |
| 108 |
ClassId idaa2("aa", 2); |
| 109 |
ClassId idaa3("aa", 3); |
| 110 |
CHECK(idaa2 != idaa3); |
| 111 |
idaa2.setVersion(3); |
| 112 |
CHECK(idaa2 == idaa3); |
| 113 |
|
| 114 |
DummyClass1 obj; |
| 115 |
ClassId id(obj); |
| 116 |
CHECK(!id.name().empty()); |
| 117 |
|
| 118 |
ostringstream os; |
| 119 |
CHECK(id.write(os)); |
| 120 |
|
| 121 |
istringstream is(os.str()); |
| 122 |
ClassId id2(is, 1); |
| 123 |
CHECK(!id2.name().empty()); |
| 124 |
CHECK(id2 == id); |
| 125 |
|
| 126 |
bool caught = false; |
| 127 |
try |
| 128 |
{ |
| 129 |
ClassId id3(is, 1); |
| 130 |
} |
| 131 |
catch (IOReadFailure& e) |
| 132 |
{ |
| 133 |
caught = true; |
| 134 |
} |
| 135 |
CHECK(caught); |
| 136 |
|
| 137 |
DummyClass2 dc2; |
| 138 |
flag = 0; |
| 139 |
ClassId::itemId(dc2); |
| 140 |
CHECK_EQUAL(2, flag); |
| 141 |
|
| 142 |
DummyClass3 dc3; |
| 143 |
flag = 0; |
| 144 |
ClassId::itemId(dc3); |
| 145 |
CHECK_EQUAL(3, flag); |
| 146 |
|
| 147 |
DummyClass4 dc4; |
| 148 |
flag = 0; |
| 149 |
ClassId::itemId(dc4); |
| 150 |
CHECK_EQUAL(4, flag); |
| 151 |
|
| 152 |
flag = 0; |
| 153 |
CHECK(ClassId::itemId(flag) == ClassId::makeId<int>()); |
| 154 |
CHECK_EQUAL(0, flag); |
| 155 |
|
| 156 |
DummyClass3& dref(dc4); |
| 157 |
flag = 0; |
| 158 |
ClassId::itemId(dref); |
| 159 |
CHECK_EQUAL(4, flag); |
| 160 |
|
| 161 |
CHECK(ClassId(std::string("int(0)")) == ClassId::makeId<int>()); |
| 162 |
} |
| 163 |
|
| 164 |
TEST(TemplateClassId) |
| 165 |
{ |
| 166 |
std::vector<std::vector<ClassId> > params; |
| 167 |
|
| 168 |
ClassId id5(DummyClass5<int>().classId()); |
| 169 |
CHECK(id5 == ClassId(std::string("DummyClass5<int(0)>(1)"))); |
| 170 |
CHECK(id5.isTemplate()); |
| 171 |
id5.templateParameters(¶ms); |
| 172 |
CHECK_EQUAL(1U, params.size()); |
| 173 |
CHECK(params[0][0] == ClassId::makeId<int>()); |
| 174 |
|
| 175 |
ClassId id6(DummyClass6<int,double>().classId()); |
| 176 |
CHECK(id6.isTemplate()); |
| 177 |
id6.templateParameters(¶ms); |
| 178 |
CHECK_EQUAL(2U, params.size()); |
| 179 |
CHECK(params[0][0] == ClassId::makeId<int>()); |
| 180 |
CHECK(params[1][0] == ClassId::makeId<double>()); |
| 181 |
|
| 182 |
ClassId id7(DummyClass6<DummyClass5<int>,double>().classId()); |
| 183 |
CHECK(id7.isTemplate()); |
| 184 |
id7.templateParameters(¶ms); |
| 185 |
CHECK_EQUAL(2U, params.size()); |
| 186 |
CHECK(params[0][0] == id5); |
| 187 |
CHECK(params[1][0] == ClassId::makeId<double>()); |
| 188 |
} |
| 189 |
|
| 190 |
TEST(BooleanString) |
| 191 |
{ |
| 192 |
CHECK(BooleanString<true>::value); |
| 193 |
CHECK(!BooleanString<false>::value); |
| 194 |
CHECK_EQUAL(0, strcmp(BooleanString<false>::name(), "false")); |
| 195 |
CHECK_EQUAL(0, strcmp(BooleanString<true>::name(), "true")); |
| 196 |
} |
| 197 |
} |