Line data Source code
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"
48 : namespace CxxTest
49 : {
50 : CXXTEST_TEMPLATE_INSTANTIATION
51 1 : class ValueTraits<const CStr8> : public ValueTraits<const CXXTEST_STD(string)>
52 : {
53 : public:
54 2 : ValueTraits( const CStr8 &s ) : ValueTraits<const CXXTEST_STD(string)>( s.c_str() ) {}
55 : };
56 :
57 5 : CXXTEST_COPY_CONST_TRAITS( CStr8 );
58 :
59 : CXXTEST_TEMPLATE_INSTANTIATION
60 2 : class ValueTraits<const CStrW> : public ValueTraits<const CXXTEST_STD(wstring)>
61 : {
62 : public:
63 2 : ValueTraits( const CStrW &s ) : ValueTraits<const CXXTEST_STD(wstring)>( s.c_str() ) {}
64 : };
65 :
66 4 : CXXTEST_COPY_CONST_TRAITS( CStrW );
67 : }
68 :
69 : // Perform nice printing of vectors
70 : #include "maths/FixedVector3D.h"
71 : #include "maths/Plane.h"
72 : #include "maths/Vector3D.h"
73 :
74 : namespace CxxTest
75 : {
76 : CXXTEST_TEMPLATE_INSTANTIATION
77 0 : class ValueTraits<CFixedVector3D>
78 : {
79 : public:
80 0 : ValueTraits(const CFixedVector3D& v)
81 0 : {
82 0 : std::stringstream s;
83 0 : s << "[" << v.X.ToDouble() << ", " << v.Y.ToDouble() << ", " << v.Z.ToDouble() << "]";
84 0 : m_StringRepr = s.str();
85 0 : }
86 0 : const char* asString() const
87 : {
88 0 : return m_StringRepr.c_str();
89 : }
90 : private:
91 : std::string m_StringRepr;
92 : };
93 :
94 : CXXTEST_TEMPLATE_INSTANTIATION
95 0 : class ValueTraits<CVector3D>
96 : {
97 : public:
98 0 : ValueTraits(const CVector3D& v)
99 0 : {
100 0 : std::stringstream s;
101 0 : s << "[" << v.X << ", " << v.Y << ", " << v.Z << "]";
102 0 : m_StringRepr = s.str();
103 0 : }
104 0 : const char* asString() const
105 : {
106 0 : return m_StringRepr.c_str();
107 : }
108 : private:
109 : std::string m_StringRepr;
110 : };
111 :
112 : CXXTEST_TEMPLATE_INSTANTIATION
113 0 : class ValueTraits<CPlane>
114 : {
115 : public:
116 0 : ValueTraits(const CPlane& p)
117 0 : {
118 0 : std::stringstream ss;
119 0 : ss << "CPlane[";
120 0 : ss << "Norm=" << TS_AS_STRING(p.m_Norm);
121 0 : ss << ", Dist=" << TS_AS_STRING(p.m_Dist);
122 0 : ss << "]";
123 0 : m_StringRepr = ss.str();
124 0 : }
125 0 : 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 :
140 : bool ts_str_contains(const std::string& str1, const std::string& str2); // defined in test_setup.cpp
141 : bool 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 :
147 : template <typename T>
148 3 : std::vector<T> ts_make_vector(T* start, size_t size_bytes)
149 : {
150 3 : 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 :
163 : namespace Script { class Interface; }
164 : // Script-based testing setup (defined in test_setup.cpp). Defines TS_* functions.
165 : void ScriptTestSetup(const Script::Interface&);
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)
170 : OsPath DataDir(); // defined in test_setup.cpp
171 :
172 : #endif // #ifndef INCLUDED_SELF_TEST
|