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