Line data Source code
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 1 : class TestVoxelManipulator : public TestBase {
29 : public:
30 1 : TestVoxelManipulator() { TestManager::registerTestModule(this); }
31 0 : const char *getName() { return "TestVoxelManipulator"; }
32 :
33 : void runTests(IGameDef *gamedef);
34 :
35 : void testVoxelArea();
36 : void testVoxelManipulator(INodeDefManager *nodedef);
37 : };
38 :
39 1 : static TestVoxelManipulator g_test_instance;
40 :
41 0 : void TestVoxelManipulator::runTests(IGameDef *gamedef)
42 : {
43 0 : TEST(testVoxelArea);
44 0 : TEST(testVoxelManipulator, gamedef->getNodeDefManager());
45 0 : }
46 :
47 : ////////////////////////////////////////////////////////////////////////////////
48 :
49 0 : void TestVoxelManipulator::testVoxelArea()
50 : {
51 0 : VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
52 0 : UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
53 0 : UASSERT(a.index(-1,-1,-1) == 0);
54 :
55 0 : VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
56 : // An area that is 1 bigger in x+ and z-
57 0 : VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
58 :
59 0 : std::list<VoxelArea> aa;
60 0 : d.diff(c, aa);
61 :
62 : // Correct results
63 0 : std::vector<VoxelArea> results;
64 0 : results.push_back(VoxelArea(v3s16(-2,-2,-3), v3s16(3,2,-3)));
65 0 : results.push_back(VoxelArea(v3s16(3,-2,-2), v3s16(3,2,2)));
66 :
67 0 : UASSERT(aa.size() == results.size());
68 :
69 0 : infostream<<"Result of diff:"<<std::endl;
70 0 : for (std::list<VoxelArea>::const_iterator
71 0 : it = aa.begin(); it != aa.end(); ++it) {
72 0 : it->print(infostream);
73 0 : infostream << std::endl;
74 :
75 0 : std::vector<VoxelArea>::iterator j;
76 0 : j = std::find(results.begin(), results.end(), *it);
77 0 : UASSERT(j != results.end());
78 0 : results.erase(j);
79 : }
80 0 : }
81 :
82 :
83 0 : void TestVoxelManipulator::testVoxelManipulator(INodeDefManager *nodedef)
84 : {
85 0 : VoxelManipulator v;
86 :
87 0 : v.print(infostream, nodedef);
88 :
89 0 : infostream << "*** Setting (-1,0,-1)=2 ***" << std::endl;
90 0 : v.setNodeNoRef(v3s16(-1,0,-1), MapNode(t_CONTENT_GRASS));
91 :
92 0 : v.print(infostream, nodedef);
93 0 : UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == t_CONTENT_GRASS);
94 :
95 0 : infostream << "*** Reading from inexistent (0,0,-1) ***" << std::endl;
96 :
97 0 : EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
98 0 : v.print(infostream, nodedef);
99 :
100 0 : infostream << "*** Adding area ***" << std::endl;
101 :
102 0 : VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
103 0 : v.addArea(a);
104 0 : v.print(infostream, nodedef);
105 :
106 0 : UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == t_CONTENT_GRASS);
107 0 : EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
108 3 : }
|