1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2008 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko
8 * ============================
11 #ifndef __TESTEVENTS_H__
12 #define __TESTEVENTS_H__
20 extern bool g_Verbose;
22 static unsigned int MakeHash(const char *name)
25 unsigned int res = 0xFFFFFFFF;
29 res ^= ((unsigned int)*name << ((a++ % 4)*8));
42 virtual bool IsEqual(State *other)
44 return (MakeHash(GetName()) == MakeHash(other->GetName())) ? true : false;
52 virtual void Dump() = 0;
53 virtual const char *GetName() = 0;
56 struct IgnoreState : public State
67 typedef std::list<State*> StateList;
70 void DumpStates(StateList *sl)
72 for (StateList::iterator iter = sl->begin(); iter != sl->end(); ++iter)
76 bool StatesOk(StateList *sl, ...)
78 StateList requiredstates;
83 State *cs = va_arg(argptr, State*);
88 requiredstates.push_back(cs);
92 if (requiredstates.size() != sl->size())
96 std::cout << std::endl << "FAIL: Should be:" << std::endl;
97 DumpStates(&requiredstates);
98 std::cout << std::endl << "FAIL: Is:" << std::endl;
102 for (StateList::iterator iter = requiredstates.begin(); iter != requiredstates.end(); ++iter)
104 for (StateList::iterator iter = sl->begin(); iter != sl->end(); ++iter)
111 StateList::iterator req_iter = requiredstates.begin();
112 for (StateList::iterator o_iter = sl->begin(); o_iter != sl->end(); ++o_iter, ++req_iter)
114 if (!(*o_iter)->IsEqual(*req_iter))
121 if (!ok && g_Verbose)
123 std::cout << std::endl << "FAIL: Should be:" << std::endl;
124 DumpStates(&requiredstates);
125 std::cout << std::endl << "FAIL: Is:" << std::endl;
129 for (StateList::iterator iter = requiredstates.begin(); iter != requiredstates.end(); ++iter)
131 for (StateList::iterator iter = sl->begin(); iter != sl->end(); ++iter)
139 #define ADD_STATE(name) g_States.push_back(new name)
140 #define ADD_STATE_PTR(statesptr, name) statesptr->push_back(new name)
142 #define CHECK_STATES(mwah, myerr) if (!StatesOk mwah) { error=myerr; return false; } else if (g_Verbose) { std::cout << "No error: " << myerr << std::endl; }
144 #define MAKE_STATE(name) struct name : State { \
145 virtual void Dump() { \
146 std::cout << " " << #name << std::endl; } \
147 const char *GetName() { return #name; } \
150 #define MAKE_STATE_1(name, p1_type) struct name : State { \
152 name(p1_type param1) : m_Param1(param1) {} \
153 virtual bool IsEqual(State *other) { \
154 if (MakeHash(GetName()) != MakeHash(other->GetName())) \
156 name *other2 = static_cast<name*>(other); \
157 return other2->m_Param1 == m_Param1;\
159 virtual void Dump() { \
160 std::cout << " " << #name << "; Param1=" << m_Param1 << std::endl; } \
161 const char *GetName() { return #name; } \
164 #define MAKE_STATE_2(name, p1_type, p2_type) struct name : State { \
167 name(p1_type param1, p2_type param2) : m_Param1(param1), m_Param2(param2) {} \
168 virtual bool IsEqual(State *other) { \
169 if (MakeHash(GetName()) != MakeHash(other->GetName())) \
171 name *other2 = static_cast<name*>(other); \
172 return other2->m_Param1 == m_Param1 && other2->m_Param2 == m_Param2;\
174 virtual void Dump() { \
175 std::cout << " " << #name << "; Param1=" << m_Param1 << "; Param2=" << m_Param2 << std::endl; } \
176 const char *GetName() { return #name; } \
179 #define MAKE_STATE_3(name, p1_type, p2_type, p3_type) struct name : State { \
183 name(p1_type param1, p2_type param2, p3_type param3) : m_Param1(param1), m_Param2(param2), m_Param3(param3) {} \
184 virtual bool IsEqual(State *other) { \
185 if (MakeHash(GetName()) != MakeHash(other->GetName())) \
187 name *other2 = static_cast<name*>(other); \
188 return other2->m_Param1 == m_Param1 && other2->m_Param2 == m_Param2 && other2->m_Param3 == m_Param3;\
190 virtual void Dump() { \
191 std::cout << " " << #name << "; Param1=" << m_Param1 << "; Param2=" << m_Param2 << "; Param3=" << m_Param3 << std::endl; } \
192 const char *GetName() { return #name; } \
195 #define MAKE_STATE_4(name, p1_type, p2_type, p3_type, p4_type) struct name : State { \
200 name(p1_type param1, p2_type param2, p3_type param3, p4_type param4) : m_Param1(param1), m_Param2(param2), m_Param3(param3), m_Param4(param4) {} \
201 virtual bool IsEqual(State *other) { \
202 if (MakeHash(GetName()) != MakeHash(other->GetName())) \
204 name *other2 = static_cast<name*>(other); \
205 return other2->m_Param1 == m_Param1 && other2->m_Param2 == m_Param2 && other2->m_Param3 == m_Param3 && other2->m_Param4 == m_Param4;\
207 virtual void Dump() { \
208 std::cout << " " << #name << "; Param1=" << m_Param1 << "; Param2=" << m_Param2 << "; Param3=" << m_Param3 << "; Param4=" << m_Param4 << std::endl; } \
209 const char *GetName() { return #name; } \
212 #define CHECK_COND(c, err) if (!(c)) { error = err; return false; }