Skip to content

Commit

Permalink
Merge pull request #174 from Algoryx/feature/unit-tests
Browse files Browse the repository at this point in the history
Add simple unit tests to project
  • Loading branch information
FilipAlg authored Dec 3, 2024
2 parents 1804861 + 10a3e4c commit 086fff5
Show file tree
Hide file tree
Showing 18 changed files with 547 additions and 3 deletions.
15 changes: 12 additions & 3 deletions AGXUnity/Model/DeformableTerrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,19 @@ public override void SetHeight( int x, int y, float height )
if ( xstart + width >= resolution || xstart < 0 || ystart + height >= resolution || ystart < 0 )
throw new ArgumentOutOfRangeException( "", $"Requested height patch with start ({xstart},{ystart}) and size ({width},{height}) extends outside of the terrain bounds [0,{TerrainDataResolution - 1}]" );

if ( Native == null )
return TerrainData.GetHeights( xstart, ystart, width, height );
float scale = TerrainData.size.y;
float [,] heights;
if ( Native == null ) {
heights = TerrainData.GetHeights( xstart, ystart, width, height );
for ( int y = 0; y < height; y++ ) {
for ( int x = 0; x < width; x++ ) {
heights[ y, x ] = heights[ y, x ] * scale;
}
}
return heights;
}

