From 991aac291fe40fb995256c89500bbe4b7d68e609 Mon Sep 17 00:00:00 2001 From: Jhett Black <10942655+jhett12321@users.noreply.github.com> Date: Wed, 4 Dec 2024 23:51:58 +0100 Subject: [PATCH] Additional VM tests. --- .../src/main/Constants/VMFunctions.cs | 9 ++++ NWNX.NET.Tests/src/main/Tests/VMTests.cs | 47 ++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 NWNX.NET.Tests/src/main/Constants/VMFunctions.cs diff --git a/NWNX.NET.Tests/src/main/Constants/VMFunctions.cs b/NWNX.NET.Tests/src/main/Constants/VMFunctions.cs new file mode 100644 index 0000000..34f6cfc --- /dev/null +++ b/NWNX.NET.Tests/src/main/Constants/VMFunctions.cs @@ -0,0 +1,9 @@ +namespace NWNX.NET.Tests.Constants; + +public static class VMFunctions +{ + public const int SubString = 65; + public const int fabs = 67; + public const int abs = 77; + public const int GetModuleName = 561; +} diff --git a/NWNX.NET.Tests/src/main/Tests/VMTests.cs b/NWNX.NET.Tests/src/main/Tests/VMTests.cs index 55212b5..cda625b 100644 --- a/NWNX.NET.Tests/src/main/Tests/VMTests.cs +++ b/NWNX.NET.Tests/src/main/Tests/VMTests.cs @@ -1,4 +1,5 @@ using NUnit.Framework; +using NWNX.NET.Tests.Constants; namespace NWNX.NET.Tests { @@ -8,10 +9,54 @@ public sealed class VMTests [Test] public void GetModuleNameTest() { - NWNXAPI.CallBuiltIn(561); + NWNXAPI.CallBuiltIn(VMFunctions.GetModuleName); string? modName = NWNXAPI.StackPopString(); Assert.That(modName, Is.EqualTo("demo")); } + + [Test] + [TestCase(1, 1)] + [TestCase(0, 0)] + [TestCase(-1, 1)] + [TestCase(-int.MaxValue, int.MaxValue)] + public void AbsTest(int value, int expectedValue) + { + NWNXAPI.StackPushInteger(value); + NWNXAPI.CallBuiltIn(VMFunctions.abs); + int result = NWNXAPI.StackPopInteger(); + + Assert.That(result, Is.EqualTo(expectedValue)); + } + + [Test] + [TestCase(1f, 1f)] + [TestCase(0f, 0f)] + [TestCase(-1f, 1f)] + [TestCase(-float.MaxValue, float.MaxValue)] + public void FAbsTest(float value, float expectedValue) + { + NWNXAPI.StackPushFloat(value); + NWNXAPI.CallBuiltIn(VMFunctions.fabs); + float result = NWNXAPI.StackPopFloat(); + + Assert.That(result, Is.EqualTo(expectedValue)); + } + + [Test] + [TestCase("example_string", 0, 7, "example")] + [TestCase("test_string", 5, 6, "string")] + [TestCase("", 0, 0, "")] + [TestCase(null, 0, 5, "")] + public void SubStringTest(string str, int start, int count, string expectedValue) + { + NWNXAPI.StackPushInteger(count); + NWNXAPI.StackPushInteger(start); + NWNXAPI.StackPushString(str); + NWNXAPI.CallBuiltIn(VMFunctions.SubString); + + string? result = NWNXAPI.StackPopString(); + Assert.That(result, Is.EqualTo(expectedValue)); + } } }