From 8cd0977a3688fa705c83867c57505a47b9269369 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 8 Jan 2023 16:34:43 +0000 Subject: Fix up all the static analyzer warnings --- application/main.cpp | 2 +- game/geoData.cpp | 2 +- game/network/link.cpp | 2 +- game/network/network.cpp | 4 ++-- game/network/network.h | 6 ++++-- game/network/rail.cpp | 4 ++-- gfx/gl/camera.cpp | 13 +++++++------ gfx/gl/camera.h | 4 ++-- gfx/gl/program.h | 4 ++++ gfx/gl/sceneProvider.h | 5 +++++ gfx/gl/sceneRenderer.cpp | 2 +- gfx/gl/shader.h | 2 +- gfx/gl/shadowMapper.cpp | 2 +- gfx/gl/shadowMapper.h | 7 ++++--- gfx/gl/uiShader.h | 5 +++-- gfx/models/texture.cpp | 4 ++-- lib/enumDetails.hpp | 5 ++++- lib/filesystem.cpp | 1 + test/Jamfile.jam | 23 +++++++++++++++++++++-- test/test-collection.cpp | 4 ++-- test/test-enumDetails.cpp | 11 +++++++---- test/test-geo.cpp | 16 +++++++++------- test/test-glContextBhvr.cpp | 34 +++++++++++++++++----------------- test/test-helpers.hpp | 21 --------------------- test/test-lib.cpp | 6 +++--- test/test-maths.cpp | 17 +++++++++++++---- test/test-network.cpp | 4 ++-- test/test-obj.cpp | 4 ++-- test/test-render.cpp | 26 +++++++++++++------------- test/test-text.cpp | 6 +++--- test/testHelpers.cpp | 16 ++++++++++++++++ test/testHelpers.h | 34 ++++++++++++++++++++++++++++++++++ test/testMainWindow.cpp | 13 +++++++------ test/testRenderOutput.cpp | 2 +- ui/font.cpp | 2 +- 35 files changed, 197 insertions(+), 116 deletions(-) delete mode 100644 test/test-helpers.hpp create mode 100644 test/testHelpers.cpp create mode 100644 test/testHelpers.h diff --git a/application/main.cpp b/application/main.cpp index 224c4f7..1f91dab 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -65,7 +65,7 @@ public: rl->addLinksBetween(l, s); rl->addLinksBetween(t, u); rl->addLinksBetween(u, m); - std::shared_ptr train = world.create(l3); + const std::shared_ptr train = world.create(l3); auto b47 = std::make_shared("brush47"); for (int N = 0; N < 6; N++) { train->create(b47); diff --git a/game/geoData.cpp b/game/geoData.cpp index af39ce3..4dd0c40 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -131,7 +131,7 @@ GeoData::RayTracer::byAxis(glm::vec2 p0, glm::vec2 p1, glm::vec2 d, glm::length_ glm::vec2 GeoData::RayTracer::next() { - glm::vec2 cur {p}; + const glm::vec2 cur {p}; static constexpr const glm::vec2 m {1, -1}; const int axis = (error > 0) ? 1 : 0; diff --git a/game/network/link.cpp b/game/network/link.cpp index 19b60ab..aecc2fc 100644 --- a/game/network/link.cpp +++ b/game/network/link.cpp @@ -68,7 +68,7 @@ LinkCurve::intersectRay(const Ray & ray) const points.reserve(segCount); for (glm::vec3 swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) { const auto t {trans * glm::rotate(half_pi - swing.x, up) * glm::translate(glm::vec3 {radius, 0.F, swing.y})}; - points.push_back(t * glm::vec4 {0, 0, 0, 1}); + points.emplace_back(t * glm::vec4 {0, 0, 0, 1}); } return ray.passesCloseToEdges(points, 1.F); } diff --git a/game/network/network.cpp b/game/network/network.cpp index dd61f9b..59ef7c8 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -22,11 +22,11 @@ Network::nodeAt(glm::vec3 pos) Network::NodeInsertion Network::newNodeAt(glm::vec3 pos) { - if (const auto [n, i] = candidateNodeAt(pos); i == NodeIs::NotInNetwork) { + if (auto [n, i] = candidateNodeAt(pos); i == NodeIs::NotInNetwork) { return {*nodes.insert(std::move(n)).first, i}; } else { - return {n, NodeIs::InNetwork}; + return {std::move(n), NodeIs::InNetwork}; } } diff --git a/game/network/network.h b/game/network/network.h index e7a65b5..4b485cc 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,7 @@ public: using LinkEnd = std::pair; explicit Network(const std::string & textureName); virtual ~Network() = default; + DEFAULT_MOVE_NO_COPY(Network); [[nodiscard]] Node::Ptr findNodeAt(glm::vec3) const; [[nodiscard]] Node::Ptr nodeAt(glm::vec3); @@ -43,7 +45,7 @@ public: virtual Link::CCollection addJoins(glm::vec3, glm::vec3) = 0; virtual Link::CCollection addExtend(glm::vec3, glm::vec3) = 0; - virtual float findNodeDirection(Node::AnyCPtr) const = 0; + [[nodiscard]] virtual float findNodeDirection(Node::AnyCPtr) const = 0; protected: static void joinLinks(const Link::Ptr & l, const Link::Ptr & ol); @@ -94,7 +96,7 @@ public: Link::CCollection addJoins(glm::vec3, glm::vec3) override; Link::CCollection addExtend(glm::vec3, glm::vec3) override; - float findNodeDirection(Node::AnyCPtr) const override; + [[nodiscard]] float findNodeDirection(Node::AnyCPtr) const override; void render(const SceneShader &) const override; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 2f09d88..e7006cf 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -40,7 +40,7 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) std::swap(start, end); } // Find start link/end - opposite entry dir to existing link; so pi +... - float dir = pi + findNodeDirection(node1ins.first); + const float dir = pi + findNodeDirection(node1ins.first); if (dir == vector_yaw(end - start)) { return addLink(start, end); } @@ -50,7 +50,7 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid); return start.z + ((end.z - start.z) * (sm / (sm + em))); }; - float dir2 = pi + findNodeDirection(node2ins.first); + const float dir2 = pi + findNodeDirection(node2ins.first); if (const auto radii = find_arcs_radius(flatStart, dir, flatEnd, dir2); radii.first < radii.second) { const auto radius {radii.first}; const auto c1 = flatStart + sincosf(dir + half_pi) * radius; diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp index 2ea326d..b596c43 100644 --- a/gfx/gl/camera.cpp +++ b/gfx/gl/camera.cpp @@ -7,9 +7,10 @@ Camera::Camera(glm::vec3 pos, float fov, float aspect, float zNear, float zFar) : position {pos}, forward {::north}, up {::up}, fov {fov}, aspect {aspect}, near {zNear}, far {zFar}, - projection {glm::perspective(fov, aspect, zNear, zFar)} + projection {glm::perspective(fov, aspect, zNear, zFar)}, + viewProjection {projection * glm::lookAt(position, position + forward, up)}, inverseViewProjection { + glm::inverse(viewProjection)} { - updateView(); } Ray @@ -38,9 +39,9 @@ std::array Camera::extentsAtDist(const float dist) const { const auto clampToSeaFloor = [this, dist](const glm::vec3 & target) { - if (target.z < -1.5f) { + if (target.z < -1.5F) { const auto vec = glm::normalize(target - position); - constexpr glm::vec3 seafloor {0, 0, -1.5}; + constexpr glm::vec3 seafloor {0, 0, -1.5F}; float outdist; if (glm::intersectRayPlane(position, vec, seafloor, ::up, outdist)) { return (vec * outdist + position) ^ outdist; @@ -48,11 +49,11 @@ Camera::extentsAtDist(const float dist) const } return target ^ dist; }; - const auto depth = -(2.f * (dist - near) * far) / (dist * (near - far)) - 1.f; + const auto depth = -(2.F * (dist - near) * far) / (dist * (near - far)) - 1.F; static constexpr const std::array extents {-1.F, 1.F}; static constexpr const auto cartesianExtents = extents * extents; return cartesianExtents * [&depth, this, &clampToSeaFloor](const auto & extent) { - const glm::vec4 in {extent.first, extent.second, depth, 1.f}; + const glm::vec4 in {extent.first, extent.second, depth, 1.F}; return clampToSeaFloor(perspective_divide(inverseViewProjection * in)); }; } diff --git a/gfx/gl/camera.h b/gfx/gl/camera.h index 94fd48b..9685a7d 100644 --- a/gfx/gl/camera.h +++ b/gfx/gl/camera.h @@ -61,9 +61,9 @@ public: return position; } - std::array extentsAtDist(float) const; + [[nodiscard]] std::array extentsAtDist(float) const; - static glm::vec3 upFromForward(const glm::vec3 & forward); + [[nodiscard]] static glm::vec3 upFromForward(const glm::vec3 & forward); private: void updateView(); diff --git a/gfx/gl/program.h b/gfx/gl/program.h index fd9edbf..787a53c 100644 --- a/gfx/gl/program.h +++ b/gfx/gl/program.h @@ -4,6 +4,7 @@ #include #include #include +#include class Location; @@ -16,10 +17,12 @@ public: linkAndValidate(); } virtual ~Program() = default; + DEFAULT_MOVE_NO_COPY(Program); class UniformLocation { public: UniformLocation(GLuint prog, const char * name); + // NOLINTNEXTLINE(hicpp-explicit-conversions) operator auto() const { return location; @@ -34,6 +37,7 @@ public: RequiredUniformLocation(GLuint prog, const char * name); }; + // NOLINTNEXTLINE(hicpp-explicit-conversions) operator GLuint() const { return m_program; diff --git a/gfx/gl/sceneProvider.h b/gfx/gl/sceneProvider.h index ef269b3..1d9ba57 100644 --- a/gfx/gl/sceneProvider.h +++ b/gfx/gl/sceneProvider.h @@ -1,12 +1,17 @@ #pragma once +#include + class SceneRenderer; class ShadowMapper; class SceneShader; class SceneProvider { public: + SceneProvider() = default; virtual ~SceneProvider() = default; + DEFAULT_MOVE_COPY(SceneProvider); + virtual void content(const SceneShader &) const = 0; virtual void environment(const SceneShader &, const SceneRenderer &) const; virtual void lights(const SceneShader &) const = 0; diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index e7ce049..31b01ff 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -27,7 +27,7 @@ SceneRenderer::SceneRenderer(glm::ivec2 s, GLuint o) : glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); for (const auto format : formats) { - glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, GL_RGB, GL_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, GL_RGB, GL_BYTE, nullptr); glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, data, 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) { diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h index a20636d..c1e3981 100644 --- a/gfx/gl/shader.h +++ b/gfx/gl/shader.h @@ -10,7 +10,7 @@ public: using ShaderRef = glRef; constexpr Shader(const GLchar * text, GLint len, GLuint type) : text {text}, len {len}, type {type} { } - ShaderRef compile() const; + [[nodiscard]] ShaderRef compile() const; static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string_view errorMessage); private: diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 01043e6..1787ee6 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -17,7 +17,7 @@ ShadowMapper::ShadowMapper(const glm::ivec2 & s) : size {s} { glBindTexture(GL_TEXTURE_2D, depthMap); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index 2805d64..b4793ed 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -14,11 +14,11 @@ public: static constexpr std::size_t SHADOW_BANDS {4}; struct Definitions { - std::array projections; - std::array regions; + std::array projections {}; + std::array regions {}; size_t maps {}; }; - Definitions update(const SceneProvider &, const glm::vec3 & direction, const Camera &) const; + [[nodiscard]] Definitions update(const SceneProvider &, const glm::vec3 & direction, const Camera &) const; class FixedPoint : public Program { public: @@ -42,6 +42,7 @@ public: FixedPoint fixedPoint; DynamicPoint dynamicPoint; + // NOLINTNEXTLINE(hicpp-explicit-conversions) operator GLuint() const { return depthMap; diff --git a/gfx/gl/uiShader.h b/gfx/gl/uiShader.h index ee44af7..054c944 100644 --- a/gfx/gl/uiShader.h +++ b/gfx/gl/uiShader.h @@ -15,9 +15,10 @@ private: class UIProgram : public Program { public: - template UIProgram(const glm::mat4 & vp, S &&... srcs) : Program {std::forward(srcs)...} + template + explicit UIProgram(const glm::mat4 & vp, S &&... srcs) : Program {std::forward(srcs)...} { - RequiredUniformLocation uiProjectionLoc {*this, "uiProjection"}; + const RequiredUniformLocation uiProjectionLoc {*this, "uiProjection"}; glUseProgram(*this); glUniformMatrix4fv(uiProjectionLoc, 1, GL_FALSE, glm::value_ptr(vp)); } diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index 942cedf..ef6d7e7 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -48,8 +48,8 @@ Texture::save(const glTexture & texture, GLenum format, GLenum type, const glm:: { using TGAHead = std::array; - size_t dataSize = (static_cast(size.x * size.y * channels)); - size_t fileSize = dataSize + sizeof(TGAHead); + const size_t dataSize = (static_cast(size.x * size.y * channels)); + const size_t fileSize = dataSize + sizeof(TGAHead); filesystem::fh out {path, O_RDWR | O_CREAT, 0660}; out.truncate(fileSize); diff --git a/lib/enumDetails.hpp b/lib/enumDetails.hpp index 7234c6d..5966be2 100644 --- a/lib/enumDetails.hpp +++ b/lib/enumDetails.hpp @@ -79,7 +79,10 @@ private: constexpr static auto get_valids(std::integer_sequence) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" return std::array {EnumValueDetails(n)>::valid...}; +#pragma GCC diagnostic pop } template constexpr static auto @@ -103,7 +106,7 @@ private: -> std::optional::value_type> { if (const auto itr = std::find(search.begin(), search.end(), key); itr != search.end()) { - return out[std::distance(search.begin(), itr)]; + return out[static_cast(std::distance(search.begin(), itr))]; } return std::nullopt; } diff --git a/lib/filesystem.cpp b/lib/filesystem.cpp index 181fb07..0e19e8d 100644 --- a/lib/filesystem.cpp +++ b/lib/filesystem.cpp @@ -36,6 +36,7 @@ namespace filesystem { } } + // NOLINTNEXTLINE(hicpp-vararg) fh::fh(const char * path, int flags, int mode) : h {open(path, flags, mode)} { if (h == -1) { diff --git a/test/Jamfile.jam b/test/Jamfile.jam index 72ec0e3..cefad7b 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -10,14 +10,33 @@ project : requirements BOOST_TEST_DYN_LINK RESDIR=\\\"$(res)/\\\" FIXTURESDIR=\\\"$(fixtures)/\\\" + debug:pedantic + debug:on + debug:-Wnon-virtual-dtor + debug:-Wcast-align + debug:-Wunused + debug:-Woverloaded-virtual + debug:-Wpedantic + debug:-Wconversion + debug:-Wsign-conversion + debug:-Wnull-dereference + debug:-Wdouble-promotion + debug:-Wformat=2 + gcc,debug:-Wold-style-cast + gcc,debug:-Wduplicated-cond + gcc,debug:-Wduplicated-branches + gcc,debug:-Wlogical-op + gcc,debug:-Wuseless-cast boost_unit_test_framework ..//ilt - tidy:hicpp-vararg tidy:accessMoved tidy:assertWithSideEffect + tidy:bugprone-macro-parentheses tidy:bugprone-use-after-move - tidy:hicpp-invalid-access-moved tidy:clang-analyzer-cplusplus.Move + tidy:clang-analyzer-security.FloatLoopCounter + tidy:hicpp-invalid-access-moved + tidy:hicpp-vararg tidy:boost ; lib test : [ glob *.cpp : test-*.cpp ] ; diff --git a/test/test-collection.cpp b/test/test-collection.cpp index f31b3a6..9884812 100644 --- a/test/test-collection.cpp +++ b/test/test-collection.cpp @@ -79,7 +79,7 @@ BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_CASE(wrapped_ptr_file_cons) { using FilePtr = wrapped_ptr; - FilePtr fp {fopen, "/dev/null", "r"}; + const FilePtr fp {fopen, "/dev/null", "r"}; BOOST_REQUIRE(fp); BOOST_CHECK_NO_THROW(fflush(fp)); @@ -108,7 +108,7 @@ BOOST_AUTO_TEST_CASE(wrapped_ptr_file_move) BOOST_AUTO_TEST_CASE(wrapped_ptr_file_typed) { using FilePtr = wrapped_ptrt; - FilePtr fp {"/dev/null", "r"}; + const FilePtr fp {"/dev/null", "r"}; BOOST_REQUIRE(fp); BOOST_CHECK_NO_THROW(fflush(fp)); } diff --git a/test/test-enumDetails.cpp b/test/test-enumDetails.cpp index c803cf8..db2d6d0 100644 --- a/test/test-enumDetails.cpp +++ b/test/test-enumDetails.cpp @@ -1,6 +1,7 @@ #define BOOST_TEST_MODULE test_enumDetails #include "enumDetailsData.hpp" +#include "testHelpers.h" #include #include #include @@ -32,14 +33,16 @@ constexpr std::array INVALID_VALUES {-1, 3, 20}; BOOST_DATA_TEST_CASE(valid_parse_##TYPE, VALID_NAMES ^ VALID_VALUES, in, out) \ { \ const auto v = EnumDetails::parse(in); \ - BOOST_REQUIRE(v.has_value()); \ - BOOST_CHECK_EQUAL(v.value(), out); \ + BOOST_CHECK_IF(vo, v.has_value()) { \ + BOOST_CHECK_EQUAL(v.value(), out); \ + } \ } \ BOOST_DATA_TEST_CASE(valid_to_string_##TYPE, VALID_VALUES ^ VALID_NAMES, in, out) \ { \ const auto v = EnumDetails::to_string(in); \ - BOOST_CHECK(v.has_value()); \ - BOOST_CHECK_EQUAL(v.value(), out); \ + BOOST_CHECK_IF(vo, v.has_value()) { \ + BOOST_CHECK_EQUAL(v.value(), out); \ + } \ } TESTS_FOR_TYPE(GlobalScoped) diff --git a/test/test-geo.cpp b/test/test-geo.cpp index 7f072bc..bc442e9 100644 --- a/test/test-geo.cpp +++ b/test/test-geo.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_MODULE test_geo -#include "test-helpers.hpp" +#include "testHelpers.h" #include #include #include @@ -169,8 +169,9 @@ BOOST_DATA_TEST_CASE(intersect_ray, nodes[at(1, 1)].height = 4; const auto intersect = intersectRay({start, glm::normalize(dir)}); - BOOST_REQUIRE(intersect); - BOOST_CHECK_CLOSE_VEC(*intersect, pos); + BOOST_CHECK_IF(has_intersect, intersect) { + BOOST_CHECK_CLOSE_VEC(*intersect, pos); + } } auto xs = boost::unit_test::data::xrange(-20.F, 0.F, 0.6F), ys = boost::unit_test::data::xrange(-20.F, 0.F, 0.7F); @@ -185,13 +186,14 @@ BOOST_DATA_TEST_CASE(intersect_ray_many, xs * ys * targetsx * targetsy, x, y, ta nodes[at(0, 1)].height = 3; nodes[at(1, 1)].height = 4; - glm::vec3 start {x, y, 10}; + const glm::vec3 start {x, y, 10}; const auto target {this->positionAt({targetx, targety})}; - Ray ray {start, glm::normalize(target - start)}; + const Ray ray {start, glm::normalize(target - start)}; BOOST_TEST_CONTEXT(ray) { const auto intersect = intersectRay(ray); - BOOST_REQUIRE(intersect); - BOOST_CHECK_CLOSE_VEC(*intersect, target); + BOOST_CHECK_IF(has_intersect, intersect) { + BOOST_CHECK_CLOSE_VEC(*intersect, target); + } } } diff --git a/test/test-glContextBhvr.cpp b/test/test-glContextBhvr.cpp index b37a072..9716f54 100644 --- a/test/test-glContextBhvr.cpp +++ b/test/test-glContextBhvr.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_MODULE test_glcontextbehaviour -#include "test-helpers.hpp" +#include "testHelpers.h" #include #include @@ -10,23 +10,23 @@ BOOST_GLOBAL_FIXTURE(ApplicationBase); -#define TEST_WINDOW_PARAMS __FILE__, 0, 0, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN +#define TEST_WINDOW_PARAMS __FILE__, 0, 0, 640, 480, static_cast(SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN) static void CreateProgramTest() { - ProgramRef p; + const ProgramRef p; BOOST_REQUIRE(p); } BOOST_AUTO_TEST_CASE(windowContextThingsBehaviour1) { BOOST_REQUIRE(!glCreateProgram); // Init not called yet { - SDL_WindowPtr window {TEST_WINDOW_PARAMS}; + const SDL_WindowPtr window {TEST_WINDOW_PARAMS}; BOOST_REQUIRE(window); BOOST_REQUIRE(!glCreateProgram); BOOST_REQUIRE_NE(glewInit(), GLEW_OK); // No context yet { - SDL_GLContextPtr context {window}; + const SDL_GLContextPtr context {window}; BOOST_REQUIRE(context); BOOST_REQUIRE(!glCreateProgram); BOOST_REQUIRE_EQUAL(glewInit(), GLEW_OK); @@ -34,17 +34,17 @@ BOOST_AUTO_TEST_CASE(windowContextThingsBehaviour1) CreateProgramTest(); } // Context destroyed BOOST_REQUIRE(glCreateProgram); // Functions still set - BOOST_REQUIRE_THROW({ ProgramRef p; }, std::exception); // Get fails with no context + BOOST_REQUIRE_THROW({ const ProgramRef p; }, std::exception); // Get fails with no context { - SDL_GLContextPtr context {window}; + const SDL_GLContextPtr context {window}; BOOST_REQUIRE(context); CreateProgramTest(); } } { - SDL_WindowPtr window {TEST_WINDOW_PARAMS}; + const SDL_WindowPtr window {TEST_WINDOW_PARAMS}; BOOST_REQUIRE(window); - SDL_GLContextPtr context {window}; + const SDL_GLContextPtr context {window}; BOOST_REQUIRE(context); CreateProgramTest(); } @@ -52,31 +52,31 @@ BOOST_AUTO_TEST_CASE(windowContextThingsBehaviour1) BOOST_AUTO_TEST_CASE(windowContextThingsBehaviour2) { - SDL_WindowPtr window1 {TEST_WINDOW_PARAMS}; + const SDL_WindowPtr window1 {TEST_WINDOW_PARAMS}; BOOST_REQUIRE(window1); { - SDL_WindowPtr window2 {TEST_WINDOW_PARAMS}; + const SDL_WindowPtr window2 {TEST_WINDOW_PARAMS}; BOOST_REQUIRE(window2); - SDL_GLContextPtr context {window2}; + const SDL_GLContextPtr context {window2}; BOOST_REQUIRE(context); CreateProgramTest(); } - BOOST_REQUIRE_THROW({ ProgramRef p; }, std::exception); // Get fails with no context + BOOST_REQUIRE_THROW({ const ProgramRef p; }, std::exception); // Get fails with no context } BOOST_AUTO_TEST_CASE(windowContextThingsBehaviour3) { std::optional window1 {std::in_place, TEST_WINDOW_PARAMS}; - std::optional window2 {std::in_place, TEST_WINDOW_PARAMS}; + const std::optional window2 {std::in_place, TEST_WINDOW_PARAMS}; BOOST_REQUIRE(window1); BOOST_REQUIRE(window1.value()); - SDL_GLContextPtr context {window1.value()}; + const SDL_GLContextPtr context {window1.value()}; BOOST_REQUIRE(context); CreateProgramTest(); window1.reset(); - BOOST_REQUIRE_THROW({ ProgramRef p; }, std::exception); // Get fails with context's window gone + BOOST_REQUIRE_THROW({ const ProgramRef p; }, std::exception); // Get fails with context's window gone window1.emplace(TEST_WINDOW_PARAMS); BOOST_REQUIRE(window1); BOOST_REQUIRE(window1.value()); - BOOST_REQUIRE_THROW({ ProgramRef p; }, std::exception); // Get still fails with context's window gone + BOOST_REQUIRE_THROW({ const ProgramRef p; }, std::exception); // Get still fails with context's window gone } diff --git a/test/test-helpers.hpp b/test/test-helpers.hpp deleted file mode 100644 index 5ebbea9..0000000 --- a/test/test-helpers.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -#define BOOST_CHECK_CLOSE_VEC(a_, b_) \ - { \ - const auto a {a_}, b {b_}; \ - BOOST_TEST_CONTEXT("BOOST_CHECK_CLOSE_VEC(" << a << ", " << b << ")") { \ - BOOST_CHECK_LT(glm::length(a - b), 0.1F); \ - } \ - } - -#define BOOST_CHECK_BETWEEN(a_, b_, c_) \ - { \ - const auto a {a_}, b {b_}, c {c_}; \ - BOOST_TEST_CONTEXT("BOOST_CHECK_BETWEEN(" << a << ", " << b << ", " << c << ")") { \ - BOOST_CHECK_LE(b, a); \ - BOOST_CHECK_GE(c, a); \ - } \ - } diff --git a/test/test-lib.cpp b/test/test-lib.cpp index e464f2f..73ed53a 100644 --- a/test/test-lib.cpp +++ b/test/test-lib.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_MODULE test_lib -#include "test-helpers.hpp" +#include "testHelpers.h" #include #include @@ -33,7 +33,7 @@ using TestArray = glArrays<5, &generator, &deleter>; BOOST_AUTO_TEST_CASE(generate_and_delete) { { - TestArray a; + const TestArray a; } BOOST_CHECK(active.empty()); } @@ -43,7 +43,7 @@ BOOST_AUTO_TEST_CASE(generate_move_and_delete) { TestArray a; BOOST_CHECK_EQUAL(TestArray::size, active.size()); - TestArray b {std::move(a)}; + const TestArray b {std::move(a)}; BOOST_CHECK_EQUAL(TestArray::size, active.size()); } BOOST_CHECK(active.empty()); diff --git a/test/test-maths.cpp b/test/test-maths.cpp index d6c0fc1..6ee2113 100644 --- a/test/test-maths.cpp +++ b/test/test-maths.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_MODULE test_maths -#include "test-helpers.hpp" +#include "testHelpers.h" #include #include #include @@ -187,7 +187,7 @@ BOOST_DATA_TEST_CASE(straight1, }), v, angFor, angBack) { - TestLinkStraight l(v); + const TestLinkStraight l(v); { const auto p = l.positionAt(0, 0); BOOST_CHECK_EQUAL(p.pos, origin); @@ -220,7 +220,7 @@ BOOST_DATA_TEST_CASE(curve1, e1, ctr, angFor, angBack) { { // One-way... - TestLinkCurve l(origin, e1, ctr); + const TestLinkCurve l(origin, e1, ctr); BOOST_CHECK_EQUAL(l.radius, 1.F); { const auto p = l.positionAt(0, 0); @@ -235,7 +235,7 @@ BOOST_DATA_TEST_CASE(curve1, } { // The other way... - TestLinkCurve l(e1, origin, ctr); + const TestLinkCurve l(e1, origin, ctr); BOOST_CHECK_EQUAL(l.radius, 1.F); { const auto p = l.positionAt(0, 0); @@ -307,6 +307,15 @@ BOOST_DATA_TEST_CASE(rayLineDistance, n_test_points_between(), // cz i1, n1x, i2, n1y, i3, n1z, i4, n2x, i5, n2y, i6, n2z, i7, cx, i8, cy, i9, cz) { + (void)i1; + (void)i2; + (void)i3; + (void)i4; + (void)i5; + (void)i6; + (void)i7; + (void)i8; + (void)i9; const glm::vec3 n1 {n1x, n1y, n1z}, n2 {n2x, n2y, n2z}, c {cx, cy, cz}; const auto nstep = n2 - n1; diff --git a/test/test-network.cpp b/test/test-network.cpp index dc2b52f..19c0977 100644 --- a/test/test-network.cpp +++ b/test/test-network.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_MODULE network -#include "test-helpers.hpp" +#include "testHelpers.h" #include #include @@ -19,7 +19,7 @@ #include struct TestLink : public LinkStraight { - TestLink(Node::Ptr a, Node::Ptr b) : TestLink {a, b, (a->pos - b->pos)} { } + TestLink(const Node::Ptr & a, const Node::Ptr & b) : TestLink {a, b, (a->pos - b->pos)} { } TestLink(Node::Ptr a, Node::Ptr b, glm::vec2 l) : Link {{std::move(a), 0}, {std::move(b), pi}, glm::length(l)} { } TestLink(Node::Ptr a, Node::Ptr b, float l) : Link {{std::move(a), 0}, {std::move(b), pi}, l} { } using StraightLink = TestLink; diff --git a/test/test-obj.cpp b/test/test-obj.cpp index c7bd6ce..e6e725d 100644 --- a/test/test-obj.cpp +++ b/test/test-obj.cpp @@ -12,7 +12,7 @@ BOOST_AUTO_TEST_CASE(objparse) { - ObjParser op {RESDIR "/brush47.obj"}; + const ObjParser op {RESDIR "/brush47.obj"}; BOOST_CHECK_EQUAL(75, op.vertices.size()); BOOST_CHECK_EQUAL(112, op.texCoords.size()); BOOST_CHECK_EQUAL(31, op.normals.size()); @@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE(objparse) BOOST_AUTO_TEST_CASE(create_meshes) { - ObjParser op {RESDIR "/brush47.obj"}; + const ObjParser op {RESDIR "/brush47.obj"}; const auto ms = op.createMeshData(); BOOST_REQUIRE_EQUAL(3, ms.size()); BOOST_REQUIRE_EQUAL("Body", ms.begin()->first); diff --git a/test/test-render.cpp b/test/test-render.cpp index 09aa9fb..7db847d 100644 --- a/test/test-render.cpp +++ b/test/test-render.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_MODULE test_render -#include "test-helpers.hpp" +#include "testHelpers.h" #include "testMainWindow.h" #include "testRenderOutput.h" #include @@ -26,18 +26,18 @@ class TestScene : public SceneProvider { return gd; }()}; void - content(const SceneShader & shader) const + content(const SceneShader & shader) const override { terrain.render(shader); train.render(shader, Location {{52, 50, 2}}, {Location {}, Location {}}); train.render(shader, Location {{52, 30, 2}}, {Location {}, Location {}}); } void - lights(const SceneShader &) const + lights(const SceneShader &) const override { } void - shadows(const ShadowMapper & shadowMapper) const + shadows(const ShadowMapper & shadowMapper) const override { terrain.shadows(shadowMapper); train.shadows(shadowMapper, Location {{52, 50, 2}}); @@ -54,7 +54,7 @@ BOOST_DATA_TEST_CASE(cam, dist, near, far) { static constexpr glm::vec4 pos {-10, -10, 60, 0}; - Camera cam {pos, half_pi, 1.f, near, far}; + const Camera cam {pos, half_pi, 1.F, near, far}; const auto e = cam.extentsAtDist(dist); @@ -70,7 +70,7 @@ BOOST_AUTO_TEST_CASE(basic) { SceneRenderer ss {size, output}; ss.camera.setView({-10, -10, 60}, glm::normalize(glm::vec3 {1, 1, -0.5F})); - TestScene scene; + const TestScene scene; ss.render(scene); glDisable(GL_DEBUG_OUTPUT); Texture::save(outImage, size, "/tmp/basic.tga"); @@ -93,12 +93,12 @@ BOOST_AUTO_TEST_CASE(pointlight) { for (int x = 50; x < 100; x += 20) { for (int y = 50; y < 2000; y += 20) { - shader.pointLight.add({x, y, 4}, {1.0, 1.0, 1.0}, 0.1); + shader.pointLight.add({x, y, 4}, {1.0, 1.0, 1.0}, 0.1F); } } } }; - PointLightScene scene; + const PointLightScene scene; ss.render(scene); glDisable(GL_DEBUG_OUTPUT); Texture::save(outImage, size, "/tmp/pointlight.tga"); @@ -119,13 +119,13 @@ BOOST_AUTO_TEST_CASE(spotlight) void lights(const SceneShader & shader) const override { - shader.spotLight.add({50, 50, 15}, down, {1.0, 1.0, 1.0}, 0.01, 1); - shader.spotLight.add({51, 59.5, 1}, north, {1.0, 1.0, 1.0}, 0.001, .5); - shader.spotLight.add({53, 59.5, 1}, north, {1.0, 1.0, 1.0}, 0.001, .5); - shader.spotLight.add({60, 50, 3}, north + east, {1.0, 1.0, 1.0}, 0.0001, .7); + shader.spotLight.add({50, 50, 15}, down, {1.0, 1.0, 1.0}, 0.01F, 1); + shader.spotLight.add({51, 59.5, 1}, north, {1.0, 1.0, 1.0}, 0.001F, .5); + shader.spotLight.add({53, 59.5, 1}, north, {1.0, 1.0, 1.0}, 0.001F, .5); + shader.spotLight.add({60, 50, 3}, north + east, {1.0, 1.0, 1.0}, 0.0001F, .7F); } }; - PointLightScene scene; + const PointLightScene scene; ss.render(scene); glDisable(GL_DEBUG_OUTPUT); Texture::save(outImage, size, "/tmp/spotlight.tga"); diff --git a/test/test-text.cpp b/test/test-text.cpp index 0df6885..0c73021 100644 --- a/test/test-text.cpp +++ b/test/test-text.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_MODULE test_text -#include "test-helpers.hpp" +#include "testHelpers.h" #include #include #include @@ -58,7 +58,7 @@ BOOST_AUTO_TEST_CASE(initialize_chardata) BOOST_CHECK_EQUAL(fontTextures.size(), 2); } -using CharDataTest = std::tuple; +using CharDataTest = std::tuple; BOOST_DATA_TEST_CASE(initialize_chardata_A, boost::unit_test::data::make({ {'A', {0, {34, 35}, {627, 0}, {-1, 35}, 32}}, @@ -82,7 +82,7 @@ static_assert(glm::vec2 {866, 35} / glm::vec2 {2048, 64} == glm::vec2 {0.4228515 BOOST_AUTO_TEST_CASE(render_text) { constexpr std::string_view text {"I Like Trains"}; - const auto spaces = std::count_if(text.begin(), text.end(), isspace); + const auto spaces = static_cast(std::count_if(text.begin(), text.end(), isspace)); const auto tqs = render(text); BOOST_REQUIRE_EQUAL(tqs.size(), 1); const auto & t1 = tqs.begin(); diff --git a/test/testHelpers.cpp b/test/testHelpers.cpp new file mode 100644 index 0000000..c69e5f3 --- /dev/null +++ b/test/testHelpers.cpp @@ -0,0 +1,16 @@ +#include "testHelpers.h" +#include + +std::unique_ptr +uasprintf(const char * fmt, ...) +{ + char * buf {}; + va_list args; + va_start(args, fmt); + if (vasprintf(&buf, fmt, args) < 0) { + va_end(args); + return {nullptr, &free}; + } + va_end(args); + return std::unique_ptr {buf, &free}; +} diff --git a/test/testHelpers.h b/test/testHelpers.h new file mode 100644 index 0000000..54b7fc6 --- /dev/null +++ b/test/testHelpers.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include +#include + +std::unique_ptr uasprintf(const char * fmt, ...) __attribute__((format(printf, 1, 2))); + +#define BOOST_CHECK_CLOSE_VEC(a_, b_) \ + { \ + const auto a {a_}, b {b_}; \ + BOOST_TEST_CONTEXT("BOOST_CHECK_CLOSE_VEC(" << a << ", " << b << ")") { \ + BOOST_CHECK_LT(glm::length(a - b), 0.1F); \ + } \ + } + +#define BOOST_CHECK_BETWEEN(a_, b_, c_) \ + { \ + const auto a {a_}, b {b_}, c {c_}; \ + BOOST_TEST_CONTEXT("BOOST_CHECK_BETWEEN(" << a << ", " << b << ", " << c << ")") { \ + BOOST_CHECK_LE(b, a); \ + BOOST_CHECK_GE(c, a); \ + } \ + } +#define BOOST_REQUIRE_THEN(VAR, EXPR) \ + if (const auto VAR = (EXPR); !(VAR)) { \ + BOOST_REQUIRE(VAR); \ + } \ + else +#define BOOST_CHECK_IF(VAR, EXPR) \ + if (const auto VAR = (EXPR); !(VAR)) { \ + BOOST_CHECK(VAR); \ + } \ + else diff --git a/test/testMainWindow.cpp b/test/testMainWindow.cpp index 66e8832..49e18f1 100644 --- a/test/testMainWindow.cpp +++ b/test/testMainWindow.cpp @@ -1,14 +1,14 @@ #include "testMainWindow.h" +#include "testHelpers.h" #include TestMainWindow::TestMainWindow() : Window {1, 1, __FILE__, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN} { glEnable(GL_DEBUG_OUTPUT); glDebugMessageCallback( - [](GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar * message, - const void *) { - char buf[BUFSIZ]; - snprintf(buf, BUFSIZ, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s", + [](GLenum /*source*/, GLenum type, GLuint /*id*/, GLenum severity, GLsizei /*length*/, + const GLchar * message, const void *) { + const auto msg = uasprintf("GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s", (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), type, severity, message); switch (type) { case GL_DEBUG_TYPE_ERROR: @@ -16,9 +16,10 @@ TestMainWindow::TestMainWindow() : Window {1, 1, __FILE__, SDL_WINDOW_OPENGL | S case GL_DEBUG_TYPE_PORTABILITY: case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: - BOOST_TEST_ERROR(buf); + BOOST_TEST_ERROR(msg.get()); + default: + BOOST_TEST_MESSAGE(msg.get()); } - BOOST_TEST_MESSAGE(buf); }, nullptr); } diff --git a/test/testRenderOutput.cpp b/test/testRenderOutput.cpp index 7a96e96..2a94a1b 100644 --- a/test/testRenderOutput.cpp +++ b/test/testRenderOutput.cpp @@ -7,7 +7,7 @@ TestRenderOutput::TestRenderOutput() : size {640, 480} const auto configuregdata = [this](const GLuint data, const GLint format, const GLenum type, const GLenum attachment) { glBindTexture(GL_TEXTURE_2D, data); - glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, GL_RGBA, type, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, GL_RGBA, type, nullptr); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, data, 0); diff --git a/ui/font.cpp b/ui/font.cpp index fc66d52..43991f0 100644 --- a/ui/font.cpp +++ b/ui/font.cpp @@ -82,7 +82,7 @@ Font::generateChars(const utf8_string_view chars) const face.emplace(*ft, path.c_str()); FT_Set_Pixel_Sizes(*face, 0, size.z); } - FT_UInt glyph_index = FT_Get_Char_Index(*face, codepoint); + const FT_UInt glyph_index = FT_Get_Char_Index(*face, codepoint); if (FT_Load_Glyph(*face, glyph_index, FT_LOAD_RENDER)) { charsData.emplace(codepoint, CharData {}); continue; -- cgit v1.2.3