initial import, >900commits predate this

This commit is contained in:
2020-09-29 13:47:50 +02:00
commit e74154ccee
352 changed files with 108120 additions and 0 deletions

View File

@ -0,0 +1,14 @@
add_executable(filesystem_service_test
filesystem_tests.cpp
res/test.zip.h
)
target_include_directories(filesystem_service_test PRIVATE ".")
target_link_libraries(filesystem_service_test
filesystem_service
gtest_main
)
add_test(NAME filesystem_service_test COMMAND filesystem_service_test)

View File

@ -0,0 +1,307 @@
#include <gtest/gtest.h>
#include <mm/services/filesystem.hpp>
//#include <physfs.h>
#include <nlohmann/json.hpp>
#include <mm/fs_const_archiver.hpp>
#include <mm/logger.hpp>
#include "res/test.zip.h" // zip in memory
char* argv0; // HACK
TEST(fs_service_system, general) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
ASSERT_FALSE(fs_ss_ptr->open("i_do_not_exist.txt"));
ASSERT_FALSE(fs_ss_ptr->exists("test_file.txt"));
ASSERT_TRUE(PHYSFS_mountMemory(test_zip, test_zip_len, NULL, "", NULL, 0));
ASSERT_TRUE(fs_ss_ptr->exists("test_file.txt"));
{
MM::Services::FilesystemService::fs_file_t file = fs_ss_ptr->open("test_file.txt", MM::Services::FilesystemService::FOPEN_t::READ);
ASSERT_TRUE(file) << "File opened";
ASSERT_FALSE(fs_ss_ptr->eof(file));
ASSERT_EQ(fs_ss_ptr->tell(file), 0) << "position at start of file";
char buffer[9] = {0};
ASSERT_EQ(fs_ss_ptr->read(file, buffer, 8), 8) << "file length == 8";
ASSERT_STREQ(buffer, "test :D\n");
ASSERT_TRUE(fs_ss_ptr->eof(file));
ASSERT_EQ(fs_ss_ptr->tell(file), 8) << "position at end of file, in this case 8";
ASSERT_TRUE(fs_ss_ptr->close(file));
}
engine.disableService<MM::Services::FilesystemService>();
}
TEST(fs_service_system, fopen_eof_tell) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
ASSERT_TRUE(PHYSFS_mountMemory(test_zip, test_zip_len, NULL, "", NULL, 0));
MM::Services::FilesystemService::fs_file_t file = fs_ss_ptr->open("test_file.txt", MM::Services::FilesystemService::FOPEN_t::READ);
ASSERT_TRUE(file) << "File opened";
auto& fs = *fs_ss_ptr;
ASSERT_FALSE(fs.eof(file));
ASSERT_EQ(fs.tell(file), 0) << "position at start of file";
ASSERT_TRUE(fs.seek(file, 3));
ASSERT_FALSE(fs.eof(file));
ASSERT_EQ(fs.tell(file), 3);
ASSERT_FALSE(fs.seek(file, 10)) << "seek past end";
ASSERT_FALSE(fs.eof(file));
ASSERT_EQ(fs.tell(file), 3);
ASSERT_TRUE(fs.close(file));
engine.disableService<MM::Services::FilesystemService>();
}
TEST(fs_service_system, fopen_w_a_write) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
auto& fs = *fs_ss_ptr;
const char* filename = "test2_write_file.txt";
// write
{
if (fs.exists(filename)) {
PHYSFS_delete(filename); // TODO: add wrapper
std::cout << "######deleted existing file" << std::endl;
}
auto file = fs.open(filename, MM::Services::FilesystemService::FOPEN_t::WRITE);
ASSERT_TRUE(file) << "File opened";
//ASSERT_TRUE(MM::fs_service_system::eof(file)); actually false
ASSERT_EQ(fs.tell(file), 0) << "position at start of file";
ASSERT_TRUE(fs.seek(file, 10)) << "seek past end";
ASSERT_EQ(fs.tell(file), 10);
const char data[4] {'a', 's', 'd', 'f'};
ASSERT_EQ(fs.write(file, data, 4), 4);
ASSERT_EQ(fs.tell(file), 14);
fs.flush(file);
ASSERT_TRUE(fs.close(file));
}
// append
{
auto file = fs.open(filename, MM::Services::FilesystemService::FOPEN_t::APPEND);
ASSERT_TRUE(file) << "File opened";
//ASSERT_TRUE(MM::fs_service_system::eof(file)); actually false
ASSERT_EQ(fs.tell(file), 14) << "position at start of file";
const char data[4] {'j', 'k', 'l', 'o'};
ASSERT_EQ(fs.write(file, data, 4), 4);
ASSERT_EQ(fs.tell(file), 18);
fs.flush(file);
ASSERT_TRUE(fs.close(file));
}
// read
{
auto file = fs.open(filename, MM::Services::FilesystemService::FOPEN_t::READ);
ASSERT_TRUE(file) << "File opened";
ASSERT_FALSE(fs.eof(file));
ASSERT_EQ(fs.tell(file), 0) << "position at start of file";
// fail
{
const char data[4] {'j', 'k', 'l', 'o'};
ASSERT_FALSE(fs.write(file, data, 4) == 4);
}
ASSERT_TRUE(fs.seek(file, 10));
char buffer[9] = {0};
ASSERT_EQ(fs.read(file, buffer, 8), 8);
ASSERT_STREQ(buffer, "asdfjklo");
ASSERT_TRUE(fs.eof(file));
ASSERT_TRUE(fs.close(file));
}
engine.disableService<MM::Services::FilesystemService>();
}
// TODO: readString
// TODO: fwrite ?
TEST(fs_service_system, read_json) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
auto& fs = *fs_ss_ptr;
{
auto j = fs.readJson("does_not_exist.json");
ASSERT_TRUE(j.empty());
}
ASSERT_TRUE(PHYSFS_mountMemory(test_zip, test_zip_len, NULL, "", NULL, 0));
{
auto j1 = fs.readJson("wall_concrete-1_se_0.2.json");
//SPDLOG_INFO("dump: \n{}", j1.dump(4));
ASSERT_FALSE(j1.empty());
ASSERT_EQ(j1["type"], "tileset");
}
engine.disableService<MM::Services::FilesystemService>();
}
TEST(fs_service_system, read_const_json) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
auto& fs = *fs_ss_ptr;
{
auto j = fs.readJson("does_not_exist.json");
ASSERT_TRUE(j.empty());
}
FS_CONST_MOUNT_FILE("json/file1.json",
R"({
"answer": {
"everything": 42
},
"happy": true,
"list": [
1,
0,
2
],
"name": "Niels",
"nothing": null,
"object": {
"currency": "USD",
"value": 42.99
},
"pi": 3.141
})")
FS_CONST_MOUNT_FILE("/json/file2.json",
R"({
"answer": 42
})")
{
auto j1 = fs.readJson("/json/file1.json");
//std::cout << "dump: \n" << std::setw(4) << j1 << std::endl;
ASSERT_FALSE(j1.empty());
ASSERT_EQ(j1["happy"], true);
}
{
auto j2 = fs.readJson("json/file2.json");
//std::cout << "dump: \n" << std::setw(4) << j2 << std::endl;
ASSERT_FALSE(j2.empty());
ASSERT_EQ(j2["answer"], 42);
}
engine.disableService<MM::Services::FilesystemService>();
}
TEST(fs_service_system, write_json) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0, "filesystem_Test");
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
auto* fs_ss_ptr = engine.tryService<MM::Services::FilesystemService>();
ASSERT_NE(fs_ss_ptr, nullptr);
auto& fs = *fs_ss_ptr;
//ASSERT_TRUE(PHYSFS_mountMemory(test_zip, test_zip_len, NULL, "", NULL, 0));
// taken from json README.md
nlohmann::json j = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
ASSERT_TRUE(fs.writeJson("write_test.json", j, 4));
engine.disableService<MM::Services::FilesystemService>();
}
//FS_CONST_MOUNT_FILE_STATIC("static_test_file.txt",
//R"(test text.)")
int main(int argc, char** argv) {
argv0 = argv[0];
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

Binary file not shown.

View File

@ -0,0 +1,64 @@
unsigned char test_zip[] = {
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0xfa, 0x64,
0x42, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x00, 0x0d, 0x00, 0x20, 0x00, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66,
0x69, 0x6c, 0x65, 0x2e, 0x74, 0x78, 0x74, 0x55, 0x54, 0x0d, 0x00, 0x07,
0x89, 0x81, 0x55, 0x5c, 0x20, 0x0b, 0x87, 0x5c, 0x89, 0x81, 0x55, 0x5c,
0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0xe8, 0x03, 0x00, 0x00, 0x04, 0xe8,
0x03, 0x00, 0x00, 0x2b, 0x49, 0x2d, 0x2e, 0x51, 0xb0, 0x72, 0xe1, 0x02,
0x00, 0x50, 0x4b, 0x07, 0x08, 0x46, 0xb6, 0x5c, 0xe8, 0x0a, 0x00, 0x00,
0x00, 0x08, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08,
0x00, 0x08, 0x00, 0xe1, 0xbc, 0x7e, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xb8, 0x05, 0x00, 0x00, 0x1b, 0x00, 0x20, 0x00, 0x77,
0x61, 0x6c, 0x6c, 0x5f, 0x63, 0x6f, 0x6e, 0x63, 0x72, 0x65, 0x74, 0x65,
0x2d, 0x31, 0x5f, 0x73, 0x65, 0x5f, 0x30, 0x2e, 0x32, 0x2e, 0x6a, 0x73,
0x6f, 0x6e, 0x55, 0x54, 0x0d, 0x00, 0x07, 0x06, 0xf0, 0x9f, 0x5c, 0x06,
0xf0, 0x9f, 0x5c, 0x06, 0xf0, 0x9f, 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84, 0xa1,
0x3e, 0x8a, 0x6c, 0xc2, 0x16, 0x47, 0x89, 0x8d, 0x78, 0x85, 0xc0, 0xf5,
0x35, 0x7e, 0x13, 0xb0, 0x2b, 0x87, 0xfd, 0x45, 0x96, 0xee, 0x6f, 0x8e,
0x6f, 0x7f, 0x72, 0xb2, 0xd4, 0xcb, 0x21, 0x47, 0x9c, 0x1c, 0xc3, 0x27,
0x8c, 0x83, 0xfd, 0x20, 0x43, 0x09, 0x07, 0xd1, 0xff, 0x41, 0x96, 0x12,
0x0e, 0xfa, 0x38, 0x03, 0x05, 0xf7, 0xa8, 0x38, 0xac, 0x2d, 0xa2, 0x4b,
0xc4, 0xd3, 0x25, 0x8b, 0x89, 0x41, 0x1d, 0xf4, 0x29, 0xae, 0xe3, 0xa4,
0x3c, 0x25, 0x85, 0x76, 0x8a, 0x1e, 0xff, 0xbc, 0x88, 0xf2, 0xa8, 0x47,
0x54, 0x11, 0x39, 0x05, 0xb4, 0x3a, 0x9b, 0xcb, 0xd0, 0xda, 0xca, 0x83,
0x36, 0xb9, 0x49, 0x08, 0x44, 0xf9, 0x45, 0xc5, 0x92, 0xae, 0xa7, 0xa3,
0xf9, 0x86, 0x72, 0xc6, 0x64, 0x7d, 0x73, 0x77, 0x9b, 0xef, 0x65, 0x73,
0x36, 0x63, 0x3a, 0x5c, 0x1b, 0x4b, 0x72, 0x36, 0x05, 0x2e, 0x95, 0xbc,
0x7e, 0x00, 0x50, 0x4b, 0x07, 0x08, 0x20, 0xba, 0x28, 0x91, 0x30, 0x01,
0x00, 0x00, 0xb8, 0x05, 0x00, 0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03,
0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0xfa, 0x64, 0x42, 0x4e, 0x46, 0xb6,
0x5c, 0xe8, 0x0a, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x0d, 0x00,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb4, 0x81,
0x00, 0x00, 0x00, 0x00, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66, 0x69, 0x6c,
0x65, 0x2e, 0x74, 0x78, 0x74, 0x55, 0x54, 0x0d, 0x00, 0x07, 0x89, 0x81,
0x55, 0x5c, 0x20, 0x0b, 0x87, 0x5c, 0x89, 0x81, 0x55, 0x5c, 0x75, 0x78,
0x0b, 0x00, 0x01, 0x04, 0xe8, 0x03, 0x00, 0x00, 0x04, 0xe8, 0x03, 0x00,
0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x08, 0x00, 0x08,
0x00, 0xe1, 0xbc, 0x7e, 0x4e, 0x20, 0xba, 0x28, 0x91, 0x30, 0x01, 0x00,
0x00, 0xb8, 0x05, 0x00, 0x00, 0x1b, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xb4, 0x81, 0x65, 0x00, 0x00, 0x00, 0x77,
0x61, 0x6c, 0x6c, 0x5f, 0x63, 0x6f, 0x6e, 0x63, 0x72, 0x65, 0x74, 0x65,
0x2d, 0x31, 0x5f, 0x73, 0x65, 0x5f, 0x30, 0x2e, 0x32, 0x2e, 0x6a, 0x73,
0x6f, 0x6e, 0x55, 0x54, 0x0d, 0x00, 0x07, 0x06, 0xf0, 0x9f, 0x5c, 0x06,
0xf0, 0x9f, 0x5c, 0x06, 0xf0, 0x9f, 0x5c, 0x75, 0x78, 0x0b, 0x00, 0x01,
0x04, 0xe8, 0x03, 0x00, 0x00, 0x04, 0xe8, 0x03, 0x00, 0x00, 0x50, 0x4b,
0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0xc4, 0x00,
0x00, 0x00, 0xfe, 0x01, 0x00, 0x00, 0x00, 0x00
};
unsigned int test_zip_len = 728;

View File

@ -0,0 +1 @@
test :D

View File

@ -0,0 +1,80 @@
{ "columns":11,
"image":"wall_concrete-1_se_0.2.png",
"imageheight":160,
"imagewidth":352,
"margin":0,
"name":"wall_concrete-1_se_0.2",
"spacing":0,
"terrains":[
{
"name":"solid",
"tile":34
}],
"tilecount":55,
"tiledversion":"1.2.3",
"tileheight":32,
"tiles":[
{
"id":1,
"terrain":[0, 0, 0, -1]
},
{
"id":2,
"terrain":[0, 0, -1, 0]
},
{
"id":12,
"terrain":[0, -1, 0, 0]
},
{
"id":13,
"terrain":[-1, 0, 0, 0]
},
{
"id":19,
"terrain":[-1, 0, 0, -1]
},
{
"id":20,
"terrain":[0, -1, -1, 0]
},
{
"id":22,
"terrain":[-1, -1, -1, 0]
},
{
"id":23,
"terrain":[-1, -1, 0, 0]
},
{
"id":24,
"terrain":[-1, -1, 0, -1]
},
{
"id":33,
"terrain":[-1, 0, -1, 0]
},
{
"id":34,
"terrain":[0, 0, 0, 0]
},
{
"id":35,
"terrain":[0, -1, 0, -1]
},
{
"id":44,
"terrain":[-1, 0, -1, -1]
},
{
"id":45,
"terrain":[0, 0, -1, -1]
},
{
"id":46,
"terrain":[0, -1, -1, -1]
}],
"tilewidth":32,
"type":"tileset",
"version":1.2
}