diff --git a/src/lol/math/bigint.h b/src/lol/math/bigint.h index 624a9a18..8b37e923 100644 --- a/src/lol/math/bigint.h +++ b/src/lol/math/bigint.h @@ -25,7 +25,7 @@ namespace lol { -/* This is OUR namespace. Don't let Windows headers fuck with it. */ +/* This is OUR namespace. Don't let Windows headers mess with it. */ #undef min #undef max diff --git a/src/lol/math/functions.h b/src/lol/math/functions.h index 21f68c96..39c119b4 100644 --- a/src/lol/math/functions.h +++ b/src/lol/math/functions.h @@ -29,7 +29,7 @@ namespace lol { -/* This is OUR namespace. Don't let Windows headers fuck with it. */ +/* This is OUR namespace. Don't let Windows headers mess with it. */ #undef min #undef max @@ -256,7 +256,8 @@ LOL_ATTR_NODISCARD static inline ldouble round(ldouble x) { return std::round(x) LOL_ATTR_NODISCARD static inline T min(T x, T y) { return std::min(x, y); } \ LOL_ATTR_NODISCARD static inline T max(T x, T y) { return std::max(x, y); } \ LOL_ATTR_NODISCARD static inline T clamp(T x, T y, T z) { return min(max(x, y), z); } \ - LOL_ATTR_NODISCARD static inline T saturate(T x) { return min(max(x, (T)0), (T)1); } + LOL_ATTR_NODISCARD static inline T saturate(T x) { return min(max(x, (T)0), (T)1); } \ + LOL_ATTR_NODISCARD static inline T gcd(T x, T y) { return y == (T)0 ? lol::abs(x) : lol::gcd(y, lol::fmod(x, y)); } #define LOL_GENERIC_FUNC_SIGNED(T) \ LOL_GENERIC_FUNC(T) \ diff --git a/src/lol/math/half.h b/src/lol/math/half.h index dcf3dbfe..bba0919e 100644 --- a/src/lol/math/half.h +++ b/src/lol/math/half.h @@ -24,7 +24,7 @@ namespace lol { -/* This is OUR namespace. Don't let Windows headers fuck with it. */ +/* This is OUR namespace. Don't let Windows headers mess with it. */ #undef min #undef max diff --git a/src/lol/math/real.h b/src/lol/math/real.h index f8cb4e02..4f22515e 100644 --- a/src/lol/math/real.h +++ b/src/lol/math/real.h @@ -24,7 +24,7 @@ namespace lol { -/* This is OUR namespace. Don't let Windows headers fuck with it. */ +/* This is OUR namespace. Don't let Windows headers mess with it. */ #undef min #undef max diff --git a/src/t/Makefile.am b/src/t/Makefile.am index 3ea42ff3..1dba7868 100644 --- a/src/t/Makefile.am +++ b/src/t/Makefile.am @@ -37,7 +37,7 @@ test_math_SOURCES = test-common.cpp \ math/cmplx.cpp math/half.cpp math/interp.cpp math/matrix.cpp \ math/quat.cpp math/rand.cpp math/real.cpp math/rotation.cpp \ math/trig.cpp math/vector.cpp math/polynomial.cpp math/noise/simplex.cpp \ - math/bigint.cpp math/sqt.cpp + math/bigint.cpp math/sqt.cpp math/numbers.cpp test_math_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/tools/lolunit test_math_DEPENDENCIES = @LOL_DEPS@ diff --git a/src/t/math/numbers.cpp b/src/t/math/numbers.cpp new file mode 100644 index 00000000..958de324 --- /dev/null +++ b/src/t/math/numbers.cpp @@ -0,0 +1,44 @@ +// +// Lol Engine — Unit tests for number theory +// +// Copyright © 2010—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#include + +#include + +namespace lol +{ + +lolunit_declare_fixture(gcd_test) +{ + lolunit_declare_test(gcd_int) + { + lolunit_assert_equal(1, lol::gcd(37, 600)); + lolunit_assert_equal(2, lol::gcd(4, 6)); + lolunit_assert_equal(2, lol::gcd(6, 4)); + lolunit_assert_equal(13, lol::gcd(13, 13)); + lolunit_assert_equal(20, lol::gcd(20, 100)); + lolunit_assert_equal(18913, lol::gcd(624129, 2061517)); + } + + lolunit_declare_test(gcd_negative) + { + lolunit_assert_equal(2, lol::gcd(4, -6)); + lolunit_assert_equal(2, lol::gcd(-4, 6)); + lolunit_assert_equal(2, lol::gcd(-4, -6)); + lolunit_assert_equal(2, lol::gcd(6, -4)); + lolunit_assert_equal(2, lol::gcd(-6, 4)); + lolunit_assert_equal(2, lol::gcd(-6, -4)); + } +}; + +} /* namespace lol */ + diff --git a/src/t/test-math.vcxproj b/src/t/test-math.vcxproj index a1ec3464..1fdc0b02 100644 --- a/src/t/test-math.vcxproj +++ b/src/t/test-math.vcxproj @@ -42,6 +42,7 @@ +