Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update stringFrom to support Point and Vector #1274

Merged
merged 9 commits into from
Mar 8, 2025
7 changes: 0 additions & 7 deletions NAS2D/Math/Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

#include "Vector.h"

#include <string>


namespace NAS2D
{
Expand Down Expand Up @@ -84,11 +82,6 @@ namespace NAS2D
{
return Point<NewBaseType>(*this);
}

explicit operator std::string() const
{
return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
}
};


Expand Down
7 changes: 0 additions & 7 deletions NAS2D/Math/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

#include "IsZero.h"

#include <string>


namespace NAS2D
{
Expand Down Expand Up @@ -125,11 +123,6 @@ namespace NAS2D
{
return Vector<NewBaseType>(*this);
}

explicit operator std::string() const
{
return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
}
};


Expand Down
3 changes: 2 additions & 1 deletion NAS2D/Resource/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "../Filesystem.h"
#include "../Utility.h"
#include "../StringFrom.h"
#include "../Math/MathUtils.h"
#include "../Math/PointInRectangleRange.h"

Expand Down Expand Up @@ -272,7 +273,7 @@ namespace
if (fontSurfaceSize != glyphSize * GlyphMatrixSize)
{
SDL_FreeSurface(fontSurface);
throw std::runtime_error("Unexpected font image size. Image dimensions " + std::string{fontSurfaceSize} + " must both be evenly divisible by " + std::to_string(GlyphMatrixSize));
throw std::runtime_error("Unexpected font image size. Image dimensions " + stringFrom(fontSurfaceSize) + " must both be evenly divisible by " + std::to_string(GlyphMatrixSize));
}

Font::FontInfo fontInfo;
Expand Down
3 changes: 2 additions & 1 deletion NAS2D/Resource/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "../Math/Rectangle.h"
#include "../Filesystem.h"
#include "../Utility.h"
#include "../StringFrom.h"

#if defined(__XCODE_BUILD__)
#include <GLEW/GLEW.h>
Expand Down Expand Up @@ -148,7 +149,7 @@ Color Image::pixelColor(Point<int> point) const
{
if (!Rectangle{{0, 0}, mSize}.contains(point))
{
throw std::runtime_error("Pixel coordinates out of bounds: " + std::string{point});
throw std::runtime_error("Pixel coordinates out of bounds: " + stringFrom(point));
}

if (!mSurface) { throw std::runtime_error("Image has no allocated surface"); }
Expand Down
18 changes: 18 additions & 0 deletions NAS2D/StringFrom.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

namespace NAS2D
{
template <typename BaseType> struct Point;
template <typename BaseType> struct Vector;


template <typename T>
std::string stringFrom(T value)
{
Expand All @@ -22,4 +26,18 @@ namespace NAS2D
return std::to_string(value);
}
}


template <typename BaseType>
std::string stringFrom(Point<BaseType> point)
{
return "(" + std::to_string(point.x) + ", " + std::to_string(point.y) + ")";
}


template <typename BaseType>
std::string stringFrom(Vector<BaseType> vector)
{
return "(" + std::to_string(vector.x) + ", " + std::to_string(vector.y) + ")";
}
}
6 changes: 0 additions & 6 deletions test/Math/Point.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ TEST(Point, to) {
EXPECT_EQ((NAS2D::Point<float>{1.0, 2.0}), (NAS2D::Point<int>{1, 2}.to<float>()));
}

TEST(Point, stringConversion) {
EXPECT_EQ("(0, 0)", (std::string{NAS2D::Point{0, 0}}));
EXPECT_EQ("(1, 0)", (std::string{NAS2D::Point{1, 0}}));
EXPECT_EQ("(0, 1)", (std::string{NAS2D::Point{0, 1}}));
}

TEST(Point, VectorPointAdd) {
EXPECT_EQ((NAS2D::Point{2, 3}), (NAS2D::Vector{1, 2} + NAS2D::Point<int>{1, 1}));
}
Expand Down
6 changes: 0 additions & 6 deletions test/Math/Vector.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,6 @@ TEST(Vector, to) {
EXPECT_EQ((NAS2D::Vector<float>{1.0, 2.0}), (NAS2D::Vector<int>{1, 2}.to<float>()));
}

TEST(Vector, stringConversion) {
EXPECT_EQ("(0, 0)", (std::string{NAS2D::Vector{0, 0}}));
EXPECT_EQ("(1, 0)", (std::string{NAS2D::Vector{1, 0}}));
EXPECT_EQ("(0, 1)", (std::string{NAS2D::Vector{0, 1}}));
}

TEST(Vector, scalarVectorMultiply) {
EXPECT_EQ((NAS2D::Vector{2, 4}), (2 * NAS2D::Vector{1, 2}));
}
Expand Down
30 changes: 26 additions & 4 deletions test/StringFrom.test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "NAS2D/StringFrom.h"

#include "NAS2D/Math/Point.h"
#include "NAS2D/Math/Vector.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>

Expand All @@ -21,13 +24,22 @@ namespace
}


TEST(StringFrom, stringFrom) {
TEST(StringFrom, implicitConversion) {
ImplicitStringConversionTestFixture implicitStringConversionTestFixture{"testString"};
EXPECT_EQ("testString", NAS2D::stringFrom(implicitStringConversionTestFixture));
}

TEST(StringFrom, string) {
EXPECT_EQ("SomeStringValue", NAS2D::stringFrom("SomeStringValue"));
EXPECT_EQ("SomeStringValue", NAS2D::stringFrom(std::string{"SomeStringValue"}));
}

TEST(StringFrom, boolean) {
EXPECT_EQ("false", NAS2D::stringFrom(false));
EXPECT_EQ("true", NAS2D::stringFrom(true));
}

TEST(StringFrom, integers) {
using signedChar = signed char;
using unsignedChar = unsigned char;
EXPECT_EQ("-1", NAS2D::stringFrom(signedChar{-1}));
Expand All @@ -51,13 +63,23 @@ TEST(StringFrom, stringFrom) {
EXPECT_EQ("-1", NAS2D::stringFrom(-1ll));
EXPECT_EQ("0", NAS2D::stringFrom(0ll));
EXPECT_EQ("1", NAS2D::stringFrom(1ull));
}

TEST(StringFrom, floatingPoint) {
// Ignore precision beyond one decimal place
EXPECT_THAT(NAS2D::stringFrom(0.0f), testing::StartsWith("0.0"));
EXPECT_THAT(NAS2D::stringFrom(0.0), testing::StartsWith("0.0"));
EXPECT_THAT(NAS2D::stringFrom(0.0l), testing::StartsWith("0.0"));
}

// Implicit string conversion
ImplicitStringConversionTestFixture implicitStringConversionTestFixture{"testString"};
EXPECT_EQ("testString", NAS2D::stringFrom(implicitStringConversionTestFixture));
TEST(StringFrom, point) {
EXPECT_EQ("(0, 0)", NAS2D::stringFrom(NAS2D::Point{0, 0}));
EXPECT_EQ("(1, 0)", NAS2D::stringFrom(NAS2D::Point{1, 0}));
EXPECT_EQ("(0, 1)", NAS2D::stringFrom(NAS2D::Point{0, 1}));
}

TEST(StringFrom, vector) {
EXPECT_EQ("(0, 0)", NAS2D::stringFrom(NAS2D::Vector{0, 0}));
EXPECT_EQ("(1, 0)", NAS2D::stringFrom(NAS2D::Vector{1, 0}));
EXPECT_EQ("(0, 1)", NAS2D::stringFrom(NAS2D::Vector{0, 1}));
}