LCOV - code coverage report
Current view: top level - source/lib - self_test.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 8 37 21.6 %
Date: 2023-01-19 00:18:29 Functions: 11 20 55.0 %

          Line data    Source code
       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           1 :     class ValueTraits<const CStr8> : public ValueTraits<const CXXTEST_STD(string)>
      47             :     {
      48             :     public:
      49           1 :         ValueTraits( const CStr8 &s ) : ValueTraits<const CXXTEST_STD(string)>( s.c_str() ) {}
      50             :     };
      51             : 
      52           3 :     CXXTEST_COPY_CONST_TRAITS( CStr8 );
      53             : 
      54             :     CXXTEST_TEMPLATE_INSTANTIATION
      55           1 :     class ValueTraits<const CStrW> : public ValueTraits<const CXXTEST_STD(wstring)>
      56             :     {
      57             :     public:
      58           1 :         ValueTraits( const CStrW &s ) : ValueTraits<const CXXTEST_STD(wstring)>( s.c_str() ) {}
      59             :     };
      60             : 
      61           3 :     CXXTEST_COPY_CONST_TRAITS( CStrW );
      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           0 :     class ValueTraits<CFixedVector3D>
      73             :     {
      74             :     public:
      75           0 :         ValueTraits(const CFixedVector3D& v)
      76           0 :         {
      77           0 :             std::stringstream s;
      78           0 :             s << "[" << v.X.ToDouble() << ", " << v.Y.ToDouble() << ", " << v.Z.ToDouble() << "]";
      79           0 :             m_StringRepr = s.str();
      80           0 :         }
      81           0 :         const char* asString() const
      82             :         {
      83           0 :             return m_StringRepr.c_str();
      84             :         }
      85             :     private:
      86             :         std::string m_StringRepr;
      87             :     };
      88             : 
      89             :     CXXTEST_TEMPLATE_INSTANTIATION
      90           0 :     class ValueTraits<CVector3D>
      91             :     {
      92             :     public:
      93           0 :         ValueTraits(const CVector3D& v)
      94           0 :         {
      95           0 :             std::stringstream s;
      96           0 :             s << "[" << v.X << ", " << v.Y << ", " << v.Z << "]";
      97           0 :             m_StringRepr = s.str();
      98           0 :         }
      99           0 :         const char* asString() const
     100             :         {
     101           0 :             return m_StringRepr.c_str();
     102             :         }
     103             :     private:
     104             :         std::string m_StringRepr;
     105             :     };
     106             : 
     107             :     CXXTEST_TEMPLATE_INSTANTIATION
     108           0 :     class ValueTraits<CPlane>
     109             :     {
     110             :     public:
     111           0 :         ValueTraits(const CPlane& p)
     112           0 :         {
     113           0 :             std::stringstream ss;
     114           0 :             ss << "CPlane[";
     115           0 :             ss << "Norm=" << TS_AS_STRING(p.m_Norm);
     116           0 :             ss << ", Dist=" << TS_AS_STRING(p.m_Dist);
     117           0 :             ss << "]";
     118           0 :             m_StringRepr = ss.str();
     119           0 :         }
     120           0 :         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           3 : std::vector<T> ts_make_vector(T* start, size_t size_bytes)
     144             : {
     145           3 :     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

Generated by: LCOV version 1.13