From fda89f726b4449fdca992dd8d5d451a2eb5971c4 Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 27 Jun 2026 09:33:34 +0900 Subject: [PATCH] =?UTF-8?q?=E5=85=B1=E6=9C=89=E3=83=A1=E3=83=A2=E3=83=AA?= =?UTF-8?q?=E3=81=A7=E3=81=AEBGRA=E6=8E=88=E5=8F=97=E3=81=AE=E6=A4=9C?= =?UTF-8?q?=E8=A8=BC=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Tests/NativeSharedMemoryTests.cs | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 LuaScript.Tests/NativeSharedMemoryTests.cs diff --git a/LuaScript.Tests/NativeSharedMemoryTests.cs b/LuaScript.Tests/NativeSharedMemoryTests.cs new file mode 100644 index 0000000..700d700 --- /dev/null +++ b/LuaScript.Tests/NativeSharedMemoryTests.cs @@ -0,0 +1,78 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.IO.MemoryMappedFiles; + +namespace LuaScript.Tests +{ + public class NativeSharedMemoryTests + { + private static string NativeDir => Path.Combine(AppContext.BaseDirectory, "native"); + + [Fact] + public void Worker_ProcessesSharedBgraBuffer_ViaFfi() + { + string luajit = Path.Combine(NativeDir, "luajit.exe"); + Assert.True(File.Exists(luajit), "native/luajit.exe must be present"); + + const int width = 8; + const int height = 8; + const int pixels = width * height; + long size = pixels * 4L; + string mapName = "LuaScriptShmPoC_" + Guid.NewGuid().ToString("N"); + + using var mmf = MemoryMappedFile.CreateNew(mapName, size); + var expectedGray = new byte[pixels]; + + using (var acc = mmf.CreateViewAccessor()) + { + for (int i = 0; i < pixels; i++) + { + byte b = (byte)(i * 3 % 256); + byte g = (byte)(i * 5 % 256); + byte r = (byte)(i * 7 % 256); + acc.Write(i * 4 + 0, b); + acc.Write(i * 4 + 1, g); + acc.Write(i * 4 + 2, r); + acc.Write(i * 4 + 3, (byte)255); + expectedGray[i] = (byte)Math.Min(255, (int)Math.Floor(r * 0.299 + g * 0.587 + b * 0.114 + 0.5)); + } + } + + var psi = new ProcessStartInfo + { + FileName = luajit, + WorkingDirectory = NativeDir, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + }; + psi.ArgumentList.Add("worker_grayscale.lua"); + psi.ArgumentList.Add(mapName); + psi.ArgumentList.Add(size.ToString()); + + using var process = Process.Start(psi)!; + string stdout = process.StandardOutput.ReadToEnd(); + string stderr = process.StandardError.ReadToEnd(); + Assert.True(process.WaitForExit(5000), "worker should finish quickly"); + Assert.Equal(0, process.ExitCode); + Assert.Equal("done", stdout); + Assert.Equal("", stderr); + + using (var acc = mmf.CreateViewAccessor()) + { + for (int i = 0; i < pixels; i++) + { + byte b = acc.ReadByte(i * 4 + 0); + byte g = acc.ReadByte(i * 4 + 1); + byte r = acc.ReadByte(i * 4 + 2); + byte a = acc.ReadByte(i * 4 + 3); + Assert.Equal(expectedGray[i], b); + Assert.Equal(expectedGray[i], g); + Assert.Equal(expectedGray[i], r); + Assert.Equal(255, a); + } + } + } + } +}