aboutsummaryrefslogtreecommitdiff
path: root/src/unittest/test_voxelmanipulator.cpp
blob: acc2707e765b06d42ec45454695b60da1a8c99dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "test.h"

#include <algorithm>

#include "gamedef.h"
#include "log.h"
#include "voxel.h"

class TestVoxelManipulator : public TestBase {
public:
	TestVoxelManipulator() { TestManager::registerTestModule(this); }
	const char *getName() { return "TestVoxelManipulator"; }

	void runTests(IGameDef *gamedef);

	void testVoxelArea();
	void testVoxelManipulator(const NodeDefManager *nodedef);
};

static TestVoxelManipulator g_test_instance;

void TestVoxelManipulator::runTests(IGameDef *gamedef)
{
	TEST(testVoxelArea);
	TEST(testVoxelManipulator, gamedef->getNodeDefManager());
}

////////////////////////////////////////////////////////////////////////////////

void TestVoxelManipulator::testVoxelArea()
{
	VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
	UASSERT(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
	UASSERT(a.index(-1,-1,-1) == 0);

	VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
	// An area that is 1 bigger in x+ and z-
	VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));

	std::list<VoxelArea> aa;
	d.diff(c, aa);

	// Correct results
	std::vector<VoxelArea> results;
	results.emplace_back(v3s16(-2,-2,-3), v3s16(3,2,-3));
	results.emplace_back(v3s16(3,-2,-2), v3s16(3,2,2));

	UASSERT(aa.size() == results.size());

	infostream<<"Result of diff:"<<std::endl;
	for (std::list<VoxelArea>::const_iterator
			it = aa.begin(); it != aa.end(); ++it) {
		it->print(infostream);
		infostream << std::endl;

		std::vector<VoxelArea>::iterator j;
		j = std::find(results.begin(), results.end(), *it);
		UASSERT(j != results.end());
		results.erase(j);
	}
}


