From 046b28f8c6c45f0e1a526a6d41a2caa60afa1777 Mon Sep 17 00:00:00 2001 From: Ynse Hoornenborg Date: Fri, 29 Dec 2023 11:00:38 +0100 Subject: [PATCH] Tests for AutoExpandingMemory class --- .../Memory/AutoExpandingMemoryTests.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/ImageSharp.Tests/Memory/AutoExpandingMemoryTests.cs diff --git a/tests/ImageSharp.Tests/Memory/AutoExpandingMemoryTests.cs b/tests/ImageSharp.Tests/Memory/AutoExpandingMemoryTests.cs new file mode 100644 index 0000000000..24ed904c0d --- /dev/null +++ b/tests/ImageSharp.Tests/Memory/AutoExpandingMemoryTests.cs @@ -0,0 +1,54 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. +using SixLabors.ImageSharp.Memory; + +// ReSharper disable InconsistentNaming +namespace SixLabors.ImageSharp.Tests.Memory; + +public class AutoExpandingMemoryTests +{ + private readonly Configuration configurtion = Configuration.Default; + + [Theory] + [InlineData(1000, 2000)] + [InlineData(1000, 1000)] + [InlineData(200, 1000)] + [InlineData(200, 200)] + [InlineData(200, 100)] + public void ExpandToRequestedCapacity(int initialCapacity, int requestedCapacity) + { + AutoExpandingMemory memory = new(this.configurtion, initialCapacity); + Span span = memory.GetSpan(requestedCapacity); + Assert.Equal(requestedCapacity, span.Length); + } + + [Theory] + [InlineData(1000, 2000)] + [InlineData(1000, 1000)] + [InlineData(200, 1000)] + [InlineData(200, 200)] + [InlineData(200, 100)] + public void KeepDataWhileExpanding(int initialCapacity, int requestedCapacity) + { + AutoExpandingMemory memory = new(this.configurtion, initialCapacity); + Span firstSpan = memory.GetSpan(initialCapacity); + firstSpan[1] = 1; + firstSpan[2] = 2; + firstSpan[3] = 3; + Span expandedSpan = memory.GetSpan(requestedCapacity); + Assert.Equal(3, firstSpan[3]); + Assert.Equal(firstSpan[3], expandedSpan[3]); + } + + [Theory] + [InlineData(1, -1)] + [InlineData(1, 0)] + [InlineData(-2, 1)] + [InlineData(-2, 0)] + public void Guards(int initialCapacity, int requestedCapacity) => + Assert.Throws(() => + { + AutoExpandingMemory memory = new(this.configurtion, initialCapacity); + _ = memory.GetSpan(requestedCapacity); + }); +}