]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_voxelmanipulator.cpp
Disable Prometheus in singleplayer mode
[minetest.git] / src / unittest / test_voxelmanipulator.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 <algorithm>
23
24 #include "gamedef.h"
25 #include "log.h"
26 #include "voxel.h"
27
28 class TestVoxelManipulator : public TestBase {
29 public:
30         TestVoxelManipulator() { TestManager::registerTestModule(this); }
31         const char *getName() { return "TestVoxelManipulator"; }
32
33         void runTests(IGameDef *gamedef);
34
35         void testVoxelArea();
36         void testVoxelManipulator(const NodeDefManager *nodedef);
37 };
38
39 static TestVoxelManipulator g_test_instance;
40
41 void TestVoxelManipulator::runTests(IGameDef *gamedef)
42 {
43         TEST(testVoxelArea);
44         TEST(testVoxelManipulator, gamedef->getNodeDefManager());
45 }
46
47 ////////////////////////////////////////////////////////////////////////////////
48
49 void TestVoxelManipulator::testVoxelArea()
50 {
51         VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
52         UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
53         UASSERT(a.index(-1,-1,-1) == 0);
54
55         VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
56         // An area that is 1 bigger in x+ and z-
57         VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
58
59         std::list<VoxelArea> aa;
60         d.diff(c, aa);
61
62         // Correct results
63         std::vector<VoxelArea> results;
64         results.emplace_back(v3s16(-2,-2,-3), v3s16(3,2,-3));
65         results.emplace_back(v3s16(3,-2,-2), v3s16(3,2,2));
66
67         UASSERT(aa.size() == results.size());
68
69         infostream<<"Result of diff:"<<std::endl;
70         for (std::list<VoxelArea>::const_iterator
71                         it = aa.begin(); it != aa.end(); ++it) {
72                 it->print(infostream);
73                 infostream << std::endl;
74
75                 std::vector<VoxelArea>::iterator j;
76                 j = std::find(results.begin(), results.end(), *it);
77                 UASSERT(j != results.end());
78                 results.erase(j);
79         }
80 }
81
82
83 void TestVoxelManipulator::testVoxelManipulator(const NodeDefManager *nodedef)
84 {
85         VoxelManipulator v;
86
87         v.print(infostream, nodedef);
88
89         infostream << "*** Setting (-1,0,-1)=2 ***" << std::endl;
90         v.setNodeNoRef(v3s16(-1,0,-1), MapNode(t_CONTENT_GRASS));
91
92         v.print(infostream, nodedef);
93         UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == t_CONTENT_GRASS);
94
95         infostream << "*** Reading from inexistent (0,0,-1) ***" << std::endl;
96
97         EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
98         v.print(infostream, nodedef);
99
100         infostream << "*** Adding area ***" << std::endl;
101
102         VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
103         v.addArea(a);
104         v.print(infostream, nodedef);
105
106         UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == t_CONTENT_GRASS);
107         EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
108 }