Skip to content

Commit

Permalink
Merge pull request #296 from olitheolix/unit-test-example
Browse files Browse the repository at this point in the history
Proof-of-Concept for Test Automation
  • Loading branch information
JulioJerez authored Oct 12, 2022
2 parents 98a18d6 + e74d76a commit f24bd09
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ jobs:
cmake --build build -j2
cmake --install build --strip
- name: Run Tests
shell: bash
run: |
newton-4.00/build/tests/newton_tests
- name: Build And Run Hello World Apps
shell: bash
run: |
Expand Down
1 change: 1 addition & 0 deletions newton-4.00/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,4 @@ endif()
add_subdirectory(sdk)
add_subdirectory(thirdParty)
add_subdirectory(applications)
add_subdirectory(tests)
55 changes: 55 additions & 0 deletions newton-4.00/tests/CMakeLists.txt
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})
95 changes: 95 additions & 0 deletions newton-4.00/tests/rigidBody_test.cpp
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);
}
21 changes: 21 additions & 0 deletions newton-4.00/tests/world_test.cpp
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();
}

0 comments on commit f24bd09

Please sign in to comment.