void TestVoxelManipulator::testVoxelManipulator(const NodeDefManager *nodedef)
{
	VoxelManipulator v;

	v.print(infostream, nodedef);

	infostream << "*** Setting (-1,0,-1)=2 ***" << std::endl;
	v.setNodeNoRef(v3s16(-1,0,-1), MapNode(t_CONTENT_GRASS));

	v.print(infostream, nodedef);
	UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == t_CONTENT_GRASS);

	infostream << "*** Reading from inexistent (0,0,-1) ***" << std::endl;

	EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
	v.print(infostream, nodedef);

	infostream << "*** Adding area ***" << std::endl;

	VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
	v.addArea(a);
	v.print(infostream, nodedef);

	UASSERT(v.getNode(v3s16(-1,0,-1)).getContent() == t_CONTENT_GRASS);
	EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
}
n>(IGameDef *gamedef) { // fixed directory, for persistence thread_local const std::string test_dir = getTestTempDirectory(); // Each set of tests is run twice for each database type: // one where we reuse the same AuthDatabase object (to test local caching), // and one where we create a new AuthDatabase object for each call // (to test actual persistence). rawstream << "-------- Files database (same object)" << std::endl; AuthDatabase *auth_db = new AuthDatabaseFiles(test_dir); auth_provider = new FixedProvider(auth_db); runTestsForCurrentDB(); delete auth_db; delete auth_provider; // reset database fs::DeleteSingleFileOrEmptyDirectory(test_dir + DIR_DELIM + "auth.txt"); rawstream << "-------- Files database (new objects)" << std::endl; auth_provider = new FilesProvider(test_dir); runTestsForCurrentDB(); delete auth_provider; rawstream << "-------- SQLite3 database (same object)" << std::endl; auth_db = new AuthDatabaseSQLite3(test_dir); auth_provider = new FixedProvider(auth_db); runTestsForCurrentDB(); delete auth_db; delete auth_provider; // reset database fs::DeleteSingleFileOrEmptyDirectory(test_dir + DIR_DELIM + "auth.sqlite"); rawstream << "-------- SQLite3 database (new objects)" << std::endl; auth_provider = new SQLite3Provider(test_dir); runTestsForCurrentDB(); delete auth_provider; } //////////////////////////////////////////////////////////////////////////////// void TestAuthDatabase::runTestsForCurrentDB() { TEST(testRecallFail); TEST(testCreate); TEST(testRecall); TEST(testChange); TEST(testRecallChanged); TEST(testChangePrivileges); TEST(testRecallChangedPrivileges); TEST(testListNames); TEST(testDelete); TEST(testRecallFail); } void TestAuthDatabase::testRecallFail() { AuthDatabase *auth_db = auth_provider->getAuthDatabase(); AuthEntry authEntry; // no such user yet UASSERT(!auth_db->getAuth("TestName", authEntry)); } void TestAuthDatabase::testCreate() { AuthDatabase *auth_db = auth_provider->getAuthDatabase(); AuthEntry authEntry; authEntry.name = "TestName"; authEntry.password = "TestPassword"; authEntry.privileges.emplace_back("shout"); authEntry.privileges.emplace_back("interact"); authEntry.last_login = 1000; UASSERT(auth_db->createAuth(authEntry)); } void TestAuthDatabase::testRecall() { AuthDatabase *auth_db = auth_provider->getAuthDatabase(); AuthEntry authEntry; UASSERT(auth_db->getAuth("TestName", authEntry)); UASSERTEQ(std::string, authEntry.name, "TestName"); UASSERTEQ(std::string, authEntry.password, "TestPassword"); // the order of privileges is unimportant std::sort(authEntry.privileges.begin(), authEntry.privileges.end()); UASSERTEQ(std::string, str_join(authEntry.privileges, ","), "interact,shout"); } void TestAuthDatabase::testChange() { AuthDatabase *auth_db = auth_provider->getAuthDatabase(); AuthEntry authEntry; UASSERT(auth_db->getAuth("TestName", authEntry)); authEntry.password = "NewPassword"; authEntry.last_login = 1002; UASSERT(auth_db->saveAuth(authEntry)); } void TestAuthDatabase::testRecallChanged() { AuthDatabase *auth_db = auth_provider->getAuthDatabase(); AuthEntry authEntry; UASSERT(auth_db->getAuth("TestName", authEntry)); UASSERTEQ(std::string, authEntry.password, "NewPassword"); // the order of privileges is unimportant std::sort(authEntry.privileges.begin(), authEntry.privileges.end()); UASSERTEQ(std::string, str_join(authEntry.privileges, ","), "interact,shout"); UASSERTEQ(u64, authEntry.last_login, 1002); } void TestAuthDatabase::testChangePrivileges() { AuthDatabase *auth_db = auth_provider->getAuthDatabase(); AuthEntry authEntry; UASSERT(auth_db->getAuth("TestName", authEntry)); authEntry.privileges.clear(); authEntry.privileges.emplace_back("interact"); authEntry.privileges.emplace_back("fly"); authEntry.privileges.emplace_back("dig"); UASSERT(auth_db->saveAuth(authEntry)); } void TestAuthDatabase::testRecallChangedPrivileges() { AuthDatabase *auth_db = auth_provider->getAuthDatabase(); AuthEntry authEntry; UASSERT(auth_db->getAuth("TestName", authEntry)); // the order of privileges is unimportant std::sort(authEntry.privileges.begin(), authEntry.privileges.end()); UASSERTEQ(std::string, str_join(authEntry.privileges, ","), "dig,fly,interact"); } void TestAuthDatabase::testListNames() { AuthDatabase *auth_db = auth_provider->getAuthDatabase(); std::vector<std::string> list; AuthEntry authEntry; authEntry.name = "SecondName"; authEntry.password = "SecondPassword"; authEntry.privileges.emplace_back("shout"); authEntry.privileges.emplace_back("interact"); authEntry.last_login = 1003; auth_db->createAuth(authEntry); auth_db->listNames(list); // not necessarily sorted, so sort before comparing std::sort(list.begin(), list.end()); UASSERTEQ(std::string, str_join(list, ","), "SecondName,TestName"); } void TestAuthDatabase::testDelete() { AuthDatabase *auth_db = auth_provider->getAuthDatabase(); UASSERT(!auth_db->deleteAuth("NoSuchName")); UASSERT(auth_db->deleteAuth("TestName")); // second try, expect failure UASSERT(!auth_db->deleteAuth("TestName")); }