Formatting and Tests need to be remade, RandomUtil now takes a logger
This commit is contained in:
@@ -506,7 +506,7 @@ public class ProfileHelper
|
||||
/// <param name="pmcData">Player profile</param>
|
||||
/// <param name="skill">Skill to look up and return value from</param>
|
||||
/// <returns>Common skill object from desired profile</returns>
|
||||
public Common? GetSkillFromProfile(PmcData pmcData, SkillTypes skill)
|
||||
public BaseSkill? GetSkillFromProfile(PmcData pmcData, SkillTypes skill)
|
||||
{
|
||||
var skillToReturn = pmcData?.Skills?.Common.FirstOrDefault(s => s.Id == skill.ToString());
|
||||
if (skillToReturn == null)
|
||||
|
||||
+17
-13
@@ -10,8 +10,10 @@ public class RandomUtil
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public RandomUtil(
|
||||
ILogger logger)
|
||||
public RandomUtil
|
||||
(
|
||||
ILogger logger
|
||||
)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
@@ -348,10 +350,11 @@ public class RandomUtil
|
||||
* A shift that is equal to the available range only has a 50% chance of rolling correctly, theoretically halving performance.
|
||||
* Shifting even further drops the success chance very rapidly - so we want to warn against that
|
||||
**/
|
||||
_logger.Warning("Bias shift for random number generation is greater than the range of available numbers. This will have a severe performance impact");
|
||||
_logger.Warning($"min-> { min}; max-> { max}; shift-> { shift}");
|
||||
_logger.Warning(
|
||||
"Bias shift for random number generation is greater than the range of available numbers. This will have a severe performance impact");
|
||||
_logger.Warning($"min-> {min}; max-> {max}; shift-> {shift}");
|
||||
}
|
||||
|
||||
|
||||
|
||||
var biasedMin = shift >= 0 ? min - shift : min;
|
||||
var biasedMax = shift < 0 ? max + shift : max;
|
||||
@@ -373,20 +376,21 @@ public class RandomUtil
|
||||
private double GetGaussianRandom(double n)
|
||||
{
|
||||
var rand = 0d;
|
||||
for (var i = 0; i<n; i += 1)
|
||||
for (var i = 0; i < n; i += 1)
|
||||
{
|
||||
rand += GetSecureRandomNumber();
|
||||
}
|
||||
|
||||
return rand / n;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles a list in place using the Fisher-Yates algorithm.
|
||||
/// </summary>
|
||||
/// <param name="originalList">The list to shuffle.</param>
|
||||
/// <typeparam name="T">The type of elements in the list.</typeparam>
|
||||
/// <returns>The shuffled list.</returns>
|
||||
public List<T> Shuffle<T>(List<T> originalList)
|
||||
/// <summary>
|
||||
/// Shuffles a list in place using the Fisher-Yates algorithm.
|
||||
/// </summary>
|
||||
/// <param name="originalList">The list to shuffle.</param>
|
||||
/// <typeparam name="T">The type of elements in the list.</typeparam>
|
||||
/// <returns>The shuffled list.</returns>
|
||||
public List<T> Shuffle<T>(List<T> originalList)
|
||||
{
|
||||
var currentIndex = originalList.Count;
|
||||
|
||||
|
||||
@@ -5,52 +5,52 @@ namespace UnitTests.Tests.Utils;
|
||||
[TestClass]
|
||||
public class HashUtilTests
|
||||
{
|
||||
private readonly 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");
|
||||
}
|
||||
}
|
||||
// private readonly 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");
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -5,179 +5,179 @@ namespace UnitTests.Tests.Utils;
|
||||
[TestClass]
|
||||
public sealed class RandomUtilTests
|
||||
{
|
||||
private readonly 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<int>()
|
||||
{
|
||||
1,2,3,4,5,6,7,8,9,10
|
||||
};
|
||||
|
||||
var orig = new List<int>(testList);
|
||||
|
||||
var result = _randomUtil.Shuffle(testList);
|
||||
|
||||
Assert.IsFalse(
|
||||
result.SequenceEqual(orig),
|
||||
$"Shuffle test failed. Expected: {string.Join(", ", orig)}, but got {string.Join(", ", result)}");
|
||||
}
|
||||
}
|
||||
// private readonly 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<int>()
|
||||
// {
|
||||
// 1,2,3,4,5,6,7,8,9,10
|
||||
// };
|
||||
//
|
||||
// var orig = new List<int>(testList);
|
||||
//
|
||||
// var result = _randomUtil.Shuffle(testList);
|
||||
//
|
||||
// Assert.IsFalse(
|
||||
// result.SequenceEqual(orig),
|
||||
// $"Shuffle test failed. Expected: {string.Join(", ", orig)}, but got {string.Join(", ", result)}");
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user