float [,] heights = new float[height,width];
heights = new float[ height, width ];
for ( int y = 0; y < height; y++ ) {
for ( int x = 0; x < width; x++ ) {
agx.Vec2i idx = new agx.Vec2i( resolution - 1 - x - xstart, resolution - 1 - y - ystart);
Expand Down
8 changes: 8 additions & 0 deletions Tests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Tests/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Tests/Editor/AGXUnityEditorTests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "AGXUnityEditorTests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"AGXUnity",
"AGXUnityEditor",
"TestingCommon"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Tests/Editor/AGXUnityEditorTests.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions Tests/Editor/EditorMetadataTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using AGXUnity;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

using Assembly = System.Reflection.Assembly;

namespace AGXUnityTesting
{
public class EditorMetadataTests
{
[Test]
public void ScriptsHaveHelpURLs()
{
var asm = Assembly.GetAssembly(typeof(RigidBody));
List<Type> missing = new List<Type>();
foreach ( var t in asm.ExportedTypes ) {
if (
(
t.IsSubclassOf( typeof( ScriptComponent ) ) ||
t.IsSubclassOf( typeof( ScriptAsset ) )
) && !t.IsAbstract ) {
var help = t.GetCustomAttribute<HelpURLAttribute>(false);
var hide = t.GetCustomAttribute<HideInInspector>();
if ( help == null && hide == null )
missing.Add( t );
}
}

if ( missing.Count > 0 ) {
var errStr = $"The following classes are missing the HelpURL Attribute: \n";
foreach ( var t in missing ) {
errStr += t.Name + "\n";
}
Assert.Fail( errStr );
}
}

private void CheckScriptIconsInDirectory( string path )
{
if ( System.IO.Directory.Exists( path ) ) {
foreach ( var subfile in System.IO.Directory.EnumerateFiles( path ) )
CheckScriptIconsInDirectory( subfile );
}
else if ( path.EndsWith( ".cs" ) ) {
var mi = AssetImporter.GetAtPath( path ) as MonoImporter;
Assert.NotNull( mi.GetIcon(), $"Script file '{path}' has no icon" );
}

}

[Test]
public void ScriptsHaveIcons()
{
var sourceDir = AGXUnityEditor.IO.Utils.AGXUnitySourceDirectory;
CheckScriptIconsInDirectory( sourceDir );
}
}
}
11 changes: 11 additions & 0 deletions Tests/Editor/EditorMetadataTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions Tests/Editor/TerrainGetSetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using AGXUnity.Model;
using NUnit.Framework;
using UnityEngine;

namespace AGXUnityTesting
{
public class TerrainGetSetTests
{
private DeformableTerrain testTerrain;
private Terrain unityTerrain;

private const float HEIGHT_DELTA = 0.0005f;

private float GetHeight( int x, int y, bool normalize = false )
{
var heightScale = normalize ? unityTerrain.terrainData.size.y : 1;
var res = unityTerrain.terrainData.heightmapResolution;
return ( 2 + Mathf.Sin( (float)y / res ) + Mathf.Cos( (float)x / res ) ) / heightScale;
}

private float RescaleUnityHeight( float height, bool normalized = false )
{
return height * ( normalized ? unityTerrain.terrainData.size.y : 1.0f );
}

[SetUp]
public void SetupWireScene()
{
GameObject go = new GameObject("Test terrain");

unityTerrain = go.AddComponent<Terrain>();
unityTerrain.terrainData = new TerrainData();
unityTerrain.terrainData.size = new Vector3( 30, 20, 30 );
unityTerrain.terrainData.heightmapResolution = 33;

float [,] heights = new float[33,33];
for ( int y = 0; y < 33; y++ )
for ( int x = 0; x < 33; x++ )
heights[ y, x ] = GetHeight( x, y, true );

unityTerrain.terrainData.SetHeights( 0, 0, heights );

testTerrain = go.AddComponent<DeformableTerrain>();
testTerrain.MaximumDepth = 2;
}

[Test]
public void TestTerrainGetSingleHeight()
{
var height = testTerrain.GetHeight( 16, 16 );
var expected = 2 + Mathf.Sin( (float)16 / 33 ) + Mathf.Cos( (float)16 /33 );
Assert.AreEqual( expected, height, HEIGHT_DELTA );
}

[Test]
public void TestTerrainGetMultipleHeights()
{
var height = testTerrain.GetHeights( 10, 10, 10, 10 );
for ( int y = 0; y < 10; y++ )
for ( int x = 0; x < 10; x++ )
Assert.AreEqual( GetHeight( x+10, y+10 ), height[ y, x ], HEIGHT_DELTA );
}

[Test]
public void TestSetSingleHeight()
{
testTerrain.SetHeight( 16, 16, 5f );
var result = testTerrain.GetHeight( 16, 16 );

Assert.AreEqual( 5f, result, HEIGHT_DELTA );
}

[Test]
public void TestSetSingleHeightIsPropagated()
{
testTerrain.SetHeight( 16, 16, 5f );

var result = unityTerrain.terrainData.GetHeight( 16, 16 );

Assert.AreEqual( 5f, RescaleUnityHeight( result ), HEIGHT_DELTA );
}

[Test]
public void TestSetMultipleHeights()
{
var source = new float[10,10];
var expected = new float[10,10];
for ( int y = 0; y < 10; y++ ) {
for ( int x = 0; x < 10; x++ ) {
source[ y, x ] = GetHeight( x, y );
expected[ y, x ] = GetHeight( x, y );
}
}

testTerrain.SetHeights( 10, 10, source );
var results = testTerrain.GetHeights( 10, 10, 10, 10 );

for ( int y = 0; y < 10; y++ )
for ( int x = 0; x < 10; x++ )
Assert.AreEqual( expected[ y, x ], results[ y, x ], HEIGHT_DELTA );
}

[Test]
public void TestSetMultipleHeightsArePropagated()
{
var source = new float[10,10];
var expected = new float[10,10];
for ( int y = 0; y < 10; y++ ) {
for ( int x = 0; x < 10; x++ ) {
source[ y, x ] = GetHeight( x, y );
expected[ y, x ] = GetHeight( x, y );
}
}
testTerrain.SetHeights( 10, 10, source );

var results = unityTerrain.terrainData.GetHeights( 10, 10, 10, 10 );

for ( int y = 0; y < 10; y++ )
for ( int x = 0; x < 10; x++ )
Assert.AreEqual( expected[ y, x ], RescaleUnityHeight( results[ y, x ], true ), HEIGHT_DELTA );
}
}
}
11 changes: 11 additions & 0 deletions Tests/Editor/TerrainGetSetTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Tests/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions Tests/Runtime/AGXUnityTests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "AGXUnityTests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"AGXUnity",
"TestingCommon"
],
"includePlatforms": [
"Editor",
"LinuxStandalone64",
"WindowsStandalone64"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Tests/Runtime/AGXUnityTests.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 086fff5

Please sign in to comment.