Skip to content

Commit

Permalink
Added unit tests for testing all shapes in a game
Browse files Browse the repository at this point in the history
Doesn't work for all games atm though...
  • Loading branch information
Donkie committed Dec 14, 2021
1 parent 45580f0 commit e75a810
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 7 deletions.
12 changes: 11 additions & 1 deletion I3DShapesTool.Lib/Model/I3DVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ public double Length()

public bool IsUnitLength()
{
return Math.Abs(Length() - 1.0) < 1e-6;
return Math.Abs(X * X + Y * Y + Z * Z - 1.0) < 1e-3;
}

public bool IsZero()
{
return Math.Abs(X) < 1e-6 && Math.Abs(Y) < 1e-6 && Math.Abs(Z) < 1e-6;
}

public bool IsValidNormal()
{
return IsUnitLength() || IsZero();
}

public override string ToString()
Expand Down
120 changes: 114 additions & 6 deletions I3DShapesToolTest/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System;
using I3DShapesTool.Lib.Tools;
using System.Collections.Generic;

namespace I3DShapesToolTest
{
Expand Down Expand Up @@ -47,11 +48,17 @@ private static void AssertShapeData(ShapesFile file)
{
foreach (var shape in file.Shapes)
{
Assert.True(shape.UVSets.All(uvSet => uvSet == null || uvSet.All(uv => uv.U >= -MaxUV && uv.U <= MaxUV && uv.V >= -MaxUV && uv.V <= MaxUV)));
Assert.True(shape.Triangles.All(tri => tri.P1Idx < shape.CornerCount && tri.P2Idx < shape.CornerCount && tri.P3Idx < shape.CornerCount));
// This UV check works in 99.9% percent of cases but some models just have extremely wacky UVs which means we can't rely on this test :(
//Assert.True(shape.UVSets.All(uvSet => uvSet == null || uvSet.All(uv => uv.U >= -MaxUV && uv.U <= MaxUV && uv.V >= -MaxUV && uv.V <= MaxUV)));

Assert.True(shape.Triangles.All(tri => tri.P1Idx <= shape.CornerCount && tri.P2Idx <= shape.CornerCount && tri.P3Idx <= shape.CornerCount));
if (shape.Normals != null)
{
Assert.True(shape.Normals.All(v => v.IsUnitLength()));
double numUnitLengths = shape.Normals.Sum(v => v.IsValidNormal() ? 1 : 0);
// The data files can contain some bad normals, but most of them should be good
Assert.True(numUnitLengths / shape.Normals.Length > 0.95);
Assert.True(shape.Normals.First().IsValidNormal());
Assert.True(shape.Normals.Last().IsValidNormal());
}
}
}
Expand Down Expand Up @@ -80,6 +87,22 @@ private static void TestRewrite(ShapesFile file)
}
}

private static void FindShapesFiles(string baseDir, ISet<string> outData)
{
foreach(var file in Directory.GetFiles(baseDir))
{
if (file.EndsWith(".i3d.shapes"))
{
outData.Add(file);
}
}

foreach(var dir in Directory.GetDirectories(baseDir))
{
FindShapesFiles(dir, outData);
}
}

[SkippableFact]
public void TestFS22()
{
Expand All @@ -94,6 +117,23 @@ public void TestFS22()
TestRewrite(file);
}

[SkippableFact]
public void TestFS22Full()
{
var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 22");
Skip.If(gameFolder == null);

var shapeFiles = new HashSet<string>();
FindShapesFiles(Path.Combine(gameFolder, "data"), shapeFiles);

foreach(var filePath in shapeFiles)
{
var file = new ShapesFile();
file.Load(filePath);
AssertShapeData(file);
}
}

[SkippableFact]
public void TestFS19()
{
Expand All @@ -109,7 +149,24 @@ public void TestFS19()
}

[SkippableFact]
public void TestFS17_1()
public void TestFS19Full()
{
var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 19");
Skip.If(gameFolder == null);

var shapeFiles = new HashSet<string>();
FindShapesFiles(Path.Combine(gameFolder, "data"), shapeFiles);

foreach (var filePath in shapeFiles)
{
var file = new ShapesFile();
file.Load(filePath);
AssertShapeData(file);
}
}

[SkippableFact]
public void TestFS17()
{
var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 17");
Skip.If(gameFolder == null);
Expand All @@ -123,7 +180,24 @@ public void TestFS17_1()
}

[SkippableFact]
public void TestFS15_1()
public void TestFS17Full()
{
var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 17");
Skip.If(gameFolder == null);

var shapeFiles = new HashSet<string>();
FindShapesFiles(Path.Combine(gameFolder, "data"), shapeFiles);

foreach (var filePath in shapeFiles)
{
var file = new ShapesFile();
file.Load(filePath);
AssertShapeData(file);
}
}

[SkippableFact]
public void TestFS15()
{
var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 15");
Skip.If(gameFolder == null);
Expand All @@ -137,7 +211,24 @@ public void TestFS15_1()
}

[SkippableFact]
public void TestFS13_1()
public void TestFS15Full()
{
var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 15");
Skip.If(gameFolder == null);

var shapeFiles = new HashSet<string>();
FindShapesFiles(Path.Combine(gameFolder, "data"), shapeFiles);

foreach (var filePath in shapeFiles)
{
var file = new ShapesFile();
file.Load(filePath);
AssertShapeData(file);
}
}

[SkippableFact]
public void TestFS13()
{
var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 2013");
Skip.If(gameFolder == null);
Expand All @@ -149,5 +240,22 @@ public void TestFS13_1()
AssertShapeData(file);
TestRewrite(file);
}

[SkippableFact]
public void TestFS13Full()
{
var gameFolder = SteamHelper.GetGameDirectory("Farming Simulator 2013");
Skip.If(gameFolder == null);

var shapeFiles = new HashSet<string>();
FindShapesFiles(Path.Combine(gameFolder, "data"), shapeFiles);

foreach (var filePath in shapeFiles)
{
var file = new ShapesFile();
file.Load(filePath);
AssertShapeData(file);
}
}
}
}

0 comments on commit e75a810

Please sign in to comment.