Skip to content

Commit

Permalink
test: refactor test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
andywiecko committed Oct 18, 2024
1 parent 0c62113 commit ecd4829
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 38 deletions.
26 changes: 10 additions & 16 deletions Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ public static void Run<T>(this Triangulator<T> triangulator) where T : unmanaged
Extensions.Run((dynamic)triangulator);
public static JobHandle Schedule<T>(this Triangulator<T> triangulator, JobHandle dependencies = default) where T : unmanaged =>
Extensions.Schedule((dynamic)triangulator, dependencies);

public static float2[] CastToFloat2<T>(this IEnumerable<T> data) where T : unmanaged => data.Select(i => default(T) switch
{
#if UNITY_MATHEMATICS_FIXEDPOINT
fp2 => math.float2((float)((dynamic)i).x, (float)((dynamic)i).y),
#endif
_ => (float2)(dynamic)i,
}).ToArray();
public static T[] DynamicCast<T>(this IEnumerable<float2> data) where T : unmanaged => default(T) switch
{
#if UNITY_MATHEMATICS_FIXEDPOINT
Expand Down Expand Up @@ -213,15 +221,7 @@ public static JobHandle Schedule<T>(this Triangulator<T> triangulator, JobHandle
};
public static void Draw(this Triangulator triangulator, Color? color = null, float duration = 5f) => TestUtils.Draw(triangulator.Output.Positions.AsArray().Select(i => (float2)i).ToArray(), triangulator.Output.Triangles.AsArray().AsReadOnlySpan(), color ?? Color.red, duration);
public static void Draw<T>(this Triangulator<T> triangulator, Color? color = null, float duration = 5f) where T : unmanaged => TestUtils.Draw(
triangulator.Output.Positions.AsArray().Select(i => default(T) switch
{
#if UNITY_MATHEMATICS_FIXEDPOINT
fp2 => math.float2((float)((dynamic)i).x, (float)((dynamic)i).y),
#endif
_ => (float2)(dynamic)i
}
)
.ToArray(),
triangulator.Output.Positions.AsArray().CastToFloat2(),
triangulator.Output.Triangles.AsArray().AsReadOnlySpan(), color ?? Color.red, duration
);
}
Expand All @@ -233,13 +233,7 @@ public static class TestUtils
/// </summary>
/// <exception cref="AssertionException"></exception>
public static void AssertValidTriangulation<T2>(Triangulator<T2> triangulator) where T2 : unmanaged => AssertValidTriangulation(
positions: triangulator.Output.Positions.AsReadOnly().Select(i => default(T2) switch
{
#if UNITY_MATHEMATICS_FIXEDPOINT
fp2 => math.float2((float)((dynamic)i).x, (float)((dynamic)i).y),
#endif
_ => (float2)(dynamic)i,
}).ToArray(),
positions: triangulator.Output.Positions.AsReadOnly().CastToFloat2(),
triangles: triangulator.Output.Triangles.AsReadOnly().AsReadOnlySpan()
);

Expand Down
11 changes: 1 addition & 10 deletions Tests/TriangulatorGenericsEditorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2369,16 +2369,7 @@ public void RefinementWithNeedleConstraintClusterTest()
triangulator.Run();
triangulator.Draw(color: Color.blue);

var result = triangulator.Output.Positions.AsReadOnly()
.Select(i => default(T) switch
{
#if UNITY_MATHEMATICS_FIXEDPOINT
fp2 => math.float2((float)((dynamic)i).x, (float)((dynamic)i).y),
#endif
_ => (float2)(dynamic)i,
})
.Select(i => math.all(i >= math.float2(-1, 0) & i <= math.float2(9, 7))).ToArray();

var result = triangulator.Output.Positions.AsReadOnly().CastToFloat2().Select(i => math.all(i >= math.float2(-1, 0) & i <= math.float2(9, 7)));
Assert.That(result, Has.All.True);
TestUtils.AssertValidTriangulation(triangulator);
}
Expand Down
14 changes: 2 additions & 12 deletions Tests/UnsafeTriangulatorEditorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,17 +571,7 @@ int FindTriangle(ReadOnlySpan<int> initialTriangles, int j)
return -1;
}

float2[] CastFloat2() => outputPositions.AsReadOnly()
.Select(i => default(T) switch
{
#if UNITY_MATHEMATICS_FIXEDPOINT
fp2 => math.float2((float)((dynamic)i).x, (float)((dynamic)i).y),
#endif
_ => (float2)(dynamic)i,
})
.ToArray();

TestUtils.Draw(CastFloat2(), triangles.AsReadOnly(), Color.red, duration: 5f);
TestUtils.Draw(outputPositions.AsReadOnly().CastToFloat2(), triangles.AsReadOnly(), Color.red, duration: 5f);

var random = new Unity.Mathematics.Random(seed: 42);
for (int iter = 0; iter < 5; iter++)
Expand All @@ -601,7 +591,7 @@ float2[] CastFloat2() => outputPositions.AsReadOnly()
}
}

var result = CastFloat2();
var result = outputPositions.AsReadOnly().CastToFloat2();
TestUtils.Draw(result.Select(i => i + math.float2((iter + 1) * 4f, 0)).ToArray(), triangles.AsReadOnly(), Color.red, duration: 5f);
TestUtils.AssertValidTriangulation(result, triangles.AsReadOnly());
}
Expand Down

0 comments on commit ecd4829

Please sign in to comment.