diff --git a/Foundation.Data.Doublets.Cli.Tests/PinnedTypesDecoratorTests.cs b/Foundation.Data.Doublets.Cli.Tests/PinnedTypesDecoratorTests.cs new file mode 100644 index 0000000..7ed269f --- /dev/null +++ b/Foundation.Data.Doublets.Cli.Tests/PinnedTypesDecoratorTests.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Platform.Data; +using Platform.Data.Doublets; +using Platform.Data.Doublets.Memory.United.Generic; +using Platform.Delegates; +using Xunit; + +namespace Foundation.Data.Doublets.Cli.Tests +{ + public class PinnedTypesDecoratorTests + { + [Fact] + public void Should_Implement_Both_ILinks_And_IPinnedTypes() + { + // Arrange + var tempDbFile = Path.GetTempFileName(); + try + { + using var links = new UnitedMemoryLinks(tempDbFile); + + // Act + var decorator = new PinnedTypesDecorator(links); + + // Assert - Should implement both interfaces + Assert.IsAssignableFrom>(decorator); + Assert.IsAssignableFrom>(decorator); + } + finally + { + File.Delete(tempDbFile); + } + } + + [Fact] + public void Should_Enumerate_PinnedTypes() + { + // Arrange + var tempDbFile = Path.GetTempFileName(); + try + { + using var links = new UnitedMemoryLinks(tempDbFile); + var decorator = new PinnedTypesDecorator(links); + var numberOfTypes = 3; + + // Act + var result = new List(); + foreach (var type in decorator.Take(numberOfTypes)) + { + result.Add(type); + } + + // Assert + Assert.Equal(numberOfTypes, result.Count); + Assert.Equal(new ulong[] { 1, 2, 3 }, result); + } + finally + { + File.Delete(tempDbFile); + } + } + + [Fact] + public void Should_Support_Deconstruction() + { + // Arrange + var tempDbFile = Path.GetTempFileName(); + try + { + using var links = new UnitedMemoryLinks(tempDbFile); + var decorator = new PinnedTypesDecorator(links); + var initialSource = 1UL; + + // Pre-create links to ensure they exist + links.GetOrCreate(initialSource, 1UL); + links.GetOrCreate(initialSource, 2UL); + links.GetOrCreate(initialSource, 3UL); + + // Act + var (type1, type2, type3) = decorator; + + // Assert + Assert.Equal(1UL, type1); + Assert.Equal(2UL, type2); + Assert.Equal(3UL, type3); + } + finally + { + File.Delete(tempDbFile); + } + } + + [Fact] + public void Should_Work_As_ILinks_Decorator() + { + // Arrange + var tempDbFile = Path.GetTempFileName(); + try + { + using var baseLinks = new UnitedMemoryLinks(tempDbFile); + var decorator = new PinnedTypesDecorator(baseLinks); + + // Act & Assert - Test that it still works as a decorator and properly implements both interfaces + Assert.NotNull(decorator); + Assert.IsAssignableFrom>(decorator); + Assert.IsAssignableFrom>(decorator); + } + finally + { + File.Delete(tempDbFile); + } + } + } +} \ No newline at end of file diff --git a/Foundation.Data.Doublets.Cli/PinnedTypes.cs b/Foundation.Data.Doublets.Cli/PinnedTypes.cs index 591de18..f9e34cb 100644 --- a/Foundation.Data.Doublets.Cli/PinnedTypes.cs +++ b/Foundation.Data.Doublets.Cli/PinnedTypes.cs @@ -6,7 +6,13 @@ namespace Foundation.Data.Doublets.Cli { - public class PinnedTypes : IEnumerable + public interface IPinnedTypes : IEnumerable + where TLinkAddress : struct, System.Numerics.IUnsignedNumber + { + void Deconstruct(out TLinkAddress type1, out TLinkAddress type2, out TLinkAddress type3); + } + + public class PinnedTypes : IPinnedTypes where TLinkAddress : struct, System.Numerics.IUnsignedNumber { private readonly ILinks _links; @@ -83,5 +89,19 @@ public void Dispose() // No resources to dispose } } + + public void Deconstruct(out TLinkAddress type1, out TLinkAddress type2, out TLinkAddress type3) + { + using var enumerator = GetEnumerator(); + + enumerator.MoveNext(); + type1 = enumerator.Current; + + enumerator.MoveNext(); + type2 = enumerator.Current; + + enumerator.MoveNext(); + type3 = enumerator.Current; + } } } \ No newline at end of file diff --git a/Foundation.Data.Doublets.Cli/PinnedTypesDecorator.cs b/Foundation.Data.Doublets.Cli/PinnedTypesDecorator.cs new file mode 100644 index 0000000..07a3b12 --- /dev/null +++ b/Foundation.Data.Doublets.Cli/PinnedTypesDecorator.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Numerics; +using Platform.Data; +using Platform.Data.Doublets; +using Platform.Data.Doublets.Decorators; +using Platform.Delegates; + +namespace Foundation.Data.Doublets.Cli +{ + public class PinnedTypesDecorator : LinksDecoratorBase, IPinnedTypes + where TLinkAddress : struct, + IUnsignedNumber, + IComparisonOperators, + IShiftOperators, + IBitwiseOperators, + IMinMaxValue + { + private readonly IPinnedTypes _pinnedTypes; + + public PinnedTypesDecorator(ILinks links) : base(links) + { + _pinnedTypes = new PinnedTypes(links); + } + + // Implement IPinnedTypes interface + public IEnumerator GetEnumerator() + { + return _pinnedTypes.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public void Deconstruct(out TLinkAddress type1, out TLinkAddress type2, out TLinkAddress type3) + { + _pinnedTypes.Deconstruct(out type1, out type2, out type3); + } + } +} \ No newline at end of file