Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Numerics;
using Platform.Data.Doublets.Decorators;
using Xunit;

using Platform.Memory;

using Platform.Data.Doublets.Memory.United.Generic;

namespace Platform.Data.Doublets.Tests
{
public static class ConsoleLoggingDecoratorTests
{
[Fact]
public static void BasicConsoleLoggingTest()
{
Using<ulong>(links =>
{
// Create a link
var link = links.Create();

// Update it
links.Update(link, link, link);

// Count links
var count = links.Count();

// Delete the link
links.Delete(link);
});
}

[Fact]
public static void ConsoleLoggingWithRestrictionsTest()
{
Using<ulong>(links =>
{
var link1 = links.Create();
var link2 = links.Create();

// Test with restrictions
var count = links.Count(new[] { links.Constants.Any, link1, links.Constants.Any });

links.Delete(link1);
links.Delete(link2);
});
}

private static void Using<TLinkAddress>(Action<ILinks<TLinkAddress>> action) where TLinkAddress : IUnsignedNumber<TLinkAddress>, IShiftOperators<TLinkAddress, int, TLinkAddress>, IBitwiseOperators<TLinkAddress, TLinkAddress, TLinkAddress>, IMinMaxValue<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
{
var unitedMemoryLinks = new UnitedMemoryLinks<TLinkAddress>(new HeapResizableDirectMemory());
ConsoleLoggingDecorator<TLinkAddress> decoratedStorage = new(unitedMemoryLinks);
action(decoratedStorage);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Numerics;
using Platform.Delegates;

namespace Platform.Data.Doublets.Decorators;

public class ConsoleLoggingDecorator<TLinkAddress> : LinksDecoratorBase<TLinkAddress> where TLinkAddress : IUnsignedNumber<TLinkAddress>
{
public ConsoleLoggingDecorator(ILinks<TLinkAddress> links) : base(links: links)
{
}

public override TLinkAddress Create(IList<TLinkAddress>? substitution, WriteHandler<TLinkAddress>? handler)
{
WriteHandlerState<TLinkAddress> handlerState = new(@continue: _constants.Continue, @break: _constants.Break, handler: handler);
return base.Create(substitution: substitution, handler: (before, after) =>
{
handlerState.Handle(before: before, after: after);
System.Console.WriteLine(value: $"Create. Before: {new Link<TLinkAddress>(values: before)}. After: {new Link<TLinkAddress>(values: after)}");
return _constants.Continue;
});
}

public override TLinkAddress Update(IList<TLinkAddress>? restriction, IList<TLinkAddress>? substitution, WriteHandler<TLinkAddress>? handler)
{
WriteHandlerState<TLinkAddress> handlerState = new(@continue: _constants.Continue, @break: _constants.Break, handler: handler);
return base.Update(restriction: restriction, substitution: substitution, handler: (before, after) =>
{
handlerState.Handle(before: before, after: after);
System.Console.WriteLine(value: $"Update. Before: {new Link<TLinkAddress>(values: before)}. After: {new Link<TLinkAddress>(values: after)}");
return _constants.Continue;
});
}

public override TLinkAddress Delete(IList<TLinkAddress>? restriction, WriteHandler<TLinkAddress>? handler)
{
WriteHandlerState<TLinkAddress> handlerState = new(@continue: _constants.Continue, @break: _constants.Break, handler: handler);
return base.Delete(restriction: restriction, handler: (before, after) =>
{
handlerState.Handle(before: before, after: after);
System.Console.WriteLine(value: $"Delete. Before: {new Link<TLinkAddress>(values: before)}. After: {new Link<TLinkAddress>(values: after)}");
return _constants.Continue;
});
}

public override TLinkAddress Count(IList<TLinkAddress>? restriction)
{
var result = base.Count(restriction: restriction);
var restrictionStr = restriction == null ? "null" : $"[{string.Join(", ", restriction)}]";
System.Console.WriteLine(value: $"Count. Restriction: {restrictionStr}. Result: {result}");
return result;
}
}
Loading