Fixes some potential memory leaks in unit tests, cleanup
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-09-23 20:18:52 +02:00
parent 2b6b3a40ed
commit 4588b2da10
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
7 changed files with 29 additions and 20 deletions

View File

@ -7,6 +7,12 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<WarningLevel>6</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors>;NU1605;CA2000</WarningsAsErrors>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<None Update="libpkmn_lib.so"> <None Update="libpkmn_lib.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

View File

@ -49,8 +49,8 @@ namespace PkmnLibSharp.Utils
return _ptr; return _ptr;
} }
} }
internal IntPtr TakeOwnership() private IntPtr TakeOwnership()
{ {
if (!_isOwner) if (!_isOwner)
{ {
@ -67,17 +67,12 @@ namespace PkmnLibSharp.Utils
return ptr; return ptr;
} }
internal void Invalidate() private void Invalidate()
{ {
_isInvalidated = true; _isInvalidated = true;
CacheHandler.RemoveCache(_ptr); CacheHandler.RemoveCache(_ptr);
} }
~ExternPointer()
{
Dispose();
}
public void Dispose() public void Dispose()
{ {
if (_isDisposed) if (_isDisposed)

View File

@ -7,6 +7,12 @@
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants>TRACE</DefineConstants>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors>;NU1605;CA2000</WarningsAsErrors>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" /> <PackageReference Include="NUnit" Version="3.13.2" />

View File

@ -9,9 +9,10 @@ namespace PkmnLibRSharpTests.StaticData
[Test] [Test]
public void BasicTests() public void BasicTests()
{ {
using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, using var stats = new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0);
new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0), new[] { "foo", "bar" }, new[] { "set" }, using var moves = new LearnableMoves();
new LearnableMoves(), Array.Empty<string>()); using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
new[] { "foo", "bar" }, new[] { "set" }, moves, Array.Empty<string>());
Assert.AreEqual("foobar", form.Name); Assert.AreEqual("foobar", form.Name);
Assert.AreEqual(0.2f, form.Height, 0.00001f); Assert.AreEqual(0.2f, form.Height, 0.00001f);
Assert.AreEqual(5.8f, form.Weight, 0.00001f); Assert.AreEqual(5.8f, form.Weight, 0.00001f);
@ -22,7 +23,6 @@ namespace PkmnLibRSharpTests.StaticData
Assert.AreEqual("foo", form.Abilities[0]); Assert.AreEqual("foo", form.Abilities[0]);
Assert.AreEqual("bar", form.Abilities[1]); Assert.AreEqual("bar", form.Abilities[1]);
Assert.AreEqual("set", form.HiddenAbilities[0]); Assert.AreEqual("set", form.HiddenAbilities[0]);
} }
} }
} }

View File

@ -13,15 +13,16 @@ namespace PkmnLibRSharpTests.StaticData.Libraries
using var lib = new SpeciesLibrary(0); using var lib = new SpeciesLibrary(0);
Assert.AreEqual(0, lib.Length); Assert.AreEqual(0, lib.Length);
} }
[Test] [Test]
public void CreateAndAdd() public void CreateAndAdd()
{ {
using var lib = new SpeciesLibrary(0); using var lib = new SpeciesLibrary(0);
Assert.AreEqual(0, lib.Length); Assert.AreEqual(0, lib.Length);
using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, using var stats = new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0);
new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0), new[] { "foo", "bar" }, new[] { "set" }, using var moves = new LearnableMoves();
new LearnableMoves(), Array.Empty<string>()); using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
new[] { "foo", "bar" }, new[] { "set" }, moves, Array.Empty<string>());
using var species = new Species(10, "testSpecies", 0.2f, "growth", 120, form, Array.Empty<string>()); using var species = new Species(10, "testSpecies", 0.2f, "growth", 120, form, Array.Empty<string>());
lib.Add("foobar", species); lib.Add("foobar", species);
Assert.AreEqual(1, lib.Length); Assert.AreEqual(1, lib.Length);

View File

@ -11,7 +11,7 @@ namespace PkmnLibRSharpTests.StaticData
[Values] Statistic decreasedStat [Values] Statistic decreasedStat
) )
{ {
var nature = new Nature(increasedStat, decreasedStat); using var nature = new Nature(increasedStat, decreasedStat);
Assert.AreEqual(increasedStat, nature.IncreasedStat); Assert.AreEqual(increasedStat, nature.IncreasedStat);
Assert.AreEqual(decreasedStat, nature.DecreasedStat); Assert.AreEqual(decreasedStat, nature.DecreasedStat);
if (increasedStat == decreasedStat) if (increasedStat == decreasedStat)

View File

@ -9,9 +9,10 @@ namespace PkmnLibRSharpTests.StaticData
[Test] [Test]
public void BasicTests() public void BasicTests()
{ {
using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, using var stats = new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0);
new StaticStatisticSet<short>(5, 10, 30, 20, 2, 0), new[] { "foo", "bar" }, new[] { "set" }, using var moves = new LearnableMoves();
new LearnableMoves(), Array.Empty<string>()); using var form = new Form("foobar", 0.2f, 5.8f, 300, new TypeIdentifier[] { new(1), new(2) }, stats,
new[] { "foo", "bar" }, new[] { "set" }, moves, Array.Empty<string>());
using var species = new Species(10, "testSpecies", 0.2f, "growth", 120, form, Array.Empty<string>()); using var species = new Species(10, "testSpecies", 0.2f, "growth", 120, form, Array.Empty<string>());
Assert.AreEqual(10, species.Id); Assert.AreEqual(10, species.Id);
Assert.AreEqual("testSpecies", species.Name); Assert.AreEqual("testSpecies", species.Name);