Celestia
61 строка · 1.8 Кб
1#include <algorithm>
2#include <fstream>
3#include <ios>
4#include <iterator>
5#include <memory>
6#include <sstream>
7#include <vector>
8
9#include <doctest.h>
10
11#include <celcompat/filesystem.h>
12#include <celmodel/model.h>
13#include <celmodel/modelfile.h>
14#include <celutil/reshandle.h>
15
16TEST_SUITE_BEGIN("CMOD integration");
17
18TEST_CASE("CMOD binary to ASCII roundtrip")
19{
20std::vector<fs::path> paths;
21cmod::HandleGetter handleGetter = [&](const fs::path& path)
22{
23auto it = std::find(paths.cbegin(), paths.cend(), path);
24if (it == paths.cend())
25{
26paths.push_back(path);
27return static_cast<ResourceHandle>(paths.size() - 1);
28}
29else
30{
31return static_cast<ResourceHandle>(it - paths.cbegin());
32}
33};
34cmod::SourceGetter sourceGetter = [&](ResourceHandle handle) { return paths[handle]; };
35
36std::ifstream f("testmodel.cmod", std::ios::in | std::ios::binary);
37REQUIRE(f.good());
38std::stringstream sourceData;
39sourceData << f.rdbuf();
40
41std::unique_ptr<cmod::Model> modelFromBinary = cmod::LoadModel(sourceData, handleGetter);
42REQUIRE(modelFromBinary != nullptr);
43
44std::stringstream asciiData;
45REQUIRE(cmod::SaveModelAscii(modelFromBinary.get(), asciiData, sourceGetter));
46
47std::unique_ptr<cmod::Model> modelFromAscii = cmod::LoadModel(asciiData, handleGetter);
48REQUIRE(modelFromAscii != nullptr);
49
50std::stringstream roundtrippedData;
51REQUIRE(cmod::SaveModelBinary(modelFromAscii.get(), roundtrippedData, sourceGetter));
52
53sourceData.clear();
54REQUIRE(sourceData.seekg(0, std::ios_base::beg).good());
55
56std::istreambuf_iterator<char> end;
57REQUIRE(std::equal(std::istreambuf_iterator<char>(sourceData), end,
58std::istreambuf_iterator<char>(roundtrippedData), end));
59}
60
61TEST_SUITE_END();
62