Browse Source

also some tests

pull/432/head
sariug 6 years ago
parent
commit
8f73873d41
  1. 15
      src/Magnum/Math/Random.h
  2. 6
      src/Magnum/Math/Test/CMakeLists.txt
  3. 112
      src/Magnum/Math/Test/RandomTest.cpp

15
src/Magnum/Math/Random.h

@ -11,6 +11,7 @@
#include "Magnum/Math/Vector3.h"
#include "Magnum/Math/Quaternion.h"
#include "Magnum/Math/Functions.h"
#include <iostream>
namespace Magnum
{
@ -70,7 +71,7 @@ T randomSignedScalar() // range [-1, 1]
}
template <class T = Float>
Vector3<T> randomUnitVector2()
Vector2<T> randomUnitVector2()
{
auto a = Implementation::RandomGenerator::generate(0.0f, 2 * Math::Constants<T>::pi());
return {std::cos(a), std::sin(a)};
@ -100,17 +101,23 @@ Vector2<T> randomPointInACircle() // always length < 1
template <class T = Float>
Vector3<T> randomPointInASphere() // always length < 1
{
auto x = randomSignedScalar();
auto y = randomSignedScalar();
while (true)
{
auto p = Vector3<T>(x, y, randomSignedScalar());
auto p = Vector3<T>(randomSignedScalar(),
randomSignedScalar(),
randomSignedScalar());
if (p.length() >= 1)
continue;
return p;
}
}
bool randomBool()
{
return static_cast<bool>(randomUnsignedScalar<Int>());
}
template <class T = Float>
Quaternion<T> randomRotation()
{

6
src/Magnum/Math/Test/CMakeLists.txt

@ -70,6 +70,8 @@ corrade_add_test(MathStrictWeakOrderingTest StrictWeakOrderingTest.cpp LIBRARIES
corrade_add_test(MathMatrixBenchmark MatrixBenchmark.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(MathRandomTest RandomTest.cpp LIBRARIES MagnumMathTestLib)
set_property(TARGET
MathVectorTest
MathMatrixTest
@ -85,6 +87,8 @@ set_property(TARGET
MathDistanceTest
MathIntersectionTest
MathRandomTest
APPEND PROPERTY COMPILE_DEFINITIONS "CORRADE_GRACEFUL_ASSERT")
set_target_properties(
@ -130,4 +134,6 @@ set_target_properties(
MathConfigurationValueTest
MathStrictWeakOrderingTest
MathRandomTest
PROPERTIES FOLDER "Magnum/Math/Test")

112
src/Magnum/Math/Test/RandomTest.cpp

@ -0,0 +1,112 @@
#include <Corrade/TestSuite/Tester.h>
#include <Corrade/Utility/DebugStl.h>
#include "Magnum/Math/Random.h"
namespace Magnum
{
namespace Math
{
namespace Test
{
namespace
{
struct RandomTest : Corrade::TestSuite::Tester
{
explicit RandomTest();
void unsignedScalar();
void signedScalar();
void unitVector2();
void unitVector3();
void pointInACircle();
void pointInASphere();
void randomRotation();
};
typedef Vector<2, Float> Vector2;
typedef Vector<3, Float> Vector3;
typedef Math::Constants<Float> Constants;
RandomTest::RandomTest()
{
addTests({&RandomTest::unsignedScalar,
&RandomTest::signedScalar,
&RandomTest::unitVector2,
&RandomTest::unitVector3,
&RandomTest::pointInACircle,
&RandomTest::pointInASphere,
&RandomTest::randomRotation});
}
// void RandomTest::construct() {
// constexpr Matrix4x4 a = {Vector4(3.0f, 5.0f, 8.0f, -3.0f),
// Vector4(4.5f, 4.0f, 7.0f, 2.0f),
// Vector4(1.0f, 2.0f, 3.0f, -1.0f),
// Vector4(7.9f, -1.0f, 8.0f, -1.5f)};
// CORRADE_COMPARE(a, Matrix4x4(Vector4(3.0f, 5.0f, 8.0f, -3.0f),
// Vector4(4.5f, 4.0f, 7.0f, 2.0f),
// Vector4(1.0f, 2.0f, 3.0f, -1.0f),
// Vector4(7.9f, -1.0f, 8.0f, -1.5f)));
// CORRADE_VERIFY((std::is_nothrow_constructible<Matrix4x4, Vector4, Vector4, Vector4, Vector4>::value));
// }
void RandomTest::unsignedScalar()
{
for (std::size_t i = 0; i < 10; i++)
{
CORRADE_VERIFY(Math::Random::randomUnsignedScalar<Float>() < 1.000000001);
CORRADE_VERIFY(Math::Random::randomUnsignedScalar<Float>() > -.000000001);
}
}
void RandomTest::signedScalar()
{
for (std::size_t i = 0; i < 10; i++)
{
CORRADE_VERIFY(Math::Random::randomUnsignedScalar<Float>() < 1.000000001);
CORRADE_VERIFY(Math::Random::randomUnsignedScalar<Float>() > -1.000000001);
}
}
void RandomTest::unitVector2()
{
for (std::size_t i = 0; i < 10; i++)
{
CORRADE_COMPARE((Math::Random::randomUnitVector2()).length(),1.0f);
}
}
void RandomTest::unitVector3()
{
for (std::size_t i = 0; i < 10; i++)
{
CORRADE_COMPARE((Math::Random::randomUnitVector3()).length(),1.0f);
}
}
void RandomTest::pointInACircle(){
for (std::size_t i = 0; i < 10; i++)
{
CORRADE_VERIFY((Math::Random::randomPointInACircle()).length()<1.0f);
}
}
void RandomTest::pointInASphere(){
for (std::size_t i = 0; i < 10; i++)
{
CORRADE_VERIFY((Math::Random::randomPointInASphere()).length()<1.0f);
}
}
void RandomTest::randomRotation(){
for (std::size_t i = 0; i < 10; i++)
{
CORRADE_COMPARE(Math::Random::randomRotation().length(), 1.0f);
}
}
} // namespace
} // namespace Test
} // namespace Math
} // namespace Magnum
CORRADE_TEST_MAIN(Magnum::Math::Test::RandomTest)
Loading…
Cancel
Save