Test invalid JSON

Also converted all .cpp and .h line endings to CRLF in this commit.
This commit is contained in:
JosiahWI 2022-10-22 16:36:29 -05:00 committed by Josiah VanderZee
parent 8c9c1e57ea
commit 21c3c16557
6 changed files with 278 additions and 192 deletions

View File

@ -1,59 +1,76 @@
#include "CGLTFMeshFileLoader.h"
#include "CMeshBuffer.h"
#include "coreutil.h"
#include "IAnimatedMesh.h"
#include "IReadFile.h"
#include "irrTypes.h"
#include "path.h"
#include "S3DVertex.h"
#include "SAnimatedMesh.h"
#include "SColor.h"
#include "SMesh.h"
#include <iostream>
namespace irr
{
namespace scene
{
CGLTFMeshFileLoader::CGLTFMeshFileLoader()
{
}
bool CGLTFMeshFileLoader::isALoadableFileExtension(
const io::path& filename) const
{
return core::hasFileExtension(filename, "gltf");
}
IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
{
if (file->getSize() == 0) {
return nullptr;
}
// sorry Bjarne
SMeshBuffer* meshbuf { new SMeshBuffer {} };
const video::S3DVertex* vertices { new video::S3DVertex[3] {
{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {}, {0.0f, 0.0f}},
{{0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {}, {1.0f, 0.0f}},
{{-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {}, {0.0f, 1.0f}} } };
const u16* indices { new u16[3] {0, 1, 2} };
meshbuf->append(vertices, 3, indices, 3);
SMesh* mesh { new SMesh {} };
mesh->addMeshBuffer(meshbuf);
SAnimatedMesh* animatedMesh { new SAnimatedMesh {} };
animatedMesh->addMesh(mesh);
return animatedMesh;
}
} // namespace irr
} // namespace scene
#include "CGLTFMeshFileLoader.h"
#include "CMeshBuffer.h"
#include "coreutil.h"
#include "IAnimatedMesh.h"
#include "IReadFile.h"
#include "irrTypes.h"
#include "path.h"
#include "S3DVertex.h"
#include "SAnimatedMesh.h"
#include "SColor.h"
#include "SMesh.h"
#define TINYGLTF_IMPLEMENTATION
#include <tiny_gltf.h>
#include <memory>
#include <string>
namespace irr
{
namespace scene
{
CGLTFMeshFileLoader::CGLTFMeshFileLoader()
{
}
bool CGLTFMeshFileLoader::isALoadableFileExtension(
const io::path& filename) const
{
return core::hasFileExtension(filename, "gltf");
}
IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
{
if (file->getSize() == 0 || !tryParseGLTF(file)) {
return nullptr;
}
// sorry Bjarne
SMeshBuffer* meshbuf { new SMeshBuffer {} };
const video::S3DVertex* vertices { new video::S3DVertex[3] {
{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {}, {0.0f, 0.0f}},
{{0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {}, {1.0f, 0.0f}},
{{-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {}, {0.0f, 1.0f}} } };
const u16* indices { new u16[3] {0, 1, 2} };
meshbuf->append(vertices, 3, indices, 3);
SMesh* mesh { new SMesh {} };
mesh->addMeshBuffer(meshbuf);
SAnimatedMesh* animatedMesh { new SAnimatedMesh {} };
animatedMesh->addMesh(mesh);
return animatedMesh;
}
bool CGLTFMeshFileLoader::tryParseGLTF(io::IReadFile* file) const
{
tinygltf::Model model {};
tinygltf::TinyGLTF loader {};
std::string err {};
std::string warn {};
auto buf = std::make_unique<char[]>(file->getSize());
file->read(buf.get(), file->getSize());
return loader.LoadASCIIFromString(
&model, &err, &warn, buf.get(), file->getSize(), "", 1);
}
} // namespace irr
} // namespace scene

View File

@ -0,0 +1,33 @@
#ifndef __C_GLTF_MESH_FILE_LOADER_INCLUDED__
#define __C_GLTF_MESH_FILE_LOADER_INCLUDED__
#include "IAnimatedMesh.h"
#include "IMeshLoader.h"
#include "IReadFile.h"
#include "path.h"
namespace irr
{
namespace scene
{
class CGLTFMeshFileLoader : public IMeshLoader
{
public:
CGLTFMeshFileLoader();
bool isALoadableFileExtension(const io::path& filename) const override;
IAnimatedMesh* createMesh(io::IReadFile* file) override;
private:
bool tryParseGLTF(io::IReadFile* file) const;
};
} // namespace scene
} // namespace irr
#endif // __C_GLTF_MESH_FILE_LOADER_INCLUDED__

View File

@ -1,54 +1,54 @@
#include "inMemoryFile.h"
#include <irrlicht.h>
#include <ios>
namespace irr
{
namespace io
{
InMemoryFile::InMemoryFile(const io::path& filename, const std::string& s)
: m_filename { filename }
, m_sstream { s }
{
}
std::size_t InMemoryFile::read(void* buffer, std::size_t sizeToRead)
{
m_sstream.read(reinterpret_cast<char*>(buffer), sizeToRead);
return m_sstream.gcount();
}
bool InMemoryFile::seek(long finalPos, bool relativeMovement)
{
if (relativeMovement) {
m_sstream.seekg(finalPos, std::ios::beg);
} else {
m_sstream.seekg(finalPos);
}
return m_sstream.fail();
}
long InMemoryFile::getSize() const
{
return m_sstream.str().size();
}
long InMemoryFile::getPos() const
{
return m_sstream.tellg();
}
const io::path& InMemoryFile::getFileName() const
{
return m_filename;
}
} // namespace irr
} // namespace io
#include "inMemoryFile.h"
#include <irrlicht.h>
#include <ios>
namespace irr
{
namespace io
{
InMemoryFile::InMemoryFile(const io::path& filename, const std::string& s)
: m_filename { filename }
, m_sstream { s }
{
}
std::size_t InMemoryFile::read(void* buffer, std::size_t sizeToRead)
{
m_sstream.read(reinterpret_cast<char*>(buffer), sizeToRead);
return m_sstream.gcount();
}
bool InMemoryFile::seek(long finalPos, bool relativeMovement)
{
if (relativeMovement) {
m_sstream.seekg(finalPos, std::ios::beg);
} else {
m_sstream.seekg(finalPos);
}
return m_sstream.fail();
}
long InMemoryFile::getSize() const
{
return m_sstream.str().size();
}
long InMemoryFile::getPos() const
{
return m_sstream.tellg();
}
const io::path& InMemoryFile::getFileName() const
{
return m_filename;
}
} // namespace irr
} // namespace io

View File

@ -1,32 +1,32 @@
#include <irrlicht.h>
#include <cstddef>
#include <sstream>
#include <string>
namespace irr
{
namespace io
{
class InMemoryFile: public IReadFile
{
public:
InMemoryFile(const io::path& filename, const std::string& s);
std::size_t read(void* buffer, std::size_t sizeToRead) override;
bool seek(long finalPos, bool relativeMovement=false) override;
long getSize() const override;
long getPos() const override;
const io::path& getFileName() const override;
private:
io::path m_filename;
mutable std::stringstream m_sstream;
};
} // namespace io
} // namespace irr
#include <irrlicht.h>
#include <cstddef>
#include <sstream>
#include <string>
namespace irr
{
namespace io
{
class InMemoryFile: public IReadFile
{
public:
InMemoryFile(const io::path& filename, const std::string& s);
std::size_t read(void* buffer, std::size_t sizeToRead) override;
bool seek(long finalPos, bool relativeMovement=false) override;
long getSize() const override;
long getPos() const override;
const io::path& getFileName() const override;
private:
io::path m_filename;
mutable std::stringstream m_sstream;
};
} // namespace io
} // namespace irr

View File

@ -1,47 +1,81 @@
#include "inMemoryFile.h"
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include <irrlicht.h>
TEST_CASE("load empty gltf file") {
irr::IrrlichtDevice* device { irr::createDevice(irr::video::EDT_NULL) };
irr::scene::ISceneManager* smgr { device->getSceneManager() };
irr::io::InMemoryFile filebuf {"test.gltf", ""};
auto* mesh { smgr->getMesh(&filebuf) };
CHECK(mesh == nullptr);
device->drop();
}
TEST_CASE("minimal triangle has correct vertices") {
irr::IrrlichtDevice* device { irr::createDevice(irr::video::EDT_NULL) };
irr::scene::ISceneManager* smgr { device->getSceneManager() };
auto* mesh { smgr->getMesh(
"source/Irrlicht/tests/assets/minimal_triangle.gltf", "") };
REQUIRE(mesh != nullptr);
REQUIRE(mesh->getMeshBufferCount() == 1);
auto* meshbuf { mesh->getMeshBuffer(0) };
REQUIRE(meshbuf->getVertexCount() == 3);
irr::video::S3DVertex* vertices {
reinterpret_cast<irr::video::S3DVertex*>(
meshbuf->getVertices()) };
CHECK(vertices[0].Pos == irr::core::vector3df {0.0f, 0.0f, 0.0f});
CHECK(vertices[1].Pos == irr::core::vector3df {0.0f, 1.0f, 0.0f});
CHECK(vertices[2].Pos == irr::core::vector3df {-1.0f, 0.0f, 0.0f});
device->drop();
}
TEST_CASE("mesh loader returns nullptr when given null file pointer") {
irr::IrrlichtDevice* device { irr::createDevice(irr::video::EDT_NULL) };
irr::scene::ISceneManager* smgr { device->getSceneManager() };
auto* mesh { smgr->getMesh(nullptr) };
CHECK(mesh == nullptr);
device->drop();
}
#include "inMemoryFile.h"
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include <irrlicht.h>
class ScopedMesh
{
public:
ScopedMesh(irr::io::IReadFile* file)
: m_device { irr::createDevice(irr::video::EDT_NULL) }
, m_mesh { nullptr }
{
auto* smgr = m_device->getSceneManager();
m_mesh = smgr->getMesh(file);
}
ScopedMesh(const irr::io::path& filepath)
: m_device { irr::createDevice(irr::video::EDT_NULL) }
, m_mesh { nullptr }
{
auto* smgr = m_device->getSceneManager();
m_mesh = smgr->getMesh(filepath, "");
}
~ScopedMesh()
{
m_device->drop();
m_mesh = nullptr;
}
const irr::scene::IAnimatedMesh* getMesh() const
{
return m_mesh;
}
private:
irr::IrrlichtDevice* m_device;
irr::scene::IAnimatedMesh* m_mesh;
};
TEST_CASE("load empty gltf file") {
irr::io::InMemoryFile filebuf {"test.gltf", ""};
ScopedMesh sm { &filebuf };
CHECK(sm.getMesh() == nullptr);
}
TEST_CASE("minimal triangle has correct vertices") {
ScopedMesh sm { "source/Irrlicht/tests/assets/minimal_triangle.gltf" };
auto* mesh = sm.getMesh();
REQUIRE(mesh != nullptr);
REQUIRE(mesh->getMeshBufferCount() == 1);
auto* meshbuf = mesh->getMeshBuffer(0);
REQUIRE(meshbuf->getVertexCount() == 3);
auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(
meshbuf->getVertices());
CHECK(vertices[0].Pos == irr::core::vector3df {0.0f, 0.0f, 0.0f});
CHECK(vertices[1].Pos == irr::core::vector3df {0.0f, 1.0f, 0.0f});
CHECK(vertices[2].Pos == irr::core::vector3df {-1.0f, 0.0f, 0.0f});
}
TEST_CASE("mesh loader returns nullptr when given null file pointer") {
ScopedMesh sm { nullptr };
CHECK(sm.getMesh() == nullptr);
}
TEST_CASE("invalid JSON returns nullptr") {
SECTION("missing closing brace") {
irr::io::InMemoryFile filebuf {"test.gltf", "{"};
ScopedMesh sm { &filebuf };
CHECK(sm.getMesh() == nullptr);
}
SECTION("missing colon") {
irr::io::InMemoryFile filebuf {"test.gltf", "{\"a\" \"b\"}"};
ScopedMesh sm { &filebuf };
CHECK(sm.getMesh() == nullptr);
}
}

View File

@ -24,6 +24,8 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-D_DEBUG)
endif()
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")