Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
self_test.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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/lib.h"
28#include "lib/os_path.h"
29#include "lib/status.h"
30#include "lib/posix/posix.h"
31
32#define CXXTEST_HAVE_EH
33#define CXXTEST_HAVE_STD
34
35// If HAVE_STD wasn't defined at the point the ValueTraits header was included
36// this header won't have been included and the default traits will be used for
37// all variables... So fix that now ;-)
38#include <cxxtest/StdValueTraits.h>
39#include <cxxtest/TestSuite.h>
40
41// TODO: The CStr and Vector code should not be in lib/
42// Perform nice printing of CStr, based on std::string
43#include "ps/CStr.h"
44namespace CxxTest
45{
46 CXXTEST_TEMPLATE_INSTANTIATION
47 class ValueTraits<const CStr8> : public ValueTraits<const CXXTEST_STD(string)>
48 {
49 public:
50 ValueTraits( const CStr8 &s ) : ValueTraits<const CXXTEST_STD(string)>( s.c_str() ) {}
51 };
52
54
55 CXXTEST_TEMPLATE_INSTANTIATION
56 class ValueTraits<const CStrW> : public ValueTraits<const CXXTEST_STD(wstring)>
57 {
58 public:
59 ValueTraits( const CStrW &s ) : ValueTraits<const CXXTEST_STD(wstring)>( s.c_str() ) {}
60 };
61
63}
64
65// Perform nice printing of vectors
66#include "maths/FixedVector3D.h"
67#include "maths/Plane.h"
68#include "maths/Vector3D.h"
69
70namespace CxxTest
71{
72 CXXTEST_TEMPLATE_INSTANTIATION
73 class ValueTraits<CFixedVector3D>
74 {
75 public:
77 {
78 std::stringstream s;
79 s << "[" << v.X.ToDouble() << ", " << v.Y.ToDouble() << ", " << v.Z.ToDouble() << "]";
80 m_StringRepr = s.str();
81 }
82 const char* asString() const
83 {
84 return m_StringRepr.c_str();
85 }
86 private:
87 std::string m_StringRepr;
88 };
89
90 CXXTEST_TEMPLATE_INSTANTIATION
91 class ValueTraits<CVector3D>
92 {
93 public:
95 {
96 std::stringstream s;
97 s << "[" << v.X << ", " << v.Y << ", " << v.Z << "]";
98 m_StringRepr = s.str();
99 }
100 const char* asString() const
101 {
102 return m_StringRepr.c_str();
103 }
104 private:
105 std::string m_StringRepr;
106 };
107
108 CXXTEST_TEMPLATE_INSTANTIATION
109 class ValueTraits<CPlane>
110 {
111 public:
113 {
114 std::stringstream ss;
115 ss << "CPlane[";
116 ss << "Norm=" << TS_AS_STRING(p.m_Norm);
117 ss << ", Dist=" << TS_AS_STRING(p.m_Dist);
118 ss << "]";
119 m_StringRepr = ss.str();
120 }
121 const char* asString() const { return m_StringRepr.c_str(); }
122 private:
123 std::string m_StringRepr;
124 };
125}
126
127#define TS_ASSERT_OK(expr) TS_ASSERT_EQUALS((expr), INFO::OK)
128#define TSM_ASSERT_OK(m, expr) TSM_ASSERT_EQUALS(m, (expr), INFO::OK)
129#define TS_ASSERT_STR_EQUALS(str1, str2) TS_ASSERT_EQUALS(std::string(str1), std::string(str2))
130#define TSM_ASSERT_STR_EQUALS(m, str1, str2) TSM_ASSERT_EQUALS(m, std::string(str1), std::string(str2))
131#define TS_ASSERT_WSTR_EQUALS(str1, str2) TS_ASSERT_EQUALS(std::wstring(str1), std::wstring(str2))
132#define TSM_ASSERT_WSTR_EQUALS(m, str1, str2) TSM_ASSERT_EQUALS(m, std::wstring(str1), std::wstring(str2))
133#define TS_ASSERT_PATH_EQUALS(path1, path2) TS_ASSERT_EQUALS((path1).string(), (path2).string())
134#define TSM_ASSERT_PATH_EQUALS(m, path1, path2) TSM_ASSERT_EQUALS(m, (path1).string(), (path2).string())
135
136bool ts_str_contains(const std::string& str1, const std::string& str2); // defined in test_setup.cpp
137bool ts_str_contains(const std::wstring& str1, const std::wstring& str2); // defined in test_setup.cpp
138#define TS_ASSERT_STR_CONTAINS(str1, str2) TSM_ASSERT(str1, ts_str_contains(str1, str2))
139#define TS_ASSERT_STR_NOT_CONTAINS(str1, str2) TSM_ASSERT(str1, !ts_str_contains(str1, str2))
140#define TS_ASSERT_WSTR_CONTAINS(str1, str2) TSM_ASSERT(str1, ts_str_contains(str1, str2))
141#define TS_ASSERT_WSTR_NOT_CONTAINS(str1, str2) TSM_ASSERT(str1, !ts_str_contains(str1, str2))
142
143template <typename T>
144std::vector<T> ts_make_vector(T* start, size_t size_bytes)
145{
146 return std::vector<T>(start, start+(size_bytes/sizeof(T)));
147}
148#define TS_ASSERT_VECTOR_EQUALS_ARRAY(vec1, array) TS_ASSERT_EQUALS(vec1, ts_make_vector((array), sizeof(array)))
149#define TS_ASSERT_VECTOR_CONTAINS(vec1, element) TS_ASSERT(std::find((vec1).begin(), (vec1).end(), element) != (vec1).end());
150
151#define TS_ASSERT_MATRIX_EQUALS_DELTA(m1, m2, size, epsilon) \
152 for (int j = 0; j < size; ++j) \
153 TS_ASSERT_DELTA(m1._data[j], m2._data[j], epsilon);
154
155#define TS_ASSERT_MATRIX_DIFFERS_DELTA(m1, m2, size, epsilon) \
156 for (int j = 0; j < size; ++j) \
157 TS_ASSERT(!feq(m1._data[j], m2._data[j], epsilon));
158
159class ScriptInterface;
160// Script-based testing setup (defined in test_setup.cpp). Defines TS_* functions.
162
163// Default game data directory
164// (TODO: game-specific functions like this probably shouldn't be inside lib/, but it's useful
165// here since lots of tests use it)
166OsPath DataDir(); // defined in test_setup.cpp
167
168#endif // #ifndef INCLUDED_SELF_TEST
Definition: FixedVector3D.h:25
fixed Y
Definition: FixedVector3D.h:27
fixed Z
Definition: FixedVector3D.h:27
fixed X
Definition: FixedVector3D.h:27
double ToDouble() const
Convert to double. Won't be lossy - double can precisely represent all values.
Definition: Fixed.h:177
Definition: Plane.h:39
CVector3D m_Norm
Definition: Plane.h:68
float m_Dist
Definition: Plane.h:69
Definition: Vector3D.h:31
float Z
Definition: Vector3D.h:33
float Y
Definition: Vector3D.h:33
float X
Definition: Vector3D.h:33
const char * asString() const
Definition: self_test.h:82
std::string m_StringRepr
Definition: self_test.h:87
ValueTraits(const CFixedVector3D &v)
Definition: self_test.h:76
ValueTraits(const CPlane &p)
Definition: self_test.h:112
std::string m_StringRepr
Definition: self_test.h:123
const char * asString() const
Definition: self_test.h:121
std::string m_StringRepr
Definition: self_test.h:105
ValueTraits(const CVector3D &v)
Definition: self_test.h:94
const char * asString() const
Definition: self_test.h:100
ValueTraits(const CStr8 &s)
Definition: self_test.h:50
ValueTraits(const CStrW &s)
Definition: self_test.h:59
Definition: path.h:80
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:72
Definition: self_test.h:45
CXXTEST_COPY_CONST_TRAITS(CStr8)
#define T(string_literal)
Definition: secure_crt.cpp:77
bool ts_str_contains(const std::string &str1, const std::string &str2)
Definition: test_setup.cpp:113
OsPath DataDir()
Definition: test_setup.cpp:127
void ScriptTestSetup(const ScriptInterface &)
Definition: test_setup.cpp:142
std::vector< T > ts_make_vector(T *start, size_t size_bytes)
Definition: self_test.h:144