割り当てが無いことを試験で守る

このコミットが含まれているのは:
routersys committed 2026-10-12 11:38:02 +09:00
1 親 cbeab1540e
コミット 0f26dccd49
3ファイル変更
+168

検索条件に一致するファイルはありません

+31
ファイルの表示
@@ -0,0 +1,31 @@
namespace SpectrumColorShift.Tests;
internal static class Allocation
{
const int Rounds = 1000;
const int Attempts = 16;
public static long Of(Action work)
{
var previous = Window(work);
for (var attempt = 1; attempt < Attempts; attempt++)
{
var current = Window(work);
if (current == previous)
return current;
previous = current;
}
return previous;
}
static long Window(Action work)
{
var before = GC.GetAllocatedBytesForCurrentThread();
for (var round = 0; round < Rounds; round++)
work();
return GC.GetAllocatedBytesForCurrentThread() - before;
}
}
+70
ファイルの表示
@@ -0,0 +1,70 @@
using System.Numerics;
using Vortice;
using static SpectrumColorShift.SpectrumColorShiftCustomEffect;
namespace SpectrumColorShift.Tests;
public sealed class AllocationTests
{
[Theory]
[InlineData(ShiftMode.Linear)]
[InlineData(ShiftMode.Radial)]
[InlineData(ShiftMode.Rotational)]
public void MappingTheRectsAllocatesNothing(ShiftMode mode)
{
var impl = new EffectImpl { Mode = (int)mode, Distance = 24f, Angle = 30f };
RawRect[] inputs = [new RawRect(0, 0, 640, 480)];
RawRect[] opaque = [new RawRect(0, 0, 640, 480)];
RawRect[] requested = new RawRect[1];
var tile = new RawRect(100, 100, 200, 200);
var allocated = Allocation.Of(() =>
{
impl.MapInputRectsToOutputRect(inputs, opaque, out _, out _);
impl.MapOutputRectToInputRects(tile, requested);
impl.MapInvalidRect(0, tile);
});
Assert.Equal(0, allocated);
}
[Fact]
public void ChangingTheSettingsAllocatesNothing()
{
var impl = new EffectImpl();
var step = 0;
var allocated = Allocation.Of(() =>
{
step++;
impl.Mode = 1 << (step % 3);
impl.Distance = step % 50;
impl.Angle = step % 360;
impl.CenterX = step % 100;
impl.CenterY = step % 100;
impl.Strength = step % 100 / 100f;
impl.Dispersion = step & 1;
impl.ReferenceWavelength = 400f + step % 300;
impl.Steps = Spectrum.MinSteps + step % (Spectrum.MaxSteps - Spectrum.MinSteps + 1);
});
Assert.Equal(0, allocated);
}
[Theory]
[InlineData(DispersionLaw.Grating)]
[InlineData(DispersionLaw.Prism)]
public void FillingTheTableAllocatesNothing(DispersionLaw law)
{
var table = new Vector4[Spectrum.MaxSteps];
var step = 0;
var allocated = Allocation.Of(() =>
{
step++;
Spectrum.Fill(table, Spectrum.MinSteps + step % (Spectrum.MaxSteps - Spectrum.MinSteps + 1), 400f + step % 300, law);
});
Assert.Equal(0, allocated);
}
}
+67
ファイルの表示
@@ -11,6 +11,7 @@ public sealed class SpectrumColorShiftEffectProcessorTests
const int Width = 64;
const int Height = 48;
const int Length = 30;
const long HostSlack = 1024;
static Bgra Textured(int x, int y)
{
@@ -52,6 +53,72 @@ public sealed class SpectrumColorShiftEffectProcessorTests
Assert.Same(description.DrawDescription, draw);
}
[Fact]
public void UpdatingTheProcessorWithFixedSettingsAllocatesNothing()
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
using var source = new SourceImage(context, Width, Height, Textured);
using var processor = Shifted(24d, shifted => shifted.Mode = ShiftMode.Radial).CreateVideoEffect(context);
processor.SetInput(source.Bitmap);
_ = RenderFrame(context, processor, 0);
var descriptions = Enumerable.Range(0, Length).Select(frame => EffectDescriptions.At(frame, Length)).ToArray();
var round = 0;
var allocated = Allocation.Of(() => processor.Update(descriptions[round++ % Length]));
Assert.Equal(0, allocated);
}
[Fact]
public void UpdatingTheProcessorAllocatesOnlyWhatTheHostAllocatesForTheAnimations()
{
using var devices = new GraphicsDevices();
using var context = devices.CreateContext();
using var source = new SourceImage(context, Width, Height, Textured);
var effect = Shifted(0d, shifted =>
{
shifted.Distance.CopyFrom(Linear(0d, 24d));
shifted.Angle.CopyFrom(Linear(0d, 90d));
shifted.CenterX.CopyFrom(Linear(0d, 20d));
shifted.CenterY.CopyFrom(Linear(0d, -20d));
shifted.Strength.CopyFrom(Linear(100d, 40d));
shifted.ReferenceWavelength.CopyFrom(Linear(450d, 650d));
});
using var processor = effect.CreateVideoEffect(context);
using var bare = new SpectrumColorShiftCustomEffect(context);
processor.SetInput(source.Bitmap);
_ = RenderFrame(context, processor, 0);
var descriptions = Enumerable.Range(0, Length).Select(frame => EffectDescriptions.At(frame, Length)).ToArray();
var reads = 0;
var sets = 0;
var updates = 0;
var reading = Allocation.Of(() =>
{
var frame = reads++ % Length;
_ = effect.Distance.GetValue(frame, Length, EffectDescriptions.Fps);
_ = effect.Angle.GetValue(frame, Length, EffectDescriptions.Fps);
_ = effect.CenterX.GetValue(frame, Length, EffectDescriptions.Fps);
_ = effect.CenterY.GetValue(frame, Length, EffectDescriptions.Fps);
_ = effect.Strength.GetValue(frame, Length, EffectDescriptions.Fps);
_ = effect.ReferenceWavelength.GetValue(frame, Length, EffectDescriptions.Fps);
});
var setting = Allocation.Of(() =>
{
var value = ++sets % 50;
bare.Distance = value;
bare.Angle = value;
bare.CenterX = value;
bare.CenterY = value;
bare.Strength = value / 100f;
bare.ReferenceWavelength = 400f + value;
});
var updating = Allocation.Of(() => processor.Update(descriptions[updates++ % Length]));
Assert.InRange(updating, reading + setting - HostSlack, reading + setting + HostSlack);
}
[Fact]
public void WithoutADistanceTheProcessorPassesTheImageThrough()
{