Line data Source code
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 1 : class TestNodeResolver : public TestBase {
29 : public:
30 1 : TestNodeResolver() { TestManager::registerTestModule(this); }
31 0 : 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 1 : static TestNodeResolver g_test_instance;
42 :
43 0 : void TestNodeResolver::runTests(IGameDef *gamedef)
44 : {
45 : IWritableNodeDefManager *ndef =
46 0 : (IWritableNodeDefManager *)gamedef->getNodeDefManager();
47 :
48 0 : ndef->resetNodeResolveState();
49 0 : TEST(testNodeResolving, ndef);
50 :
51 0 : ndef->resetNodeResolveState();
52 0 : TEST(testPendingResolveCancellation, ndef);
53 0 : }
54 :
55 0 : 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 0 : 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 0 : void Foobar::resolveNodeNames()
81 : {
82 0 : UASSERT(getIdFromNrBacklog(&test_nr_node1, "", CONTENT_IGNORE) == true);
83 0 : UASSERT(getIdsFromNrBacklog(&test_nr_list) == true);
84 0 : UASSERT(getIdsFromNrBacklog(&test_nr_list_group) == true);
85 0 : UASSERT(getIdsFromNrBacklog(&test_nr_list_required,
86 0 : true, CONTENT_AIR) == false);
87 0 : UASSERT(getIdsFromNrBacklog(&test_nr_list_empty) == true);
88 :
89 0 : UASSERT(getIdFromNrBacklog(&test_nr_node2, "", CONTENT_IGNORE) == true);
90 0 : UASSERT(getIdFromNrBacklog(&test_nr_node3,
91 0 : "default:brick", CONTENT_IGNORE) == true);
92 0 : UASSERT(getIdFromNrBacklog(&test_nr_node4,
93 0 : "default:gobbledygook", CONTENT_AIR) == false);
94 0 : UASSERT(getIdFromNrBacklog(&test_nr_node5, "", CONTENT_IGNORE) == false);
95 0 : }
96 :
97 :
98 0 : void Foobaz::resolveNodeNames()
99 : {
100 0 : UASSERT(getIdFromNrBacklog(&test_content1, "", CONTENT_IGNORE) == true);
101 0 : UASSERT(getIdFromNrBacklog(&test_content2, "", CONTENT_IGNORE) == false);
102 0 : }
103 :
104 :
105 0 : void TestNodeResolver::testNodeResolving(IWritableNodeDefManager *ndef)
106 : {
107 0 : Foobar foobar;
108 : size_t i;
109 :
110 0 : foobar.m_nodenames.push_back("default:torch");
111 :
112 0 : foobar.m_nodenames.push_back("default:dirt_with_grass");
113 0 : foobar.m_nodenames.push_back("default:water");
114 0 : foobar.m_nodenames.push_back("default:abloobloobloo");
115 0 : foobar.m_nodenames.push_back("default:stone");
116 0 : foobar.m_nodenames.push_back("default:shmegoldorf");
117 0 : foobar.m_nnlistsizes.push_back(5);
118 :
119 0 : foobar.m_nodenames.push_back("group:liquids");
120 0 : foobar.m_nnlistsizes.push_back(1);
121 :
122 0 : foobar.m_nodenames.push_back("default:warf");
123 0 : foobar.m_nodenames.push_back("default:stone");
124 0 : foobar.m_nodenames.push_back("default:bloop");
125 0 : foobar.m_nnlistsizes.push_back(3);
126 :
127 0 : foobar.m_nnlistsizes.push_back(0);
128 :
129 0 : foobar.m_nodenames.push_back("default:brick");
130 0 : foobar.m_nodenames.push_back("default:desert_stone");
131 0 : foobar.m_nodenames.push_back("default:shnitzle");
132 :
133 0 : ndef->pendNodeResolve(&foobar);
134 0 : UASSERT(foobar.m_ndef == ndef);
135 :
136 0 : ndef->setNodeRegistrationStatus(true);
137 0 : ndef->runNodeResolveCallbacks();
138 :
139 : // Check that we read single nodes successfully
140 0 : UASSERTEQ(content_t, foobar.test_nr_node1, t_CONTENT_TORCH);
141 0 : UASSERTEQ(content_t, foobar.test_nr_node2, t_CONTENT_BRICK);
142 0 : UASSERTEQ(content_t, foobar.test_nr_node3, t_CONTENT_BRICK);
143 0 : UASSERTEQ(content_t, foobar.test_nr_node4, CONTENT_AIR);
144 0 : 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 0 : };
152 0 : UASSERTEQ(size_t, foobar.test_nr_list.size(), 3);
153 0 : for (i = 0; i != foobar.test_nr_list.size(); i++)
154 0 : 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 0 : };
161 0 : UASSERTEQ(size_t, foobar.test_nr_list_group.size(), 2);
162 0 : for (i = 0; i != foobar.test_nr_list_group.size(); i++) {
163 0 : UASSERT(CONTAINS(foobar.test_nr_list_group,
164 0 : 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 0 : };
173 0 : UASSERTEQ(size_t, foobar.test_nr_list_required.size(), 3);
174 0 : for (i = 0; i != foobar.test_nr_list_required.size(); i++)
175 0 : UASSERTEQ(content_t, foobar.test_nr_list_required[i],
176 0 : expected_test_nr_list_required[i]);
177 :
178 : // Check that the edge case of 0 is successful
179 0 : UASSERTEQ(size_t, foobar.test_nr_list_empty.size(), 0);
180 0 : }
181 :
182 :
183 0 : void TestNodeResolver::testPendingResolveCancellation(IWritableNodeDefManager *ndef)
184 : {
185 0 : Foobaz foobaz1;
186 0 : foobaz1.test_content1 = 1234;
187 0 : foobaz1.test_content2 = 5678;
188 0 : foobaz1.m_nodenames.push_back("default:dirt_with_grass");
189 0 : foobaz1.m_nodenames.push_back("default:abloobloobloo");
190 0 : ndef->pendNodeResolve(&foobaz1);
191 :
192 0 : Foobaz foobaz2;
193 0 : foobaz2.test_content1 = 1234;
194 0 : foobaz2.test_content2 = 5678;
195 0 : foobaz2.m_nodenames.push_back("default:dirt_with_grass");
196 0 : foobaz2.m_nodenames.push_back("default:abloobloobloo");
197 0 : ndef->pendNodeResolve(&foobaz2);
198 :
199 0 : ndef->cancelNodeResolveCallback(&foobaz1);
200 :
201 0 : ndef->setNodeRegistrationStatus(true);
202 0 : ndef->runNodeResolveCallbacks();
203 :
204 0 : UASSERT(foobaz1.test_content1 == 1234);
205 0 : UASSERT(foobaz1.test_content2 == 5678);
206 0 : UASSERT(foobaz2.test_content1 == t_CONTENT_GRASS);
207 0 : UASSERT(foobaz2.test_content2 == CONTENT_IGNORE);
208 3 : }
|