test_clientactiveobjectmgr.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Minetest
  3. Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "client/activeobjectmgr.h"
  17. #include <algorithm>
  18. #include "test.h"
  19. #include "profiler.h"
  20. class TestClientActiveObject : public ClientActiveObject
  21. {
  22. public:
  23. TestClientActiveObject() : ClientActiveObject(0, nullptr, nullptr) {}
  24. ~TestClientActiveObject() = default;
  25. ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_TEST; }
  26. virtual void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) {}
  27. };
  28. class TestClientActiveObjectMgr : public TestBase
  29. {
  30. public:
  31. TestClientActiveObjectMgr() { TestManager::registerTestModule(this); }
  32. const char *getName() { return "TestClientActiveObjectMgr"; }
  33. void runTests(IGameDef *gamedef);
  34. void testFreeID();
  35. void testRegisterObject();
  36. void testRemoveObject();
  37. };
  38. static TestClientActiveObjectMgr g_test_instance;
  39. void TestClientActiveObjectMgr::runTests(IGameDef *gamedef)
  40. {
  41. TEST(testFreeID);
  42. TEST(testRegisterObject)
  43. TEST(testRemoveObject)
  44. }
  45. ////////////////////////////////////////////////////////////////////////////////
  46. void TestClientActiveObjectMgr::testFreeID()
  47. {
  48. client::ActiveObjectMgr caomgr;
  49. std::vector<u16> aoids;
  50. u16 aoid = caomgr.getFreeId();
  51. // Ensure it's not the same id
  52. UASSERT(caomgr.getFreeId() != aoid);
  53. aoids.push_back(aoid);
  54. // Register basic objects, ensure we never found
  55. for (u8 i = 0; i < UINT8_MAX; i++) {
  56. // Register an object
  57. auto tcao = new TestClientActiveObject();
  58. caomgr.registerObject(tcao);
  59. aoids.push_back(tcao->getId());
  60. // Ensure next id is not in registered list
  61. UASSERT(std::find(aoids.begin(), aoids.end(), caomgr.getFreeId()) ==
  62. aoids.end());
  63. }
  64. caomgr.clear();
  65. }
  66. void TestClientActiveObjectMgr::testRegisterObject()
  67. {
  68. client::ActiveObjectMgr caomgr;
  69. auto tcao = new TestClientActiveObject();
  70. UASSERT(caomgr.registerObject(tcao));
  71. u16 id = tcao->getId();
  72. auto tcaoToCompare = caomgr.getActiveObject(id);
  73. UASSERT(tcaoToCompare->getId() == id);
  74. UASSERT(tcaoToCompare == tcao);
  75. tcao = new TestClientActiveObject();
  76. UASSERT(caomgr.registerObject(tcao));
  77. UASSERT(caomgr.getActiveObject(tcao->getId()) == tcao);
  78. UASSERT(caomgr.getActiveObject(tcao->getId()) != tcaoToCompare);
  79. caomgr.clear();
  80. }
  81. void TestClientActiveObjectMgr::testRemoveObject()
  82. {
  83. client::ActiveObjectMgr caomgr;
  84. auto tcao = new TestClientActiveObject();
  85. UASSERT(caomgr.registerObject(tcao));
  86. u16 id = tcao->getId();
  87. UASSERT(caomgr.getActiveObject(id) != nullptr)
  88. caomgr.removeObject(tcao->getId());
  89. UASSERT(caomgr.getActiveObject(id) == nullptr)
  90. caomgr.clear();
  91. }