Skip to content

Commit

Permalink
Merge pull request #535 from mbbsemu/sprintf_tests
Browse files Browse the repository at this point in the history
Sprintf tests
  • Loading branch information
paladine authored Jan 6, 2022
2 parents ffac284 + e01d5d3 commit 6c2dce8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
1 change: 0 additions & 1 deletion MBBSEmu.Tests/ExportedModules/Majorbbs/prf_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,5 @@ protected override void Reset()
mbbsEmuMemoryCore.SetPointer("PRFPTR", mbbsEmuMemoryCore.GetVariablePointer("PRFBUF"));
mbbsEmuMemoryCore.SetZero(mbbsEmuMemoryCore.GetVariablePointer("PRFBUF"), 0x4000);
}

}
}
69 changes: 69 additions & 0 deletions MBBSEmu.Tests/ExportedModules/Majorbbs/sprintf_Tests.cs
Original file line number Diff line number Diff line change
@@ -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<ushort> parameters = new List<ushort>();

[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);
}
}
}
4 changes: 2 additions & 2 deletions MBBSEmu/HostProcess/ExportedModules/ExportedModuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private protected ReadOnlySpan<byte> FormatPrintf(ReadOnlySpan<byte> 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])
{
Expand All @@ -317,7 +317,7 @@ private protected ReadOnlySpan<byte> FormatPrintf(ReadOnlySpan<byte> 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++;
Expand Down

0 comments on commit 6c2dce8

Please sign in to comment.