-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from olitheolix/unit-test-example
Proof-of-Concept for Test Automation
- Loading branch information
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
cmake_minimum_required(VERSION 3.18) | ||
project(newton_tests) | ||
|
||
# ---------------------------------------------------------------------- | ||
# Newton Settings. | ||
# ---------------------------------------------------------------------- | ||
|
||
include_directories(../sdk/dCore) | ||
include_directories(../sdk/dNewton) | ||
include_directories(../sdk/dTinyxml) | ||
include_directories(../sdk/dCollision) | ||
include_directories(../sdk/dNewton/dJoints) | ||
include_directories(../sdk/dNewton/dModels) | ||
include_directories(../sdk/dNewton/dIkSolver) | ||
include_directories(../sdk/dNewton/dParticles) | ||
include_directories(../sdk/dNewton/dModels/dVehicle) | ||
include_directories(../sdk/dNewton/dModels/dCharacter) | ||
|
||
# ---------------------------------------------------------------------- | ||
# Google Test Settings. | ||
# Adopted from https://google.github.io/googletest/quickstart-cmake.html | ||
# ---------------------------------------------------------------------- | ||
|
||
# GoogleTest requires at least C++14 | ||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
include(FetchContent) | ||
FetchContent_Declare( | ||
googletest | ||
GIT_REPOSITORY https://github.com/google/googletest.git | ||
GIT_TAG release-1.12.1 | ||
) | ||
|
||
# For Windows: Prevent overriding the parent project's compiler/linker settings | ||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | ||
FetchContent_MakeAvailable(googletest) | ||
|
||
enable_testing() | ||
include(GoogleTest) | ||
|
||
# ---------------------------------------------------------------------- | ||
# Compile the test cases into a single binary. | ||
# ---------------------------------------------------------------------- | ||
add_executable( | ||
${PROJECT_NAME} | ||
world_test.cpp | ||
rigidBody_test.cpp | ||
) | ||
target_link_libraries( | ||
${PROJECT_NAME} | ||
GTest::gtest_main | ||
) | ||
target_link_libraries (${PROJECT_NAME} ndNewton ndSolverAvx2 pthread) | ||
target_compile_options(${PROJECT_NAME} PRIVATE -Wall) | ||
gtest_discover_tests(${PROJECT_NAME}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* Copyright (c) <2003-2019> <Newton Game Dynamics> | ||
* | ||
* 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 | ||
*/ | ||
|
||
#include <cstdio> | ||
#include "ndNewton.h" | ||
#include <gtest/gtest.h> | ||
|
||
/* Return pointer to dynamic unit sphere. */ | ||
ndBodyDynamic *BuildSphere(const ndVector pos, const ndVector gravity={0.f}) { | ||
// Create the rigid body and configure gravity for it. | ||
ndBodyDynamic *const body = new ndBodyDynamic(); | ||
body->SetNotifyCallback(new ndBodyNotify(gravity)); | ||
|
||
// Set the position of the sphere in the world. | ||
ndMatrix matrix(ndGetIdentityMatrix()); | ||
matrix.m_posit = pos; | ||
body->SetMatrix(matrix); | ||
|
||
// Attach the collision shape and use a convenience function to automatically | ||
// compute the inertia matrix for the body. | ||
ndShapeInstance sphere(new ndShapeSphere(1.0f)); | ||
body->SetCollisionShape(sphere); | ||
body->SetMassMatrix(1.0f, sphere); | ||
|
||
// Disable damping for the tests to better compare the Newton | ||
// results with the analytical ones. | ||
body->SetAngularDamping(ndVector(0.f)); | ||
body->SetLinearDamping(0.f); | ||
|
||
return body; | ||
} | ||
|
||
|
||
/* Rigid body must not move in the absence of forces. */ | ||
TEST(RigidBody, NoMoveWithoutForce) { | ||
ndWorld world; | ||
world.SetSubSteps(2); | ||
|
||
// Create a sphere at the origin. No gravity will act on it by default. | ||
ndVector spherePos = ndVector(0.f); | ||
ndBodyDynamic *sphere = BuildSphere(spherePos); | ||
world.AddBody(sphere); | ||
|
||
// Sanity check: sphere must be at the origin. | ||
ndVector errVec = sphere->GetMatrix().m_posit - spherePos; | ||
ndFloat32 err = errVec.DotProduct(errVec).GetScalar(); | ||
EXPECT_NEAR(err, 0, 1E-6); | ||
|
||
// Simulate one second. | ||
for (int i = 0; i < 60; i++) { | ||
world.Update(1.0f / 60.0f); | ||
world.Sync(); | ||
} | ||
|
||
// Since no forces acted on the sphere it must not have moved. | ||
EXPECT_NEAR(err, 0, 1E-6); | ||
} | ||
|
||
|
||
/* Apply a force of 1N for 1 second to a body with 1kg of mass | ||
and verify that it moved 0.5 meters. */ | ||
TEST(RigidBody, MoveWithUnitForce) { | ||
ndWorld world; | ||
world.SetSubSteps(2); | ||
|
||
// Create a sphere at the origin and apply a gravity force of 1N. | ||
ndVector spherePos = ndVector(0.f, 0.f, 0.f, 1.f); | ||
ndVector gravity = ndVector(1.f, 0.f, 0.f, 1.f); | ||
ndBodyDynamic *sphere = BuildSphere(spherePos, gravity); | ||
world.AddBody(sphere); | ||
|
||
// Sanity check: the distance to the origin must be zero. | ||
ndVector errVec = sphere->GetMatrix().m_posit - spherePos; | ||
ndFloat32 err = errVec.DotProduct(errVec).GetScalar(); | ||
EXPECT_NEAR(err, 0, 1E-6); | ||
|
||
// Simulate one second. | ||
for (int i = 0; i < 60; i++) { | ||
world.Update(1.0f / 60.0f); | ||
world.Sync(); | ||
} | ||
|
||
// Verify that the sphere moved 0.5 meters in the X-direction. | ||
errVec = sphere->GetMatrix().m_posit - ndVector(0.5f, 0.f, 0.f, 1.f); | ||
err = errVec.DotProduct(errVec).GetScalar(); | ||
EXPECT_NEAR(err, 0, 1E-4); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* Copyright (c) <2003-2019> <Newton Game Dynamics> | ||
* | ||
* 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 | ||
*/ | ||
|
||
#include "ndNewton.h" | ||
#include <gtest/gtest.h> | ||
|
||
/* Baseline test: create and destroy an empty Newton world. */ | ||
TEST(HelloNewton, CreateWorld) { | ||
ndWorld world; | ||
world.SetSubSteps(2); | ||
world.Update(1.0f / 60.0f); | ||
world.Sync(); | ||
} |