Contact.cpp

This is the googletest based unit testing file for the playrho::d2::Contact class.

/*
* Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "UnitTests.hpp"
using namespace playrho;
using namespace playrho::d2;
TEST(Contact, ByteSize)
{
switch (sizeof(Real))
{
case 4: EXPECT_EQ(sizeof(Contact), std::size_t(128)); break;
case 8: EXPECT_EQ(sizeof(Contact), std::size_t(192)); break;
case 16: EXPECT_EQ(sizeof(Contact), std::size_t(384)); break;
default: FAIL(); break;
}
}
TEST(Contact, Traits)
{
EXPECT_FALSE(std::is_default_constructible<Contact>::value);
EXPECT_TRUE(std::is_copy_constructible<Contact>::value);
EXPECT_FALSE(std::is_copy_assignable<Contact>::value);
}
TEST(Contact, Enabled)
{
const auto shape = DiskShapeConf{};
auto world = World{};
const auto bA = world.CreateBody(BodyConf{}.UseType(BodyType::Dynamic));
const auto bB = world.CreateBody(BodyConf{}.UseType(BodyType::Dynamic));
const auto fA = bA->CreateFixture(Shape{shape});
const auto fB = bB->CreateFixture(Shape{shape});
auto c = Contact{fA, 0u, fB, 0u};
EXPECT_TRUE(c.IsEnabled());
c.UnsetEnabled();
EXPECT_FALSE(c.IsEnabled());
c.SetEnabled();
EXPECT_TRUE(c.IsEnabled());
}
{
const auto shape = DiskShapeConf{};
auto world = World{};
const auto bA = world.CreateBody(BodyConf{}.UseType(BodyType::Dynamic));
const auto bB = world.CreateBody(BodyConf{}.UseType(BodyType::Dynamic));
const auto fA = bA->CreateFixture(Shape{shape});
const auto fB = bB->CreateFixture(Shape{shape});
const auto c = Contact{fA, 0u, fB, 0u};
bA->UnsetAwake();
ASSERT_FALSE(bA->IsAwake());
bB->UnsetAwake();
ASSERT_FALSE(bB->IsAwake());
EXPECT_TRUE(bA->IsAwake());
EXPECT_TRUE(bB->IsAwake());
}
{
const auto shape = DiskShapeConf{};
auto world = World{};
const auto bA = world.CreateBody(BodyConf{}.UseType(BodyType::Dynamic));
const auto bB = world.CreateBody(BodyConf{}.UseType(BodyType::Dynamic));
const auto fA = bA->CreateFixture(Shape{shape});
const auto fB = bB->CreateFixture(Shape{shape});
auto c = Contact{fA, 0u, fB, 0u};
ASSERT_GT(GetFriction(shape), Real(0));
ASSERT_NEAR(static_cast<double>(c.GetFriction()), static_cast<double>(Real{GetFriction(shape)}), 0.01);
c.SetFriction(GetFriction(shape) * Real(2));
ASSERT_NE(c.GetFriction(), GetFriction(shape));
EXPECT_NEAR(static_cast<double>(c.GetFriction()), static_cast<double>(Real{GetFriction(shape)}), 0.01);
}
{
const auto shape = DiskShapeConf{};
auto world = World{};
const auto bA = world.CreateBody(BodyConf{}.UseType(BodyType::Dynamic));
const auto bB = world.CreateBody(BodyConf{}.UseType(BodyType::Dynamic));
const auto fA = bA->CreateFixture(Shape{shape});
const auto fB = bB->CreateFixture(Shape{shape});
auto c = Contact{fA, 0u, fB, 0u};
ASSERT_EQ(GetRestitution(shape), Real(0));
ASSERT_EQ(c.GetRestitution(), GetRestitution(shape));
c.SetRestitution(Real(2));
ASSERT_NE(c.GetRestitution(), GetRestitution(shape));
EXPECT_EQ(c.GetRestitution(), GetRestitution(shape));
}