test_player.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2016 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 "test.h"
  17. #include "exceptions.h"
  18. #include "remoteplayer.h"
  19. #include "content_sao.h"
  20. #include "server.h"
  21. class TestPlayer : public TestBase {
  22. public:
  23. TestPlayer() { TestManager::registerTestModule(this); }
  24. const char *getName() { return "TestPlayer"; }
  25. void runTests(IGameDef *gamedef);
  26. void testSave(IGameDef *gamedef);
  27. void testLoad(IGameDef *gamedef);
  28. };
  29. static TestPlayer g_test_instance;
  30. void TestPlayer::runTests(IGameDef *gamedef)
  31. {
  32. TEST(testSave, gamedef);
  33. TEST(testLoad, gamedef);
  34. }
  35. void TestPlayer::testSave(IGameDef *gamedef)
  36. {
  37. RemotePlayer rplayer("testplayer_save", gamedef->idef());
  38. PlayerSAO sao(NULL, 1, false);
  39. sao.initialize(&rplayer, std::set<std::string>());
  40. rplayer.setPlayerSAO(&sao);
  41. sao.setBreath(10);
  42. sao.setHP(8, true);
  43. sao.setYaw(0.1f, false);
  44. sao.setPitch(0.6f, false);
  45. sao.setBasePosition(v3f(450.2f, -15.7f, 68.1f));
  46. rplayer.save(".", gamedef);
  47. UASSERT(fs::PathExists("testplayer_save"));
  48. }
  49. void TestPlayer::testLoad(IGameDef *gamedef)
  50. {
  51. RemotePlayer rplayer("testplayer_load", gamedef->idef());
  52. PlayerSAO sao(NULL, 1, false);
  53. sao.initialize(&rplayer, std::set<std::string>());
  54. rplayer.setPlayerSAO(&sao);
  55. sao.setBreath(10);
  56. sao.setHP(8, true);
  57. sao.setYaw(0.1f, false);
  58. sao.setPitch(0.6f, false);
  59. sao.setBasePosition(v3f(450.2f, -15.7f, 68.1f));
  60. rplayer.save(".", gamedef);
  61. UASSERT(fs::PathExists("testplayer_load"));
  62. RemotePlayer rplayer_load("testplayer_load", gamedef->idef());
  63. PlayerSAO sao_load(NULL, 2, false);
  64. std::ifstream is("testplayer_load", std::ios_base::binary);
  65. UASSERT(is.good());
  66. rplayer_load.deSerialize(is, "testplayer_load", &sao_load);
  67. is.close();
  68. UASSERT(strcmp(rplayer_load.getName(), "testplayer_load") == 0);
  69. UASSERT(sao_load.getBreath() == 10);
  70. UASSERT(sao_load.getHP() == 8);
  71. UASSERT(sao_load.getYaw() == 0.1f);
  72. UASSERT(sao_load.getPitch() == 0.6f);
  73. UASSERT(sao_load.getBasePosition() == v3f(450.2f, -15.7f, 68.1f));
  74. }