m3
This commit is contained in:
165
tests/CacheItemTest.cs
Normal file
165
tests/CacheItemTest.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Skeleton.DataStructure;
|
||||
|
||||
namespace SkeletonTest.tests;
|
||||
|
||||
public static class CacheItemTest
|
||||
{
|
||||
|
||||
private class TestClass
|
||||
{
|
||||
public TestClass(int v) => Value = v;
|
||||
|
||||
public int UpdateCount { get; private set; } = 0;
|
||||
public int ReferenceCount { get; private set; } = 0;
|
||||
private int InnerValue { get; set; } = 0;
|
||||
public int Value {
|
||||
get
|
||||
{
|
||||
ReferenceCount += 1;
|
||||
return InnerValue;
|
||||
}
|
||||
set
|
||||
{
|
||||
UpdateCount += 1;
|
||||
InnerValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
public static TestClass operator +(TestClass self, TestClass other) => new (self.Value + other.Value);
|
||||
public static TestClass operator *(TestClass self, TestClass other) => new(self.Value * other.Value);
|
||||
public static TestClass operator *(TestClass self, int other) => new(self.Value * other);
|
||||
public static TestClass operator +(TestClass self, int other) => new(self.Value + other);
|
||||
public static TestClass operator *(int other, TestClass self) => self * other;
|
||||
public static TestClass operator +(int other, TestClass self) => self + other;
|
||||
public static implicit operator TestClass(int x) => new (x);
|
||||
}
|
||||
|
||||
private class TestClass2
|
||||
{
|
||||
public TestClass2()
|
||||
{
|
||||
BA = new(x => Childs.Count);
|
||||
BB = new(x => Childs.Select(c => c.BA.GetFrom(x)).Sum());
|
||||
}
|
||||
public HashSet<TestClass2> Childs { get; set; } = new();
|
||||
public int A { get; set; }
|
||||
public CacheItem<int> BA { get; set; }
|
||||
public CacheItem<int> BB { get; set; }
|
||||
|
||||
public void Add(TestClass2 x)
|
||||
{
|
||||
BA.Expire();
|
||||
BB.Expire();
|
||||
Childs.Add(x);
|
||||
}
|
||||
|
||||
public void Remove(TestClass2 x)
|
||||
{
|
||||
BA.Expire();
|
||||
BB.Expire();
|
||||
Childs.Remove(x);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void Test()
|
||||
{
|
||||
|
||||
CacheItem<TestClass> t0 = new CacheItem<TestClass>(_ => 0);
|
||||
CacheItem<TestClass> a = new CacheItem<TestClass>(_ => 1);
|
||||
CacheItem<TestClass> b = new CacheItem<TestClass>(x => a.GetFrom(x).Value * 2);
|
||||
CacheItem<TestClass> c =
|
||||
new CacheItem<TestClass>(x => b.GetFrom(x).Value * b.GetFrom(x).Value);
|
||||
CacheItem<TestClass> d = new CacheItem<TestClass>(x => b.GetFrom(x).Value - a.GetFrom(x).Value + t0.GetFrom(x));
|
||||
|
||||
Assert.IsTrue(a.Expired);
|
||||
Assert.IsTrue(b.Expired);
|
||||
Assert.IsTrue(c.Expired);
|
||||
Assert.IsTrue(d.Expired);
|
||||
Assert.That(a.Get.Value, Is.EqualTo(1));
|
||||
Assert.That(b.Get.Value, Is.EqualTo(2));
|
||||
Assert.That(c.Get.Value, Is.EqualTo(4));
|
||||
Assert.That(d.Get.Value, Is.EqualTo(1));
|
||||
a.UpdateCalculation(_ => 3);
|
||||
Assert.IsTrue(a.Expired);
|
||||
Assert.IsTrue(b.Expired);
|
||||
Assert.IsTrue(c.Expired);
|
||||
Assert.IsTrue(d.Expired);
|
||||
Assert.That(a.Get.Value, Is.EqualTo(3));
|
||||
Assert.That(b.Get.Value, Is.EqualTo(6));
|
||||
Assert.That(c.Get.Value, Is.EqualTo(36));
|
||||
Assert.That(d.Get.Value, Is.EqualTo(3));
|
||||
b.UpdateCalculation(_ => 9);
|
||||
Assert.IsFalse(a.Expired);
|
||||
Assert.IsTrue(b.Expired);
|
||||
Assert.IsTrue(c.Expired);
|
||||
Assert.IsTrue(d.Expired);
|
||||
Assert.That(a.Get.Value, Is.EqualTo(3));
|
||||
Assert.That(b.Get.Value, Is.EqualTo(9));
|
||||
Assert.That(c.Get.Value, Is.EqualTo(81));
|
||||
Assert.That(d.Get.Value, Is.EqualTo(6));
|
||||
c.UpdateCalculation(x => a.GetFrom(x) * b.GetFrom(x));
|
||||
Assert.IsFalse(a.Expired);
|
||||
Assert.IsFalse(b.Expired);
|
||||
Assert.IsTrue(c.Expired);
|
||||
Assert.IsFalse(d.Expired);
|
||||
Assert.That(a.Get.Value, Is.EqualTo(3));
|
||||
Assert.That(b.Get.Value, Is.EqualTo(9));
|
||||
Assert.That(c.Get.Value, Is.EqualTo(27));
|
||||
Assert.That(d.Get.Value, Is.EqualTo(6));
|
||||
d.UpdateCalculation(x => a.GetFrom(x) + b.GetFrom(x) + c.GetFrom(x));
|
||||
Assert.IsFalse(a.Expired);
|
||||
Assert.IsFalse(b.Expired);
|
||||
Assert.IsFalse(c.Expired);
|
||||
Assert.IsTrue(d.Expired);
|
||||
Assert.That(a.Get.Value, Is.EqualTo(3));
|
||||
Assert.That(b.Get.Value, Is.EqualTo(9));
|
||||
Assert.That(c.Get.Value, Is.EqualTo(27));
|
||||
Assert.That(d.Get.Value, Is.EqualTo(39));
|
||||
Assert.That(t0.Get.UpdateCount, Is.EqualTo(1));
|
||||
Assert.That(t0.Get.ReferenceCount, Is.EqualTo(3));
|
||||
int noUse = d.Get.Value;
|
||||
noUse += d.Get.Value;
|
||||
noUse += d.Get.Value;
|
||||
Assert.That(noUse, Is.EqualTo(117));
|
||||
Assert.That(t0.Get.ReferenceCount, Is.EqualTo(3));
|
||||
|
||||
|
||||
}
|
||||
[Test]
|
||||
public static void Test2()
|
||||
{
|
||||
CacheItem<int> a = new (_ => 1);
|
||||
CacheItem<int> b = new (_ => 3);
|
||||
CacheItem<int> c = new(x => a.GetFrom(x) + b.GetFrom(x));
|
||||
Assert.IsTrue(a.Expired);
|
||||
Assert.IsTrue(b.Expired);
|
||||
Assert.IsTrue(c.Expired);
|
||||
Assert.That(a.Get, Is.EqualTo(1));
|
||||
Assert.That(b.Get, Is.EqualTo(3));
|
||||
Assert.That(c.Get, Is.EqualTo(4));
|
||||
Assert.IsFalse(a.Expired);
|
||||
Assert.IsFalse(b.Expired);
|
||||
Assert.IsFalse(c.Expired);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void Test3()
|
||||
{
|
||||
TestClass2 a = new();
|
||||
TestClass2 b = new();
|
||||
TestClass2 c = new();
|
||||
a.Add(b);
|
||||
a.Add(c);
|
||||
b.Add(c);
|
||||
Assert.That(a.BA.Get, Is.EqualTo(2));
|
||||
Assert.That(a.BB.Get, Is.EqualTo(1));
|
||||
a.Remove(b);
|
||||
Assert.That(a.BA.Get, Is.EqualTo(1));
|
||||
Assert.That(a.BB.Get, Is.EqualTo(0));
|
||||
Assert.That(b.BA.Get, Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user