File: CMapFormatTest.cpp

package info (click to toggle)
vcmi 0.99%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 10,264 kB
  • ctags: 16,826
  • sloc: cpp: 121,945; objc: 248; sh: 193; makefile: 28; python: 13; ansic: 9
file content (98 lines) | stat: -rw-r--r-- 2,351 bytes parent folder | download
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

/*
 * CMapFormatTest.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#include "StdInc.h"

#include <boost/test/unit_test.hpp>

#include "../lib/filesystem/CMemoryBuffer.h"

#include "../lib/mapping/CMap.h"
#include "../lib/rmg/CMapGenOptions.h"
#include "../lib/rmg/CMapGenerator.h"
#include "../lib/mapping/MapFormatJson.h"

#include "../lib/VCMIDirs.h"

#include "MapComparer.h"


static const int TEST_RANDOM_SEED = 1337;

static std::unique_ptr<CMap> initialMap;

class CMapTestFixture
{
public:
	CMapTestFixture()
	{
		CMapGenOptions opt;

		opt.setHeight(CMapHeader::MAP_SIZE_MIDDLE);
		opt.setWidth(CMapHeader::MAP_SIZE_MIDDLE);
		opt.setHasTwoLevels(true);
		opt.setPlayerCount(4);

		opt.setPlayerTypeForStandardPlayer(PlayerColor(0), EPlayerType::HUMAN);
		opt.setPlayerTypeForStandardPlayer(PlayerColor(1), EPlayerType::AI);
		opt.setPlayerTypeForStandardPlayer(PlayerColor(2), EPlayerType::AI);
		opt.setPlayerTypeForStandardPlayer(PlayerColor(3), EPlayerType::AI);

		CMapGenerator gen;

		initialMap = gen.generate(&opt, TEST_RANDOM_SEED);
		initialMap->name = "Test";
	};
	~CMapTestFixture()
	{
		initialMap.reset();
	};
};

BOOST_GLOBAL_FIXTURE(CMapTestFixture);

BOOST_AUTO_TEST_CASE(CMapFormatVCMI_Simple)
{
	logGlobal->info("CMapFormatVCMI_Simple start");
	BOOST_TEST_CHECKPOINT("CMapFormatVCMI_Simple start");
	CMemoryBuffer serializeBuffer;
	{
		CMapSaverJson saver(&serializeBuffer);
		saver.saveMap(initialMap);
	}
	BOOST_TEST_CHECKPOINT("CMapFormatVCMI_Simple serialized");
	#if 1
	{
		auto path = VCMIDirs::get().userDataPath()/"test.vmap";
		boost::filesystem::remove(path);
		boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::binary);

		tmp.write((const char *)serializeBuffer.getBuffer().data(),serializeBuffer.getSize());
		tmp.flush();
		tmp.close();

		logGlobal->info("Test map has been saved to:");
		logGlobal->info(path.string());
	}
	BOOST_TEST_CHECKPOINT("CMapFormatVCMI_Simple saved");

	#endif // 1

	serializeBuffer.seek(0);
	{
		CMapLoaderJson loader(&serializeBuffer);
		std::unique_ptr<CMap> serialized = loader.loadMap();

		MapComparer c;
		c(serialized, initialMap);
	}

	logGlobal->info("CMapFormatVCMI_Simple finish");
}