diff --git a/Core/Utils/RandomUtil.cs b/Core/Utils/RandomUtil.cs index db7c90e1..276c7fbd 100644 --- a/Core/Utils/RandomUtil.cs +++ b/Core/Utils/RandomUtil.cs @@ -8,7 +8,7 @@ namespace Core.Utils; [Injectable(InjectionType.Singleton)] public class RandomUtil { - protected ISptLogger _logger; + protected ISptLogger? _logger = null; public RandomUtil ( @@ -17,6 +17,8 @@ public class RandomUtil { _logger = logger; } + + public RandomUtil() { } public readonly Random Random = new(); diff --git a/Server/DependencyInjectionRegistrator.cs b/Server/DependencyInjectionRegistrator.cs index f7a6e4ba..8cce3e87 100644 --- a/Server/DependencyInjectionRegistrator.cs +++ b/Server/DependencyInjectionRegistrator.cs @@ -21,7 +21,7 @@ public static class DependencyInjectionRegistrator var groupedTypes = types.SelectMany( t => { - var attributes = (Injectable[])Attribute.GetCustomAttributes(t, typeof(Injectable))!; + var attributes = (Injectable[])Attribute.GetCustomAttributes(t, typeof(Injectable)); var registerableType = t; var registerableComponents = new List(); foreach (var attribute in attributes) @@ -37,7 +37,7 @@ public static class DependencyInjectionRegistrator registerableType = registerableType.GetInterfaces()[0]; } - registerableComponents.Add(new(registerableType, t, attribute)); + registerableComponents.Add(new RegisterableType(registerableType, t, attribute)); } return registerableComponents; @@ -62,19 +62,18 @@ public static class DependencyInjectionRegistrator } } - private static List AllLoadedTypes; - private static List AllConstructors; + private static List? _allLoadedTypes; + private static List? _allConstructors; private static void RegisterGenericComponents(IServiceCollection builderServices, RegisterableType valueTuple) { - if (AllLoadedTypes == null) - AllLoadedTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(t => t.GetTypes()).ToList(); - if (AllConstructors == null) - AllConstructors = AllLoadedTypes.SelectMany(t => t.GetConstructors()).ToList(); + _allLoadedTypes ??= AppDomain.CurrentDomain.GetAssemblies().SelectMany(t => t.GetTypes()).ToList(); + _allConstructors ??= _allLoadedTypes.SelectMany(t => t.GetConstructors()).ToList(); + var typeName = $"{valueTuple.RegisterableInterface.Namespace}.{valueTuple.RegisterableInterface.Name}"; try { - var matchedConstructors = AllConstructors.Where( + var matchedConstructors = _allConstructors.Where( c => c.GetParameters() .Any( p => p.ParameterType.IsGenericType && @@ -82,25 +81,25 @@ public static class DependencyInjectionRegistrator ) ); - if (matchedConstructors.Any()) + var constructorInfos = matchedConstructors.ToList(); + if (constructorInfos.Count == 0) return; + + foreach (var matchedConstructor in constructorInfos) { - foreach (var matchedConstructor in matchedConstructors) + foreach (var parameterInfo in matchedConstructor.GetParameters() + .Where( + p => p.ParameterType.IsGenericType && + p.ParameterType.GetGenericTypeDefinition().FullName == typeName + )) { - foreach (var parameterInfo in matchedConstructor.GetParameters() - .Where( - p => p.ParameterType.IsGenericType && - p.ParameterType.GetGenericTypeDefinition().FullName == typeName - )) - { - var parameters = parameterInfo.ParameterType.GetGenericArguments(); - var typedGeneric = valueTuple.TypeToRegister.MakeGenericType(parameters); - RegisterComponent( - builderServices, - valueTuple.InjectableAttribute.InjectionType, - parameterInfo.ParameterType, - typedGeneric - ); - } + var parameters = parameterInfo.ParameterType.GetGenericArguments(); + var typedGeneric = valueTuple.TypeToRegister.MakeGenericType(parameters); + RegisterComponent( + builderServices, + valueTuple.InjectableAttribute.InjectionType, + parameterInfo.ParameterType, + typedGeneric + ); } } } diff --git a/Server/Logger/WebApplicationLogger.cs b/Server/Logger/WebApplicationLogger.cs index 455d16ca..aaf45f05 100644 --- a/Server/Logger/WebApplicationLogger.cs +++ b/Server/Logger/WebApplicationLogger.cs @@ -12,7 +12,7 @@ public class SptWebApplicationLogger : ISptLogger public SptWebApplicationLogger(ILoggerProvider provider) { - _logger = provider.CreateLogger(typeof(T).FullName); + _logger = provider.CreateLogger(typeof(T).FullName ?? "SPT Logger Default Name"); } public void LogWithColor( diff --git a/Server/Program.cs b/Server/Program.cs index 651c82b7..00612a5c 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -1,5 +1,3 @@ -using System.Reflection; -using Core.Annotations; using Core.Context; using Core.Models.External; using Core.Models.Spt.Config; @@ -36,7 +34,7 @@ public static class Program var serviceProvider = builder.Services.BuildServiceProvider(); var watermark = serviceProvider.GetService(); // Initialize Watermak - watermark.Initialize(); + watermark?.Initialize(); // Initialize PreSptMods var preSptLoadMods = serviceProvider.GetServices(); @@ -46,18 +44,19 @@ public static class Program } var appContext = serviceProvider.GetService(); // Add the Loaded Mod Assemblies for later - appContext.AddValue(ContextVariableType.LOADED_MOD_ASSEMBLIES, assemblies); + appContext?.AddValue(ContextVariableType.LOADED_MOD_ASSEMBLIES, assemblies); // This is the builder that will get use by the HttpServer to start up the web application - appContext.AddValue(ContextVariableType.APP_BUILDER, builder); + appContext?.AddValue(ContextVariableType.APP_BUILDER, builder); // Get the Built app and run it var app = serviceProvider.GetService(); - app.Run().Wait(); + app?.Run().Wait(); - var httpConfig = serviceProvider.GetService().GetConfig(); + var httpConfig = serviceProvider.GetService()?.GetConfig(); // When we application gets started by the HttpServer it will add into the AppContext the WebApplication // object, which we can use here to start the webapp. - (appContext.GetLatestValue(ContextVariableType.WEB_APPLICATION).GetValue()).Run($"http://{httpConfig.Ip}:{httpConfig.Port}"); + if (httpConfig != null) + (appContext?.GetLatestValue(ContextVariableType.WEB_APPLICATION)?.GetValue())?.Run($"http://{httpConfig.Ip}:{httpConfig.Port}"); } catch (Exception ex) { diff --git a/UnitTests/Tests/Test.cs b/UnitTests/Tests/Test.cs index bde5fd37..72e3b20d 100644 --- a/UnitTests/Tests/Test.cs +++ b/UnitTests/Tests/Test.cs @@ -8,7 +8,7 @@ namespace UnitTests.Tests; [TestClass] public class Test { - private Templates _templates; + private Templates? _templates; [TestInitialize] public void Setup() diff --git a/UnitTests/Tests/Utils/HashUtilTests.cs b/UnitTests/Tests/Utils/HashUtilTests.cs index f23023a1..02b08d80 100644 --- a/UnitTests/Tests/Utils/HashUtilTests.cs +++ b/UnitTests/Tests/Utils/HashUtilTests.cs @@ -5,52 +5,52 @@ namespace UnitTests.Tests.Utils; [TestClass] public class HashUtilTests { - // protected HashUtil _hashUtil = new(new RandomUtil()); - // - // [TestMethod] - // public void GenerateTest() - // { - // // Generate 100 MongoId's - // for (var i = 0; i < 100; i++) - // { - // // Invalid mongoId character - // var result = _hashUtil.Generate(); - // - // // Invalid mongoId length - // var test = _hashUtil.IsValidMongoId(result); - // - // Assert.AreEqual( - // true, - // test, - // $"IsValidMongoId() `{result}` is not a valid MongoId."); - // } - // } - // - // [TestMethod] - // public void IsValidMongoIdTest() - // { - // // Invalid mongoId character - // var ResultBadChar = _hashUtil.IsValidMongoId("677ddb67406e9918a0264bbz"); - // - // Assert.AreEqual( - // false, - // ResultBadChar, - // "IsValidMongoId() `677ddb67406e9918a0264bbz` contains invalid char `z`, but result was true"); - // - // // Invalid mongoId length - // var resultBadLength = _hashUtil.IsValidMongoId("677ddb67406e9918a0264bbcc"); - // - // Assert.AreEqual( - // false, - // resultBadLength, - // "IsValidMongoId() `677ddb67406e9918a0264bbcc` is 25 characters, but result was true"); - // - // // Valid mongoId - // var resultPass = _hashUtil.IsValidMongoId("677ddb67406e9918a0264bbc"); - // - // Assert.AreEqual( - // true, - // resultPass, - // "IsValidMongoId() `677ddb67406e9918a0264bbc` is a valid mongoId, but result was false"); - // } + protected HashUtil _hashUtil = new(new RandomUtil()); + + [TestMethod] + public void GenerateTest() + { + // Generate 100 MongoId's + for (var i = 0; i < 100; i++) + { + // Invalid mongoId character + var result = _hashUtil.Generate(); + + // Invalid mongoId length + var test = _hashUtil.IsValidMongoId(result); + + Assert.AreEqual( + true, + test, + $"IsValidMongoId() `{result}` is not a valid MongoId."); + } + } + + [TestMethod] + public void IsValidMongoIdTest() + { + // Invalid mongoId character + var ResultBadChar = _hashUtil.IsValidMongoId("677ddb67406e9918a0264bbz"); + + Assert.AreEqual( + false, + ResultBadChar, + "IsValidMongoId() `677ddb67406e9918a0264bbz` contains invalid char `z`, but result was true"); + + // Invalid mongoId length + var resultBadLength = _hashUtil.IsValidMongoId("677ddb67406e9918a0264bbcc"); + + Assert.AreEqual( + false, + resultBadLength, + "IsValidMongoId() `677ddb67406e9918a0264bbcc` is 25 characters, but result was true"); + + // Valid mongoId + var resultPass = _hashUtil.IsValidMongoId("677ddb67406e9918a0264bbc"); + + Assert.AreEqual( + true, + resultPass, + "IsValidMongoId() `677ddb67406e9918a0264bbc` is a valid mongoId, but result was false"); + } } diff --git a/UnitTests/Tests/Utils/JsonUtilTests.cs b/UnitTests/Tests/Utils/JsonUtilTests.cs index 0c50a426..f7f72745 100644 --- a/UnitTests/Tests/Utils/JsonUtilTests.cs +++ b/UnitTests/Tests/Utils/JsonUtilTests.cs @@ -12,8 +12,8 @@ public class JsonUtilTests { var value = new Dictionary { { QuestStatusEnum.AvailableForStart, 1 } }; var result = _jsonUtil.Deserialize>(_jsonUtil.Serialize(value)); - Assert.AreEqual(value.Count, result.Count); - Assert.AreEqual(value.First().Key, result.First().Key); - Assert.AreEqual(value.First().Value, result.First().Value); + Assert.AreEqual(value.Count, result?.Count); + Assert.AreEqual(value.First().Key, result?.First().Key); + Assert.AreEqual(value.First().Value, result?.First().Value); } } diff --git a/UnitTests/Tests/Utils/MathUtilTests.cs b/UnitTests/Tests/Utils/MathUtilTests.cs index 5adaada3..e8bc1b4e 100644 --- a/UnitTests/Tests/Utils/MathUtilTests.cs +++ b/UnitTests/Tests/Utils/MathUtilTests.cs @@ -1,83 +1,83 @@ -// using Core.Utils; -// -// namespace UnitTests.Tests.Utils; -// -// [TestClass] -// public class MathUtilTests -// { -// protected MathUtil _mathUtil = new(); -// -// [TestMethod] -// public void ListSumTest() -// { -// var test = new List { 1.1f, 2.1f, 3.3f }; -// const float expected = 6.5f; -// -// var actual = _mathUtil.ListSum(test); -// -// Assert.AreEqual(expected, actual, -// $"ListSum() Expected: {expected}, Actual: {actual}"); -// } -// -// [TestMethod] -// public void ListCumSumTest() -// { -// var test = new List { 1f, 2f, 3f, 4f }; -// var expected = new List { 1f, 3f, 6f, 10f }; -// -// var actual = _mathUtil.ListCumSum(test); -// -// for (var i = 0; i < actual.Count; i++) -// { -// if (Math.Abs(expected[i] - actual[i]) > 0.00001f) -// { -// Assert.Fail($"ListCumSum() Expected: {string.Join(", ", expected)}, Actual: {string.Join(", ", actual)}"); -// } -// } -// } -// -// [TestMethod] -// public void ListProductTest() -// { -// var test = new List { 1f, 2f, 3f, 4f }; -// var expected = new List { 2f, 4f, 6f, 8f }; -// -// var actual = _mathUtil.ListProduct(test, 2); -// -// for (var i = 0; i < actual.Count; i++) -// { -// if (Math.Abs(expected[i] - actual[i]) > 0.00001f) -// { -// Assert.Fail($"ListProduct() Expected: {string.Join(", ", expected)}, Actual: {string.Join(", ", actual)}"); -// } -// } -// } -// -// [TestMethod] -// public void ListAddTest() -// { -// var test = new List { 1f, 2f, 3f, 4f }; -// var expected = new List { 3f, 4f, 5f, 6f }; -// -// var actual = _mathUtil.ListAdd(test, 2); -// -// for (var i = 0; i < actual.Count; i++) -// { -// if (Math.Abs(expected[i] - actual[i]) > 0.00001f) -// { -// Assert.Fail($"ListProduct() Expected: {string.Join(", ", expected)}, Actual: {string.Join(", ", actual)}"); -// } -// } -// } -// -// [TestMethod] -// public void MapToRangeTest() -// { -// const double expected = 2; -// -// var actual = _mathUtil.MapToRange(0.5, 0, 1, 1, 3); -// -// Assert.AreEqual(expected, actual, -// $"MapToRange() Expected: {expected}, Actual: {actual}"); -// } -// } +using Core.Utils; + +namespace UnitTests.Tests.Utils; + +[TestClass] +public class MathUtilTests +{ + protected MathUtil _mathUtil = new(); + + [TestMethod] + public void ListSumTest() + { + var test = new List { 1.1f, 2.1f, 3.3f }; + const double expected = 6.5f; + + var actual = _mathUtil.ListSum(test); + + Assert.AreEqual(expected, actual, + $"ListSum() Expected: {expected}, Actual: {actual}"); + } + + [TestMethod] + public void ListCumSumTest() + { + var test = new List { 1f, 2f, 3f, 4f }; + var expected = new List { 1f, 3f, 6f, 10f }; + + var actual = _mathUtil.ListCumSum(test); + + for (var i = 0; i < actual.Count; i++) + { + if (Math.Abs(expected[i] - actual[i]) > 0.00001f) + { + Assert.Fail($"ListCumSum() Expected: {string.Join(", ", expected)}, Actual: {string.Join(", ", actual)}"); + } + } + } + + [TestMethod] + public void ListProductTest() + { + var test = new List { 1f, 2f, 3f, 4f }; + var expected = new List { 2f, 4f, 6f, 8f }; + + var actual = _mathUtil.ListProduct(test, 2); + + for (var i = 0; i < actual.Count; i++) + { + if (Math.Abs(expected[i] - actual[i]) > 0.00001f) + { + Assert.Fail($"ListProduct() Expected: {string.Join(", ", expected)}, Actual: {string.Join(", ", actual)}"); + } + } + } + + [TestMethod] + public void ListAddTest() + { + var test = new List { 1f, 2f, 3f, 4f }; + var expected = new List { 3f, 4f, 5f, 6f }; + + var actual = _mathUtil.ListAdd(test, 2); + + for (var i = 0; i < actual.Count; i++) + { + if (Math.Abs(expected[i] - actual[i]) > 0.00001f) + { + Assert.Fail($"ListProduct() Expected: {string.Join(", ", expected)}, Actual: {string.Join(", ", actual)}"); + } + } + } + + [TestMethod] + public void MapToRangeTest() + { + const double expected = 2; + + var actual = _mathUtil.MapToRange(0.5, 0, 1, 1, 3); + + Assert.AreEqual(expected, actual, + $"MapToRange() Expected: {expected}, Actual: {actual}"); + } +} diff --git a/UnitTests/Tests/Utils/RandomUtilTests.cs b/UnitTests/Tests/Utils/RandomUtilTests.cs index 9aa8f9b9..22ca5f19 100644 --- a/UnitTests/Tests/Utils/RandomUtilTests.cs +++ b/UnitTests/Tests/Utils/RandomUtilTests.cs @@ -5,179 +5,179 @@ namespace UnitTests.Tests.Utils; [TestClass] public sealed class RandomUtilTests { - // protected RandomUtil _randomUtil = new(); - // - // [TestMethod] - // public void GetIntTest() - // { - // // Run 100 test cases - // for (var i = 0; i < 100; i++) - // { - // var result = _randomUtil.GetInt(0, 10); - // - // if (result < 0 || result > 10) - // { - // Assert.Fail($"GetInt(0, 10) out of range. Expected range [0, 10] but was {result}."); - // } - // } - // } - // - // [TestMethod] - // public void GetIntExTest() - // { - // // Run 100 test cases - // for (var i = 0; i < 100; i++) - // { - // var result = _randomUtil.GetIntEx(10); - // - // if (result < 1 || result > 9) - // { - // Assert.Fail($"GetInt(10) out of range. Expected range [1, 9] but was {result}."); - // } - // } - // } - // - // [TestMethod] - // public void GetFloatTest() - // { - // // Run 100 test cases - // for (var i = 0; i < 100; i++) - // { - // var result = _randomUtil.GetFloat(0f, 10f); - // - // if (result < 0f || result >= 9f) - // { - // Assert.Fail($"GetFloat(0f, 10f) out of range. Expected range [0.0f, 9.999f] but was {result}."); - // } - // } - // } - // - // [TestMethod] - // public void GetPercentOfValueTest() - // { - // const float expected = 45.5f; - // var result = _randomUtil.GetPercentOfValue(45.5f, 100f); - // - // Assert.AreEqual( - // expected, - // result, - // 0.0001f, - // $"GetPercentOfValue(45.5f, 100f) out of range. Expected: {expected}. Actual: {result}."); - // } - // - // [TestMethod] - // public void ReduceValueByPercentTest() - // { - // const float expected = 54.5f; - // var result = _randomUtil.ReduceValueByPercent(100f, 45.5f); - // - // Assert.AreEqual( - // expected, - // result, - // 0.0001f, - // $"ReduceValueByPercent(100f, 45.5f) out of range. Expected: {expected}. Actual: {result}."); - // } - // - // [TestMethod] - // public void GetChance100Test() - // { - // for (var i = 0; i < 100; i++) - // { - // const bool expectedTrue = true; - // var resultTrue = _randomUtil.GetChance100(100f); - // - // Assert.AreEqual( - // expectedTrue, - // resultTrue, - // $"GetChance100(100f) out of range. Expected: {expectedTrue}. Actual: {resultTrue}."); - // } - // - // for (var i = 0; i < 100; i++) - // { - // const bool expectedFalse = false; - // var resultFalse = _randomUtil.GetChance100(0f); - // - // Assert.AreEqual( - // expectedFalse, - // resultFalse, - // $"GetChance100(0f) out of range. Expected: {expectedFalse}. Actual: {resultFalse}."); - // } - // } - // - // // TODO: Missing methods between these two - // - // [TestMethod] - // public void RandIntTest() - // { - // for (var i = 0; i < 100; i++) - // { - // var result = _randomUtil.RandInt(0, 10); - // - // if (result < 0 || result > 9) - // { - // Assert.Fail($"RandInt(0, 10) out of range. Expected range [0, 9] but was {result}."); - // } - // } - // - // for (var i = 0; i < 100; i++) - // { - // var result = _randomUtil.RandInt(10); - // - // if (result < 0 || result > 9) - // { - // Assert.Fail($"RandInt(10, null) out of range. Expected range [0, 9] but was {result}."); - // } - // } - // } - // - // [TestMethod] - // public void RandNumTest() - // { - // for (var i = 0; i < 100; i++) - // { - // var result = _randomUtil.RandNum(0, 10); - // - // if (result < 0 || result > 9) - // { - // Assert.Fail($"RandNum(0, 10) out of range. Expected range [0, 9.999d] but was {result}."); - // } - // - // if (_randomUtil.GetNumberPrecision(result) > RandomUtil.MaxSignificantDigits) - // { - // Assert.Fail($"RandNum(0, 10) precision of {result} exceeds the allowable precision ({RandomUtil.MaxSignificantDigits}) for the given values."); - // } - // } - // - // for (var i = 0; i < 100; i++) - // { - // var result = _randomUtil.RandNum(10); - // - // if (result < 0 || result > 9) - // { - // Assert.Fail($"RandNum(10) out of range. Expected range [0, 9.999d] but was {result}."); - // } - // - // if (_randomUtil.GetNumberPrecision(result) > RandomUtil.MaxSignificantDigits) - // { - // Assert.Fail($"RandNum(10) precision of {result} exceeds the allowable precision ({RandomUtil.MaxSignificantDigits}) for the given values."); - // } - // } - // } - // - // [TestMethod] - // public void ShuffleTest() - // { - // var testList = new List() - // { - // 1,2,3,4,5,6,7,8,9,10 - // }; - // - // var orig = new List(testList); - // - // var result = _randomUtil.Shuffle(testList); - // - // Assert.IsFalse( - // result.SequenceEqual(orig), - // $"Shuffle test failed. Expected: {string.Join(", ", orig)}, but got {string.Join(", ", result)}"); - // } + private RandomUtil _randomUtil = new(); + + [TestMethod] + public void GetIntTest() + { + // Run 100 test cases + for (var i = 0; i < 100; i++) + { + var result = _randomUtil.GetInt(0, 10); + + if (result < 0 || result > 10) + { + Assert.Fail($"GetInt(0, 10) out of range. Expected range [0, 10] but was {result}."); + } + } + } + + [TestMethod] + public void GetIntExTest() + { + // Run 100 test cases + for (var i = 0; i < 100; i++) + { + var result = _randomUtil.GetIntEx(10); + + if (result < 1 || result > 9) + { + Assert.Fail($"GetInt(10) out of range. Expected range [1, 9] but was {result}."); + } + } + } + + [TestMethod] + public void GetFloatTest() + { + // Run 100 test cases + for (var i = 0; i < 100; i++) + { + var result = _randomUtil.GetFloat(0f, 10f); + + if (result < 0f || result >= 9f) + { + Assert.Fail($"GetFloat(0f, 10f) out of range. Expected range [0.0f, 9.999f] but was {result}."); + } + } + } + + [TestMethod] + public void GetPercentOfValueTest() + { + const float expected = 45.5f; + var result = _randomUtil.GetPercentOfValue(45.5f, 100f); + + Assert.AreEqual( + expected, + result, + 0.0001f, + $"GetPercentOfValue(45.5f, 100f) out of range. Expected: {expected}. Actual: {result}."); + } + + [TestMethod] + public void ReduceValueByPercentTest() + { + const float expected = 54.5f; + var result = _randomUtil.ReduceValueByPercent(100f, 45.5f); + + Assert.AreEqual( + expected, + result, + 0.0001f, + $"ReduceValueByPercent(100f, 45.5f) out of range. Expected: {expected}. Actual: {result}."); + } + + [TestMethod] + public void GetChance100Test() + { + for (var i = 0; i < 100; i++) + { + const bool expectedTrue = true; + var resultTrue = _randomUtil.GetChance100(100f); + + Assert.AreEqual( + expectedTrue, + resultTrue, + $"GetChance100(100f) out of range. Expected: {expectedTrue}. Actual: {resultTrue}."); + } + + for (var i = 0; i < 100; i++) + { + const bool expectedFalse = false; + var resultFalse = _randomUtil.GetChance100(0f); + + Assert.AreEqual( + expectedFalse, + resultFalse, + $"GetChance100(0f) out of range. Expected: {expectedFalse}. Actual: {resultFalse}."); + } + } + + // TODO: Missing methods between these two + + [TestMethod] + public void RandIntTest() + { + for (var i = 0; i < 100; i++) + { + var result = _randomUtil.RandInt(0, 10); + + if (result < 0 || result > 9) + { + Assert.Fail($"RandInt(0, 10) out of range. Expected range [0, 9] but was {result}."); + } + } + + for (var i = 0; i < 100; i++) + { + var result = _randomUtil.RandInt(10); + + if (result < 0 || result > 9) + { + Assert.Fail($"RandInt(10, null) out of range. Expected range [0, 9] but was {result}."); + } + } + } + + [TestMethod] + public void RandNumTest() + { + for (var i = 0; i < 100; i++) + { + var result = _randomUtil.RandNum(0, 10); + + if (result < 0 || result > 9) + { + Assert.Fail($"RandNum(0, 10) out of range. Expected range [0, 9.999d] but was {result}."); + } + + if (_randomUtil.GetNumberPrecision(result) > RandomUtil.MaxSignificantDigits) + { + Assert.Fail($"RandNum(0, 10) precision of {result} exceeds the allowable precision ({RandomUtil.MaxSignificantDigits}) for the given values."); + } + } + + for (var i = 0; i < 100; i++) + { + var result = _randomUtil.RandNum(10); + + if (result < 0 || result > 9) + { + Assert.Fail($"RandNum(10) out of range. Expected range [0, 9.999d] but was {result}."); + } + + if (_randomUtil.GetNumberPrecision(result) > RandomUtil.MaxSignificantDigits) + { + Assert.Fail($"RandNum(10) precision of {result} exceeds the allowable precision ({RandomUtil.MaxSignificantDigits}) for the given values."); + } + } + } + + [TestMethod] + public void ShuffleTest() + { + var testList = new List() + { + 1,2,3,4,5,6,7,8,9,10 + }; + + var orig = new List(testList); + + var result = _randomUtil.Shuffle(testList); + + Assert.IsFalse( + result.SequenceEqual(orig), + $"Shuffle test failed. Expected: {string.Join(", ", orig)}, but got {string.Join(", ", result)}"); + } }