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
parent 4feaf17871
commit df5a413083
No known key found for this signature in database
GPG Key ID: C7BB8573A4ABC4B9
6 changed files with 277 additions and 224 deletions

View File

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

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

View File

@ -10,7 +10,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-D_DEBUG) add_definitions(-D_DEBUG)
endif() endif()
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE) set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$") if(CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$")

View File

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

View File

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

View File

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