]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_player.cpp
Dungeongen: Fix out-of-voxelmanip access segfault
[minetest.git] / src / unittest / test_player.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2016 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "test.h"
21
22 #include "exceptions.h"
23 #include "remoteplayer.h"
24 #include "content_sao.h"
25 #include "server.h"
26
27 class TestPlayer : public TestBase {
28 public:
29         TestPlayer() { TestManager::registerTestModule(this); }
30         const char *getName() { return "TestPlayer"; }
31
32         void runTests(IGameDef *gamedef);
33
34         void testSave(IGameDef *gamedef);
35         void testLoad(IGameDef *gamedef);
36 };
37
38 static TestPlayer g_test_instance;
39
40 void TestPlayer::runTests(IGameDef *gamedef)
41 {
42         TEST(testSave, gamedef);
43         TEST(testLoad, gamedef);
44 }
45
46 void TestPlayer::testSave(IGameDef *gamedef)
47 {
48         RemotePlayer rplayer("testplayer_save", gamedef->idef());
49         PlayerSAO sao(NULL, 1, false);
50         sao.initialize(&rplayer, std::set<std::string>());
51         rplayer.setPlayerSAO(&sao);
52         sao.setBreath(10, false);
53         sao.setHPRaw(8);
54         sao.setYaw(0.1f);
55         sao.setPitch(0.6f);
56         sao.setBasePosition(v3f(450.2f, -15.7f, 68.1f));
57         rplayer.save(".", gamedef);
58         UASSERT(fs::PathExists("testplayer_save"));
59 }
60
61 void TestPlayer::testLoad(IGameDef *gamedef)
62 {
63         RemotePlayer rplayer("testplayer_load", gamedef->idef());
64         PlayerSAO sao(NULL, 1, false);
65         sao.initialize(&rplayer, std::set<std::string>());
66         rplayer.setPlayerSAO(&sao);
67         sao.setBreath(10, false);
68         sao.setHPRaw(8);
69         sao.setYaw(0.1f);
70         sao.setPitch(0.6f);
71         sao.setBasePosition(v3f(450.2f, -15.7f, 68.1f));
72         rplayer.save(".", gamedef);
73         UASSERT(fs::PathExists("testplayer_load"));
74
75         RemotePlayer rplayer_load("testplayer_load", gamedef->idef());
76         PlayerSAO sao_load(NULL, 2, false);
77         std::ifstream is("testplayer_load", std::ios_base::binary);
78         UASSERT(is.good());
79         rplayer_load.deSerialize(is, "testplayer_load", &sao_load);
80         is.close();
81
82         UASSERT(strcmp(rplayer_load.getName(), "testplayer_load") == 0);
83         UASSERT(sao_load.getBreath() == 10);
84         UASSERT(sao_load.getHP() == 8);
85         UASSERT(sao_load.getYaw() == 0.1f);
86         UASSERT(sao_load.getPitch() == 0.6f);
87         UASSERT(sao_load.getBasePosition() == v3f(450.2f, -15.7f, 68.1f));
88 }