Type Library wrapper and tests

This commit is contained in:
Deukhoofd 2020-05-06 13:32:43 +02:00
parent cc66729529
commit 8845f33d04
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 131 additions and 1 deletions

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library
{
public class TypeLibrary : PointerWrapper
{
private readonly Dictionary<string, byte> _cache =
new Dictionary<string, byte>(StringComparer.InvariantCultureIgnoreCase);
public TypeLibrary(ulong initialCapacity) : base(
Creatureliblibrary.Generated.TypeLibrary.Construct(initialCapacity))
{
}
public byte GetTypeId(string typeName)
{
if (_cache.TryGetValue(typeName, out var b))
return b;
Creatureliblibrary.Generated.TypeLibrary.GetTypeId(ref b, Ptr, typeName.ToPtr()).Assert();
_cache.Add(typeName, b);
return b;
}
public byte RegisterType(string typeName)
{
byte b = 0;
Creatureliblibrary.Generated.TypeLibrary.RegisterType(ref b, Ptr, typeName.ToPtr()).Assert();
_cache.Add(typeName, b);
return b;
}
public void SetEffectiveness(byte attackingId, byte defensiveId, float effectiveness)
{
Creatureliblibrary.Generated.TypeLibrary.SetEffectiveness(Ptr, attackingId, defensiveId, effectiveness);
}
public float GetSingleEffectiveness(byte attackingId, byte defensiveId)
{
float f = 0;
Creatureliblibrary.Generated.TypeLibrary.GetSingleEffectiveness(ref f, Ptr, attackingId, defensiveId)
.Assert();
return f;
}
public float GetEffectiveness(byte attackingId, byte[] defensiveIds)
{
float f = 0;
Creatureliblibrary.Generated.TypeLibrary.GetEffectiveness(ref f, Ptr, attackingId, defensiveIds.ArrayPtr(),
(ulong) defensiveIds.Length).Assert();
return f;
}
protected override void DeletePtr()
{
Creatureliblibrary.Generated.TypeLibrary.Destruct(Ptr);
}
}
}

Binary file not shown.

View File

@ -0,0 +1,70 @@
using System;
using NUnit.Framework;
using PkmnLibSharp.Library;
namespace PkmnLibSharpTests.Library
{
public class TypeLibraryTests
{
[Test]
public void ConstructDestruct()
{
var tl = new TypeLibrary(0);
tl.Dispose();
}
[Test]
public void RegisterType()
{
var tl = new TypeLibrary(0);
tl.RegisterType("testType1");
tl.Dispose();
}
[Test]
public void GetTypeId()
{
var tl = new TypeLibrary(0);
tl.RegisterType("testType1");
var id = tl.GetTypeId("testType1");
Assert.AreEqual(0, id);
tl.RegisterType("testType2");
id = tl.GetTypeId("testType2");
Assert.AreEqual(1, id);
tl.Dispose();
}
[Test]
public void SetEffectiveness()
{
var tl = new TypeLibrary(0);
tl.RegisterType("testType1");
tl.RegisterType("testType2");
tl.SetEffectiveness(0, 1, 0.5f);
tl.SetEffectiveness(1, 0, 2f);
tl.Dispose();
}
[Test]
public void GetSingleEffectiveness()
{
var tl = new TypeLibrary(0);
tl.RegisterType("testType1");
tl.RegisterType("testType2");
tl.SetEffectiveness(0, 1, 0.5f);
tl.SetEffectiveness(1, 0, 2f);
Assert.AreEqual(0.5f, tl.GetSingleEffectiveness(0, 1));
Assert.AreEqual(2f, tl.GetSingleEffectiveness(1, 0));
tl.Dispose();
}
[Test]
public void GetEffectiveness()
{
var tl = new TypeLibrary(0);
tl.RegisterType("testType1");
tl.RegisterType("testType2");
tl.SetEffectiveness(0, 1, 0.5f);
tl.SetEffectiveness(0, 0, 0.5f);
tl.SetEffectiveness(1, 0, 2f);
Assert.AreEqual(0.25f, tl.GetEffectiveness(0, new byte[] {0, 1}));
tl.Dispose();
}
}
}