LCOV - code coverage report
Current view: top level - source/simulation2/components - CCmpTest.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 99 114 86.8 %
Date: 2026-12-02 01:21:50 Functions: 50 57 87.7 %

          Line data    Source code
       1             : /* Copyright (C) 2025 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             : #include "precompiled.h"
      19             : 
      20             : #include "ICmpTest.h"
      21             : 
      22             : #include "simulation2/MessageTypes.h"
      23             : #include "simulation2/scripting/ScriptComponent.h"
      24             : #include "simulation2/system/Component.h"
      25             : #include "simulation2/system/Message.h"
      26             : 
      27             : #include <cstdint>
      28             : #include <string>
      29             : 
      30          19 : class CCmpTest1A : public ICmpTest1
      31             : {
      32             : public:
      33         137 :     static void ClassInit(CComponentManager& componentManager)
      34             :     {
      35         137 :         componentManager.SubscribeToMessageType(MT_TurnStart);
      36         137 :         componentManager.SubscribeToMessageType(MT_Interpolate);
      37         137 :         componentManager.SubscribeToMessageType(MT_Destroy);
      38         137 :     }
      39             : 
      40          46 :     DEFAULT_COMPONENT_ALLOCATOR(Test1A)
      41             : 
      42             :     int32_t m_x;
      43             : 
      44         137 :     static std::string GetSchema()
      45             :     {
      46         274 :         return "<a:component type='test'/><ref name='anything'/>";
      47             :     }
      48             : 
      49          17 :     void Init(const CParamNode& paramNode) override
      50             :     {
      51          17 :         if (paramNode.GetChild("x").IsOk())
      52           9 :             m_x = paramNode.GetChild("x").ToInt();
      53             :         else
      54           8 :             m_x = 11000;
      55          17 :     }
      56             : 
      57          19 :     void Deinit() override
      58             :     {
      59          19 :     }
      60             : 
      61           6 :     void Serialize(ISerializer& serialize) override
      62             :     {
      63           6 :         serialize.NumberI32_Unbounded("x", m_x);
      64           6 :     }
      65             : 
      66           2 :     void Deserialize(const CParamNode&, IDeserializer& deserialize) override
      67             :     {
      68           2 :         deserialize.NumberI32_Unbounded("x", m_x);
      69           2 :     }
      70             : 
      71          31 :     int GetX() override
      72             :     {
      73          31 :         return m_x;
      74             :     }
      75             : 
      76          11 :     void HandleMessage(const CMessage& msg, bool /*global*/) override
      77             :     {
      78          11 :         switch (msg.GetType())
      79             :         {
      80           3 :         case MT_Destroy:
      81           3 :             GetSimContext().GetComponentManager().DynamicSubscriptionNonsync(MT_RenderSubmit, this, false);
      82           3 :             break;
      83           5 :         case MT_TurnStart:
      84           5 :             m_x += 1;
      85           5 :             break;
      86           3 :         case MT_Interpolate:
      87           3 :             m_x += 2;
      88           3 :             break;
      89           0 :         default:
      90           0 :             m_x = 0;
      91           0 :             break;
      92             :         }
      93          11 :     }
      94             : };
      95             : 
      96         274 : REGISTER_COMPONENT_TYPE(Test1A)
      97             : 
      98           2 : class CCmpTest1B : public ICmpTest1
      99             : {
     100             : public:
     101         137 :     static void ClassInit(CComponentManager& componentManager)
     102             :     {
     103         137 :         componentManager.SubscribeToMessageType(MT_Update);
     104         137 :         componentManager.SubscribeGloballyToMessageType(MT_Interpolate);
     105             :     }
     106             : 
     107           4 :     DEFAULT_COMPONENT_ALLOCATOR(Test1B)
     108             : 
     109             :     int32_t m_x;
     110             : 
     111         137 :     static std::string GetSchema()
     112             :     {
     113         274 :         return "<a:component type='test'/><empty/>";
     114             :     }
     115             : 
     116           2 :     void Init(const CParamNode&) override
     117             :     {
     118           2 :         m_x = 12000;
     119           2 :     }
     120             : 
     121           2 :     void Deinit() override
     122             :     {
     123           2 :     }
     124             : 
     125           0 :     void Serialize(ISerializer& serialize) override
     126             :     {
     127           0 :         serialize.NumberI32_Unbounded("x", m_x);
     128           0 :     }
     129             : 
     130           0 :     void Deserialize(const CParamNode&, IDeserializer& deserialize) override
     131             :     {
     132           0 :         deserialize.NumberI32_Unbounded("x", m_x);
     133           0 :     }
     134             : 
     135           6 :     int GetX() override
     136             :     {
     137           6 :         return m_x;
     138             :     }
     139             : 
     140           3 :     void HandleMessage(const CMessage& msg, bool /*global*/) override
     141             :     {
     142           3 :         switch (msg.GetType())
     143             :         {
     144           1 :         case MT_Update:
     145           1 :             m_x += 10;
     146           1 :             break;
     147           2 :         case MT_Interpolate:
     148           2 :             m_x += 20;
     149           2 :             break;
     150           0 :         default:
     151           0 :             m_x = 0;
     152           0 :             break;
     153             :         }
     154           3 :     }
     155             : };
     156             : 
     157         411 : REGISTER_COMPONENT_TYPE(Test1B)
     158             : 
     159           9 : class CCmpTest2A : public ICmpTest2
     160             : {
     161             : public:
     162         137 :     static void ClassInit(CComponentManager& componentManager)
     163             :     {
     164         137 :         componentManager.SubscribeToMessageType(MT_TurnStart);
     165         137 :         componentManager.SubscribeToMessageType(MT_Update);
     166             :     }
     167             : 
     168          26 :     DEFAULT_COMPONENT_ALLOCATOR(Test2A)
     169             : 
     170             :     int32_t m_x;
     171             : 
     172         137 :     static std::string GetSchema()
     173             :     {
     174         137 :         return "<a:component type='test'/><empty/>";
     175             :     }
     176             : 
     177           8 :     void Init(const CParamNode&) override
     178             :     {
     179           8 :         m_x = 21000;
     180           8 :     }
     181             : 
     182           9 :     void Deinit() override
     183             :     {
     184           9 :     }
     185             : 
     186           4 :     void Serialize(ISerializer& serialize) override
     187             :     {
     188           4 :         serialize.NumberI32_Unbounded("x", m_x);
     189           4 :     }
     190             : 
     191           1 :     void Deserialize(const CParamNode&, IDeserializer& deserialize) override
     192             :     {
     193           1 :         deserialize.NumberI32_Unbounded("x", m_x);
     194           1 :     }
     195             : 
     196          18 :     int GetX() override
     197             :     {
     198          18 :         return m_x;
     199             :     }
     200             : 
     201           6 :     void HandleMessage(const CMessage& msg, bool /*global*/) override
     202             :     {
     203           6 :         switch (msg.GetType())
     204             :         {
     205           2 :         case MT_TurnStart:
     206           2 :             m_x += 50;
     207           2 :             break;
     208           4 :         case MT_Update:
     209           4 :             m_x += static_cast<const CMessageUpdate&> (msg).turnLength.ToInt_RoundToZero();
     210           4 :             break;
     211           0 :         default:
     212           0 :             m_x = 0;
     213           0 :             break;
     214             :         }
     215           6 :     }
     216             : };
     217             : 
     218         411 : REGISTER_COMPONENT_TYPE(Test2A)
     219             : 
     220             : ////////////////////////////////////////////////////////////////
     221             : 
     222             : class CCmpTest1Scripted : public ICmpTest1
     223             : {
     224             : public:
     225         539 :     DEFAULT_SCRIPT_WRAPPER(Test1Scripted)
     226             : 
     227          46 :     int GetX() override
     228             :     {
     229          46 :         return m_Script.Call<int> ("GetX");
     230             :     }
     231             : };
     232             : 
     233         274 : REGISTER_COMPONENT_SCRIPT_WRAPPER(Test1Scripted)
     234             : 
     235             : ////////////////////////////////////////////////////////////////
     236             : 
     237             : class CCmpTest2Scripted : public ICmpTest2
     238             : {
     239             : public:
     240         333 :     DEFAULT_SCRIPT_WRAPPER(Test2Scripted)
     241             : 
     242          14 :     int GetX() override
     243             :     {
     244          14 :         return m_Script.Call<int> ("GetX");
     245             :     }
     246             : };
     247             : 
     248         274 : REGISTER_COMPONENT_SCRIPT_WRAPPER(Test2Scripted)

Generated by: LCOV version 1.16