using System.Buffers.Binary;
using XFESystem.Desktop;
using XFESystem.Platform;
namespace XFESystem.Platform.Tests;
public sealed class FramebufferPackedTests
{
[Theory]
[InlineData(1, false)]
[InlineData(3, false)]
[InlineData(4, false)]
[InlineData(5, false)]
[InlineData(17, false)]
[InlineData(1, true)]
[InlineData(4, true)]
[InlineData(5, true)]
[InlineData(17, true)]
public unsafe void PackedRowsMatchScalarRgbAndPreserveAllClippedAndPaddingBytes(int width, bool swap)
{
const int height = 4;
int pitch = width * 4 + 5;
byte[] actual = Enumerable.Repeat((byte)0xA5, pitch * height + 32).ToArray();
byte[] expected = (byte[])actual.Clone();
int[] source = Enumerable.Range(0, width * height).Select(i => unchecked((int)(0xD0000000u | (uint)(i * 7919 + 0x123456)))).ToArray();
int left = width == 1 ? 0 : 1;
fixed (byte* address = actual)
{
var presenter = new MemoryFramebufferPresenter(new((nuint)address, (nuint)(pitch * height), width, height, pitch, 32,
swap ? 0xFFu : 0xFF0000u, 0xFF00, swap ? 0xFF0000u : 0xFFu));
presenter.Present(source, new(left, 1, width + 8, 2));
}
for (int y = 1; y < 3; y++)
for (int x = left; x < width; x++)
{
uint rgb = unchecked((uint)source[y * width + x]);
uint packed = swap ? ((rgb & 255) << 16) | (rgb & 0xFF00) | ((rgb >> 16) & 255) : rgb & 0xFFFFFF;
BinaryPrimitives.WriteUInt32LittleEndian(expected.AsSpan(y * pitch + x * 4, 4), packed);
}
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(100000, 20000000, 5000)]
[InlineData(200000, 40000000, 5000)]
[InlineData(100000, 25000000, 4000)]
public void TimerUsesActualCalibrationDurationForOneMillisecond(uint counts, ulong elapsedNs, uint expected)
{
Assert.True(NativeKernel.TimerCalibration.TryInitialCount(counts, elapsedNs, NativeKernel.TimerCalibration.DesktopPeriodNanoseconds, out uint actual));
Assert.Equal(expected, actual);
}
[Fact]
public void InvalidOrOverflowingTimerCalibrationFailsClosed()
{
Assert.False(NativeKernel.TimerCalibration.TryInitialCount(0, 1, 1, out _));
Assert.False(NativeKernel.TimerCalibration.TryInitialCount(1, 0, 1, out _));
Assert.False(NativeKernel.TimerCalibration.TryInitialCount(1, 1, 0, out _));
Assert.False(NativeKernel.TimerCalibration.TryInitialCount(uint.MaxValue, 1, 1000000000, out _));
Assert.False(NativeKernel.TimerCalibration.TryInitialCount(1, ulong.MaxValue, 1, out _));
}
}
using System.Buffers.Binary;
using XFESystem.Desktop;
using XFESystem.Platform;
namespace XFESystem.Platform.Tests;
public sealed class FramebufferPackedTests
{
[Theory]
[InlineData(1, false)]
[InlineData(3, false)]
[InlineData(4, false)]
[InlineData(5, false)]
[InlineData(17, false)]
[InlineData(1, true)]
[InlineData(4, true)]
[InlineData(5, true)]
[InlineData(17, true)]
public unsafe void PackedRowsMatchScalarRgbAndPreserveAllClippedAndPaddingBytes(int width, bool swap)
{
const int height = 4;
int pitch = width * 4 + 5;
byte[] actual = Enumerable.Repeat((byte)0xA5, pitch * height + 32).ToArray();
byte[] expected = (byte[])actual.Clone();
int[] source = Enumerable.Range(0, width * height).Select(i => unchecked((int)(0xD0000000u | (uint)(i * 7919 + 0x123456)))).ToArray();
int left = width == 1 ? 0 : 1;
fixed (byte* address = actual)
{
var presenter = new MemoryFramebufferPresenter(new((nuint)address, (nuint)(pitch * height), width, height, pitch, 32,
swap ? 0xFFu : 0xFF0000u, 0xFF00, swap ? 0xFF0000u : 0xFFu));
presenter.Present(source, new(left, 1, width + 8, 2));
}
for (int y = 1; y < 3; y++)
for (int x = left; x < width; x++)
{
uint rgb = unchecked((uint)source[y * width + x]);
uint packed = swap ? ((rgb & 255) << 16) | (rgb & 0xFF00) | ((rgb >> 16) & 255) : rgb & 0xFFFFFF;
BinaryPrimitives.WriteUInt32LittleEndian(expected.AsSpan(y * pitch + x * 4, 4), packed);
}
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(100000, 20000000, 5000)]
[InlineData(200000, 40000000, 5000)]
[InlineData(100000, 25000000, 4000)]
public void TimerUsesActualCalibrationDurationForOneMillisecond(uint counts, ulong elapsedNs, uint expected)
{
Assert.True(NativeKernel.TimerCalibration.TryInitialCount(counts, elapsedNs, NativeKernel.TimerCalibration.DesktopPeriodNanoseconds, out uint actual));
Assert.Equal(expected, actual);
}
[Fact]
public void InvalidOrOverflowingTimerCalibrationFailsClosed()
{
Assert.False(NativeKernel.TimerCalibration.TryInitialCount(0, 1, 1, out _));
Assert.False(NativeKernel.TimerCalibration.TryInitialCount(1, 0, 1, out _));
Assert.False(NativeKernel.TimerCalibration.TryInitialCount(1, 1, 0, out _));
Assert.False(NativeKernel.TimerCalibration.TryInitialCount(uint.MaxValue, 1, 1000000000, out _));
Assert.False(NativeKernel.TimerCalibration.TryInitialCount(1, ulong.MaxValue, 1, out _));
}
}