From b427190ab409f27606a4372692121d3995ca3b95 Mon Sep 17 00:00:00 2001 From: Huw Pascoe Date: Sun, 1 Nov 2015 18:25:22 +0000 Subject: [PATCH] HitShape Unit Tests The shapes should accurately calculate their distancce from a point. --- Makefile | 4 +- OpenRA.Test/OpenRA.Mods.Common/ShapeTest.cs | 95 +++++++++++++++++++++ OpenRA.Test/OpenRA.Test.csproj | 6 ++ 3 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 OpenRA.Test/OpenRA.Mods.Common/ShapeTest.cs diff --git a/Makefile b/Makefile index f259e91d6d..f59d70d6a8 100644 --- a/Makefile +++ b/Makefile @@ -140,9 +140,9 @@ mod_common: $(mod_common_TARGET) test_dll_SRCS := $(shell find OpenRA.Test/ -iname '*.cs') test_dll_TARGET = OpenRA.Test.dll test_dll_KIND = library -test_dll_DEPS = $(game_TARGET) +test_dll_DEPS = $(game_TARGET) $(mod_common_TARGET) test_dll_FLAGS = -warn:1 -test_dll_LIBS = $(COMMON_LIBS) $(game_TARGET) $(NUNIT_LIBS) +test_dll_LIBS = $(COMMON_LIBS) $(game_TARGET) $(mod_common_TARGET) $(NUNIT_LIBS) PROGRAMS += test_dll test_dll: $(test_dll_TARGET) diff --git a/OpenRA.Test/OpenRA.Mods.Common/ShapeTest.cs b/OpenRA.Test/OpenRA.Mods.Common/ShapeTest.cs new file mode 100644 index 0000000000..5788aee1c0 --- /dev/null +++ b/OpenRA.Test/OpenRA.Mods.Common/ShapeTest.cs @@ -0,0 +1,95 @@ +#region Copyright & License Information +/* + * Copyright 2007-2015 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using NUnit.Framework; +using OpenRA.Mods.Common.HitShapes; + +namespace OpenRA.Test +{ + [TestFixture] + public class ShapeTest + { + IHitShape shape; + + [TestCase(TestName = "CircleShape reports accurate distance")] + public void Circle() + { + shape = new CircleShape(new WDist(1234)); + shape.Initialize(); + + Assert.That(shape.DistanceFromEdge(new WVec(100, 100, 0)).Length, + Is.EqualTo(0)); + + Assert.That(shape.DistanceFromEdge(new WVec(1000, 0, 0)).Length, + Is.EqualTo(0)); + + Assert.That(shape.DistanceFromEdge(new WVec(2000, 2000, 0)).Length, + Is.EqualTo(1594)); + + Assert.That(new CircleShape(new WDist(73)) + .DistanceFromEdge(new WVec(150, -100, 0)).Length, + Is.EqualTo(107)); + + Assert.That(new CircleShape(new WDist(55)) + .DistanceFromEdge(new WVec(30, -45, 0)).Length, + Is.EqualTo(0)); + } + + [TestCase(TestName = "CapsuleShape report accurate distance")] + public void Capsule() + { + shape = new CapsuleShape(new int2(-50, 0), new int2(500, 235), new WDist(50)); + shape.Initialize(); + + Assert.That(shape.DistanceFromEdge(new WVec(300, 100, 0)).Length, + Is.EqualTo(0)); + + Assert.That(shape.DistanceFromEdge(new WVec(-50, 0, 0)).Length, + Is.EqualTo(0)); + + Assert.That(shape.DistanceFromEdge(new WVec(518, 451, 0)).Length, + Is.EqualTo(166)); + + Assert.That(shape.DistanceFromEdge(new WVec(-50, -50, 0)).Length, + Is.EqualTo(0)); + + Assert.That(shape.DistanceFromEdge(new WVec(-41, 97, 0)).Length, + Is.EqualTo(35)); + + Assert.That(shape.DistanceFromEdge(new WVec(339, 41, 0)).Length, + Is.EqualTo(64)); + } + + [TestCase(TestName = "RectangleShape report accurate distance")] + public void Rectangle() + { + shape = new RectangleShape(new int2(-123, -456), new int2(100, 100)); + shape.Initialize(); + + Assert.That(shape.DistanceFromEdge(new WVec(10, 10, 0)).Length, + Is.EqualTo(0)); + + Assert.That(shape.DistanceFromEdge(new WVec(-100, 50, 0)).Length, + Is.EqualTo(0)); + + Assert.That(shape.DistanceFromEdge(new WVec(0, 200, 0)).Length, + Is.EqualTo(100)); + + Assert.That(shape.DistanceFromEdge(new WVec(123, 0, 0)).Length, + Is.EqualTo(24)); + + Assert.That(shape.DistanceFromEdge(new WVec(-100, -400, 0)).Length, + Is.EqualTo(0)); + + Assert.That(shape.DistanceFromEdge(new WVec(-1000, -400, 0)).Length, + Is.EqualTo(877)); + } + } +} diff --git a/OpenRA.Test/OpenRA.Test.csproj b/OpenRA.Test/OpenRA.Test.csproj index c74f9b429d..ec093e2fc8 100644 --- a/OpenRA.Test/OpenRA.Test.csproj +++ b/OpenRA.Test/OpenRA.Test.csproj @@ -49,6 +49,7 @@ + @@ -58,6 +59,11 @@ OpenRA.Game False + + {fe6c8cc0-2f07-442a-b29f-17617b3b7fc6} + OpenRA.Mods.Common + False +