LCOV - code coverage report
Current view: top level - source/simulation2/system - ComponentTest.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 71 92 77.2 %
Date: 2026-12-03 01:19:39 Functions: 11 25 44.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_COMPONENTTEST
      19             : #define INCLUDED_COMPONENTTEST
      20             : 
      21             : #include "lib/self_test.h"
      22             : 
      23             : #include "graphics/Terrain.h"
      24             : #include "maths/Fixed.h"
      25             : #include "maths/FixedVector3D.h"
      26             : #include "maths/Vector3D.h"
      27             : #include "ps/Errors.h"
      28             : #include "scriptinterface/Interface.h"
      29             : #include "simulation2/components/ICmpTerrain.h"
      30             : #include "simulation2/helpers/Position.h"
      31             : #include "simulation2/serialization/DebugSerializer.h"
      32             : #include "simulation2/serialization/HashSerializer.h"
      33             : #include "simulation2/serialization/StdDeserializer.h"
      34             : #include "simulation2/serialization/StdSerializer.h"
      35             : #include "simulation2/system/Component.h"
      36             : #include "simulation2/system/Entity.h"
      37             : 
      38             : #include <cstdio>
      39             : #include <iostream>
      40             : #include <sstream>
      41             : #include <string>
      42             : 
      43             : class CMessage;
      44             : namespace Script { class Context; }
      45             : 
      46             : /**
      47             :  * @file
      48             :  * Various common features for component test cases.
      49             :  */
      50             : 
      51             : /**
      52             :  * Class to test a single component.
      53             :  * - Create an instance of this class
      54             :  * - Use AddMock to add mock components that the tested component relies on
      55             :  * - Use Add to add the test component itself, and it returns a component pointer
      56             :  * - Call methods on the component pointer
      57             :  * - Use Roundtrip to test the consistency of serialization
      58             :  */
      59             : class ComponentTestHelper
      60             : {
      61             :     CSimContext m_Context;
      62             :     CComponentManager m_ComponentManager;
      63             :     CParamNode m_Param;
      64             :     IComponent* m_Cmp;
      65             :     EComponentTypeId m_Cid;
      66             :     bool m_isSystemEntityInit = false;
      67             : 
      68             : public:
      69          30 :     ComponentTestHelper(Script::Context& scriptContext) :
      70          30 :         m_Context(), m_ComponentManager(m_Context, scriptContext), m_Cmp(NULL)
      71             :     {
      72          30 :         m_ComponentManager.LoadComponentTypes();
      73          30 :     }
      74             : 
      75          31 :     const Script::Interface& GetScriptInterface()
      76             :     {
      77           6 :         return m_ComponentManager.GetScriptInterface();
      78             :     }
      79             : 
      80             :     CSimContext& GetSimContext()
      81             :     {
      82             :         return m_Context;
      83             :     }
      84             : 
      85             :     /**
      86             :      * Call this once to initialise the test helper with a component.
      87             :      */
      88             :     template<typename T>
      89          25 :     T* Add(EComponentTypeId cid, const std::string& xml, entity_id_t ent = 10)
      90             :     {
      91          25 :         TS_ASSERT(m_Cmp == NULL);
      92             : 
      93          25 :         CEntityHandle handle;
      94          25 :         if (ent == SYSTEM_ENTITY)
      95             :         {
      96          22 :             if (!m_isSystemEntityInit)
      97             :             {
      98          22 :                 m_ComponentManager.InitSystemEntity();
      99          22 :                 m_isSystemEntityInit = true;
     100             :             }
     101          22 :             handle = m_ComponentManager.GetSystemEntity();
     102          22 :             m_Context.SetSystemEntity(handle);
     103             :         }
     104             :         else
     105           3 :             handle = m_ComponentManager.LookupEntityHandle(ent, true);
     106             : 
     107          25 :         m_Cid = cid;
     108          75 :         TS_ASSERT_EQUALS(CParamNode::LoadXMLString(m_Param, ("<test>" + xml + "</test>").c_str()), PSRETURN_OK);
     109          25 :         TS_ASSERT(m_ComponentManager.AddComponent(handle, m_Cid, m_Param.GetChild("test")));
     110          25 :         m_Cmp = m_ComponentManager.QueryInterface(ent, T::GetInterfaceId());
     111          25 :         TS_ASSERT(m_Cmp != NULL);
     112          25 :         return static_cast<T*> (m_Cmp);
     113             :     }
     114             : 
     115          40 :     void AddMock(entity_id_t ent, EInterfaceId iid, IComponent& component)
     116             :     {
     117          40 :         CEntityHandle handle;
     118          40 :         if (ent == SYSTEM_ENTITY)
     119             :         {
     120           7 :             if (!m_isSystemEntityInit)
     121             :             {
     122           3 :                 m_ComponentManager.InitSystemEntity();
     123           3 :                 m_isSystemEntityInit = true;
     124             :             }
     125           7 :             handle = m_ComponentManager.GetSystemEntity();
     126           7 :             m_Context.SetSystemEntity(handle);
     127             :         }
     128             :         else
     129          33 :             handle = m_ComponentManager.LookupEntityHandle(ent, true);
     130             : 
     131          40 :         m_ComponentManager.AddMockComponent(handle, iid, component);
     132          40 :     }
     133             : 
     134           2 :     void HandleMessage(IComponent* cmp, const CMessage& msg, bool global)
     135             :     {
     136           2 :         cmp->HandleMessage(msg, global);
     137           2 :     }
     138             : 
     139             :     /**
     140             :      * Checks that the object roundtrips through its serialize/deserialize functions correctly.
     141             :      * Computes the debug output, hash, and binary serialization; then deserializes into a new
     142             :      * system and checks the serialization outputs are unchanged.
     143             :      */
     144           5 :     void Roundtrip(bool verbose = false)
     145             :     {
     146           5 :         std::stringstream dbgstr1;
     147           5 :         CDebugSerializer dbg1(GetScriptInterface(), dbgstr1);
     148           5 :         m_Cmp->Serialize(dbg1);
     149             : 
     150           5 :         if (verbose)
     151           0 :             std::cout << "--------\n" << dbgstr1.str() << "--------\n";
     152             : 
     153           5 :         CHashSerializer hash1(GetScriptInterface());
     154           5 :         m_Cmp->Serialize(hash1);
     155             : 
     156           5 :         std::stringstream stdstr1;
     157           5 :         CStdSerializer std1(GetScriptInterface(), stdstr1);
     158           5 :         m_Cmp->Serialize(std1);
     159             : 
     160           5 :         ComponentTestHelper test2(GetScriptInterface().GetContext());
     161             :         // (We should never need to add any mock objects etc to test2, since deserialization
     162             :         // mustn't depend on other components already existing)
     163             : 
     164           5 :         CEntityHandle ent = test2.m_ComponentManager.LookupEntityHandle(10, true);
     165             : 
     166           5 :         CStdDeserializer stdde2(test2.GetScriptInterface(), stdstr1);
     167             : 
     168           5 :         IComponent* cmp2 = test2.m_ComponentManager.ConstructComponent(ent, m_Cid);
     169           5 :         cmp2->Deserialize(m_Param.GetChild("test"), stdde2);
     170             : 
     171           5 :         TS_ASSERT(stdstr1.peek() == EOF); // Deserialize must read whole stream
     172             : 
     173           5 :         std::stringstream dbgstr2;
     174           5 :         CDebugSerializer dbg2(test2.GetScriptInterface(), dbgstr2);
     175           5 :         cmp2->Serialize(dbg2);
     176             : 
     177           5 :         if (verbose)
     178           0 :             std::cout << "--------\n" << dbgstr2.str() << "--------\n";
     179             : 
     180           5 :         CHashSerializer hash2(test2.GetScriptInterface());
     181           5 :         cmp2->Serialize(hash2);
     182             : 
     183           5 :         std::stringstream stdstr2;
     184           5 :         CStdSerializer std2(test2.GetScriptInterface(), stdstr2);
     185           5 :         cmp2->Serialize(std2);
     186             : 
     187          15 :         TS_ASSERT_EQUALS(dbgstr1.str(), dbgstr2.str());
     188             : 
     189           5 :         TS_ASSERT_EQUALS(hash1.GetHashLength(), hash2.GetHashLength());
     190           5 :         TS_ASSERT_SAME_DATA(hash1.ComputeHash(), hash2.ComputeHash(), hash1.GetHashLength());
     191             : 
     192          14 :         TS_ASSERT_EQUALS(stdstr1.str(), stdstr2.str());
     193             : 
     194             :         // TODO: need to extend this so callers can run methods on the cloned component
     195             :         // to check that all its data is still correct
     196           5 :     }
     197             : };
     198             : 
     199             : /**
     200             :  * Simple terrain implementation with constant height of 50.
     201             :  */
     202           6 : class MockTerrain : public ICmpTerrain
     203             : {
     204             : public:
     205           0 :     DEFAULT_MOCK_COMPONENT()
     206             : 
     207           0 :     bool IsLoaded() const override
     208             :     {
     209           0 :         return true;
     210             :     }
     211             : 
     212           0 :     CFixedVector3D CalcNormal(entity_pos_t /*x*/, entity_pos_t /*z*/) const override
     213             :     {
     214           0 :         return CFixedVector3D(fixed::FromInt(0), fixed::FromInt(1), fixed::FromInt(0));
     215             :     }
     216             : 
     217           0 :     CVector3D CalcExactNormal(float /*x*/, float /*z*/) const override
     218             :     {
     219           0 :         return CVector3D(0.f, 1.f, 0.f);
     220             :     }
     221             : 
     222          18 :     entity_pos_t GetGroundLevel(entity_pos_t /*x*/, entity_pos_t /*z*/) const override
     223             :     {
     224          18 :         return entity_pos_t::FromInt(50);
     225             :     }
     226             : 
     227          43 :     float GetExactGroundLevel(float /*x*/, float /*z*/) const override
     228             :     {
     229          43 :         return 50.f;
     230             :     }
     231             : 
     232           0 :     std::uint16_t GetTilesPerSide() const override
     233             :     {
     234           0 :         return 16;
     235             :     }
     236             : 
     237           0 :     std::uint32_t GetMapSize() const override
     238             :     {
     239           0 :         return GetTilesPerSide() * TERRAIN_TILE_SIZE;
     240             :     }
     241             : 
     242           0 :     std::uint16_t GetVerticesPerSide() const override
     243             :     {
     244           0 :         return 17;
     245             :     }
     246             : 
     247           0 :     CTerrain* GetCTerrain() override
     248             :     {
     249           0 :         return nullptr;
     250             :     }
     251             : 
     252           0 :     void MakeDirty(std::int32_t /*i0*/, std::int32_t /*j0*/, std::int32_t /*i1*/, std::int32_t /*j1*/) override
     253             :     {
     254           0 :     }
     255             : 
     256           0 :     void ReloadTerrain(bool /*ReloadWater*/) override
     257             :     {
     258           0 :     }
     259             : };
     260             : 
     261             : #endif // INCLUDED_COMPONENTTEST

Generated by: LCOV version 1.16