Add flag unit tests, more xmldoc

This commit is contained in:
Kurt 2021-05-15 11:32:25 -07:00
parent ebefa0a918
commit 44affe3caa
5 changed files with 70 additions and 6 deletions

View File

@ -10,7 +10,6 @@ namespace PKHeX.Core
/// </summary>
public sealed class ShowdownSet : IBattleTemplate
{
private static readonly string[] genders = {"M", "F", ""};
private static readonly string[] StatNames = { "HP", "Atk", "Def", "SpA", "SpD", "Spe" };
private static readonly string[] Splitters = {"\r\n", "\n"};
private static readonly string[] StatSplitters = { " / ", " " };
@ -268,8 +267,13 @@ private string GetStringFirstLine(string form)
specForm = specForm.Replace("♀", "-F");
string result = GetSpeciesNickname(specForm);
if (Gender is 0 or 1) // omit genderless or nonspecific
result += $" ({genders[Gender]})";
// omit genderless or nonspecific
if (Gender is 1)
result += " (F)";
else if (Gender is 0)
result += " (M)";
if (HeldItem > 0)
{
var items = Strings.GetItemStrings(Format);

View File

@ -5,17 +5,31 @@
/// </summary>
public static class FlagUtil
{
/// <summary>
/// Gets the requested <see cref="bitIndex"/> from the byte at <see cref="offset"/>.
/// </summary>
/// <param name="arr">Buffer to read</param>
/// <param name="offset">Offset of the byte</param>
/// <param name="bitIndex">Bit to read</param>
public static bool GetFlag(byte[] arr, int offset, int bitIndex)
{
bitIndex &= 7; // ensure bit access is 0-7
return (arr[offset] >> bitIndex & 1) != 0;
}
/// <summary>
/// Sets the requested <see cref="bitIndex"/> value to the byte at <see cref="offset"/>.
/// </summary>
/// <param name="arr">Buffer to modify</param>
/// <param name="offset">Offset of the byte</param>
/// <param name="bitIndex">Bit to write</param>
/// <param name="value">Bit flag value to set</param>
public static void SetFlag(byte[] arr, int offset, int bitIndex, bool value)
{
bitIndex &= 7; // ensure bit access is 0-7
arr[offset] &= (byte)~(1 << bitIndex);
arr[offset] |= (byte)((value ? 1 : 0) << bitIndex);
var current = arr[offset] & ~(1 << bitIndex);
var newValue = current | ((value ? 1 : 0) << bitIndex);
arr[offset] = (byte)newValue;
}
}
}
}

View File

@ -4,6 +4,10 @@ namespace PKHeX.Core
{
public static partial class Util
{
/// <summary>
/// Cleans the local <see cref="fileName"/> by removing any invalid filename characters.
/// </summary>
/// <returns>New string without any invalid characters.</returns>
public static string CleanFileName(string fileName)
{
return string.Concat(fileName.Split(Path.GetInvalidFileNameChars()));

View File

@ -137,6 +137,10 @@ public static bool IsStringListCached(string fileName, out string[] result)
return stringListCache.TryGetValue(fileName, out result);
}
/// <summary>
/// Loads a text <see cref="file"/> into the program with a value of <see cref="txt"/>.
/// </summary>
/// <remarks>Caches the result array for future fetches.</remarks>
public static string[] LoadStringList(string file, string? txt)
{
if (txt == null)
@ -144,6 +148,7 @@ public static string[] LoadStringList(string file, string? txt)
string[] raw = txt.Split('\n');
for (int i = 0; i < raw.Length; i++)
{
// check for extra trimming; not all resources are "clean" with only \n line breaks.
var line = raw[i];
if (line.Length == 0)
continue;

View File

@ -73,4 +73,41 @@ public void CheckConvertBCD_Big(uint raw, int expect)
data.SequenceEqual(newData).Should().BeTrue();
}
}
public class FlagUtilTests
{
[Theory]
[InlineData(1, 0, 0)]
[InlineData(2, 0, 1)]
[InlineData(0x8000_0000, 3, 7)]
public void GetSetFlag(uint raw, int byteIndex, int bitIndex)
{
var data = BitConverter.GetBytes(raw);
var value = Core.FlagUtil.GetFlag(data, byteIndex, bitIndex);
value.Should().Be(true);
var copy = new byte[data.Length];
Core.FlagUtil.SetFlag(copy, byteIndex, bitIndex, true);
copy.SequenceEqual(data).Should().BeTrue();
}
[Theory]
[InlineData(0x7FFF_FFFE, 0, 0)]
public void ClearFlag(uint raw, int byteIndex, int bitIndex)
{
var data = BitConverter.GetBytes(raw);
var value = Core.FlagUtil.GetFlag(data, byteIndex, bitIndex);
value.Should().Be(false);
// does nothing on empty
var copy = new byte[data.Length];
Core.FlagUtil.SetFlag(copy, byteIndex, bitIndex, false);
copy.All(z => z == 0).Should().BeTrue();
// doesn't clear any other flag
copy = (byte[])data.Clone();
Core.FlagUtil.SetFlag(copy, byteIndex, bitIndex, false);
copy.SequenceEqual(data).Should().BeTrue();
}
}
}