From 62215cca728028d1f895666214b9dfe22651a52d Mon Sep 17 00:00:00 2001 From: Todd Jeffreys Date: Wed, 5 Jan 2022 21:07:24 -0700 Subject: [PATCH 1/2] Create sprintf_Tests.cs --- .../ExportedModules/Majorbbs/prf_Tests.cs | 1 - .../ExportedModules/Majorbbs/sprintf_Tests.cs | 69 +++++++++++++++++++ .../ExportedModules/ExportedModuleBase.cs | 4 +- scripts/README.MD | 1 + 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 MBBSEmu.Tests/ExportedModules/Majorbbs/sprintf_Tests.cs create mode 100644 scripts/README.MD diff --git a/MBBSEmu.Tests/ExportedModules/Majorbbs/prf_Tests.cs b/MBBSEmu.Tests/ExportedModules/Majorbbs/prf_Tests.cs index 5d7cff55..5f6bbb63 100644 --- a/MBBSEmu.Tests/ExportedModules/Majorbbs/prf_Tests.cs +++ b/MBBSEmu.Tests/ExportedModules/Majorbbs/prf_Tests.cs @@ -70,6 +70,5 @@ protected override void Reset() mbbsEmuMemoryCore.SetPointer("PRFPTR", mbbsEmuMemoryCore.GetVariablePointer("PRFBUF")); mbbsEmuMemoryCore.SetZero(mbbsEmuMemoryCore.GetVariablePointer("PRFBUF"), 0x4000); } - } } diff --git a/MBBSEmu.Tests/ExportedModules/Majorbbs/sprintf_Tests.cs b/MBBSEmu.Tests/ExportedModules/Majorbbs/sprintf_Tests.cs new file mode 100644 index 00000000..58355d66 --- /dev/null +++ b/MBBSEmu.Tests/ExportedModules/Majorbbs/sprintf_Tests.cs @@ -0,0 +1,69 @@ +using FluentAssertions; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace MBBSEmu.Tests.ExportedModules.Majorbbs +{ + public class sprintf_Tests : ExportedModuleTestBase + { + private const int SPRINTF_ORDINAL = 560; + + private List parameters = new List(); + + [Theory] + [InlineData("%d", "1", (ushort)1)] + [InlineData("%d", "0", (ushort)0)] + [InlineData("%d", "-1", (ushort)0xFFFF)] + [InlineData("%u", "1", (ushort)1)] + [InlineData("%u", "0", (ushort)0)] + [InlineData("%u", "65535", (ushort)0xFFFF)] + [InlineData("ITEM%3.3d", "ITEM010", (ushort)10)] + [InlineData("ITEM%3d", "ITEM 10", (ushort)10)] + [InlineData("ITEM%3.3d", "ITEM100", (ushort)100)] + [InlineData("ITEM%3d", "ITEM100", (ushort)100)] + [InlineData("Level: %5d", "Level: 3", (ushort)3)] + [InlineData("Level: %-5d", "Level: 3 ", (ushort)3)] + [InlineData("Level: %5.5d", "Level: 00003", (ushort)3)] + [InlineData("Level: %-5.5d", "Level: 00003", (ushort)3)] + [InlineData("%s-%d", "TEST-1", "TEST", (ushort)1)] + [InlineData("%s-%ld", "TEST-2147483647", "TEST", 2147483647)] + [InlineData("%s-%ld-%d-%s", "TEST-2147483647-1-FOO", "TEST", 2147483647, (ushort)1, "FOO")] + [InlineData("%s-%ld-%d-%s", "TEST--1-1-FOO", "TEST", (uint)0xFFFFFFFF, (ushort)1, "FOO")] + [InlineData("%s-%lu-%d-%s", "TEST-2147483647-1-FOO", "TEST", 2147483647u, (ushort)1, "FOO")] + [InlineData("%s-%lu-%d-%s", "TEST-3147483647-1-FOO", "TEST", 3147483647u, (ushort)1, "FOO")] + [InlineData("99% of the time, this will print %s", "99% of the time, this will print TEST", "TEST")] //Unescaped % + [InlineData("Mid 50% Test", "Mid 50% Test", null)] //Unescaped % + [InlineData("End 50% ", "End 50% ", null)] //Unescaped % + [InlineData("End 50%", "End 50%", null)] //Unescaped % + [InlineData("This is 100%% accurate", "This is 100% accurate", null)] //Escaped % + [InlineData("%%%%", "%%", null)] //Escaped % + [InlineData("%%%%%", "%%%", null)] //Escaped & Unescaped % + [InlineData("%%%%% ", "%%% ", null)] //Escaped & Unescaped % + public void sprintf_Test(string formatString, string expectedString, params object[] values) + { + Reset(); + + var destBuffer = mbbsEmuMemoryCore.Malloc((ushort)(expectedString.Length * 2)); + var formatStringParameterPointer = mbbsEmuMemoryCore.Malloc((ushort)(formatString.Length + 1)); + mbbsEmuMemoryCore.SetArray(formatStringParameterPointer, Encoding.ASCII.GetBytes(formatString)); + + parameters.Add(destBuffer.Offset); + parameters.Add(destBuffer.Segment); + + parameters.Add(formatStringParameterPointer.Offset); + parameters.Add(formatStringParameterPointer.Segment); + + if (values != null) + { + var parameterList = GenerateParameters(values); + foreach (var p in parameterList) + parameters.Add(p); + } + + ExecuteApiTest(HostProcess.ExportedModules.Majorbbs.Segment, SPRINTF_ORDINAL, parameters); + + Encoding.ASCII.GetString(mbbsEmuMemoryCore.GetString(destBuffer, true)).Should().Be(expectedString); + } + } +} diff --git a/MBBSEmu/HostProcess/ExportedModules/ExportedModuleBase.cs b/MBBSEmu/HostProcess/ExportedModules/ExportedModuleBase.cs index 27120f66..54018ea7 100644 --- a/MBBSEmu/HostProcess/ExportedModules/ExportedModuleBase.cs +++ b/MBBSEmu/HostProcess/ExportedModules/ExportedModuleBase.cs @@ -295,7 +295,7 @@ private protected ReadOnlySpan FormatPrintf(ReadOnlySpan stringToPar var controlStart = i; //Handle escaped %% as a single % -- or if % is the last character in a string - if (stringToParse[i] == '%') + if (stringToParse[i] == '%' && (i + 1) < stringToParse.Length) { switch ((char)stringToParse[i + 1]) { @@ -317,7 +317,7 @@ private protected ReadOnlySpan FormatPrintf(ReadOnlySpan stringToPar } //Found a Control Character - if (stringToParse[i] == '%' && stringToParse[i + 1] != '%') + if (stringToParse[i] == '%' && (i + 1) < stringToParse.Length && stringToParse[i + 1] != '%') { using var msFormattedValue = new MemoryStream(); i++; diff --git a/scripts/README.MD b/scripts/README.MD new file mode 100644 index 00000000..17fea732 --- /dev/null +++ b/scripts/README.MD @@ -0,0 +1 @@ +These are scripts From e01d5d313cf153d6ba49cfecd0ddf12ed7f14662 Mon Sep 17 00:00:00 2001 From: Todd Jeffreys Date: Wed, 5 Jan 2022 21:08:51 -0700 Subject: [PATCH 2/2] Don't want README.MD --- scripts/README.MD | 1 - 1 file changed, 1 deletion(-) delete mode 100644 scripts/README.MD diff --git a/scripts/README.MD b/scripts/README.MD deleted file mode 100644 index 17fea732..00000000 --- a/scripts/README.MD +++ /dev/null @@ -1 +0,0 @@ -These are scripts