using AIStudio.Tools.DocumentSegments; using AIStudio.Tools.DocumentSegments.Html; using AIStudio.Tools.DocumentSegments.Latex; namespace AIStudio.Tests.Tools.DocumentSegments; /// /// Checks how large the text of a document may be for a format which holds it in memory, as the memory of the computer allows. /// [TestFixture] public sealed class DocumentMemoryBudgetTests { private const long GIGABYTE = 1024 * DocumentMemoryBudget.BYTES_PER_MEGABYTE; [TestCase(90, 2, ExpectedResult = 5)] [TestCase(55, 2, ExpectedResult = 9)] [TestCase(35, 2, ExpectedResult = 14)] [TestCase(90, 4, ExpectedResult = 11)] [TestCase(55, 4, ExpectedResult = 18)] [TestCase(35, 4, ExpectedResult = 29)] [TestCase(90, 16, ExpectedResult = 45)] [TestCase(55, 16, ExpectedResult = 74)] [TestCase(35, 16, ExpectedResult = 117)] [TestCase(90, 64, ExpectedResult = 182)] [TestCase(55, 64, ExpectedResult = 297)] [TestCase(35, 64, ExpectedResult = 468)] public long ADocumentMayTakeAQuarterOfTheMemory(int memoryFactor, long gigabytes) => DocumentMemoryBudget.MaxTextSize(memoryFactor, gigabytes * GIGABYTE) / DocumentMemoryBudget.BYTES_PER_MEGABYTE; [Test] public void ALargeMemoryStillKeepsTheLimitWhichDotNetCanHold() { Assert.That(DocumentMemoryBudget.MaxTextSize(35, 256 * GIGABYTE), Is.EqualTo(DocumentMemoryBudget.LARGEST_TEXT_SIZE), "No string holds more than about a billion characters."); } [Test] public void ATinyMemoryStillAllowsOneMegabyte() { Assert.That(DocumentMemoryBudget.MaxTextSize(90, 256 * DocumentMemoryBudget.BYTES_PER_MEGABYTE), Is.EqualTo(DocumentMemoryBudget.BYTES_PER_MEGABYTE), "A message must never name a limit of 0 MB."); } [Test] public void AnUnknownMemoryCountsAsASmallRaspberryPi() { Assert.That(DocumentMemoryBudget.MaxTextSize(90, 0), Is.EqualTo(DocumentMemoryBudget.MaxTextSize(90, 4 * GIGABYTE))); } [Test] public void TheFormatsFollowTheirMemoryFactors() { var html = new HtmlSegmentFormat().MaxTextSize; var markdown = new MarkdownSegmentFormat().MaxTextSize; var latex = new LatexSegmentFormat().MaxTextSize; var office = DocumentSegmentFormats.For("presentation.pptx")!.MaxTextSize; using (Assert.EnterMultipleScope()) { Assert.That(html, Is.LessThanOrEqualTo(markdown), "HtmlAgilityPack takes the most memory per byte."); Assert.That(markdown, Is.LessThanOrEqualTo(latex), "The tree of Markdig takes more memory per byte than the parser of LaTeX."); Assert.That(latex, Is.LessThanOrEqualTo(office), "The DOM of the SDK takes the least memory per byte of XML."); Assert.That(html, Is.InRange(DocumentMemoryBudget.BYTES_PER_MEGABYTE, DocumentMemoryBudget.LARGEST_TEXT_SIZE)); } } [TestCase(1, ExpectedResult = 1)] [TestCase(1024 * 1024, ExpectedResult = 1)] [TestCase(1024 * 1024 + 1, ExpectedResult = 2)] public long ASizeReadsInWholeMegabytesRoundedUp(long bytes) => DocumentMemoryBudget.MegabytesOf(bytes); }