]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_noderesolver.cpp
Fix build on Debian broken by b45df9d (missing include, somehow?)
[minetest.git] / src / unittest / test_noderesolver.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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 "util/numeric.h"
23 #include "exceptions.h"
24 #include "gamedef.h"
25 #include "nodedef.h"
26
27
28 class TestNodeResolver : public TestBase {
29 public:
30         TestNodeResolver() { TestManager::registerTestModule(this); }
31         const char *getName() { return "TestNodeResolver"; }
32
33         void runTests(IGameDef *gamedef);
34
35         void testNodeResolving(IWritableNodeDefManager *ndef);
36         void testPendingResolveCancellation(IWritableNodeDefManager *ndef);
37         void testDirectResolveMethod(IWritableNodeDefManager *ndef);
38         void testNoneResolveMethod(IWritableNodeDefManager *ndef);
39 };
40
41 static TestNodeResolver g_test_instance;
42
43 void TestNodeResolver::runTests(IGameDef *gamedef)
44 {
45         IWritableNodeDefManager *ndef =
46                 (IWritableNodeDefManager *)gamedef->getNodeDefManager();
47
48         ndef->resetNodeResolveState();
49         TEST(testNodeResolving, ndef);
50
51         ndef->resetNodeResolveState();
52         TEST(testPendingResolveCancellation, ndef);
53
54         ndef->resetNodeResolveState();
55         TEST(testDirectResolveMethod, ndef);
56
57         ndef->resetNodeResolveState();
58         TEST(testNoneResolveMethod, ndef);
59 }
60
61 class Foobar : public NodeResolver {
62 public:
63         void resolveNodeNames();
64
65         content_t test_nr_node1;
66         content_t test_nr_node2;
67         content_t test_nr_node3;
68         content_t test_nr_node4;
69         content_t test_nr_node5;
70         std::vector<content_t> test_nr_list;
71         std::vector<content_t> test_nr_list_group;
72         std::vector<content_t> test_nr_list_required;
73         std::vector<content_t> test_nr_list_empty;
74 };
75
76 class Foobaz : public NodeResolver {
77 public:
78         void resolveNodeNames();
79
80         content_t test_content1;
81         content_t test_content2;
82 };
83
84 ////////////////////////////////////////////////////////////////////////////////
85
86 void Foobar::resolveNodeNames()
87 {
88         UASSERT(getIdFromNrBacklog(&test_nr_node1, "", CONTENT_IGNORE) == true);
89         UASSERT(getIdsFromNrBacklog(&test_nr_list) == true);
90         UASSERT(getIdsFromNrBacklog(&test_nr_list_group) == true);
91         UASSERT(getIdsFromNrBacklog(&test_nr_list_required,
92                 true, CONTENT_AIR) == false);
93         UASSERT(getIdsFromNrBacklog(&test_nr_list_empty) == true);
94
95         UASSERT(getIdFromNrBacklog(&test_nr_node2, "", CONTENT_IGNORE) == true);
96         UASSERT(getIdFromNrBacklog(&test_nr_node3,
97                 "default:brick", CONTENT_IGNORE) == true);
98         UASSERT(getIdFromNrBacklog(&test_nr_node4,
99                 "default:gobbledygook", CONTENT_AIR) == false);
100         UASSERT(getIdFromNrBacklog(&test_nr_node5, "", CONTENT_IGNORE) == false);
101 }
102
103
104 void Foobaz::resolveNodeNames()
105 {
106         UASSERT(getIdFromNrBacklog(&test_content1, "", CONTENT_IGNORE) == true);
107         UASSERT(getIdFromNrBacklog(&test_content2, "", CONTENT_IGNORE) == false);
108 }
109
110
111 void TestNodeResolver::testNodeResolving(IWritableNodeDefManager *ndef)
112 {
113         Foobar foobar;
114         size_t i;
115
116         foobar.m_nodenames.push_back("default:torch");
117
118         foobar.m_nodenames.push_back("default:dirt_with_grass");
119         foobar.m_nodenames.push_back("default:water");
120         foobar.m_nodenames.push_back("default:abloobloobloo");
121         foobar.m_nodenames.push_back("default:stone");
122         foobar.m_nodenames.push_back("default:shmegoldorf");
123         foobar.m_nnlistsizes.push_back(5);
124
125         foobar.m_nodenames.push_back("group:liquids");
126         foobar.m_nnlistsizes.push_back(1);
127
128         foobar.m_nodenames.push_back("default:warf");
129         foobar.m_nodenames.push_back("default:stone");
130         foobar.m_nodenames.push_back("default:bloop");
131         foobar.m_nnlistsizes.push_back(3);
132
133         foobar.m_nnlistsizes.push_back(0);
134
135         foobar.m_nodenames.push_back("default:brick");
136         foobar.m_nodenames.push_back("default:desert_stone");
137         foobar.m_nodenames.push_back("default:shnitzle");
138
139         ndef->pendNodeResolve(&foobar, NODE_RESOLVE_DEFERRED);
140         UASSERT(foobar.m_ndef == ndef);
141
142         ndef->setNodeRegistrationStatus(true);
143         ndef->runNodeResolveCallbacks();
144
145         // Check that we read single nodes successfully
146         UASSERTEQ(content_t, foobar.test_nr_node1, t_CONTENT_TORCH);
147         UASSERTEQ(content_t, foobar.test_nr_node2, t_CONTENT_BRICK);
148         UASSERTEQ(content_t, foobar.test_nr_node3, t_CONTENT_BRICK);
149         UASSERTEQ(content_t, foobar.test_nr_node4, CONTENT_AIR);
150         UASSERTEQ(content_t, foobar.test_nr_node5, CONTENT_IGNORE);
151
152         // Check that we read all the regular list items
153         static const content_t expected_test_nr_list[] = {
154                 t_CONTENT_GRASS,
155                 t_CONTENT_WATER,
156                 t_CONTENT_STONE,
157         };
158         UASSERTEQ(size_t, foobar.test_nr_list.size(), 3);
159         for (i = 0; i != foobar.test_nr_list.size(); i++)
160                 UASSERTEQ(content_t, foobar.test_nr_list[i], expected_test_nr_list[i]);
161
162         // Check that we read all the list items that were from a group entry
163         static const content_t expected_test_nr_list_group[] = {
164                 t_CONTENT_WATER,
165                 t_CONTENT_LAVA,
166         };
167         UASSERTEQ(size_t, foobar.test_nr_list_group.size(), 2);
168         for (i = 0; i != foobar.test_nr_list_group.size(); i++) {
169                 UASSERT(CONTAINS(foobar.test_nr_list_group,
170                         expected_test_nr_list_group[i]));
171         }
172
173         // Check that we read all the items we're able to in a required list
174         static const content_t expected_test_nr_list_required[] = {
175                 CONTENT_AIR,
176                 t_CONTENT_STONE,
177                 CONTENT_AIR,
178         };
179         UASSERTEQ(size_t, foobar.test_nr_list_required.size(), 3);
180         for (i = 0; i != foobar.test_nr_list_required.size(); i++)
181                 UASSERTEQ(content_t, foobar.test_nr_list_required[i],
182                         expected_test_nr_list_required[i]);
183
184         // Check that the edge case of 0 is successful
185         UASSERTEQ(size_t, foobar.test_nr_list_empty.size(), 0);
186 }
187
188
189 void TestNodeResolver::testPendingResolveCancellation(IWritableNodeDefManager *ndef)
190 {
191         Foobaz foobaz1;
192         foobaz1.test_content1 = 1234;
193         foobaz1.test_content2 = 5678;
194         foobaz1.m_nodenames.push_back("default:dirt_with_grass");
195         foobaz1.m_nodenames.push_back("default:abloobloobloo");
196         ndef->pendNodeResolve(&foobaz1, NODE_RESOLVE_DEFERRED);
197
198         Foobaz foobaz2;
199         foobaz2.test_content1 = 1234;
200         foobaz2.test_content2 = 5678;
201         foobaz2.m_nodenames.push_back("default:dirt_with_grass");
202         foobaz2.m_nodenames.push_back("default:abloobloobloo");
203         ndef->pendNodeResolve(&foobaz2, NODE_RESOLVE_DEFERRED);
204
205         ndef->cancelNodeResolveCallback(&foobaz1);
206
207         ndef->setNodeRegistrationStatus(true);
208         ndef->runNodeResolveCallbacks();
209
210         UASSERT(foobaz1.test_content1 == 1234);
211         UASSERT(foobaz1.test_content2 == 5678);
212         UASSERT(foobaz2.test_content1 == t_CONTENT_GRASS);
213         UASSERT(foobaz2.test_content2 == CONTENT_IGNORE);
214 }
215
216
217 void TestNodeResolver::testDirectResolveMethod(IWritableNodeDefManager *ndef)
218 {
219         Foobaz foobaz;
220
221         foobaz.m_nodenames.push_back("default:dirt_with_grass");
222         foobaz.m_nodenames.push_back("default:abloobloobloo");
223
224         UASSERTEQ(std::string, foobaz.getNodeName(1), "default:abloobloobloo");
225
226         ndef->pendNodeResolve(&foobaz, NODE_RESOLVE_DIRECT);
227
228         UASSERTEQ(content_t, foobaz.test_content1, t_CONTENT_GRASS);
229         UASSERTEQ(content_t, foobaz.test_content2, CONTENT_IGNORE);
230
231         // We expect this to be *different* because the resolution of this node had
232         // failed.  The internal nodename buffer is cleared and lookups should now
233         // use the nodedef manager.
234         UASSERT(foobaz.getNodeName(1) != "default:abloobloobloo");
235 }
236
237
238 void TestNodeResolver::testNoneResolveMethod(IWritableNodeDefManager *ndef)
239 {
240         Foobaz foobaz;
241
242         foobaz.m_nodenames.push_back("default:dirt_with_grass");
243         foobaz.m_nodenames.push_back("default:abloobloobloo");
244
245         ndef->pendNodeResolve(&foobaz, NODE_RESOLVE_NONE);
246
247         UASSERTEQ(std::string, foobaz.getNodeName(1), "default:abloobloobloo");
248 }