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

@ -10,7 +10,11 @@
#include "SColor.h" #include "SColor.h"
#include "SMesh.h" #include "SMesh.h"
#include <iostream> #define TINYGLTF_IMPLEMENTATION
#include <tiny_gltf.h>
#include <memory>
#include <string>
namespace irr namespace irr
{ {
@ -30,7 +34,7 @@ bool CGLTFMeshFileLoader::isALoadableFileExtension(
IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file) IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
{ {
if (file->getSize() == 0) { if (file->getSize() == 0 || !tryParseGLTF(file)) {
return nullptr; return nullptr;
} }
@ -53,6 +57,19 @@ IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
return animatedMesh; 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 irr
} // namespace scene } // namespace scene

View File

@ -21,6 +21,8 @@ public:
IAnimatedMesh* createMesh(io::IReadFile* file) override; IAnimatedMesh* createMesh(io::IReadFile* file) override;
private:
bool tryParseGLTF(io::IReadFile* file) const;
}; };
} // namespace scene } // namespace scene

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

@ -4,44 +4,78 @@
#include <catch.hpp> #include <catch.hpp>
#include <irrlicht.h> #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") { 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", ""}; irr::io::InMemoryFile filebuf {"test.gltf", ""};
auto* mesh { smgr->getMesh(&filebuf) }; ScopedMesh sm { &filebuf };
CHECK(sm.getMesh() == nullptr);
CHECK(mesh == nullptr);
device->drop();
} }
TEST_CASE("minimal triangle has correct vertices") { TEST_CASE("minimal triangle has correct vertices") {
irr::IrrlichtDevice* device { irr::createDevice(irr::video::EDT_NULL) }; ScopedMesh sm { "source/Irrlicht/tests/assets/minimal_triangle.gltf" };
irr::scene::ISceneManager* smgr { device->getSceneManager() };
auto* mesh { smgr->getMesh(
"source/Irrlicht/tests/assets/minimal_triangle.gltf", "") };
auto* mesh = sm.getMesh();
REQUIRE(mesh != nullptr); REQUIRE(mesh != nullptr);
REQUIRE(mesh->getMeshBufferCount() == 1); REQUIRE(mesh->getMeshBufferCount() == 1);
auto* meshbuf { mesh->getMeshBuffer(0) }; auto* meshbuf = mesh->getMeshBuffer(0);
REQUIRE(meshbuf->getVertexCount() == 3); REQUIRE(meshbuf->getVertexCount() == 3);
irr::video::S3DVertex* vertices { auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(
reinterpret_cast<irr::video::S3DVertex*>( meshbuf->getVertices());
meshbuf->getVertices()) };
CHECK(vertices[0].Pos == irr::core::vector3df {0.0f, 0.0f, 0.0f}); 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[1].Pos == irr::core::vector3df {0.0f, 1.0f, 0.0f});
CHECK(vertices[2].Pos == irr::core::vector3df {-1.0f, 0.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") { TEST_CASE("mesh loader returns nullptr when given null file pointer") {
irr::IrrlichtDevice* device { irr::createDevice(irr::video::EDT_NULL) }; ScopedMesh sm { nullptr };
irr::scene::ISceneManager* smgr { device->getSceneManager() }; CHECK(sm.getMesh() == nullptr);
auto* mesh { smgr->getMesh(nullptr) }; }
CHECK(mesh == nullptr); TEST_CASE("invalid JSON returns nullptr") {
SECTION("missing closing brace") {
device->drop(); 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);
}
} }