Pyrogenesis  trunk
self_test.h
Go to the documentation of this file.
1 /* Copyright (C) 2019 Wildfire Games.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef INCLUDED_SELF_TEST
24 #define INCLUDED_SELF_TEST
25 
26 // for convenience, to avoid having to include all of these manually
27 #include "lib/status.h"
28 #include "lib/os_path.h"
29 #include "lib/posix/posix.h"
30 
31 #define CXXTEST_HAVE_EH
32 #define CXXTEST_HAVE_STD
33 
34 // If HAVE_STD wasn't defined at the point the ValueTraits header was included
35 // this header won't have been included and the default traits will be used for
36 // all variables... So fix that now ;-)
37 #include <cxxtest/StdValueTraits.h>
38 #include <cxxtest/TestSuite.h>
39 
40 // TODO: The CStr and Vector code should not be in lib/
41 // Perform nice printing of CStr, based on std::string
42 #include "ps/CStr.h"
43 namespace CxxTest
44 {
45  CXXTEST_TEMPLATE_INSTANTIATION
46  class ValueTraits<const CStr8> : public ValueTraits<const CXXTEST_STD(string)>
47  {
48  public:
49  ValueTraits( const CStr8 &s ) : ValueTraits<const CXXTEST_STD(string)>( s.c_str() ) {}
50  };
51 
53 
54  CXXTEST_TEMPLATE_INSTANTIATION
55  class ValueTraits<const CStrW> : public ValueTraits<const CXXTEST_STD(wstring)>
56  {
57  public:
58  ValueTraits( const CStrW &s ) : ValueTraits<const CXXTEST_STD(wstring)>( s.c_str() ) {}
59  };
60 
62 }
63 
64 // Perform nice printing of vectors
65 #include "maths/FixedVector3D.h"
66 #include "maths/Plane.h"
67 #include "maths/Vector3D.h"
68 
69 namespace CxxTest
70 {
71  CXXTEST_TEMPLATE_INSTANTIATION
72  class ValueTraits<CFixedVector3D>
73  {
74  public:
76  {
77  std::stringstream s;
78  s << "[" << v.X.ToDouble() << ", " << v.Y.ToDouble() << ", " << v.Z.ToDouble() << "]";
79  m_StringRepr = s.str();
80  }
81  const char* asString() const
82  {
83  return m_StringRepr.c_str();
84  }
85  private:
86  std::string m_StringRepr;
87  };
88 
89  CXXTEST_TEMPLATE_INSTANTIATION
90  class ValueTraits<CVector3D>
91  {
92  public:
94  {
95  std::stringstream s;
96  s << "[" << v.X << ", " << v.Y << ", " << v.Z << "]";
97  m_StringRepr = s.str();
98  }
99  const char* asString() const
100  {
101  return m_StringRepr.c_str();
102  }
103  private:
104  std::string m_StringRepr;
105  };
106 
107  CXXTEST_TEMPLATE_INSTANTIATION
108  class ValueTraits<CPlane>
109  {
110  public:
111  ValueTraits(const CPlane& p)
112  {
113  std::stringstream ss;
114  ss << "CPlane[";
115  ss << "Norm=" << TS_AS_STRING(p.m_Norm);
116  ss << ", Dist=" << TS_AS_STRING(p.m_Dist);
117  ss << "]";
118  m_StringRepr = ss.str();
119  }
120  const char* asString() const { return m_StringRepr.c_str(); }
121  private:
122  std::string m_StringRepr;
123  };
124 }
125 
126 #define TS_ASSERT_OK(expr) TS_ASSERT_EQUALS((expr), INFO::OK)
127 #define TSM_ASSERT_OK(m, expr) TSM_ASSERT_EQUALS(m, (expr), INFO::OK)
128 #define TS_ASSERT_STR_EQUALS(str1, str2) TS_ASSERT_EQUALS(std::string(str1), std::string(str2))
129 #define TSM_ASSERT_STR_EQUALS(m, str1, str2) TSM_ASSERT_EQUALS(m, std::string(str1), std::string(str2))
130 #define TS_ASSERT_WSTR_EQUALS(str1, str2) TS_ASSERT_EQUALS(std::wstring(str1), std::wstring(str2))
131 #define TSM_ASSERT_WSTR_EQUALS(m, str1, str2) TSM_ASSERT_EQUALS(m, std::wstring(str1), std::wstring(str2))
132 #define TS_ASSERT_PATH_EQUALS(path1, path2) TS_ASSERT_EQUALS((path1).string(), (path2).string())
133 #define TSM_ASSERT_PATH_EQUALS(m, path1, path2) TSM_ASSERT_EQUALS(m, (path1).string(), (path2).string())
134 
135 bool ts_str_contains(const std::string& str1, const std::string& str2); // defined in test_setup.cpp
136 bool ts_str_contains(const std::wstring& str1, const std::wstring& str2); // defined in test_setup.cpp
137 #define TS_ASSERT_STR_CONTAINS(str1, str2) TSM_ASSERT(str1, ts_str_contains(str1, str2))
138 #define TS_ASSERT_STR_NOT_CONTAINS(str1, str2) TSM_ASSERT(str1, !ts_str_contains(str1, str2))
139 #define TS_ASSERT_WSTR_CONTAINS(str1, str2) TSM_ASSERT(str1, ts_str_contains(str1, str2))
140 #define TS_ASSERT_WSTR_NOT_CONTAINS(str1, str2) TSM_ASSERT(str1, !ts_str_contains(str1, str2))
141 
142 template <typename T>
143 std::vector<T> ts_make_vector(T* start, size_t size_bytes)
144 {
145  return std::vector<T>(start, start+(size_bytes/sizeof(T)));
146 }
147 #define TS_ASSERT_VECTOR_EQUALS_ARRAY(vec1, array) TS_ASSERT_EQUALS(vec1, ts_make_vector((array), sizeof(array)))
148 #define TS_ASSERT_VECTOR_CONTAINS(vec1, element) TS_ASSERT(std::find((vec1).begin(), (vec1).end(), element) != (vec1).end());
149 
150 class ScriptInterface;
151 // Script-based testing setup (defined in test_setup.cpp). Defines TS_* functions.
152 void ScriptTestSetup(const ScriptInterface&);
153 
154 // Default game data directory
155 // (TODO: game-specific functions like this probably shouldn't be inside lib/, but it's useful
156 // here since lots of tests use it)
157 OsPath DataDir(); // defined in test_setup.cpp
158 
159 #endif // #ifndef INCLUDED_SELF_TEST
OsPath DataDir()
Definition: test_setup.cpp:137
Definition: Vector3D.h:30
const char * asString() const
Definition: self_test.h:120
fixed Y
Definition: FixedVector3D.h:27
ValueTraits(const CStrW &s)
Definition: self_test.h:58
std::string m_StringRepr
Definition: self_test.h:104
float X
Definition: Vector3D.h:33
CXXTEST_COPY_CONST_TRAITS(CStr8)
Definition: path.h:79
float Y
Definition: Vector3D.h:33
Definition: FixedVector3D.h:24
ValueTraits(const CFixedVector3D &v)
Definition: self_test.h:75
#define T(string_literal)
Definition: secure_crt.cpp:77
ValueTraits(const CPlane &p)
Definition: self_test.h:111
void ScriptTestSetup(const ScriptInterface &)
Definition: test_setup.cpp:152
const char * asString() const
Definition: self_test.h:99
CVector3D m_Norm
Definition: Plane.h:68
fixed Z
Definition: FixedVector3D.h:27
fixed X
Definition: FixedVector3D.h:27
std::vector< T > ts_make_vector(T *start, size_t size_bytes)
Definition: self_test.h:143
bool ts_str_contains(const std::string &str1, const std::string &str2)
Definition: test_setup.cpp:123
ValueTraits(const CVector3D &v)
Definition: self_test.h:93
float Z
Definition: Vector3D.h:33
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:71
Definition: Plane.h:38
double ToDouble() const
Convert to double. Won&#39;t be lossy - double can precisely represent all values.
Definition: Fixed.h:177
ValueTraits(const CStr8 &s)
Definition: self_test.h:49
Definition: self_test.h:43
std::string m_StringRepr
Definition: self_test.h:86
const char * asString() const
Definition: self_test.h:81
std::string m_StringRepr
Definition: self_test.h:122
float m_Dist
Definition: Plane.h:69