]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_noderesolver.cpp
test_map_settings_manager: Fix Wunused-result warning
[dragonfireclient.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
55 class Foobar : public NodeResolver {
56 public:
57         void resolveNodeNames();
58
59         content_t test_nr_node1;
60         content_t test_nr_node2;
61         content_t test_nr_node3;
62         content_t test_nr_node4;
63         content_t test_nr_node5;
64         std::vector<content_t> test_nr_list;
65         std::vector<content_t> test_nr_list_group;
66         std::vector<content_t> test_nr_list_required;
67         std::vector<content_t> test_nr_list_empty;
68 };
69
70 class Foobaz : public NodeResolver {
71 public:
72         void resolveNodeNames();
73
74         content_t test_content1;
75         content_t test_content2;
76 };
77
78 ////////////////////////////////////////////////////////////////////////////////
79
80 void Foobar::resolveNodeNames()
81 {
82         UASSERT(getIdFromNrBacklog(&test_nr_node1, "", CONTENT_IGNORE) == true);
83         UASSERT(getIdsFromNrBacklog(&test_nr_list) == true);
84         UASSERT(getIdsFromNrBacklog(&test_nr_list_group) == true);
85         UASSERT(getIdsFromNrBacklog(&test_nr_list_required,
86                 true, CONTENT_AIR) == false);
87         UASSERT(getIdsFromNrBacklog(&test_nr_list_empty) == true);
88
89         UASSERT(getIdFromNrBacklog(&test_nr_node2, "", CONTENT_IGNORE) == true);
90         UASSERT(getIdFromNrBacklog(&test_nr_node3,
91                 "default:brick", CONTENT_IGNORE) == true);
92         UASSERT(getIdFromNrBacklog(&test_nr_node4,
93                 "default:gobbledygook", CONTENT_AIR) == false);
94         UASSERT(getIdFromNrBacklog(&test_nr_node5, "", CONTENT_IGNORE) == false);
95 }
96
97
98 void Foobaz::resolveNodeNames()
99 {
100         UASSERT(getIdFromNrBacklog(&test_content1, "", CONTENT_IGNORE) == true);
101         UASSERT(getIdFromNrBacklog(&test_content2, "", CONTENT_IGNORE) == false);
102 }
103
104
105 void TestNodeResolver::testNodeResolving(IWritableNodeDefManager *ndef)
106 {
107         Foobar foobar;
108         size_t i;
109
110         foobar.m_nodenames.push_back("default:torch");
111
112         foobar.m_nodenames.push_back("default:dirt_with_grass");
113         foobar.m_nodenames.push_back("default:water");
114         foobar.m_nodenames.push_back("default:abloobloobloo");
115         foobar.m_nodenames.push_back("default:stone");
116         foobar.m_nodenames.push_back("default:shmegoldorf");
117         foobar.m_nnlistsizes.push_back(5);
118
119         foobar.m_nodenames.push_back("group:liquids");
120         foobar.m_nnlistsizes.push_back(1);
121
122         foobar.m_nodenames.push_back("default:warf");
123         foobar.m_nodenames.push_back("default:stone");
124         foobar.m_nodenames.push_back("default:bloop");
125         foobar.m_nnlistsizes.push_back(3);
126
127         foobar.m_nnlistsizes.push_back(0);
128
129         foobar.m_nodenames.push_back("default:brick");
130         foobar.m_nodenames.push_back("default:desert_stone");
131         foobar.m_nodenames.push_back("default:shnitzle");
132
133         ndef->pendNodeResolve(&foobar);
134         UASSERT(foobar.m_ndef == ndef);
135
136         ndef->setNodeRegistrationStatus(true);
137         ndef->runNodeResolveCallbacks();
138
139         // Check that we read single nodes successfully
140         UASSERTEQ(content_t, foobar.test_nr_node1, t_CONTENT_TORCH);
141         UASSERTEQ(content_t, foobar.test_nr_node2, t_CONTENT_BRICK);
142         UASSERTEQ(content_t, foobar.test_nr_node3, t_CONTENT_BRICK);
143         UASSERTEQ(content_t, foobar.test_nr_node4, CONTENT_AIR);
144         UASSERTEQ(content_t, foobar.test_nr_node5, CONTENT_IGNORE);
145
146         // Check that we read all the regular list items
147         static const content_t expected_test_nr_list[] = {
148                 t_CONTENT_GRASS,
149                 t_CONTENT_WATER,
150                 t_CONTENT_STONE,
151         };
152         UASSERTEQ(size_t, foobar.test_nr_list.size(), 3);
153         for (i = 0; i != foobar.test_nr_list.size(); i++)
154                 UASSERTEQ(content_t, foobar.test_nr_list[i], expected_test_nr_list[i]);
155
156         // Check that we read all the list items that were from a group entry
157         static const content_t expected_test_nr_list_group[] = {
158                 t_CONTENT_WATER,
159                 t_CONTENT_LAVA,
160         };
161         UASSERTEQ(size_t, foobar.test_nr_list_group.size(), 2);
162         for (i = 0; i != foobar.test_nr_list_group.size(); i++) {
163                 UASSERT(CONTAINS(foobar.test_nr_list_group,
164                         expected_test_nr_list_group[i]));
165         }
166
167         // Check that we read all the items we're able to in a required list
168         static const content_t expected_test_nr_list_required[] = {
169                 CONTENT_AIR,
170                 t_CONTENT_STONE,
171                 CONTENT_AIR,
172         };
173         UASSERTEQ(size_t, foobar.test_nr_list_required.size(), 3);
174         for (i = 0; i != foobar.test_nr_list_required.size(); i++)
175                 UASSERTEQ(content_t, foobar.test_nr_list_required[i],
176                         expected_test_nr_list_required[i]);
177
178         // Check that the edge case of 0 is successful
179         UASSERTEQ(size_t, foobar.test_nr_list_empty.size(), 0);
180 }
181
182
183 void TestNodeResolver::testPendingResolveCancellation(IWritableNodeDefManager *ndef)
184 {
185         Foobaz foobaz1;
186         foobaz1.test_content1 = 1234;
187         foobaz1.test_content2 = 5678;
188         foobaz1.m_nodenames.push_back("default:dirt_with_grass");
189         foobaz1.m_nodenames.push_back("default:abloobloobloo");
190         ndef->pendNodeResolve(&foobaz1);
191
192         Foobaz foobaz2;
193         foobaz2.test_content1 = 1234;
194         foobaz2.test_content2 = 5678;
195         foobaz2.m_nodenames.push_back("default:dirt_with_grass");
196         foobaz2.m_nodenames.push_back("default:abloobloobloo");
197         ndef->pendNodeResolve(&foobaz2);
198
199         ndef->cancelNodeResolveCallback(&foobaz1);
200
201         ndef->setNodeRegistrationStatus(true);
202         ndef->runNodeResolveCallbacks();
203
204         UASSERT(foobaz1.test_content1 == 1234);
205         UASSERT(foobaz1.test_content2 == 5678);
206         UASSERT(foobaz2.test_content1 == t_CONTENT_GRASS);
207         UASSERT(foobaz2.test_content2 == CONTENT_IGNORE);
208 }