3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
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.
22 #include "util/numeric.h"
23 #include "exceptions.h"
28 class TestNodeResolver : public TestBase {
30 TestNodeResolver() { TestManager::registerTestModule(this); }
31 const char *getName() { return "TestNodeResolver"; }
33 void runTests(IGameDef *gamedef);
35 void testNodeResolving(IWritableNodeDefManager *ndef);
36 void testPendingResolveCancellation(IWritableNodeDefManager *ndef);
37 void testDirectResolveMethod(IWritableNodeDefManager *ndef);
38 void testNoneResolveMethod(IWritableNodeDefManager *ndef);
41 static TestNodeResolver g_test_instance;
43 void TestNodeResolver::runTests(IGameDef *gamedef)
45 IWritableNodeDefManager *ndef =
46 (IWritableNodeDefManager *)gamedef->getNodeDefManager();
48 ndef->resetNodeResolveState();
49 TEST(testNodeResolving, ndef);
51 ndef->resetNodeResolveState();
52 TEST(testPendingResolveCancellation, ndef);
55 class Foobar : public NodeResolver {
57 void resolveNodeNames();
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;
70 class Foobaz : public NodeResolver {
72 void resolveNodeNames();
74 content_t test_content1;
75 content_t test_content2;
78 ////////////////////////////////////////////////////////////////////////////////
80 void Foobar::resolveNodeNames()
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);
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);
98 void Foobaz::resolveNodeNames()
100 UASSERT(getIdFromNrBacklog(&test_content1, "", CONTENT_IGNORE) == true);
101 UASSERT(getIdFromNrBacklog(&test_content2, "", CONTENT_IGNORE) == false);
105 void TestNodeResolver::testNodeResolving(IWritableNodeDefManager *ndef)
110 foobar.m_nodenames.push_back("default:torch");
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);
119 foobar.m_nodenames.push_back("group:liquids");
120 foobar.m_nnlistsizes.push_back(1);
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);
127 foobar.m_nnlistsizes.push_back(0);
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");
133 ndef->pendNodeResolve(&foobar);
134 UASSERT(foobar.m_ndef == ndef);
136 ndef->setNodeRegistrationStatus(true);
137 ndef->runNodeResolveCallbacks();
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);
146 // Check that we read all the regular list items
147 static const content_t expected_test_nr_list[] = {
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]);
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[] = {
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]));
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[] = {
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]);
178 // Check that the edge case of 0 is successful
179 UASSERTEQ(size_t, foobar.test_nr_list_empty.size(), 0);
183 void TestNodeResolver::testPendingResolveCancellation(IWritableNodeDefManager *ndef)
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);
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);
199 ndef->cancelNodeResolveCallback(&foobaz1);
201 ndef->setNodeRegistrationStatus(true);
202 ndef->runNodeResolveCallbacks();
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);